improve sql parser for sql server

This commit is contained in:
wenshao
2013-08-05 21:48:47 +08:00
parent a55ea486b9
commit 8bf50f9179
12 changed files with 522 additions and 13 deletions
@@ -0,0 +1,49 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockPreparedStatement;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
public class PSCacheTest5 extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:x1");
dataSource.setPoolPreparedStatements(true);
dataSource.setMaxOpenPreparedStatements(10);
}
protected void tearDown() throws Exception {
JdbcUtils.close(dataSource);
}
public void test_0() throws Exception {
MockPreparedStatement mockStmt = null;
{
Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("select 1");
mockStmt = ps.unwrap(MockPreparedStatement.class);
ps.execute();
conn.close();
}
for (int i = 0; i < 1000; ++i) {
Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("select 1");
Assert.assertSame(mockStmt, ps.unwrap(MockPreparedStatement.class));
ps.execute();
ps.close();
conn.close();
}
}
}
@@ -0,0 +1,84 @@
/*
* 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 java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.util.JdbcConstants;
public class SQLServerCreateTableTest_3 extends TestCase {
public void test_0() throws Exception {
String sql = "CREATE TABLE dbo.cwd_directory (" //
+ "ID NUMERIC NOT NULL, directory_name NVARCHAR(255), "//
+ "lower_directory_name NVARCHAR(255), created_date DATETIME, " //
+ "updated_date DATETIME, active int, description NVARCHAR(255), " //
+ "impl_class NVARCHAR(255), lower_impl_class NVARCHAR(255), " //
+ "directory_type NVARCHAR(60), directory_position NUMERIC, "//
+ "CONSTRAINT PK_cwd_directory PRIMARY KEY (ID))";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("CREATE TABLE dbo.cwd_directory (" //
+ "\n\tID NUMERIC NOT NULL,"//
+ "\n\tdirectory_name NVARCHAR(255),"//
+ "\n\tlower_directory_name NVARCHAR(255),"//
+ "\n\tcreated_date DATETIME,"//
+ "\n\tupdated_date DATETIME,"//
+ "\n\tactive int,"//
+ "\n\tdescription NVARCHAR(255),"//
+ "\n\timpl_class NVARCHAR(255),"//
+ "\n\tlower_impl_class NVARCHAR(255),"//
+ "\n\tdirectory_type NVARCHAR(60),"//
+ "\n\tdirectory_position NUMERIC,"//
+ "\n\tCONSTRAINT PK_cwd_directory PRIMARY KEY (ID)"//
+ "\n)", output);
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(12, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.cwd_directory")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.cwd_directory", "ID")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.cwd_directory", "directory_name")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.cwd_directory", "lower_directory_name")));
}
}
@@ -0,0 +1,79 @@
/*
* 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 java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.util.JdbcConstants;
public class SQLServerCreateTableTest_4 extends TestCase {
public void test_0() throws Exception {
String sql = "CREATE TABLE dbo.customfieldoption (" //
+ "ID NUMERIC NOT NULL, CUSTOMFIELD NUMERIC, CUSTOMFIELDCONFIG NUMERIC, "//
+ "PARENTOPTIONID NUMERIC, SEQUENCE NUMERIC, customvalue NVARCHAR(255), "//
+ "optiontype NVARCHAR(60), disabled NVARCHAR(60), "//
+ "CONSTRAINT PK_customfieldoption PRIMARY KEY (ID))";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("CREATE TABLE dbo.customfieldoption (" //
+ "\n\tID NUMERIC NOT NULL,"//
+ "\n\tCUSTOMFIELD NUMERIC,"//
+ "\n\tCUSTOMFIELDCONFIG NUMERIC,"//
+ "\n\tPARENTOPTIONID NUMERIC,"//
+ "\n\tSEQUENCE NUMERIC,"//
+ "\n\tcustomvalue NVARCHAR(255),"//
+ "\n\toptiontype NVARCHAR(60),"//
+ "\n\tdisabled NVARCHAR(60),"//
+ "\n\tCONSTRAINT PK_customfieldoption PRIMARY KEY (ID)"//
+ "\n)", output);
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(9, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.customfieldoption")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "ID")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "CUSTOMFIELD")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "CUSTOMFIELDCONFIG")));
}
}
@@ -0,0 +1,77 @@
/*
* 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 java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.util.JdbcConstants;
public class SQLServerCreateTableTest_5 extends TestCase {
public void test_0() throws Exception {
String sql = "CREATE TABLE dbo.AO_563AEE_ACTOR_ENTITY (" //
+ " FULL_NAME VARCHAR(255),"//
+ " ID INTEGER IDENTITY(1,1) NOT NULL,"//
+ " PROFILE_PAGE_URI VARCHAR(767),"//
+ " PROFILE_PICTURE_URI VARCHAR(767),"//
+ " USERNAME VARCHAR(255),"//
+ ")";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("CREATE TABLE dbo.AO_563AEE_ACTOR_ENTITY (" //
+ "\n\tFULL_NAME VARCHAR(255),"//
+ "\n\tID INTEGER DEFAULT NULL IDENTITY (1, 1),"//
+ "\n\tPROFILE_PAGE_URI VARCHAR(767),"//
+ "\n\tPROFILE_PICTURE_URI VARCHAR(767),"//
+ "\n\tUSERNAME VARCHAR(255)"//
+ "\n)", output);
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(5, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.AO_563AEE_ACTOR_ENTITY")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_563AEE_ACTOR_ENTITY", "FULL_NAME")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_563AEE_ACTOR_ENTITY", "ID")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_563AEE_ACTOR_ENTITY", "USERNAME")));
}
}
@@ -0,0 +1,95 @@
/*
* 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 java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.util.JdbcConstants;
public class SQLServerCreateTableTest_6 extends TestCase {
public void test_0() throws Exception {
String sql = "CREATE TABLE dbo.AO_E8B6CC_ISSUE_MAPPING_V2 (" //
+ " AUTHOR VARCHAR(255),"//
+ " BRANCH VARCHAR(255),"//
+ " \"DATE\" DATETIME,"//
+ " FILES_DATA NTEXT,"//
+ " ID INTEGER IDENTITY(1,1) NOT NULL,"//
+ " ISSUE_ID VARCHAR(255),"//
+ " MESSAGE NTEXT,"//
+ " NODE VARCHAR(255),"//
+ " PARENTS_DATA VARCHAR(255),"//
+ " RAW_AUTHOR VARCHAR(255),"//
+ " RAW_NODE VARCHAR(255),"//
+ " REPOSITORY_ID INTEGER CONSTRAINT df_AO_E8B6CC_ISSUE_MAPPING_V2_REPOSITORY_ID DEFAULT 0,"//
+ " VERSION INTEGER,"//
+ "CONSTRAINT pk_AO_E8B6CC_ISSUE_MAPPING_V2_ID PRIMARY KEY(ID)"//
+ ")";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("CREATE TABLE dbo.AO_E8B6CC_ISSUE_MAPPING_V2 (" //
+ "\n\tAUTHOR VARCHAR(255),"//
+ "\n\tBRANCH VARCHAR(255),"//
+ "\n\t\"DATE\" DATETIME,"//
+ "\n\tFILES_DATA NTEXT,"//
+ "\n\tID INTEGER DEFAULT NULL IDENTITY (1, 1),"//
+ "\n\tISSUE_ID VARCHAR(255),"//
+ "\n\tMESSAGE NTEXT,"//
+ "\n\tNODE VARCHAR(255),"//
+ "\n\tPARENTS_DATA VARCHAR(255),"//
+ "\n\tRAW_AUTHOR VARCHAR(255),"//
+ "\n\tRAW_NODE VARCHAR(255),"//
+ "\n\tREPOSITORY_ID INTEGER DEFAULT 0,"//
+ "\n\tVERSION INTEGER,"//
+ "\n\tCONSTRAINT pk_AO_E8B6CC_ISSUE_MAPPING_V2_ID PRIMARY KEY (ID)"//
+ "\n)", output);
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(14, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.AO_E8B6CC_ISSUE_MAPPING_V2")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_E8B6CC_ISSUE_MAPPING_V2", "AUTHOR")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_E8B6CC_ISSUE_MAPPING_V2", "BRANCH")));
Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_E8B6CC_ISSUE_MAPPING_V2", "VERSION")));
}
}
@@ -0,0 +1,41 @@
/*
* 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 org.junit.Assert;
import junit.framework.TestCase;
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 SQLServerInsertTest4 extends TestCase {
public void test_isEmpty() throws Exception {
String sql = "INSERT INTO dbo.AO_B9A0F0_APPLIED_TEMPLATE DEFAULT VALUES";
String expect = "INSERT INTO dbo.AO_B9A0F0_APPLIED_TEMPLATE DEFAULT VALUES";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
String text = TestUtils.outputSqlServer(stmt);
Assert.assertEquals(expect, text);
System.out.println(text);
}
}