com.amazon.redshift.core

## Class Parser

- java.lang.Object
- - com.amazon.redshift.core.Parser

    public class Parser
    extends Object

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

Author:
  Michael Paesold (mpaesold@gmx.at), Christopher Deckers (chrriis@gmail.com)

### Constructor Summary

Constructors
Constructor and Description
`Parser()`

### Method Summary

Methods
`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` `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)`

`static JdbcCallParseInfo`

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

`static boolean` `parseAsKeyword(char[] query, int offset)` Parse string to check presence of AS 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` `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 isMultiSqlSupport, String... returningColumnNames)` Parses JDBC query into Redshift'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` `parsePrepareKeyword(char[] query, int offset)` Parse string to check presence of PREPARE 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 Detail

#### Parser

    public Parser()

### Method Detail

#### parseJdbcSql

    public static List<NativeQuery> parseJdbcSql(String query,
                                 boolean standardConformingStrings,
                                 boolean withParameters,
                                 boolean splitStatements,
                                 boolean isBatchedReWriteConfigured,
                                 boolean isMultiSqlSupport,
                                 String... returningColumnNames)
                                          throws SQLException

Parses JDBC query into Redshift'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
  `isMultiSqlSupport` - whether multiple SQL commands support is enabled
  `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

#### 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

#### parsePrepareKeyword

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

Parse string to check presence of PREPARE 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

#### 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)

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

#### 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 Redshift.

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 Redshift 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:
  Redshift-compatible SQL
Throws:
  `SQLException` - if given SQL is wrong