2013-05-06 08:39:20| 分类: PgSQL Develop|举报|字号 订阅
PostgreSQL 9.3 引入的一个格式化输出函数, 有点类似C的sprintf用法.
语法如下 :
format(formatstr text [, formatarg "any" [, ...] ])
其中formatstr是需要格式化的字符串, 包含普通字符以及格式字符.
后面的动态参数用来替换formatstr中的格式字符.
格式字符的语法如下 :
%[position][flags][width]type
其中position, flags, width都是可选项. type是必须的.
1. position 指变量位置.
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.
注意the next argument是指前一个已经取过的位置的下一个位置.
2. flags目前只有-, 且需要与width配合使用, -表示右补齐, 没有-表示左补齐.
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指显示的宽度, 当宽度大于实际字符串时用空格补齐, 当宽度小于实际字符串长度时不会截断字符串, 相当于width不起作用. width也可以使用3. format函数的参数值来代替直接写在formatstr中.
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).
注意如果同一个格式中宽度用到了参数, 那么宽度优先消耗这个参数. 因此如果没有指定位置时, 默认会消耗下一个参数. 也就是宽度消耗的参数的下一个参数.
4. type指字符串类型, 目前可以使用s, I, L. 分别表示字符串, identified, 和literal.
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. L quotes the argument value as an SQL literal. A null value is displayed as the string NULL, without quotes. In addition to the format specifiers described above, the special sequence %% may be used to output a literal % character.
[使用举例]
1.
digoal=# select format (‘|abc|%*2$s|‘, E‘wab c\t‘, ‘10‘, ‘ab c‘); format ------------------ |abc| ab c|(1 row)
*2$指width取第二个参数. 10
s指字符串, 这里去下一个参数也就是第三个参数‘ab c‘
2.
digoal=# select format (‘|abc|%*s|‘,‘8‘, E‘wab c‘, ‘10‘, ‘ab c‘); format ---------------- |abc| wab c|(1 row)
*指width, 取下一个参数. 因为前面没有取过任何参数, 所以这里是‘8‘
s指字符串, 这里去下一个参数也就是第2个参数E‘wab c‘.
3.
digoal=# select format (‘|abc|%-*s|‘,‘8‘, E‘wab c‘, ‘10‘, ‘ab c‘); format ---------------- |abc|wab c |(1 row)
*指width, 取下一个参数. 因为前面没有取过任何参数, 所以这里是‘8‘
s指字符串, 这里去下一个参数也就是第2个参数E‘wab c‘.
- 表示右边补齐
4.
postgres=# select format (‘|abc|%3$-*s|‘,‘4‘, E‘wab c‘, ‘10‘, ‘ab c‘); format ------------ |abc|10 |(1 row)
这里的*取的是第一个参数‘4‘, 因为前面没有取过参数, 从1开始.
digoal=# select format (‘|abc|%2$s|%3$-*s|‘,‘4‘, E‘wab c‘, ‘10‘, ‘ab c‘); format ------------------------ |abc|wab c|10 |(1 row)
这里的*取的是第3个参数. 也就是%2$后面的一个参数.
%3$-*s这个格式中, 是先取width参数, 再取其他参数的.
5.
digoal=# select format (‘|abc|%2$I|%3$-*I|‘,‘4‘, E‘wab c‘, ‘10‘, ‘ab c‘); format -------------------------- |abc|"wab c"|"10" |(1 row)
I指identified, 类似表名, 字段名. 所以如果是包含了特殊字符则需要用双引号.
digoal=# select format (‘|abc|%2$I|%3$-*I|‘,‘4‘, E‘wABc‘, ‘10‘, ‘ab c‘); format ------------------------- |abc|"wABc"|"10" |(1 row)digoal=# select format (‘|abc|%2$I|%3$-*I|‘,‘4‘, E‘wabc‘, ‘10‘, ‘ab c‘); format ----------------------- |abc|wabc|"10" |(1 row)
6.
digoal=# select format (‘|abc|%2$L|%3$-*L|‘,‘4‘, E‘wa\bc\t‘, ‘10‘, ‘ab c‘); format ------------------------------- |abc|‘wa\x08c ‘|‘10‘ |(1 row)digoal=# select format (‘|abc|%2$L|%3$-*L|‘,‘4‘, E‘wa\bc\t\\‘, ‘10‘, ‘ab c‘); format --------------------------------- |abc|E‘wa\x08c \\‘|‘10‘ |(1 row)
L指literal, 类似字符串类型的值, 所以涉及逃逸. 如山.
7.
因此format可用于构造动态SQL.
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‘)
【参考】
1. http://www.postgresql.org/docs/devel/static/functions-string.html
2. http://www.postgresql.org/docs/devel/static/plpgsql-statements.html#PLPGSQL-QUOTE-LITERAL-EXAMPLE
3. man 3 sprintf