Documentation
¶
Overview ¶
Package pglex reads PostgreSQL SQL text lexically: where its top-level statements end, and where a string literal, a quoted identifier, a dollar-quoted body or a comment starts and stops. It parses nothing; the application database kind's statement checks, its migration lint and the admin console's statement lists read SQL through it (#2410, #2427).
Index ¶
- Constants
- func BuildsSQL(unit string) bool
- func FirstWord(sql string) string
- func IsEString(sql string, i int) bool
- func IsIdentByte(c byte) bool
- func PasswordLiteral(stmt string) (object string, ok bool)
- func RedactPasswords(sql string) string
- func RunsConcurrently(sql string) bool
- func SkipComment(sql string, i int) int
- func SkipDollarQuoted(sql string, i int) int
- func SkipLexeme(sql string, i int) int
- func SkipString(sql string, i int, backslash bool) int
- func Split(sql string) []string
- func StringLiterals(sql string) []string
- func Words(unit string) []string
Constants ¶
const BuiltAtRunTime = "SQL built at run time"
BuiltAtRunTime is the object PasswordLiteral reports for SQL that is built at run time and gives a password (#2750): an EXECUTE of anything but one literal, or a dblink call with an argument that is not a literal, in a unit that names a role, user or group and PASSWORD. Such SQL cannot be read statically, so it is refused rather than passed.
const RedactedLiteral = "'[REDACTED]'"
RedactedLiteral is what RedactPasswords writes in place of a password literal: a string literal still, so the statement reads as SQL.
Variables ¶
This section is empty.
Functions ¶
func BuildsSQL ¶ added in v1.166.4
BuildsSQL reports whether unit runs SQL it builds at run time: an EXECUTE whose argument is anything but one literal (format(...), a concatenation, a variable), or a dblink or dblink_exec call with an argument that is not a literal. EXECUTE as a privilege (GRANT EXECUTE ON) or a trigger's EXECUTE FUNCTION is not one.
func FirstWord ¶ added in v1.153.2
FirstWord is the lowercased first word of sql after whitespace, comments and opening parentheses: the keyword a statement is read by.
func IsEString ¶
IsEString reports whether the quote at i opens an E'...' string, whose backslashes escape.
func IsIdentByte ¶
IsIdentByte reports whether c may appear in an unquoted identifier.
func PasswordLiteral ¶
PasswordLiteral reports whether stmt, one statement, writes a credential: a role, user, group or user mapping given a PASSWORD other than NULL, in the statement itself, in a statement of a dollar-quoted body it carries (a DO block, a function's body), or in a string literal that reads as one once unquoted (a role given a password inside an EXECUTE literal, #2750). It returns the object the statement acts on ("CREATE ROLE"), or BuiltAtRunTime for a statement or body that builds SQL at run time naming a role and PASSWORD, which cannot be read. The migration review's password_literal rule and RedactPasswords both read a statement by it, so what the review refuses is what a stored copy has redacted.
func RedactPasswords ¶
RedactPasswords returns sql with every literal after the PASSWORD of a statement PasswordLiteral reports replaced by RedactedLiteral, and any other text as it is. Every literal after the PASSWORD is replaced, not only the first: what follows it (VALID UNTIL, a user mapping's later options) is read no further than that.
A statement that sets no password itself has each dollar-quoted body and string literal it carries read the same way, so a role created inside a DO block, a function's body or an EXECUTE '...' is redacted as one created at the top level; a string literal is written back quoted. A statement or body that builds SQL at run time naming a role and PASSWORD has every literal it holds replaced, since which one carries the password cannot be read (#2750).
func RunsConcurrently ¶ added in v1.153.2
RunsConcurrently reports whether sql holds a CREATE, DROP or REINDEX statement with the word CONCURRENTLY outside its literals and comments, which PostgreSQL runs only outside a transaction block (#2490).
func SkipComment ¶
SkipComment returns the index just past a comment starting at i, or i.
func SkipDollarQuoted ¶
SkipDollarQuoted skips $tag$ ... $tag$, or returns i when $ starts no dollar quote (a positional parameter, $1).
func SkipLexeme ¶
SkipLexeme returns the index just past the literal, quoted identifier, dollar-quoted body or comment starting at i, or i when none starts there. One that never ends runs to the end of sql.
func SkipString ¶
SkipString skips a quoted run where a doubled quote is an escaped one and, in an E-string, a backslash escapes the next byte.
func Split ¶
Split cuts SQL into its top-level statements at the semicolons that end them, skipping a semicolon inside a string literal (with an E-string's backslash escapes), a quoted identifier, a dollar-quoted body (a function's, a DO block's), a comment, or a SQL-standard function body (BEGIN ATOMIC ... END, whose statements end in semicolons of their own). Each statement is trimmed; an empty one is dropped.
func StringLiterals ¶ added in v1.166.4
StringLiterals returns the text each string literal in sql stands for once unquoted (a doubled quote read as one, and in an E'...' string an escaped quote too), in order: the SQL a body passes to EXECUTE or dblink_exec as a literal. A dollar-quoted body is not among them.
Types ¶
This section is empty.