Skip to content

Commit

Permalink
Merge pull request #1049 from tuohai666/dev
Browse files Browse the repository at this point in the history
for #993, distinguish PostgreSQL from other databases when parsing Identifier and Chars
  • Loading branch information
terrymanu authored Jul 25, 2018
2 parents 94d8dc6 + 16dd45b commit ae89665
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private boolean isIdentifierBegin() {
return isIdentifierBegin(getCurrentChar(0));
}

private boolean isIdentifierBegin(final char ch) {
protected boolean isIdentifierBegin(final char ch) {
return CharType.isAlphabet(ch) || '`' == ch || '_' == ch || '$' == ch;
}

Expand All @@ -126,7 +126,7 @@ private boolean isSymbolBegin() {
return CharType.isSymbol(getCurrentChar(0));
}

private boolean isCharsBegin() {
protected boolean isCharsBegin() {
return '\'' == getCurrentChar(0) || '\"' == getCurrentChar(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public Token scanIdentifier() {
int length = getLengthUntilTerminatedChar('`');
return new Token(Literals.IDENTIFIER, input.substring(offset, offset + length), offset + length);
}
if ('"' == charAt(offset)) {
int length = getLengthUntilTerminatedChar('"');
return new Token(Literals.IDENTIFIER, input.substring(offset, offset + length), offset + length);
}
int length = 0;
while (isIdentifierChar(charAt(offset + length))) {
length++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.shardingsphere.core.parsing.lexer.dialect.postgresql;

import io.shardingsphere.core.parsing.lexer.Lexer;
import io.shardingsphere.core.parsing.lexer.analyzer.CharType;
import io.shardingsphere.core.parsing.lexer.analyzer.Dictionary;

/**
Expand All @@ -32,4 +33,14 @@ public final class PostgreSQLLexer extends Lexer {
public PostgreSQLLexer(final String input) {
super(input, dictionary);
}

@Override
protected boolean isIdentifierBegin(final char ch) {
return CharType.isAlphabet(ch) || '\"' == ch || '_' == ch;
}

@Override
protected boolean isCharsBegin() {
return '\'' == getCurrentChar(0);
}
}
19 changes: 16 additions & 3 deletions sharding-core/src/test/resources/parser/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
</and-condition>
</or-condition>
</parser-result>

<parser-result sql-case-id="select_equal_with_geography" parameters="'{&quot;rule2&quot;:&quot;null2&quot;}', 100, 200, 1">
<tables>
<table name="t_place" />
Expand All @@ -329,7 +329,7 @@
</and-condition>
</or-condition>
</parser-result>

<parser-result sql-case-id="select_in_with_geography" parameters="'{&quot;rule2&quot;:&quot;null2&quot;}', '{&quot;rule3&quot;:&quot;null3&quot;}', 100, 200, 1">
<tables>
<table name="t_place" />
Expand All @@ -345,7 +345,7 @@
</and-condition>
</or-condition>
</parser-result>

<parser-result sql-case-id="select_between_with_geography" parameters="'{&quot;rule2&quot;:&quot;null2&quot;}', '{&quot;rule3&quot;:&quot;null3&quot;}', 100, 200, 1">
<tables>
<table name="t_place" />
Expand All @@ -361,4 +361,17 @@
</and-condition>
</or-condition>
</parser-result>

<parser-result sql-case-id="select_with_double_quotes" parameters="1">
<tables>
<table name="t_order_item" />
</tables>
<tokens>
<table-token begin-position="14" original-literals="&quot;t_order_item&quot;" />
</tokens>
<order-by-columns>
<order-by-column name="item_id" order-direction="ASC" />
</order-by-columns>
</parser-result>

</parser-result-sets>
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,9 @@
<dql-test-case sql-case-id="select_sub_query_with_group_by">
<assertion expected-data-file="select_sub_query_with_group_by.xml" />
</dql-test-case>

<dql-test-case sql-case-id="select_with_double_quotes">
<assertion parameters="100001:int" expected-data-file="select_not_equal_with_single_table.xml" />
</dql-test-case>

</integrate-test-cases>
1 change: 1 addition & 0 deletions sharding-sql-test/src/main/resources/sql/dql/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<sql-case id="select_equal_with_geography" value="SELECT * FROM t_place WHERE rule = ?::jsonb AND start_point=ST_GeographyFromText('SRID=4326;POINT('||?||' '||?||')') AND user_new_id = ?" db-types="PostgreSQL" />
<sql-case id="select_in_with_geography" value="SELECT * FROM t_place WHERE rule IN (?::jsonb, ?::jsonb) AND start_point=ST_GeographyFromText('SRID=4326;POINT('||?||' '||?||')') AND user_new_id = ?" db-types="PostgreSQL" />
<sql-case id="select_between_with_geography" value="SELECT * FROM t_place WHERE rule BETWEEN ?::jsonb AND ?::jsonb AND start_point=ST_GeographyFromText('SRID=4326;POINT('||?||' '||?||')') AND user_new_id = ?" db-types="PostgreSQL" />
<sql-case id="select_with_double_quotes" value="SELECT * FROM &quot;t_order_item&quot; WHERE &quot;item_id&quot; != ? ORDER BY &quot;item_id&quot;" db-types="PostgreSQL" />
</sql-cases>

0 comments on commit ae89665

Please sign in to comment.