APIdia Launch Partner
Top Description Fields Constructors Methods
java.sql

pack-priv Class SQLUtils

extends Object
Class Inheritance
Imports
java.util.Arrays, .HashSet, .Set, java.util.regex.Pattern

Utility class used by the Connection & Statement interfaces for their shared default methods.

Field Summary

Modifier and TypeField and Description
private static final Pattern
private static final Pattern
private static final String[]
private static final Set<String>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv static String

Returns:

A simple SQL identifier or a delimited identifier
enquoteIdentifier
(String delimiter, String
a SQL identifier
identifier
,
boolean
indicates if a simple SQL identifier should be returned as a delimited identifier
alwaysDelimit
)

Returns a simple SQL identifier or a delimited identifier.

pack-priv static String

Returns:

A string enclosed by single quotes with every single quote converted to two single quotes
enquoteLiteral
(String
a character string
val
)

Returns a String enclosed in single quotes.

pack-priv static String

Returns:

the result of replacing every single quote character in the argument by two single quote characters where this entire result is then prefixed with 'N'.
enquoteNCharLiteral
(String
a character string
val
)

Returns a String representing a National Character Set Literal enclosed in single quotes and prefixed with an upper case letter N.

pack-priv static boolean

Returns:

true if a simple SQL identifier, false otherwise
isSimpleIdentifier
(String
a SQL identifier
identifier
)

Returns whether identifier is a simple SQL identifier.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INVALID_IDENTIFIER_CHARACTERS_PATTERNback to summary
private static final Pattern INVALID_IDENTIFIER_CHARACTERS_PATTERN
SIMPLE_IDENTIFIER_PATTERNback to summary
private static final Pattern SIMPLE_IDENTIFIER_PATTERN
SQL2023_RESERVED_WORDSback to summary
private static final String[] SQL2023_RESERVED_WORDS
SQL_RESERVED_WORDSback to summary
private static final Set<String> SQL_RESERVED_WORDS

Constructor Detail

SQLUtilsback to summary
pack-priv SQLUtils()

Method Detail

enquoteIdentifierback to summary
pack-priv static String enquoteIdentifier(String delimiter, String identifier, boolean alwaysDelimit) throws SQLException

Returns a simple SQL identifier or a delimited identifier. A delimited identifier represents the name of a database object such as a table, column, or view that is enclosed by a delimiter, which is typically a double quote as defined by the SQL standard.

If identifier is a simple SQL identifier:

  • If alwaysDelimit is false, return the original value
  • if alwaysDelimit is true, enquote the original value and return as a delimited identifier
If identifier is not a simple SQL identifier, the delimited identifier to be returned must be enclosed by the delimiter returned from DatabaseMetaData#getIdentifierQuoteString. If the datasource does not support delimited identifiers, a SQLFeatureNotSupportedException is thrown.

A SQLException will be thrown if identifier contains any invalid characters within a delimited identifier or the identifier length is invalid for the datasource.

Implementation Specification

The default implementation uses the following criteria to determine a valid simple SQL identifier:

  • The string is not enclosed in double quotes
  • The first character is an alphabetic character from a ('\0061') through z ('\u007A'), or from A ('\u0041') through Z ('\u005A')
  • The name only contains alphanumeric characters or the character "_"
The default implementation will throw a SQLException if:
Examples of the conversion:
identifier alwaysDelimit Result
Hello false Hello
Hello true "Hello"
G'Day false "G'Day"
"Bruce Wayne" false "Bruce Wayne"
"Bruce Wayne" true "Bruce Wayne"
"select" false "select"
"select" true "select"
GoodDay$ false "GoodDay$"
Hello"World false SQLException
"Hello"World" false SQLException

Implementation Note

JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource.

Parameters
identifier:String

a SQL identifier

alwaysDelimit:boolean

indicates if a simple SQL identifier should be returned as a delimited identifier

Returns:String

A simple SQL identifier or a delimited identifier

Exceptions
SQLException:
if identifier is not a valid identifier
SQLFeatureNotSupportedException:
if the datasource does not support delimited identifiers
NullPointerException:
if identifier is null
enquoteLiteralback to summary
pack-priv static String enquoteLiteral(String val) throws SQLException

Returns a String enclosed in single quotes. Any occurrence of a single quote within the string will be replaced by two single quotes.

Examples of the conversion:
ValueResult
Hello 'Hello'
G'Day 'G''Day'
'G''Day' '''G''''Day'''
I'''M 'I''''''M'

Implementation Note

JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource.

Parameters
val:String

a character string

Returns:String

A string enclosed by single quotes with every single quote converted to two single quotes

Exceptions
SQLException:
if a database access error occurs
NullPointerException:
if val is null
enquoteNCharLiteralback to summary
pack-priv static String enquoteNCharLiteral(String val) throws SQLException

Returns a String representing a National Character Set Literal enclosed in single quotes and prefixed with an upper case letter N. Any occurrence of a single quote within the string will be replaced by two single quotes.

Examples of the conversion:
Value Result
Hello N'Hello'
G'Day N'G''Day'
'G''Day' N'''G''''Day'''
I'''M N'I''''''M'
N'Hello' N'N''Hello'''

Implementation Note

JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource. An implementation of enquoteNCharLiteral may accept a different set of characters than that accepted by the same drivers implementation of enquoteLiteral.

Parameters
val:String

a character string

Returns:String

the result of replacing every single quote character in the argument by two single quote characters where this entire result is then prefixed with 'N'.

Exceptions
SQLException:
if a database access error occurs
NullPointerException:
if val is null
isSimpleIdentifierback to summary
pack-priv static boolean isSimpleIdentifier(String identifier) throws SQLException

Returns whether identifier is a simple SQL identifier. A simple SQL identifier is referred to as regular (or ordinary) identifier within the SQL standard. A regular identifier represents the name of a database object such as a table, column, or view.

The rules for a regular Identifier are:

  • The first character is an alphabetic character from a ('\u0061') through z ('\u007A'), or from A ('\u0041') through Z ('\u005A')
  • The name only contains alphanumeric characters or the character "_"
  • It cannot be a SQL reserved word

A datasource may have additional rules for a regular identifier such as:

  • Supports additional characters within the name based on the locale being used
  • Supports a different maximum length for the identifier

Implementation Specification

The default implementation uses the following criteria to determine a valid simple SQL identifier:

  • The identifier is not enclosed in double quotes
  • The first character is an alphabetic character from a through z, or from A through Z
  • The identifier only contains alphanumeric characters or the character "_"
  • The identifier is not a SQL reserved word
  • The identifier is between 1 and 128 characters in length inclusive
Examples of the conversion:
identifier Simple Identifier
Hello true
G'Day false
"Bruce Wayne" false
GoodDay$ false
Hello"World false
"Hello"World" false
"select" false
"from" false

Implementation Note

JDBC driver implementations may need to provide their own implementation of this method in order to meet the requirements of the underlying datasource.

Parameters
identifier:String

a SQL identifier

Returns:boolean

true if a simple SQL identifier, false otherwise

Exceptions
SQLException:
if a database access error occurs
NullPointerException:
if identifier is null