refactor sql parser & support tableMapping. issue #1432

This commit is contained in:
wenshao
2016-09-25 05:06:50 +08:00
parent 2e478b1869
commit a26e4c546d
33 changed files with 430 additions and 177 deletions
@@ -21,17 +21,13 @@ public class SQLServerIfTest extends TestCase {
String sql = "if @is_exists is null begin insert into Inventory_1 (co_id,sku_id,order_lock) values (@0,@1,@2) end else begin insert into Inventory_2 (co_id,sku_id,order_lock) values (@0,@1,@2) end ";
String expect = "IF @is_exists IS NULL"//
+ "\n\tBEGIN"//
+ "\n\t\tINSERT INTO Inventory_1"//
+ "\n\t\t\t(co_id, sku_id, order_lock)"//
+ "\n\t\tVALUES"//
+ "\n\t\t(@0, @1, @2);"//
+ "\n\t\tINSERT INTO Inventory_1 (co_id, sku_id, order_lock)"//
+ "\n\t\tVALUES (@0, @1, @2);"//
+ "\n\tEND"//
+ "\nELSE"//
+ "\n\tBEGIN"//
+ "\n\t\tINSERT INTO Inventory_2"//
+ "\n\t\t\t(co_id, sku_id, order_lock)"//
+ "\n\t\tVALUES"//
+ "\n\t\t(@0, @1, @2);"//
+ "\n\t\tINSERT INTO Inventory_2 (co_id, sku_id, order_lock)"//
+ "\n\t\tVALUES (@0, @1, @2);"//
+ "\n\tEND";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
@@ -28,10 +28,8 @@ public class SQLServerInsertTest extends TestCase {
String sql = "INSERT INTO Cities (Location)" + //
"VALUES ( CONVERT(Point, '12.3:46.2') );";
String expect = "INSERT INTO Cities" + //
"\n\t(Location)" + //
"\nVALUES" + //
"\n(CONVERT(Point, '12.3:46.2'))";
String expect = "INSERT INTO Cities (Location)" + //
"\nVALUES (CONVERT(Point, '12.3:46.2'))";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
@@ -28,10 +28,8 @@ public class SQLServerInsertTest1 extends TestCase {
String sql = "INSERT INTO Cities (Location)" +
"VALUES ( dbo.CreateNewPoint(x, y) );";
String expect = "INSERT INTO Cities" + //
"\n\t(Location)" + //
"\nVALUES" + //
"\n(dbo.CreateNewPoint(x, y))";
String expect = "INSERT INTO Cities (Location)" + //
"\nVALUES (dbo.CreateNewPoint(x, y))";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
@@ -29,10 +29,8 @@ public class SQLServerInsertTest2 extends TestCase {
String sql = "INSERT INTO test (name)" + //
"VALUES (N'a'), (N'b'), (N'c')";
String expect = "INSERT INTO test" + //
"\n\t(name)" + //
"\nVALUES" + //
"\n(N'a')," + //
String expect = "INSERT INTO test (name)" + //
"\nVALUES (N'a')," + //
"\n(N'b')," + //
"\n(N'c')";
@@ -29,8 +29,7 @@ public class SQLServerInsertTest3 extends TestCase {
"VALUES (N'F2', N'Square Feet', GETDATE());;";
String expect = "INSERT INTO Production.UnitMeasure" + //
"\nVALUES" + //
"\n(N'F2', N'Square Feet', GETDATE())";
"\nVALUES (N'F2', N'Square Feet', GETDATE())";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
@@ -48,9 +48,8 @@ public class SQLServerInsertTest5 extends TestCase {
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
String formatSql = "INSERT INTO t"//
+ "\n\t(a, b)"//
+ "\nVALUES" + "\n('a1', 'b1'),"//
String formatSql = "INSERT INTO t (a, b)"//
+ "\nVALUES ('a1', 'b1'),"//
+ "\n('a2', 'b2'),"//
+ "\n('a3', 'b3')";
Assert.assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
@@ -74,10 +73,8 @@ public class SQLServerInsertTest5 extends TestCase {
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
String formatSql = "INSERT INTO t"//
+ "\n\t(a, b)"//
+ "\nVALUES"//
+ "\n('a1', 'b1'),"//
String formatSql = "INSERT INTO t (a, b)"//
+ "\nVALUES ('a1', 'b1'),"//
+ "\n('a2', 'b2'),"//
+ "\n('a3', 'b3'),"//
+ "\n('a4', 'b4')";
@@ -48,10 +48,8 @@ public class SQLServerInsertTest6 extends TestCase {
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
String formatSql = "INSERT INTO [dbo].[SurveyAnswer]"//
+ "\n\t([CustomerId], [QuestionId], [OptionId], [CreateTime], [LastUpdateTime])"//
+ "\nVALUES" //
+ "\n(@0, @1, @2, @3, @4)";
String formatSql = "INSERT INTO [dbo].[SurveyAnswer] ([CustomerId], [QuestionId], [OptionId], [CreateTime], [LastUpdateTime])"//
+ "\nVALUES (@0, @1, @2, @3, @4)";
Assert.assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
}
@@ -51,8 +51,7 @@ public class SQLServerInsertTest8 extends TestCase {
String formatSql = "INSERT INTO Production.ScrapReason"//
+ "\nOUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate"//
+ "\n\tINTO @MyTableVar"//
+ "\nVALUES"//
+ "\n(N'Operator error', GETDATE())";
+ "\nVALUES (N'Operator error', GETDATE())";
Assert.assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
}