mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #4777 from AlphaHinex/h2visitor
H2OutputVisitor enhancement
This commit is contained in:
-1
@@ -19,7 +19,6 @@ import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import sun.security.pkcs11.Secmod;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@@ -16,13 +16,15 @@
|
||||
package com.alibaba.druid.sql.dialect.h2.visitor;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLReplaceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class H2OutputVisitor extends SQLASTOutputVisitor implements H2ASTVisitor {
|
||||
public H2OutputVisitor(Appendable appender) {
|
||||
@@ -81,4 +83,59 @@ public class H2OutputVisitor extends SQLASTOutputVisitor implements H2ASTVisitor
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateDatabaseStatement x) {
|
||||
|
||||
/*
|
||||
https://h2database.com/html/commands.html#create_schema
|
||||
CREATE SCHEMA [ IF NOT EXISTS ]
|
||||
{ name [ AUTHORIZATION ownerName ] | [ AUTHORIZATION ownerName ] }
|
||||
[ WITH tableEngineParamName [,...] ]
|
||||
*/
|
||||
|
||||
printUcase("CREATE SCHEMA ");
|
||||
|
||||
if (x.isIfNotExists()) {
|
||||
printUcase("IF NOT EXISTS ");
|
||||
}
|
||||
x.getName().accept(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateIndexStatement x) {
|
||||
|
||||
/* h2 CREATE INDEX BNF: https://h2database.com/html/commands.html#create_index
|
||||
CREATE [ UNIQUE | SPATIAL ] INDEX
|
||||
[ [ IF NOT EXISTS ] [schemaName.]indexName ]
|
||||
ON [schemaName.]tableName ( indexColumn [,...] )
|
||||
[ INCLUDE ( indexColumn [,...] ) ]
|
||||
*/
|
||||
|
||||
printUcase("CREATE ");
|
||||
|
||||
String type = x.getType();
|
||||
if ( "UNIQUE".equalsIgnoreCase(type) || "SPATIAL".equalsIgnoreCase(type) ) {
|
||||
printUcase(type + ' ');
|
||||
}
|
||||
|
||||
printUcase("INDEX ");
|
||||
|
||||
if (x.isIfNotExists()) {
|
||||
printUcase("IF NOT EXISTS ");
|
||||
}
|
||||
|
||||
x.getName().accept(this);
|
||||
|
||||
printUcase(" ON ");
|
||||
x.getTable().accept(this);
|
||||
print0(" (");
|
||||
printAndAccept(x.getItems(), ", ");
|
||||
print(')');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.alibaba.druid.sql.dialect.h2.visitor;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import org.junit.*;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class H2OutputVisitorTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void loadDriver() {
|
||||
org.h2.Driver.load();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void unloadDriver() {
|
||||
org.h2.Driver.unload();
|
||||
}
|
||||
|
||||
private Connection connection;
|
||||
|
||||
@Before
|
||||
public void initConnection() throws SQLException {
|
||||
connection = DriverManager.getConnection("jdbc:h2:mem:test;MODE=MySQL");
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeConnection() throws SQLException {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
private void executeUpdate(String sql) throws SQLException {
|
||||
try (Statement sqlStat = connection.createStatement()) {
|
||||
sqlStat.executeUpdate(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertCreateIndex() throws SQLException {
|
||||
String mysqlSql = "CREATE SCHEMA hinex;CREATE TABLE hinex.employees (jobTitle VARCHAR2(50));CREATE FULLTEXT INDEX hinex.jobTitle USING BTREE ON hinex.employees(jobTitle);";
|
||||
|
||||
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(mysqlSql, DbType.mysql);
|
||||
|
||||
String h2Sql = SQLUtils.toSQLString(sqlStatements, DbType.h2);
|
||||
|
||||
System.out.println(h2Sql);
|
||||
executeUpdate(h2Sql);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user