-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve escaping of identifiers (#3295)
- Loading branch information
Showing
15 changed files
with
158 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
ksql-parser/src/main/java/io/confluent/ksql/util/IdentifierUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.util; | ||
|
||
import io.confluent.ksql.parser.CaseInsensitiveStream; | ||
import io.confluent.ksql.parser.SqlBaseLexer; | ||
import io.confluent.ksql.parser.SqlBaseParser; | ||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
|
||
public final class IdentifierUtil { | ||
|
||
private IdentifierUtil() { } | ||
|
||
/** | ||
* @param identifier the identifier | ||
* @return whether or not {@code identifier} is a valid identifier without quotes | ||
*/ | ||
public static boolean needsQuotes(final String identifier) { | ||
final SqlBaseLexer sqlBaseLexer = new SqlBaseLexer( | ||
new CaseInsensitiveStream(CharStreams.fromString(identifier))); | ||
final CommonTokenStream tokenStream = new CommonTokenStream(sqlBaseLexer); | ||
final SqlBaseParser sqlBaseParser = new SqlBaseParser(tokenStream); | ||
|
||
// don't log or print anything in the case of error since this is expected | ||
// for this method | ||
sqlBaseLexer.removeErrorListeners(); | ||
sqlBaseParser.removeErrorListeners(); | ||
|
||
sqlBaseParser.identifier(); | ||
|
||
// needs quotes if the `identifier` was not able to read the entire line | ||
return sqlBaseParser.getNumberOfSyntaxErrors() != 0 | ||
|| sqlBaseParser.getCurrentToken().getCharPositionInLine() != identifier.length(); | ||
} | ||
|
||
/** | ||
* @param identifier the identifier to escape | ||
* @return wraps the {@code identifier} in back quotes (`) if {@link #needsQuotes(String)} | ||
*/ | ||
public static String escape(final String identifier) { | ||
return needsQuotes(identifier) ? '`' + identifier + '`' : identifier; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.