improve sql parser postgresql support

This commit is contained in:
shaojin.wensj
2023-02-19 19:10:47 +08:00
parent 3f33301e24
commit 47d2a88913
2 changed files with 35 additions and 1 deletions
@@ -638,7 +638,15 @@ public class PGSQLStatementParser extends SQLStatementParser {
accept(Token.LPAREN);
for (; ; ) {
SQLSelectOrderByItem item = this.exprParser.parseSelectOrderByItem();
SQLSelectOrderByItem item;
item = this.exprParser.parseSelectOrderByItem();
if (lexer.identifierEquals("jsonb_path_ops") && item.getExpr() instanceof SQLIdentifierExpr) {
String ident = ((SQLIdentifierExpr) item.getExpr()).getName() + " " + lexer.stringVal();
lexer.nextToken();
item.setExpr(new SQLIdentifierExpr(ident));
}
item.setParent(stmt);
stmt.addItem(item);
if (lexer.token() == Token.COMMA) {
@@ -70,6 +70,32 @@ public class PGCreateIndexTest_0 extends PGTest {
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("public.city")));
//
// assertTrue(visitor.getTables().get(new TableStat.Name("public.city")).getCreateIndexCount() == 1);
//
// assertEquals(1, visitor.getColumns().size() );
}
public void test_2() throws Exception {
String sql = "create index if not exists idx_json_data_path on yida_entity_instance using gin(json_data jsonb_path_ops)";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals("CREATE INDEX IF NOT EXISTS idx_json_data_path ON yida_entity_instance USING gin (json_data jsonb_path_ops)", SQLUtils.toPGString(stmt));
assertEquals("create index if not exists idx_json_data_path on yida_entity_instance using gin (json_data jsonb_path_ops)", stmt.toLowerCaseString());
assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("public.city")));
//
// assertTrue(visitor.getTables().get(new TableStat.Name("public.city")).getCreateIndexCount() == 1);