Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PostgreSQL datatype jsonb #714

Merged
merged 5 commits into from
Apr 11, 2018
Merged
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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
1. [ISSUE #652](https://github.com/shardingjdbc/sharding-jdbc/issues/652) Spring Boot Starter 2.x支持

### 缺陷修正
1. [ISSUE #628](https://github.com/shardingjdbc/sharding-jdbc/issues/628) 支持PostgreSql的数据类型jsonb
1. [ISSUE #646](https://github.com/shardingjdbc/sharding-jdbc/issues/646) 当SELECT ITEMS中的别名与GROUP BY或ORDER BY的真实列名对应时,无需补列

## 2.0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private void parseValues(final InsertStatement insertStatement) {
List<SQLExpression> sqlExpressions = new LinkedList<>();
do {
sqlExpressions.add(basicExpressionParser.parse(insertStatement));
skipsDoubleColon();
} while (lexerEngine.skipIfEqual(Symbol.COMMA));
insertStatement.setValuesListLastPosition(lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length());
int count = 0;
Expand Down Expand Up @@ -127,4 +128,10 @@ private void parseMultipleValues(final InsertStatement insertStatement) {
}
insertStatement.getSqlTokens().add(valuesToken);
}

private void skipsDoubleColon() {
if (lexerEngine.skipIfEqual(Symbol.DOUBLE_COLON)) {
lexerEngine.nextToken();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void parse(final ShardingRule shardingRule, final SQLStatement sqlStateme
private void parseConditions(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<SelectItem> items) {
do {
parseComparisonCondition(shardingRule, sqlStatement, items);
skipsDoubleColon();
} while (lexerEngine.skipIfEqual(DefaultKeyword.AND));
lexerEngine.unsupportedIfEqual(DefaultKeyword.OR);
}
Expand Down Expand Up @@ -174,6 +175,7 @@ private void parseInCondition(final ShardingRule shardingRule, final SQLStatemen
private void parseBetweenCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final SQLExpression left) {
List<SQLExpression> rights = new LinkedList<>();
rights.add(basicExpressionParser.parse(sqlStatement));
skipsDoubleColon();
lexerEngine.accept(DefaultKeyword.AND);
rights.add(basicExpressionParser.parse(sqlStatement));
Optional<Column> column = find(sqlStatement.getTables(), left);
Expand Down Expand Up @@ -253,4 +255,10 @@ private Optional<Column> getColumnWithOwner(final Tables tables, final SQLProper
private Optional<Column> getColumnWithoutOwner(final Tables tables, final SQLIdentifierExpression identifierExpression) {
return tables.isSingleTable() ? Optional.of(new Column(SQLUtil.getExactlyValue(identifierExpression.getName()), tables.getSingleTableName())) : Optional.<Column>absent();
}

private void skipsDoubleColon() {
if (lexerEngine.skipIfEqual(Symbol.DOUBLE_COLON)) {
lexerEngine.nextToken();
}
}
}
19 changes: 19 additions & 0 deletions sharding-core/src/test/resources/parser/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@
</or-conditions>
</parser-result>

<parser-result sql-case-id="assertInsertWithJsonAndGeo" parameters="7 200 100 200 '{&quot;rule&quot;:&quot;null&quot;}'">
<tables>assertInsertWithJsonAndGeo
<table name="t_place" />
</tables>
<tokens>
<table-token begin-position="12" original-literals="t_place" />
</tokens>
<or-conditions>
<and-conditions>
<condition column-name="user_new_id" table-name="t_place" operator="EQUAL">
<value index="0" literal="7" type="int" />
</condition>
<condition column-name="guid" table-name="t_place" operator="EQUAL">
<value index="1" literal="200" type="int"/>
</condition>
</and-conditions>
</or-conditions>
</parser-result>

<!-- // TODO
<parser-result sql-case-id="assertInsertSelect" sql="INSERT INTO `order` ('order_id', 'state') (SELECT 1, 'RUNNING' FROM dual UNION ALL SELECT 2, 'RUNNING' FROM dual )"">
<tables>
Expand Down
45 changes: 45 additions & 0 deletions sharding-core/src/test/resources/parser/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,49 @@
</and-conditions>
</or-conditions>
</parser-result>
<parser-result sql-case-id="assertSelectEqualsWithJsonAnGeo" parameters="'{&quot;rule2&quot;:&quot;null2&quot;}' 100 200 1">
<tables>
<table name="t_place" />
</tables>
<tokens>
<table-token begin-position="14" original-literals="t_place" />
</tokens>
<or-conditions>
<and-conditions>
<condition column-name="user_new_id" table-name="t_place" operator="EQUAL">
<value index="3" literal="1" type="int" />
</condition>
</and-conditions>
</or-conditions>
</parser-result>
<parser-result sql-case-id="assertSelectInWithJsonAnGeo" parameters="'{&quot;rule2&quot;:&quot;null2&quot;}' '{&quot;rule3&quot;:&quot;null3&quot;}' 100 200 1">
<tables>
<table name="t_place" />
</tables>
<tokens>
<table-token begin-position="14" original-literals="t_place" />
</tokens>
<or-conditions>
<and-conditions>
<condition column-name="user_new_id" table-name="t_place" operator="EQUAL">
<value index="4" literal="1" type="int" />
</condition>
</and-conditions>
</or-conditions>
</parser-result>
<parser-result sql-case-id="assertSelectBetweenWithJsonAnGeo" parameters="'{&quot;rule2&quot;:&quot;null2&quot;}' '{&quot;rule3&quot;:&quot;null3&quot;}' 100 200 1">
<tables>
<table name="t_place" />
</tables>
<tokens>
<table-token begin-position="14" original-literals="t_place" />
</tokens>
<or-conditions>
<and-conditions>
<condition column-name="user_new_id" table-name="t_place" operator="EQUAL">
<value index="4" literal="1" type="int" />
</condition>
</and-conditions>
</or-conditions>
</parser-result>
</parser-result-sets>
2 changes: 1 addition & 1 deletion sharding-core/src/test/resources/parser/update.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</or-conditions>
</parser-result>

<parser-result sql-case-id="assertUpdateWithGeoInPostgreSQL" parameters="'2017-06-07' 100 200 '{&quot;rule2&quot;:&quot;null2&quot;}' 3 5 7 200">
<parser-result sql-case-id="assertUpdateWithJsonAndGeo" parameters="'2017-06-07' 100 200 '{&quot;rule2&quot;:&quot;null2&quot;}' 3 5 7 200">
<tables>
<table name="t_place" />
</tables>
Expand Down
1 change: 1 addition & 0 deletions sharding-sql-test/src/main/resources/sql/dml/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<sql-case id="assertInsertWithPartialPlaceholder" value="INSERT INTO t_order (order_id, user_id, status) VALUES (%s, %s, 'insert')" />
<sql-case id="assertInsertWithGenerateKeyColumn" value="INSERT INTO t_order_item(item_id, order_id, user_id, status) values(%s, %s, %s, 'insert')" />
<sql-case id="assertInsertWithoutGenerateKeyColumn" value="INSERT INTO t_order_item(order_id, user_id, status) values(%s, %s, 'insert')" />
<sql-case id="assertInsertWithJsonAndGeo" value="INSERT INTO t_place(user_new_id, guid, start_point,rule) VALUES (%s, %s, ST_GeographyFromText('SRID=4326;POINT('||%s||' '||%s||')'), %s::jsonb)" db-types="PostgreSQL" />
</sql-cases>
2 changes: 1 addition & 1 deletion sharding-sql-test/src/main/resources/sql/dml/update.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<sql-cases>
<sql-case id="assertUpdateWithAlias" value="UPDATE t_order AS o SET o.status = %s WHERE o.order_id = %s AND o.user_id = %s" db-types="MySQL,H2" />
<sql-case id="assertUpdateWithoutAlias" value="UPDATE t_order SET status = %s WHERE order_id = %s AND user_id = %s" />
<sql-case id="assertUpdateWithGeoInPostgreSQL" value="UPDATE t_place SET start_time = %s, status = 0, start_point = ST_GeographyFromText('SRID=4326;POINT('||%s||' '||%s||')'), rule = %s::jsonb, discount_type = %s, order_type = %s WHERE user_new_id = %s AND guid = %s" db-types="PostgreSQL" />
<sql-case id="assertUpdateWithJsonAndGeo" value="UPDATE t_place SET start_time = %s, status = 0, start_point = ST_GeographyFromText('SRID=4326;POINT('||%s||' '||%s||')'), rule = %s::jsonb, discount_type = %s, order_type = %s WHERE user_new_id = %s AND guid = %s" db-types="PostgreSQL" />
</sql-cases>
3 changes: 3 additions & 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,7 @@
<sql-case id="assertSelectCountWithBindingTableWithJoin" value="SELECT COUNT(*) AS items_count FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s" />
<sql-case id="assertSelectAliasWithKeyword" value="SELECT length.item_id password FROM t_order_item length where length.item_id = %s " db-types="MySQL,H2,SQLServer,Oracle" />
<sql-case id="assertSelectWithJoinForceIndex" value="SELECT o.*,i.* FROM t_order o FORCE INDEX(order_index) JOIN t_order_item i WHERE o.order_id = %s" db-types="MySQL" />
<sql-case id="assertSelectEqualsWithJsonAnGeo" value="SELECT * FROM t_place WHERE rule = %s::jsonb AND start_point=ST_GeographyFromText('SRID=4326;POINT('||%s||' '||%s||')') AND user_new_id = %s" db-types="PostgreSQL" />
<sql-case id="assertSelectInWithJsonAnGeo" value="SELECT * FROM t_place WHERE rule IN (%s::jsonb, %s::jsonb) AND start_point=ST_GeographyFromText('SRID=4326;POINT('||%s||' '||%s||')') AND user_new_id = %s" db-types="PostgreSQL" />
<sql-case id="assertSelectBetweenWithJsonAnGeo" value="SELECT * FROM t_place WHERE rule BETWEEN %s::jsonb AND %s::jsonb AND start_point=ST_GeographyFromText('SRID=4326;POINT('||%s||' '||%s||')') AND user_new_id = %s" db-types="PostgreSQL" />
</sql-cases>