Uses underscore prefix for private tables 173

This commit is contained in:
Guido Fioravantti 2015-10-22 19:00:18 +02:00
parent 58deeb088d
commit aed8671e77
2 changed files with 19 additions and 19 deletions

View File

@ -428,7 +428,7 @@ END;
$$ LANGUAGE 'plpgsql';
-- DEPRECATED: Use cartodb.CDB_Unique_Identifier since it's UTF8 Safe and length
-- DEPRECATED: Use _CDB_Unique_Identifier since it's UTF8 Safe and length
-- aware. Find a unique relation name in the given schema, starting from the
-- template given. If the template is already unique, just return it;
-- otherwise, append an increasing integer until you find a unique variant.
@ -441,7 +441,7 @@ DECLARE
newrelname TEXT;
BEGIN
RAISE ERROR '_CDB_Unique_Relation_Name is DEPRECATED. Use CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)';
RAISE ERROR '_CDB_Unique_Relation_Name is DEPRECATED. Use _CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)';
i := 0;
newrelname := relationname;
@ -471,7 +471,7 @@ END;
$$ LANGUAGE 'plpgsql';
-- DEPRECATED: Use cartodb.CDB_Unique_Column_Identifier since it's UTF8 Safe and length
-- DEPRECATED: Use _CDB_Unique_Column_Identifier since it's UTF8 Safe and length
-- aware. Find a unique column name in the given relation, starting from the
-- column name given. If the column name is already unique, just return it;
-- otherwise, append an increasing integer until you find a unique variant.
@ -484,7 +484,7 @@ DECLARE
newcolname TEXT;
BEGIN
RAISE ERROR '_CDB_Unique_Column_Name is DEPRECATED. Use CDB_Unique_Column_Identifier(prefix TEXT, relname TEXT, suffix TEXT, reloid REGCLASS DEFAULT NULL)';
RAISE ERROR '_CDB_Unique_Column_Name is DEPRECATED. Use _CDB_Unique_Column_Identifier(prefix TEXT, relname TEXT, suffix TEXT, reloid REGCLASS DEFAULT NULL)';
i := 0;
newcolname := columnname;
@ -596,7 +596,7 @@ BEGIN
PERFORM _CDB_SQL(
Format('ALTER TABLE %s RENAME COLUMN %s TO %I',
reloid::text, rec.attname,
cartodb.CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
cartodb._CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
'_CDB_Has_Usable_Primary_ID');
END IF;
@ -613,7 +613,7 @@ BEGIN
PERFORM _CDB_SQL(
Format('ALTER TABLE %s RENAME COLUMN %s TO %I',
reloid::text, rec.attname, cartodb.CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
reloid::text, rec.attname, cartodb._CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
'_CDB_Has_Usable_Primary_ID');
END IF;
@ -779,7 +779,7 @@ BEGIN
WHEN others THEN
IF SQLERRM = 'parse error - invalid geometry' THEN
text_geom_column := false;
str := cartodb.CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
str := cartodb._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
@ -791,7 +791,7 @@ BEGIN
-- Just change its name so we can write a new column into that name.
ELSE
str := cartodb.CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
str := cartodb._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
@ -968,14 +968,14 @@ BEGIN
-- Put the primary key sequence in the right schema
-- If the new table is not moving, better ensure the sequence name
-- is unique
destseq := cartodb.CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema);
destseq := cartodb._CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema);
destseq := Format('%I.%I', destschema, destseq);
PERFORM _CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table');
-- Salt a temporary table name if we are re-writing in place
-- Note copyname is already escaped and safe to use as identifier
IF destschema = relschema THEN
copyname := Format('%I.%I', destschema, cartodb.CDB_Unique_Identifier(NULL, destname, '_' || salt), destschema);
copyname := Format('%I.%I', destschema, cartodb._CDB_Unique_Identifier(NULL, destname, '_' || salt), destschema);
ELSE
copyname := Format('%I.%I', destschema, destname);
END IF;

View File

@ -1,6 +1,6 @@
-- UTF8 safe and lenght aware. Find a unique identifier with a given prefix
-- and/or suffix and withing a schema.
CREATE OR REPLACE FUNCTION cartodb.CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)
RETURNS TEXT
AS $$
DECLARE
@ -17,10 +17,10 @@ BEGIN
usedspace := usedspace + coalesce(octet_length(prefix), 0);
usedspace := usedspace + coalesce(octet_length(suffix), 0);
relname := CDB_Octet_Trim(relname, usedspace + octet_length(relname) - maxlen);
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,13 +52,13 @@ 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)
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Column_Identifier(prefix TEXT, relname TEXT, suffix TEXT, reloid REGCLASS DEFAULT NULL)
RETURNS TEXT
AS $$
DECLARE
@ -75,10 +75,10 @@ BEGIN
usedspace := usedspace + coalesce(octet_length(prefix), 0);
usedspace := usedspace + coalesce(octet_length(suffix), 0);
relname := CDB_Octet_Trim(relname, usedspace + octet_length(relname) - maxlen);
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_Column_Identifier');
END IF;
ident := coalesce(prefix, '') || relname || coalesce(suffix, '');
@ -114,13 +114,13 @@ BEGIN
i := i + 1;
END LOOP;
PERFORM _CDB_Error('looping too far', 'CDB_Unique_Column_Identifier');
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.
CREATE OR REPLACE FUNCTION cartodb.CDB_Octet_Trim(tostrip TEXT, octets INTEGER)
CREATE OR REPLACE FUNCTION cartodb._CDB_Octet_Trim(tostrip TEXT, octets INTEGER)
RETURNS TEXT
AS $$
DECLARE