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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , Oracle , q quote , 字符串


背景

Oracle,当需要在字符串中包含单引号时,我们需要输入一对单引号。

例如

SQL> select 'Hello, I''m digoal.' from dual;  
  
'HELLO,I''MDIGOAL.  
------------------  
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;  
Q'#ORACLE'SQUOTEOPERATO  
-----------------------  
Oracle's quote operator  
  
  
SQL> select q'(Hello I'm digoal.)' from dual;  
  
Q'(HELLOI'MDIGOAL  
-----------------  
Hello I'm digoal.  

PostgreSQL q quote

https://www.postgresql.org/docs/10/static/sql-syntax-lexical.html

使用成对双$即可,或者$tag$成对。

例子:

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

更多PostgreSQL高级quote

关键字quote与字符串quote

                               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)  

1、关键字(例如表名、字段、库名等对象名),自动封装双引号。

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

2、字符串,自动封装单引号。(输入NULL,返回空)

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

3、识别空字符串,并返回NULL字符串。

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

格式化字符串

https://www.postgresql.org/docs/10/static/functions-string.html#FUNCTIONS-STRING-FORMAT

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

%[position][flags][width]type  

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 https://www.postgresql.org/docs/10/static/plpgsql-statements.html#PLPGSQL-QUOTE-LITERAL-EXAMPLE .

format常用于PLPGSQL,生成动态SQL。

unicode、ESCAPE

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

参考

https://www.postgresql.org/docs/10/static/sql-syntax-lexical.html

https://www.postgresql.org/docs/10/static/functions-string.html#FUNCTIONS-STRING-FORMAT

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
29天前
|
Oracle 关系型数据库 分布式数据库
PolarDB常见问题之PolarDB(Oracle兼容版) 执行命令报错如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
2月前
|
SQL Oracle 关系型数据库
Oracle查询优化-计算字符在字符串中出现的次数
【2月更文挑战第3天】【2月更文挑战第7篇】只接上SQL
49 0
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
4月前
|
SQL Oracle 关系型数据库
Oracle之如何遍历字符串
Oracle之如何遍历字符串
44 1
|
1月前
|
SQL Oracle 关系型数据库
Oracle insert数据时字符串中有‘单引号问题
Oracle insert数据时字符串中有‘单引号问题
|
2月前
|
Oracle 关系型数据库
Oracle查询优化-在字符串删除特定字符
【2月更文挑战第4天】【2月更文挑战第8篇】比较灵活,列举三个常见的方式
53 0
|
2月前
|
Oracle 关系型数据库
Oracle查询优化-遍历字符串
【2月更文挑战第3天】【2月更文挑战第6篇】Oracle查询优化-遍历字符串
17 0
|
7月前
|
Oracle 关系型数据库 数据库
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
201 0
|
3月前
|
Oracle 关系型数据库 数据库
Oracle查询优化-按照数字和字母混合字符串中的字母排序
【1月更文挑战第3天】【1月更文挑战第7篇】在对Oracle数据库进行查询优化,尤其是按照数字和字母混合字符串中的字母进行排序时,可以使用多种方法来达到预期的结果。
31 0
|
4月前
|
SQL Oracle 关系型数据库
Oracle,Postgresql等数据库使用
Oracle,Postgresql等数据库简单使用
133 0
Oracle,Postgresql等数据库使用

相关产品

  • 云原生数据库 PolarDB