Package org.postgresql.core

# Class Parser

java.lang.Object
org.postgresql.core.Parser

public class Parser extends Object
Basic query parser infrastructure. Note: This class should not be considered as pgjdbc public API.

## Constructor Summary

Constructors
Constructor
Description
`Parser()`

## Method Summary

Modifier and Type
Method
Description
`static boolean`
`charTerminatesIdentifier(char c)`
`static int`
`digitAt(String s, int pos)`
Converts digit at position `pos` in string `s` to integer or throws.
`static boolean`
`isArrayWhiteSpace(char c)`
Identifies white space characters which the backend uses to determine if a `String` value needs to be quoted in array representation.
`static boolean`
`isDigitAt(String s, int pos)`
Returns true if a given string `s` has digit at position `pos`.
`static boolean`
`isDollarQuoteContChar(char c)`
Checks if a character is valid as the second or later character of a dollar quoting tag.
`static boolean`
`isDollarQuoteStartChar(char c)`
Checks if a character is valid as the start of a dollar quoting tag.
`static boolean`
`isIdentifierContChar(char c)`
Checks if a character is valid as the second or later character of an identifier.
`static boolean`
`isIdentifierStartChar(char c)`
Checks if a character is valid as the start of an identifier.
`static boolean`
`isOperatorChar(char c)`
`static boolean`
`isSpace(char c)`
Identifies characters which the backend scanner considers to be whitespace.
`static JdbcCallParseInfo`
`modifyJdbcCall(String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion, EscapeSyntaxCallMode escapeSyntaxCallMode)`
Converts JDBC-specific callable statement escapes `{ [? =] call <some_function> [(?, [?,..])] }` into the PostgreSQL format which is `select <some_function> (?, [?, ...]) as result` or `select * from <some_function> (?, [?, ...]) as result` (7.3)
`static boolean`
`parseAlterKeyword(char[] query, int offset)`
Parse string to check presence of CREATE keyword regardless of case.
`static boolean`
`parseAsKeyword(char[] query, int offset)`
Parse string to check presence of AS keyword regardless of case.
`static boolean`
`parseAtomicKeyword(char[] query, int offset)`
Parse string to check presence of ATOMIC keyword regardless of case.
`static boolean`
`parseBeginKeyword(char[] query, int offset)`
Parse string to check presence of BEGIN keyword regardless of case.
`static int`
`parseBlockComment(char[] query, int offset)`
Test if the `/` character at `offset` starts a block comment, and return the position of the last `/` character.
`static boolean`
`parseCreateKeyword(char[] query, int offset)`
Parse string to check presence of CREATE keyword regardless of case.
`static boolean`
`parseDeleteKeyword(char[] query, int offset)`
Parse string to check presence of DELETE keyword regardless of case.
`static int`
`parseDollarQuotes(char[] query, int offset)`
Test if the dollar character (`$`) at the given offset starts a dollar-quoted string and return the offset of the ending dollar character.
`static int`
`parseDoubleQuotes(char[] query, int offset)`
Find the end of the double-quoted string starting at the given offset.
`static boolean`
`parseInsertKeyword(char[] query, int offset)`
Parse string to check presence of INSERT keyword regardless of case.
`static List<NativeQuery>`
`parseJdbcSql(String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements, boolean isBatchedReWriteConfigured, boolean quoteReturningIdentifiers, String... returningColumnNames)`
Parses JDBC query into PostgreSQL's native format.
`static int`
`parseLineComment(char[] query, int offset)`
Test if the `-` character at `offset` starts a `--` style line comment, and return the position of the first `\r` or `\n` character.
`static long`
`parseLong(String s, int beginIndex, int endIndex)`
Faster version of `Long.parseLong(String)` when parsing a substring is required
`static boolean`
`parseMoveKeyword(char[] query, int offset)`
Parse string to check presence of MOVE keyword regardless of case.
`static boolean`
`parseReturningKeyword(char[] query, int offset)`
Parse string to check presence of RETURNING keyword regardless of case.
`static boolean`
`parseSelectKeyword(char[] query, int offset)`
Parse string to check presence of SELECT keyword regardless of case.
`static int`
`parseSingleQuotes(char[] query, int offset, boolean standardConformingStrings)`
Find the end of the single-quoted string starting at the given offset.
`static boolean`
`parseUpdateKeyword(char[] query, int offset)`
Parse string to check presence of UPDATE keyword regardless of case.
`static boolean`
`parseValuesKeyword(char[] query, int offset)`
Parse string to check presence of VALUES keyword regardless of case.
`static boolean`
`parseWithKeyword(char[] query, int offset)`
Parse string to check presence of WITH keyword regardless of case.
`static String`
`replaceProcessing(String sql, boolean replaceProcessingEnabled, boolean standardConformingStrings)`
Filter the SQL string of Java SQL Escape clauses.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### Parser

