PostgreSQL Oracle 兼容性之 - 字符串 q quote 逃脱符号

4 minute read




SQL> select 'Hello, I''m digoal.' from dual;  
Hello, I'm digoal.  

使用q quote的写法,可以将quote内部的字符串原样输出,避免写多个单引号带来的困惑。

q'c text-to-be-quoted c' c is a single character (called the quote delimiter).   
With the ?quote operator? apostrophes don't have to  
 be doubled:   
SQL> select q'#Oracle's quote operator#' from dual;  
Oracle's quote operator  
SQL> select q'(Hello I'm digoal.)' from dual;  
Hello I'm digoal.  

PostgreSQL q quote



postgres=# select 'Hello, I''m digoal';  
 Hello, I'm digoal  
(1 row)  
postgres=# select $$Hello, I'm digoal$$;  
 Hello, I'm digoal  
(1 row)  
postgres=# select $abc$Hello, I'm digoal$abc$;  
 Hello, I'm digoal  
(1 row)  



                               List of functions  
   Schema   |      Name      | Result data type | Argument data types |  Type    
 pg_catalog | quote_ident    | text             | text                | normal  
 pg_catalog | quote_literal  | text             | anyelement          | normal  
 pg_catalog | quote_literal  | text             | text                | normal  
 pg_catalog | quote_nullable | text             | anyelement          | normal  
 pg_catalog | quote_nullable | text             | text                | normal  
(5 rows)  


postgres=# select quote_ident('Tbl');  
(1 row)  


postgres=# select quote_literal('hello, i''m digoal');  
 'hello, i''m digoal'  
(1 row)  
postgres=# select quote_literal(null);  
(1 row)  


postgres=# select quote_nullable(null);  
(1 row)  
postgres=# select quote_nullable('hello, i''m digoal');  
 'hello, i''m digoal'  
(1 row)  


postgres=# \df format  
                           List of functions  
   Schema   |  Name  | Result data type | Argument data types  |  Type    
 pg_catalog | format | text             | text                 | normal  
 pg_catalog | format | text             | text, VARIADIC "any" | normal  
(2 rows)  

The function format produces output formatted according to a format string, in a style similar to the C function sprintf.

format(formatstr text [, formatarg "any" [, ...] ])  

formatstr is a format string that specifies how the result should be formatted. Text in the format string is copied directly to the result, except where format specifiers are used. Format specifiers act as placeholders in the string, defining how subsequent function arguments should be formatted and inserted into the result. Each formatarg argument is converted to text according to the usual output rules for its data type, and then formatted and inserted into the result string according to the format specifier(s).

Format specifiers are introduced by a % character and have the form


where the component fields are:

position (optional)

A string of the form n$ where n is the index of the argument to print. Index 1 means the first argument after formatstr. If the position is omitted, the default is to use the next argument in sequence.

flags (optional)

Additional options controlling how the format specifier’s output is formatted. Currently the only supported flag is a minus sign (-) which will cause the format specifier’s output to be left-justified. This has no effect unless the width field is also specified.

width (optional)

Specifies the minimum number of characters to use to display the format specifier’s output. The output is padded on the left or right (depending on the - flag) with spaces as needed to fill the width. A too-small width does not cause truncation of the output, but is simply ignored. The width may be specified using any of the following: a positive integer; an asterisk (*) to use the next function argument as the width; or a string of the form *n$ to use the nth function argument as the width.

If the width comes from a function argument, that argument is consumed before the argument that is used for the format specifier’s value. If the width argument is negative, the result is left aligned (as if the - flag had been specified) within a field of length abs(width).

type (required)

The type of format conversion to use to produce the format specifier’s output. The following types are supported:

  • s formats the argument value as a simple string. A null value is treated as an empty string.

  • I treats the argument value as an SQL identifier, double-quoting it if necessary. It is an error for the value to be null (equivalent to quote_ident).

  • L quotes the argument value as an SQL literal. A null value is displayed as the string NULL, without quotes (equivalent to quote_nullable).

In addition to the format specifiers described above, the special sequence %% may be used to output a literal % character.


Here are some examples of the basic format conversions:

SELECT format('Hello %s', 'World');  
Result: Hello World  
SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three');  
Result: Testing one, two, three, %  
SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly');  
Result: INSERT INTO "Foo bar" VALUES('O''Reilly')  
SELECT format('INSERT INTO %I VALUES(%L)', 'locations', E'C:\\Program Files');  
Result: INSERT INTO locations VALUES(E'C:\\Program Files')  

Here are examples using width fields and the - flag:

SELECT format('|%10s|', 'foo');  
Result: |       foo|  
SELECT format('|%-10s|', 'foo');  
Result: |foo       |  
SELECT format('|%*s|', 10, 'foo');  
Result: |       foo|  
SELECT format('|%*s|', -10, 'foo');  
Result: |foo       |  
SELECT format('|%-*s|', 10, 'foo');  
Result: |foo       |  
SELECT format('|%-*s|', -10, 'foo');  
Result: |foo       |  

These examples show use of position fields:

SELECT format('Testing %3$s, %2$s, %1$s', 'one', 'two', 'three');  
Result: Testing three, two, one  
SELECT format('|%*2$s|', 'foo', 10, 'bar');  
Result: |       bar|  
SELECT format('|%1$*2$s|', 'foo', 10, 'bar');  
Result: |       foo|  
Unlike the standard C function sprintf, PostgreSQL's format function allows format specifiers with and without position fields to be mixed in the same format string. A format specifier without a position field always uses the next argument after the last argument consumed. In addition, the format function does not require all function arguments to be used in the format string. For example:  
SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');  
Result: Testing three, two, three  

The %I and %L format specifiers are particularly useful for safely constructing dynamic SQL statements. See .



《PostgreSQL 转义、UNICODE、与SQL注入》


Flag Counter

digoal’s 大量PostgreSQL文章入口