mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved pg support, fix #4021
This commit is contained in:
@@ -37,6 +37,7 @@ public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLCrea
|
||||
protected List<SQLAssignItem> properties = new ArrayList<SQLAssignItem>();
|
||||
protected List<SQLAssignItem> tableProperties = new ArrayList<SQLAssignItem>();
|
||||
protected boolean storing;
|
||||
protected boolean ifNotExists;
|
||||
|
||||
public SQLCreateIndexStatement() {
|
||||
indexDefinition.setParent(this);
|
||||
@@ -162,6 +163,7 @@ public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLCrea
|
||||
public SQLCreateIndexStatement clone() {
|
||||
SQLCreateIndexStatement x = new SQLCreateIndexStatement();
|
||||
indexDefinition.cloneTo(x.indexDefinition);
|
||||
x.setIfNotExists(ifNotExists);
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -318,4 +320,12 @@ public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLCrea
|
||||
public List<SQLSelectOrderByItem> getColumns() {
|
||||
return indexDefinition.getColumns();
|
||||
}
|
||||
|
||||
public boolean isIfNotExists() {
|
||||
return ifNotExists;
|
||||
}
|
||||
|
||||
public void setIfNotExists(boolean ifNotExists) {
|
||||
this.ifNotExists = ifNotExists;
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -608,6 +608,13 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
|
||||
accept(Token.INDEX);
|
||||
|
||||
if (lexer.token() == Token.IF) {
|
||||
lexer.nextToken();
|
||||
accept(Token.NOT);
|
||||
accept(Token.EXISTS);
|
||||
stmt.setIfNotExists(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CONCURRENTLY)) {
|
||||
lexer.nextToken();
|
||||
stmt.setConcurrently(true);
|
||||
|
||||
@@ -2401,6 +2401,10 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
print0(ucase ? "INDEX" : "index");
|
||||
|
||||
if (x.isIfNotExists()) {
|
||||
print0(ucase ? " IF NOT EXISTS" : " if not exists");
|
||||
}
|
||||
|
||||
if (x.isConcurrently()) {
|
||||
print0(ucase ? " CONCURRENTLY" : " concurrently");
|
||||
}
|
||||
|
||||
@@ -52,4 +52,29 @@ public class PGCreateIndexTest_0 extends PGTest {
|
||||
assertEquals(1, visitor.getColumns().size() );
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
String sql = "CREATE INDEX if not exists \"calculate_measure_dataset_id_idx\" ON \"calculate_measure\" USING btree (\"dataset_id\");";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals("CREATE INDEX IF NOT EXISTS \"calculate_measure_dataset_id_idx\" ON \"calculate_measure\" USING btree (\"dataset_id\");", SQLUtils.toPGString(stmt));
|
||||
|
||||
assertEquals("create index if not exists \"calculate_measure_dataset_id_idx\" on \"calculate_measure\" using btree (\"dataset_id\");", 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);
|
||||
//
|
||||
// assertEquals(1, visitor.getColumns().size() );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user