mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql server parser
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerSelectTest10 extends TestCase {
|
||||
|
||||
public void test_isEmpty() throws Exception {
|
||||
String sql = "SELECT c.FirstName, c.LastName, e.Title, a.AddressLine1, a.City" + //
|
||||
" , sp.Name AS [State/Province], a.PostalCode" + //
|
||||
" INTO dbo.EmployeeAddresses" + //
|
||||
" FROM Person.Contact AS c" + //
|
||||
" JOIN HumanResources.Employee AS e ON e.ContactID = c.ContactID" + //
|
||||
" JOIN HumanResources.EmployeeAddress AS ea ON ea.EmployeeID = e.EmployeeID" + //
|
||||
" JOIN Person.Address AS a on a.AddressID = ea.AddressID" + //
|
||||
" JOIN Person.StateProvince as sp ON sp.StateProvinceID = a.StateProvinceID;";
|
||||
|
||||
String expect = "SELECT c.FirstName, c.LastName, e.Title, a.AddressLine1, a.City" + //
|
||||
"\n\t, sp.Name AS [State/Province], a.PostalCode" + //
|
||||
"\nINTO dbo.EmployeeAddresses" + //
|
||||
"\nFROM Person.Contact c" + //
|
||||
"\n\tJOIN HumanResources.Employee e ON e.ContactID = c.ContactID" + //
|
||||
"\n\tJOIN HumanResources.EmployeeAddress ea ON ea.EmployeeID = e.EmployeeID" + //
|
||||
"\n\tJOIN Person.Address a ON a.AddressID = ea.AddressID" + //
|
||||
"\n\tJOIN Person.StateProvince sp ON sp.StateProvinceID = a.StateProvinceID";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class SQLServerSelectTest5 extends TestCase {
|
||||
String sql = "SELECT name, password_hash FROM [master].[sys].sql_logins";
|
||||
|
||||
String expect = "SELECT name, password_hash\n" + //
|
||||
"FROM master.sys.sql_logins";
|
||||
"FROM [master].[sys].sql_logins";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerSelectTest8 extends TestCase {
|
||||
|
||||
public void test_isEmpty() throws Exception {
|
||||
String sql = "SELECT Location.Distance (@p)" + " FROM Cities;";
|
||||
|
||||
String expect = "SELECT Location.Distance(@p)" + //
|
||||
"\nFROM Cities";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerSelectTest9 extends TestCase {
|
||||
|
||||
public void test_isEmpty() throws Exception {
|
||||
String sql = "SELECT AVG(UnitPrice) AS 'Average Price' " + //
|
||||
"FROM Sales.SalesOrderDetail;";
|
||||
|
||||
String expect = "SELECT AVG(UnitPrice) AS 'Average Price'" + //
|
||||
"\nFROM Sales.SalesOrderDetail";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,8 @@ public class SQLServerUpdateTest extends TestCase {
|
||||
"\nFROM reg_student_charge_item a, (SELECT a.FId, CASE WHEN SUM(b.FChargeAmount) IS NULL THEN 0 ELSE SUM(b.FChargeAmount) END AS amount" +
|
||||
"\n\t\tFROM reg_student_charge_item a" +
|
||||
"\n\t\t\tLEFT JOIN reg_student_charge_daybook b ON a.FId = b.FChargeItemId" +
|
||||
"\n\t\tWHERE a.FId = ? GROUP BY a.FId" +
|
||||
"\n\t\tWHERE a.FId = ?" +
|
||||
"\n\t\tGROUP BY a.FId" +
|
||||
"\n\t\t) b" +
|
||||
"\nWHERE a.FId = b.FId" +
|
||||
"\nAND a.FId = ?";
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest1 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE dbo.Table2 " + //
|
||||
"SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB " + //
|
||||
"FROM dbo.Table2 " + //
|
||||
" INNER JOIN dbo.Table1 " + //
|
||||
" ON (dbo.Table2.ColA = dbo.Table1.ColA);";
|
||||
|
||||
String expect = "UPDATE dbo.Table2" + //
|
||||
"\nSET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB" + //
|
||||
"\nFROM dbo.Table2" + //
|
||||
"\n\tINNER JOIN dbo.Table1 ON dbo.Table2.ColA = dbo.Table1.ColA";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest2 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE Cities " + //
|
||||
"SET Location = CONVERT(Point, '12.3:46.2') " + //
|
||||
"WHERE Name = 'Anchorage';";
|
||||
|
||||
String expect = "UPDATE Cities" + //
|
||||
"\nSET Location = CONVERT(Point, '12.3:46.2')" + //
|
||||
"\nWHERE Name = 'Anchorage'";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest3 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE Sales.SalesPerson " + //
|
||||
"SET SalesYTD = SalesYTD + " + //
|
||||
" (SELECT SUM(so.SubTotal) " + //
|
||||
" FROM Sales.SalesOrderHeader AS so" + //
|
||||
" WHERE so.OrderDate = (SELECT MAX(OrderDate)" + //
|
||||
" FROM Sales.SalesOrderHeader AS so2" + //
|
||||
" WHERE so2.SalesPersonID = " + //
|
||||
" so.SalesPersonID)" + //
|
||||
" AND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" + //
|
||||
" GROUP BY so.SalesPersonID);";
|
||||
|
||||
String expect = "UPDATE Sales.SalesPerson" +
|
||||
"\nSET SalesYTD = SalesYTD + (" +
|
||||
"\n\tSELECT SUM(so.SubTotal)" +
|
||||
"\n\tFROM Sales.SalesOrderHeader so" +
|
||||
"\n\tWHERE so.OrderDate = (" +
|
||||
"\n\t\t\tSELECT MAX(OrderDate)" +
|
||||
"\n\t\t\tFROM Sales.SalesOrderHeader so2" +
|
||||
"\n\t\t\tWHERE so2.SalesPersonID = so.SalesPersonID" +
|
||||
"\n\t\t\t)" +
|
||||
"\n\t\tAND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" +
|
||||
"\n\tGROUP BY so.SalesPersonID" +
|
||||
"\n\t)";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.sqlserver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
|
||||
public class SQLServerUpdateTest4 extends TestCase {
|
||||
|
||||
public void test_update() throws Exception {
|
||||
String sql = "UPDATE TOP (10) HumanResources.Employee " +
|
||||
"SET VacationHours = VacationHours * 1.25 ;";
|
||||
|
||||
String expect = "UPDATE TOP (10) HumanResources.Employee" +
|
||||
"\nSET VacationHours = VacationHours * 1.25";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user