• API
  • 工具

工具函数

¥Utility Functions

pg.escapeIdentifier

将字符串转义为 SQL 标识符

¥Escapes a string as a SQL identifier.

const { escapeIdentifier } = require('pg')
const escapedIdentifier = escapeIdentifier('FooIdentifier')
console.log(escapedIdentifier) // '"FooIdentifier"'
⚠️

注意:当在 CREATE TABLE ${escapedIdentifier(identifier)} 等操作中使用作为此函数结果的标识符时,创建的表将区分大小写。如果你在转义标识符中使用任何大写字母,则必须始终引用创建的表,如 SELECT * from "MyCaseSensitiveTable";像 SELECT * FROM MyCaseSensitiveTable 这样的查询将导致 "不存在的表" 错误,因为查询中的大小写信息被剥离。

¥Note: When using an identifier that is the result of this function in an operation like CREATE TABLE ${escapedIdentifier(identifier)}, the table that is created will be CASE SENSITIVE. If you use any capital letters in the escaped identifier, you must always refer to the created table like SELECT * from "MyCaseSensitiveTable"; queries like SELECT * FROM MyCaseSensitiveTable will result in a "Non-existent table" error since case information is stripped from the query.

pg.escapeLiteral

⚠️

注意:建议使用参数化查询,而不是手动转义 SQL 字面量。有关更多信息,请参阅 参数化查询client.query API。

¥Note: Instead of manually escaping SQL literals, it is recommended to use parameterized queries. Refer to parameterized queries and the client.query API for more information.

将字符串转义为 SQL 字面量

¥Escapes a string as a SQL literal.

const { escapeLiteral } = require('pg')
const escapedLiteral = escapeLiteral("hello 'world'")
console.log(escapedLiteral) // "'hello ''world'''"
Last updated on August 24, 2024