urlencode with only built-in functions

后端 未结 4 2031
抹茶落季
抹茶落季 2021-01-20 06:51

Without using plpgsql, I\'m trying to urlencode a given text within a pgsql SELECT statement.

The problem with this approach:

select regexp_replace(\         


        
4条回答
  •  悲&欢浪女
    2021-01-20 07:40

    Here's a function I wrote that handles encoding using built in functions while preserving the readability of the URL.

    Regex matches to capture pairs of (optional) safe characters and (at most one) non-safe character. Nested selects allow those pairs to be encoded and re-combined returning a fully encoded string.

    I've run through a test suite with all sorts of permutations (leading/trailing/only/repeated encoded characters and thus far it seems to encode correctly.

    The safe special characters are _ ~ . - and /. My inclusion of "/" on that list is probably non-standard, but fits the use case I have where the input text may be a path and I want that to remain.

    CREATE OR REPLACE FUNCTION oseberg.encode_uri(input text)
      RETURNS text
      LANGUAGE plpgsql
      IMMUTABLE STRICT
    AS $function$
    DECLARE
      parsed text;
      safePattern text;
    BEGIN
      safePattern = 'a-zA-Z0-9_~/\-\.';
      IF input ~ ('[^' || safePattern || ']') THEN
        SELECT STRING_AGG(fragment, '')
        INTO parsed
        FROM (
          SELECT prefix || encoded AS fragment
          FROM (
            SELECT COALESCE(match[1], '') AS prefix,
                   COALESCE('%' || encode(match[2]::bytea, 'hex'), '') AS encoded
            FROM (
              SELECT regexp_matches(
                input,
                '([' || safePattern || ']*)([^' || safePattern || '])?',
                'g') AS match
            ) matches
          ) parsed
        ) fragments;
        RETURN parsed;
      ELSE
        RETURN input;
      END IF;
    END;
    $function$
    

提交回复
热议问题