public Parser()

## Method Details

### parseJdbcSql

public static List<NativeQuery> parseJdbcSql(String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements, boolean isBatchedReWriteConfigured, boolean quoteReturningIdentifiers, String... returningColumnNames) throws SQLException
Parses JDBC query into PostgreSQL's native format. Several queries might be given if separated by semicolon.

Parameters:
  `query` - jdbc query to parse
  `standardConformingStrings` - whether to allow backslashes to be used as escape characters in single quote literals
  `withParameters` - whether to replace ?, ? with $1, $2, etc
  `splitStatements` - whether to split statements by semicolon
  `isBatchedReWriteConfigured` - whether re-write optimization is enabled
  `quoteReturningIdentifiers` - whether to quote identifiers returned using returning clause
  `returningColumnNames` - for simple insert, update, delete add returning with given column names
Returns:
  list of native queries
Throws:
  `SQLException` - if unable to add returning clause (invalid column names)

### parseSingleQuotes

public static int parseSingleQuotes(char[] query, int offset, boolean standardConformingStrings)

Find the end of the single-quoted string starting at the given offset.

Note: for `'single '' quote in string'`, this method currently returns the offset of first `'` character after the initial one. The caller must call the method a second time for the second part of the quoted string.

Parameters:
  `query` - query
  `offset` - start offset
  `standardConformingStrings` - standard conforming strings
Returns:
  position of the end of the single-quoted string

### parseDoubleQuotes

public static int parseDoubleQuotes(char[] query, int offset)

Find the end of the double-quoted string starting at the given offset.

Note: for `"double "" quote in string"`, this method currently returns the offset of first `&quot;` character after the initial one. The caller must call the method a second time for the second part of the quoted string.

Parameters:
  `query` - query
  `offset` - start offset
Returns:
  position of the end of the double-quoted string

### parseDollarQuotes

public static int parseDollarQuotes(char[] query, int offset)
Test if the dollar character (`$`) at the given offset starts a dollar-quoted string and return the offset of the ending dollar character.

Parameters:
  `query` - query
  `offset` - start offset
Returns:
  offset of the ending dollar character

### parseLineComment

public static int parseLineComment(char[] query, int offset)
Test if the `-` character at `offset` starts a `--` style line comment, and return the position of the first `\r` or `\n` character.

Parameters:
  `query` - query
  `offset` - start offset
Returns:
  position of the first `\r` or `\n` character

### parseBlockComment

public static int parseBlockComment(char[] query, int offset)
Test if the `/` character at `offset` starts a block comment, and return the position of the last `/` character.

Parameters:
  `query` - query
  `offset` - start offset
Returns:
  position of the last `/` character

### parseDeleteKeyword

public static boolean parseDeleteKeyword(char[] query, int offset)
Parse string to check presence of DELETE keyword regardless of case. The initial character is assumed to have been matched.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseInsertKeyword

public static boolean parseInsertKeyword(char[] query, int offset)
Parse string to check presence of INSERT keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseBeginKeyword

public static boolean parseBeginKeyword(char[] query, int offset)
Parse string to check presence of BEGIN keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseAtomicKeyword

public static boolean parseAtomicKeyword(char[] query, int offset)
Parse string to check presence of ATOMIC keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseMoveKeyword

public static boolean parseMoveKeyword(char[] query, int offset)
Parse string to check presence of MOVE keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseReturningKeyword

public static boolean parseReturningKeyword(char[] query, int offset)
Parse string to check presence of RETURNING keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseSelectKeyword

public static boolean parseSelectKeyword(char[] query, int offset)
Parse string to check presence of SELECT keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseAlterKeyword

public static boolean parseAlterKeyword(char[] query, int offset)
Parse string to check presence of CREATE keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseCreateKeyword

public static boolean parseCreateKeyword(char[] query, int offset)
Parse string to check presence of CREATE keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseUpdateKeyword

public static boolean parseUpdateKeyword(char[] query, int offset)
Parse string to check presence of UPDATE keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseValuesKeyword

public static boolean parseValuesKeyword(char[] query, int offset)
Parse string to check presence of VALUES keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseLong

public static long parseLong(String s, int beginIndex, int endIndex)
Faster version of `Long.parseLong(String)` when parsing a substring is required

Parameters:
  `s` - string to parse
  `beginIndex` - begin index
  `endIndex` - end index
Returns:
  long value

### parseWithKeyword

public static boolean parseWithKeyword(char[] query, int offset)
Parse string to check presence of WITH keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### parseAsKeyword

public static boolean parseAsKeyword(char[] query, int offset)
Parse string to check presence of AS keyword regardless of case.

Parameters:
  `query` - char[] of the query statement
  `offset` - position of query to start checking
