Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.spanner.connection.AbstractStatementParser.ParametersInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
import java.io.Serializable;
import java.util.Collections;
Expand Down Expand Up @@ -320,7 +321,7 @@ public static final class StatementFactory {
private final Dialect dialect;

StatementFactory(Dialect dialect) {
this.dialect = dialect;
this.dialect = checkNotNull(dialect);
}

public Statement of(String sql) {
Expand Down Expand Up @@ -353,26 +354,45 @@ public Statement of(String sql) {
*
* <p>
*
* @param sql SQL statement with unnamed parameters denoted as ?
* @param sql SQL statement with unnamed parameters denoted as {@code ?}
* @param values positional list of values for the unnamed parameters in the SQL string
* @return Statement a statement that can be executed on Spanner
* @throws NullPointerException if {@code sql} or {@code values} is {@code null}
* @throws SpannerException with {@link ErrorCode#INVALID_ARGUMENT} if the number of {@code ?}
* positional parameters in {@code sql} does not match {@code values.length} (only {@code ?}
* placeholders outside string literals, quoted identifiers, and comments are counted;
* dialect-native placeholders such as {@code @p1} or {@code $1} are not counted) or if
* {@code sql} contains an unclosed literal
* @see DatabaseClient#getStatementFactory
*/
public Statement withUnnamedParameters(String sql, Object... values) {
Map<String, Value> parameters = getUnnamedParametersMap(values);
checkNotNull(sql);
checkNotNull(values);
AbstractStatementParser statementParser = AbstractStatementParser.getInstance(this.dialect);
ParametersInfo parametersInfo =
statementParser.convertPositionalParametersToNamedParameters('?', sql);
return new Statement(parametersInfo.sqlWithNamedParameters, parameters, null);
if (parametersInfo.numberOfParameters != values.length) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
String.format(
"The number of positional parameters (?) in the SQL statement (%d) does not match"
+ " the number of provided values (%d).",
parametersInfo.numberOfParameters, values.length));
}
if (values.length == 0) {
return Statement.of(parametersInfo.sqlWithNamedParameters);
}
return new Statement(
parametersInfo.sqlWithNamedParameters, getUnnamedParametersMap(values), null);
}

private Map<String, Value> getUnnamedParametersMap(Object[] values) {
Map<String, Value> parameters = new HashMap<>();
Map<String, Value> parameters = Maps.newHashMapWithExpectedSize(values.length);
int index = 1;
for (Object value : values) {
parameters.put(getParameterName(index++), Value.toValue(value));
}
return parameters;
return Collections.unmodifiableMap(parameters);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.spanner.connection;

import static com.google.cloud.spanner.connection.SimpleParser.isValidIdentifierChar;
import static com.google.cloud.spanner.connection.SimpleParser.isValidIdentifierFirstChar;
import static com.google.cloud.spanner.connection.StatementHintParser.convertHintsToOptions;

import com.google.api.core.InternalApi;
Expand Down Expand Up @@ -920,6 +921,12 @@ public boolean checkReturningClause(String sql) {
/** Returns true if the dialect supports starting an escape sequence with a backslash. */
abstract boolean supportsBackslashEscape();

/**
* Returns true if the dialect supports escape string constants prefixed with {@code E} or {@code
* e} (e.g. {@code E'foo\'bar'}).
*/
abstract boolean supportsEscapeStringConstants();

/**
* Returns true if the dialect supports single-line comments that start with a dash.
*
Expand All @@ -937,16 +944,18 @@ public boolean checkReturningClause(String sql) {
/** Returns the query parameter prefix that should be used for this dialect. */
abstract String getQueryParameterPrefix();

/** Reads a dollar-quoted string literal from position index in the given sql string. */
/** Reads a dollar-quoted string literal tag from position index in the given sql string. */
String parseDollarQuotedString(String sql, int index) {
// Look ahead to the next dollar sign (if any). Everything in between is the quote tag.
// Look ahead to the next dollar sign (if any). A non-empty dollar-quote tag follows unquoted
// identifier rules: it must start with a letter or underscore and cannot contain '$'.
StringBuilder tag = new StringBuilder();
int startIndex = index;
while (index < sql.length()) {
char c = sql.charAt(index);
if (c == DOLLAR) {
return tag.toString();
}
if (!isValidIdentifierChar(c)) {
if (index == startIndex ? !isValidIdentifierFirstChar(c) : !isValidIdentifierChar(c)) {
break;
}
tag.append(c);
Expand All @@ -970,7 +979,22 @@ int skip(String sql, int currentIndex, @Nullable StringBuilder result) {
|| (supportsBacktickQuote() && currentChar == BACKTICK_QUOTE)) {
appendIfNotNull(result, currentChar);
return skipQuoted(sql, currentIndex, currentChar, result);
} else if (supportsDollarQuotedStrings() && currentChar == DOLLAR) {
} else if (supportsEscapeStringConstants()
&& (currentChar == 'E' || currentChar == 'e')
&& sql.length() > (currentIndex + 1)
&& sql.charAt(currentIndex + 1) == SINGLE_QUOTE
&& (currentIndex == 0 || !isValidIdentifierChar(sql.charAt(currentIndex - 1)))) {
// PostgreSQL escape string constants (E'...' / e'...') interpret backslash escapes even
// though standard PostgreSQL string literals do not, and can only start at a token boundary.
appendIfNotNull(result, currentChar);
appendIfNotNull(result, SINGLE_QUOTE);
return skipQuoted(
sql, currentIndex + 1, SINGLE_QUOTE, null, result, /* allowBackslashEscape= */ true);
} else if (supportsDollarQuotedStrings()
&& currentChar == DOLLAR
&& (currentIndex == 0 || !isValidIdentifierChar(sql.charAt(currentIndex - 1)))) {
// Unquoted PostgreSQL identifiers may contain '$', so a dollar-quoted literal can only start
// when '$' is not preceded by an identifier character.
String dollarTag = parseDollarQuotedString(sql, currentIndex + 1);
if (dollarTag != null) {
appendIfNotNull(result, currentChar, dollarTag, currentChar);
Expand Down Expand Up @@ -1015,16 +1039,17 @@ static int skipSingleLineComment(
/** Skips a multi-line comment from startIndex and adds it to result if result is not null. */
int skipMultiLineComment(String sql, int startIndex, @Nullable StringBuilder result) {
SimpleParser simpleParser = new SimpleParser(getDialect(), sql, startIndex, false);
if (simpleParser.skipMultiLineComment()) {
appendIfNotNull(result, sql.substring(startIndex, simpleParser.getPos()));
}
simpleParser.skipMultiLineComment();
// Always preserve the comment text even when unterminated so malformed SQL is not truncated
// before reaching the server.
appendIfNotNull(result, sql.substring(startIndex, simpleParser.getPos()));
return simpleParser.getPos();
}

/** Skips a quoted string from startIndex. */
private int skipQuoted(
String sql, int startIndex, char startQuote, @Nullable StringBuilder result) {
return skipQuoted(sql, startIndex, startQuote, null, result);
return skipQuoted(sql, startIndex, startQuote, null, result, supportsBackslashEscape());
}

/**
Expand All @@ -1037,6 +1062,16 @@ int skipQuoted(
char startQuote,
@Nullable String dollarTag,
@Nullable StringBuilder result) {
return skipQuoted(sql, startIndex, startQuote, dollarTag, result, supportsBackslashEscape());
}

int skipQuoted(
String sql,
int startIndex,
char startQuote,
@Nullable String dollarTag,
@Nullable StringBuilder result,
boolean allowBackslashEscape) {
boolean isTripleQuoted =
supportsTripleQuotedStrings()
&& sql.length() > startIndex + 2
Expand Down Expand Up @@ -1080,7 +1115,7 @@ int skipQuoted(
appendIfNotNull(result, currentChar);
return currentIndex + 1;
}
} else if (supportsBackslashEscape()
} else if (allowBackslashEscape
&& currentChar == BACKSLASH
&& length > currentIndex + 1
&& (sql.charAt(currentIndex + 1) == startQuote
Expand All @@ -1092,7 +1127,9 @@ int skipQuoted(
appendIfNotNull(result, sql.charAt(currentIndex + 1));
currentIndex += 2;
continue;
} else if (currentChar == '\n' && !isTripleQuoted && !supportsLineFeedInQuotedString()) {
} else if ((currentChar == '\n' || currentChar == '\r')
&& !isTripleQuoted
&& !supportsLineFeedInQuotedString()) {
break;
}
currentIndex++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ boolean supportsBackslashEscape() {
return false;
}

@Override
boolean supportsEscapeStringConstants() {
return true;
}

@Override
boolean supportsHashSingleLineComments() {
return false;
Expand Down Expand Up @@ -120,9 +125,9 @@ String removeCommentsAndTrimInternal(String sql) {
while (index < length) {
char c = sql.charAt(index);
if (isInSingleLineComment) {
if (c == '\n') {
if (c == '\n' || c == '\r') {
isInSingleLineComment = false;
// Include the line feed in the result.
// Include the line break in the result.
res.append(c);
}
} else if (multiLineCommentLevel > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,18 @@ void skipWhitespaces() {
*/
@VisibleForTesting
boolean skipSingleLineComment(int prefixLength) {
int endIndex = sql.indexOf('\n', pos + prefixLength);
if (endIndex == -1) {
pos = sql.length();
return true;
for (int i = pos + prefixLength; i < sql.length(); i++) {
char c = sql.charAt(i);
if (c == '\n') {
pos = i + 1;
return true;
}
if (c == '\r') {
pos = (i + 1 < sql.length() && sql.charAt(i + 1) == '\n') ? i + 2 : i + 1;
return true;
}
}
pos = endIndex + 1;
pos = sql.length();
return true;
}

Expand All @@ -394,13 +400,18 @@ boolean skipMultiLineComment() {
&& sql.length() > (pos + 1)
&& sql.charAt(pos + 1) == ASTERISK) {
level++;
// Advance past both characters of '/*' so '/*/*/' cannot reuse '*' as '*/'.
pos += 2;
continue;
}
if (sql.charAt(pos) == ASTERISK && sql.length() > (pos + 1) && sql.charAt(pos + 1) == SLASH) {
level--;
// Advance past both characters of '*/' so '*/*' cannot reuse '/' as '/*'.
pos += 2;
if (level == 0) {
pos += 2;
return true;
}
continue;
}
pos++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ boolean supportsBackslashEscape() {
return true;
}

@Override
boolean supportsEscapeStringConstants() {
return false;
}

@Override
boolean supportsHashSingleLineComments() {
return true;
Expand Down Expand Up @@ -153,9 +158,9 @@ String removeCommentsAndTrimInternal(String sql) {
} else {
// We are not in a quoted string.
if (isInSingleLineComment) {
if (c == '\n') {
if (c == '\n' || c == '\r') {
isInSingleLineComment = false;
// Include the line feed in the result.
// Include the line break in the result.
res.append(c);
}
} else if (isInMultiLineComment) {
Expand Down
Loading
Loading