Adds CDB_Unique_Column_Identifier for columns 173
This commit is contained in:
@@ -20,7 +20,7 @@ BEGIN
|
||||
relname := CDB_Octet_Trim(relname, usedspace + octet_length(relname) - maxlen);
|
||||
|
||||
IF relname = '' THEN
|
||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Identifier');
|
||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', 'CDB_Unique_Identifier');
|
||||
END IF;
|
||||
|
||||
ident := coalesce(prefix, '') || relname || coalesce(suffix, '');
|
||||
@@ -52,10 +52,71 @@ BEGIN
|
||||
i := i + 1;
|
||||
END LOOP;
|
||||
|
||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Identifier');
|
||||
PERFORM _CDB_Error('looping too far', 'CDB_Unique_Identifier');
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
-- UTF8 safe and lenght aware. Find a unique identifier for a column with a given prefix
|
||||
-- and/or suffix and withing a realtion. If no reloid is give, all relations are examined
|
||||
CREATE OR REPLACE FUNCTION cartodb.CDB_Unique_Column_Identifier(prefix TEXT, relname TEXT, suffix TEXT, reloid REGCLASS DEFAULT NULL)
|
||||
RETURNS TEXT
|
||||
AS $$
|
||||
DECLARE
|
||||
rec RECORD;
|
||||
usedspace INTEGER;
|
||||
ident TEXT;
|
||||
i INTEGER;
|
||||
origident TEXT;
|
||||
maxlen INTEGER;
|
||||
BEGIN
|
||||
maxlen := 63;
|
||||
|
||||
usedspace := 3;
|
||||
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
||||
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
||||
|
||||
relname := CDB_Octet_Trim(relname, usedspace + octet_length(relname) - maxlen);
|
||||
|
||||
IF relname = '' THEN
|
||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', 'CDB_Unique_Identifier');
|
||||
END IF;
|
||||
|
||||
ident := coalesce(prefix, '') || relname || coalesce(suffix, '');
|
||||
|
||||
i := 0;
|
||||
origident := ident;
|
||||
|
||||
WHILE i < 100 LOOP
|
||||
IF reloid IS NOT NULL THEN
|
||||
SELECT a.attname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||
WHERE NOT a.attisdropped
|
||||
AND a.attnum > 0
|
||||
AND c.oid = reloid
|
||||
AND a.attname = ident;
|
||||
ELSE
|
||||
SELECT a.attname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||
WHERE NOT a.attisdropped
|
||||
AND a.attnum > 0
|
||||
AND a.attname = ident;
|
||||
END IF;
|
||||
|
||||
IF NOT FOUND THEN
|
||||
RETURN ident;
|
||||
END IF;
|
||||
|
||||
ident := origident || '_' || i;
|
||||
i := i + 1;
|
||||
END LOOP;
|
||||
|
||||
PERFORM _CDB_Error('looping too far', 'CDB_Unique_Column_Identifier');
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
-- Trims the end of a given string by the given number of octets taking care
|
||||
-- not to leave characters in half. UTF8 safe.
|
||||
|
||||
Reference in New Issue
Block a user