Returns:
  boolean indicates presence of word

### isDigitAt

public static boolean isDigitAt(String s, int pos)
Returns true if a given string `s` has digit at position `pos`.

Parameters:
  `s` - input string
  `pos` - position (0-based)
Returns:
  true if input string s has digit at position pos

### digitAt

public static int digitAt(String s, int pos)
Converts digit at position `pos` in string `s` to integer or throws.

Parameters:
  `s` - input string
  `pos` - position (0-based)
Returns:
  integer value of a digit at position pos
Throws:
  `NumberFormatException` - if character at position pos is not an integer

### isSpace

public static boolean isSpace(char c)
Identifies characters which the backend scanner considers to be whitespace.

https://github.com/postgres/postgres/blob/17bb62501787c56e0518e61db13a523d47afd724/src/backend/parser/scan.l#L194-L198

Parameters:
  `c` - character
Returns:
  true if the character is a whitespace character as defined in the backend's parser

### isArrayWhiteSpace

public static boolean isArrayWhiteSpace(char c)
Identifies white space characters which the backend uses to determine if a `String` value needs to be quoted in array representation.

https://github.com/postgres/postgres/blob/f2c587067a8eb9cf1c8f009262381a6576ba3dd0/src/backend/utils/adt/arrayfuncs.c#L421-L438

Parameters:
  `c` - Character to examine.
Returns:
  Indication if the character is a whitespace which back end will escape.

### isOperatorChar

public static boolean isOperatorChar(char c)

Parameters:
  `c` - character
Returns:
  true if the given character is a valid character for an operator in the backend's parser

### isIdentifierStartChar

public static boolean isIdentifierStartChar(char c)
Checks if a character is valid as the start of an identifier. PostgreSQL 9.4 allows column names like _, ‿, ⁀, ⁔, ︳, ︴, ﹍, ﹎, ﹏, ＿, so it is assumed isJavaIdentifierPart is good enough for PostgreSQL.

Parameters:
  `c` - the character to check
Returns:
  true if valid as first character of an identifier; false if not
See Also:
  - Identifiers and Key Words

### isIdentifierContChar

public static boolean isIdentifierContChar(char c)
Checks if a character is valid as the second or later character of an identifier.

Parameters:
  `c` - the character to check
Returns:
  true if valid as second or later character of an identifier; false if not

### charTerminatesIdentifier

public static boolean charTerminatesIdentifier(char c)

Parameters:
  `c` - character
Returns:
  true if the character terminates an identifier

### isDollarQuoteStartChar

public static boolean isDollarQuoteStartChar(char c)
Checks if a character is valid as the start of a dollar quoting tag.

Parameters:
  `c` - the character to check
Returns:
  true if valid as first character of a dollar quoting tag; false if not

### isDollarQuoteContChar

public static boolean isDollarQuoteContChar(char c)
Checks if a character is valid as the second or later character of a dollar quoting tag.

Parameters:
  `c` - the character to check
Returns:
  true if valid as second or later character of a dollar quoting tag; false if not

### modifyJdbcCall

public static JdbcCallParseInfo modifyJdbcCall(String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion, EscapeSyntaxCallMode escapeSyntaxCallMode) throws SQLException
Converts JDBC-specific callable statement escapes `{ [? =] call <some_function> [(?, [?,..])] }` into the PostgreSQL format which is `select <some_function> (?, [?, ...]) as result` or `select * from <some_function> (?, [?, ...]) as result` (7.3)

Parameters:
  `jdbcSql` - sql text with JDBC escapes
  `stdStrings` - if backslash in single quotes should be regular character or escape one
  `serverVersion` - server version
  `protocolVersion` - protocol version
  `escapeSyntaxCallMode` - mode specifying whether JDBC escape call syntax is transformed into a CALL/SELECT statement
Returns:
  SQL in appropriate for given server format
Throws:
  `SQLException` - if given SQL is malformed

### replaceProcessing

public static String replaceProcessing(String sql, boolean replaceProcessingEnabled, boolean standardConformingStrings) throws SQLException

Filter the SQL string of Java SQL Escape clauses.

Currently implemented Escape clauses are those mentioned in 11.3 in the specification. Basically we look through the sql string for {d xxx}, {t xxx}, {ts xxx}, {oj xxx} or {fn xxx} in non-string sql code. When we find them, we just strip the escape part leaving only the xxx part. So, something like "select * from x where d={d '2001-10-09'}" would return "select * from x where d= '2001-10-09'".

Parameters:
  `sql` - the original query text
  `replaceProcessingEnabled` - whether replace_processing_enabled is on
  `standardConformingStrings` - whether standard_conforming_strings is on
Returns:
  PostgreSQL-compatible SQL
Throws:
  `SQLException` - if given SQL is wrong