2017-06-29 23:54:42 +08:00
|
|
|
-- Create a sequence that belongs to the schema of the extension.
|
|
|
|
-- It will be used to generate unique identifiers within the
|
|
|
|
|
|
|
|
|
2015-10-27 19:11:00 +08:00
|
|
|
-- UTF8 safe and length aware. Find a unique identifier with a given prefix
|
2015-10-26 22:54:08 +08:00
|
|
|
-- and/or suffix and withing a schema. If a schema is not specified, the identifier
|
|
|
|
-- is guaranteed to be unique for all schemas.
|
2015-10-23 01:00:18 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)
|
2015-10-22 18:38:00 +08:00
|
|
|
RETURNS TEXT
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
2015-10-27 01:01:03 +08:00
|
|
|
maxlen CONSTANT INTEGER := 63;
|
|
|
|
|
2015-10-22 18:38:00 +08:00
|
|
|
rec RECORD;
|
|
|
|
usedspace INTEGER;
|
|
|
|
ident TEXT;
|
|
|
|
origident TEXT;
|
2015-10-27 17:33:15 +08:00
|
|
|
candrelname TEXT;
|
2015-10-22 18:38:00 +08:00
|
|
|
|
2015-10-27 01:01:03 +08:00
|
|
|
i INTEGER;
|
2015-10-26 23:15:55 +08:00
|
|
|
BEGIN
|
2017-06-29 23:54:42 +08:00
|
|
|
-- Accounts for the XXXX incremental suffix in case the identifier is taken
|
|
|
|
usedspace := 4;
|
2015-10-22 18:38:00 +08:00
|
|
|
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
|
|
|
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
candrelname := _CDB_Octet_Truncate(relname, maxlen - usedspace);
|
2015-10-22 18:38:00 +08:00
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
IF candrelname = '' THEN
|
2015-10-23 01:00:18 +08:00
|
|
|
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Identifier');
|
2015-10-22 18:38:00 +08:00
|
|
|
END IF;
|
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
ident := coalesce(prefix, '') || candrelname || coalesce(suffix, '');
|
2015-10-22 18:38:00 +08:00
|
|
|
|
|
|
|
i := 0;
|
|
|
|
origident := ident;
|
|
|
|
|
2017-06-29 23:54:42 +08:00
|
|
|
WHILE i < 10000 LOOP
|
2015-10-22 18:38:00 +08:00
|
|
|
IF schema IS NOT NULL THEN
|
|
|
|
SELECT c.relname, n.nspname
|
|
|
|
INTO rec
|
|
|
|
FROM pg_class c
|
|
|
|
JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
|
|
WHERE c.relname = ident
|
|
|
|
AND n.nspname = schema;
|
|
|
|
ELSE
|
|
|
|
SELECT c.relname, n.nspname
|
|
|
|
INTO rec
|
|
|
|
FROM pg_class c
|
|
|
|
JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
|
|
WHERE c.relname = ident;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN ident;
|
|
|
|
END IF;
|
|
|
|
|
2017-06-29 23:54:42 +08:00
|
|
|
ident := origident || i;
|
2015-10-22 18:38:00 +08:00
|
|
|
i := i + 1;
|
|
|
|
END LOOP;
|
|
|
|
|
2015-10-23 01:00:18 +08:00
|
|
|
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Identifier');
|
2015-10-22 18:38:00 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
|
2015-10-27 01:01:03 +08:00
|
|
|
|
2015-10-27 19:11:00 +08:00
|
|
|
-- UTF8 safe and length aware. Find a unique identifier for a column with a given prefix
|
2015-10-27 01:35:12 +08:00
|
|
|
-- and/or suffix based on colname and within a relation specified via reloid.
|
|
|
|
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Column_Identifier(prefix TEXT, colname TEXT, suffix TEXT, reloid REGCLASS)
|
2015-10-22 23:01:47 +08:00
|
|
|
RETURNS TEXT
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
2015-10-27 01:01:03 +08:00
|
|
|
maxlen CONSTANT INTEGER := 63;
|
|
|
|
|
2015-10-22 23:01:47 +08:00
|
|
|
rec RECORD;
|
2015-10-27 17:33:15 +08:00
|
|
|
candcolname TEXT;
|
2015-10-22 23:01:47 +08:00
|
|
|
usedspace INTEGER;
|
|
|
|
ident TEXT;
|
|
|
|
origident TEXT;
|
|
|
|
|
2015-10-27 01:01:03 +08:00
|
|
|
i INTEGER;
|
2015-10-26 23:15:55 +08:00
|
|
|
BEGIN
|
2017-06-29 23:54:42 +08:00
|
|
|
-- Accounts for the XXXX incremental suffix in case the identifier is taken
|
|
|
|
usedspace := 4;
|
2015-10-22 23:01:47 +08:00
|
|
|
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
|
|
|
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
candcolname := _CDB_Octet_Truncate(colname, maxlen - usedspace);
|
2015-10-22 23:01:47 +08:00
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
IF candcolname = '' THEN
|
2015-10-23 01:00:18 +08:00
|
|
|
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Column_Identifier');
|
2015-10-22 23:01:47 +08:00
|
|
|
END IF;
|
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
ident := coalesce(prefix, '') || candcolname || coalesce(suffix, '');
|
2015-10-22 23:01:47 +08:00
|
|
|
|
|
|
|
i := 0;
|
|
|
|
origident := ident;
|
|
|
|
|
2017-06-29 23:54:42 +08:00
|
|
|
WHILE i < 10000 LOOP
|
2015-10-26 23:17:25 +08:00
|
|
|
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;
|
2015-10-22 23:01:47 +08:00
|
|
|
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN ident;
|
|
|
|
END IF;
|
|
|
|
|
2017-06-29 23:54:42 +08:00
|
|
|
ident := origident || i;
|
2015-10-22 23:01:47 +08:00
|
|
|
i := i + 1;
|
|
|
|
END LOOP;
|
|
|
|
|
2015-10-23 01:00:18 +08:00
|
|
|
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Column_Identifier');
|
2015-10-22 23:01:47 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
2015-10-22 18:38:00 +08:00
|
|
|
|
2015-10-27 01:01:03 +08:00
|
|
|
|
2015-10-27 19:11:00 +08:00
|
|
|
-- Truncates a given string to a max_octets octets taking care
|
2015-10-27 01:01:03 +08:00
|
|
|
-- not to leave characters in half. UTF8 safe.
|
|
|
|
CREATE OR REPLACE FUNCTION cartodb._CDB_Octet_Truncate(string TEXT, max_octets INTEGER)
|
2015-10-22 18:38:00 +08:00
|
|
|
RETURNS TEXT
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
2015-10-27 01:01:03 +08:00
|
|
|
extcharlen CONSTANT INTEGER := octet_length('ñ');
|
|
|
|
|
2015-10-22 18:38:00 +08:00
|
|
|
expected INTEGER;
|
|
|
|
examined INTEGER;
|
2015-10-27 01:01:03 +08:00
|
|
|
strlen INTEGER;
|
2015-10-22 18:38:00 +08:00
|
|
|
|
|
|
|
i INTEGER;
|
|
|
|
BEGIN
|
2015-10-27 01:01:03 +08:00
|
|
|
|
|
|
|
IF max_octets <= 0 THEN
|
2015-10-22 18:38:00 +08:00
|
|
|
RETURN '';
|
2015-10-27 01:01:03 +08:00
|
|
|
ELSIF max_octets >= octet_length(string) THEN
|
|
|
|
RETURN string;
|
2015-10-22 18:38:00 +08:00
|
|
|
END IF;
|
|
|
|
|
2015-10-27 01:01:03 +08:00
|
|
|
strlen := char_length(string);
|
|
|
|
|
|
|
|
expected := char_length(string);
|
|
|
|
examined := octet_length(string);
|
2015-10-22 18:38:00 +08:00
|
|
|
|
2015-10-27 01:01:03 +08:00
|
|
|
IF expected = examined THEN
|
2015-10-27 17:33:15 +08:00
|
|
|
RETURN left(string, max_octets);
|
2015-10-27 01:01:03 +08:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
i := max_octets / extcharlen;
|
2015-10-22 18:38:00 +08:00
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
WHILE octet_length(left(string, i)) <= max_octets LOOP
|
2015-10-27 01:01:03 +08:00
|
|
|
i := i + 1;
|
2015-10-22 18:38:00 +08:00
|
|
|
END LOOP;
|
|
|
|
|
2015-10-27 17:33:15 +08:00
|
|
|
RETURN left(string, (i - 1));
|
2015-10-22 18:38:00 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql';
|