Reworks _CDB_Octet_Trim 173

This commit is contained in:
Guido Fioravantti
2015-10-26 16:35:33 +01:00
parent 710a3c9672
commit 2e701f73ba
+16 -15
View File
@@ -18,7 +18,7 @@ BEGIN
usedspace := usedspace + coalesce(octet_length(prefix), 0); usedspace := usedspace + coalesce(octet_length(prefix), 0);
usedspace := usedspace + coalesce(octet_length(suffix), 0); usedspace := usedspace + coalesce(octet_length(suffix), 0);
relname := _CDB_Octet_Trim(relname, usedspace + octet_length(relname) - maxlen); relname := _CDB_Trim_Octets(relname, usedspace + octet_length(relname) - maxlen);
IF relname = '' THEN 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');
@@ -76,7 +76,7 @@ BEGIN
usedspace := usedspace + coalesce(octet_length(prefix), 0); usedspace := usedspace + coalesce(octet_length(prefix), 0);
usedspace := usedspace + coalesce(octet_length(suffix), 0); usedspace := usedspace + coalesce(octet_length(suffix), 0);
relname := _CDB_Octet_Trim(relname, usedspace + octet_length(relname) - maxlen); relname := _CDB_Trim_Octets(relname, usedspace + octet_length(relname) - maxlen);
IF relname = '' THEN IF relname = '' THEN
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Column_Identifier'); PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Column_Identifier');
@@ -110,14 +110,15 @@ END;
$$ LANGUAGE 'plpgsql'; $$ LANGUAGE 'plpgsql';
-- Trims the end of a given string by the given number of octets taking care -- Trims the end of a given string by the given number of octets taking care
-- not to leave characters in half. UTF8 safe. -- not to leave characters in half. If a negative or 0 amount of octects to trim
CREATE OR REPLACE FUNCTION cartodb._CDB_Octet_Trim(tostrip TEXT, octets INTEGER) -- is specified, the suplied text is returned unaltered. UTF8 safe.
CREATE OR REPLACE FUNCTION cartodb._CDB_Trim_Octets(totrim TEXT, octets INTEGER)
RETURNS TEXT RETURNS TEXT
AS $$ AS $$
DECLARE DECLARE
expected INTEGER; expected INTEGER;
examined INTEGER; examined INTEGER;
tostriplen INTEGER; totrimlen INTEGER;
charlen INTEGER; charlen INTEGER;
i INTEGER; i INTEGER;
@@ -126,28 +127,28 @@ DECLARE
trimmed TEXT; trimmed TEXT;
BEGIN BEGIN
charlen := bit_length('a'); charlen := bit_length('a');
tostriplen := char_length(tostrip); totrimlen := char_length(totrim);
expected := tostriplen * charlen; expected := totrimlen * charlen;
examined := bit_length(tostrip); examined := bit_length(totrim);
IF expected = examined OR octets = 0 THEN IF octets <= 0 THEN
RETURN SUBSTRING(tostrip from 1 for (tostriplen - octets)); RETURN totrim;
ELSIF octets < 0 THEN ELSIF expected = examined THEN
RETURN tostrip; RETURN SUBSTRING(totrim from 1 for (totrimlen - octets));
ELSIF (octets * charlen) > examined THEN ELSIF (octets * charlen) > examined THEN
RETURN ''; RETURN '';
END IF; END IF;
i := tostriplen - ((octets - 1) / 2); i := totrimlen - ((octets - 1) / 2);
LOOP LOOP
tail := SUBSTRING(tostrip from i for tostriplen); tail := SUBSTRING(totrim from i for totrimlen);
EXIT WHEN octet_length(tail) >= octets OR i <= 0; EXIT WHEN octet_length(tail) >= octets OR i <= 0;
i := i - 1; i := i - 1;
END LOOP; END LOOP;
trimmed := SUBSTRING(tostrip from 1 for (tostriplen - char_length(tail))); trimmed := SUBSTRING(totrim from 1 for (totrimlen - char_length(tail)));
RETURN trimmed; RETURN trimmed;
END; END;
$$ LANGUAGE 'plpgsql'; $$ LANGUAGE 'plpgsql';