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

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介:

标签

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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
9月前
|
Oracle 关系型数据库 数据库
【赵渝强老师】在PostgreSQL中访问Oracle
本文介绍了如何在PostgreSQL中使用oracle_fdw扩展访问Oracle数据库数据。首先需从Oracle官网下载三个Instance Client安装包并解压,设置Oracle环境变量。接着从GitHub下载oracle_fdw扩展,配置pg_config环境变量后编译安装。之后启动PostgreSQL服务器,在数据库中创建oracle_fdw扩展及外部数据库服务,建立用户映射。最后通过创建外部表实现对Oracle数据的访问。文末附有具体操作步骤与示例代码。
596 6
【赵渝强老师】在PostgreSQL中访问Oracle
|
11月前
|
SQL Oracle 关系型数据库
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
我们经常发现,部分国产数据库声称与 Oracle兼容性高达90%,但在实际迁移过程中,仍需要频繁地修改业务应用的代码。为何实现与Oracle高兼容度的数据库产品如此困难?其中一个重要原因是Oracle兼容性不仅是模仿,而是一个非常复杂和工程量庞大的逆向工程。其技术实现的复杂性以及多如牛毛的细节,足以让多数“年轻”的数据库团队望洋兴叹。YashanDB作为一款从核心理论到关键系统均为原创的数据库产品,从构建初期就具备了技术优势,在Oracle兼容性实现上,敢于亮剑并充分发挥工匠精神,不断打磨,努力构筑一个真正形神兼备的数据库产品。以下将从YashanDB SQL引擎技术、Oracle兼容性的开发
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
Oracle兼容性是目前国产数据库的关键任务之一,其直接影响到商业迁移的成本和竞争力。
259 8
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
2906 3
|
人工智能 Oracle 关系型数据库
一篇文章弄懂Oracle和PostgreSQL的Database Link
一篇文章弄懂Oracle和PostgreSQL的Database Link
|
存储 Oracle 关系型数据库
PolarDB 开源版通过orafce支持Oracle兼容性
背景PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.本文将介绍PolarDB开源版通过orafce支持Oracle兼容性 .测试环境为m...
385 0
|
SQL Cloud Native 关系型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
2003 1
|
数据可视化 关系型数据库 MySQL
将 PostgreSQL 迁移到 MySQL 数据库
将 PostgreSQL 迁移到 MySQL 数据库
2704 2
|
SQL 存储 自然语言处理
玩转阿里云RDS PostgreSQL数据库通过pg_jieba插件进行分词
在当今社交媒体的时代,人们通过各种平台分享自己的生活、观点和情感。然而,对于平台管理员和品牌经营者来说,了解用户的情感和意见变得至关重要。为了帮助他们更好地了解用户的情感倾向,我们可以使用PostgreSQL中的pg_jieba插件对这些发帖进行分词和情感分析,来构建一个社交媒体情感分析系统,系统将根据用户的发帖内容,自动判断其情感倾向是积极、消极还是中性,并将结果存储在数据库中。
1218 1
玩转阿里云RDS PostgreSQL数据库通过pg_jieba插件进行分词

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多