File: check_name_length.sql

package info (click to toggle)
mimeo 1.5.1-20
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,300 kB
  • sloc: sql: 85,916; python: 81; makefile: 23; sh: 16
file content (31 lines) | stat: -rw-r--r-- 1,036 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * Truncate the name of the given object if it is greater than the postgres default max (63 characters). Otherwise returns given name.
 * Also appends suffix if given and truncates the name so that the entire suffix will fit.
 * Setting p_convert_standard will change any non-standard character in the object name to an underscore. Allows for new object names that use 
 *   existing object names to be more widely usable.
 */
CREATE FUNCTION check_name_length(p_table_name text, p_suffix text DEFAULT NULL, p_convert_standard boolean DEFAULT false) RETURNS text
    LANGUAGE plpgsql IMMUTABLE
    AS $$
DECLARE

v_result    text;

BEGIN

IF ( char_length(p_table_name) + char_length(COALESCE(p_suffix, '')) ) >= 63 THEN
    v_result := substring(p_table_name from 1 for 63 - char_length(COALESCE(p_suffix, ''))) || COALESCE(p_suffix, '') ;
ELSE
    v_result := p_table_name || COALESCE(p_suffix, '');
END IF;

IF p_convert_standard THEN
    v_result := regexp_replace(v_result, '\W', '_', 'g');
END IF;

RETURN v_result;

END
$$;