improved sql parser. support show columns.

This commit is contained in:
wenshao
2017-07-28 14:44:02 +08:00
parent 3c7efab522
commit 9aa9406bfa
25 changed files with 1274 additions and 10 deletions
@@ -0,0 +1,155 @@
/*
* Copyright 1999-2017 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class MySqlCreateTable_showColumns_repository_test extends MysqlTest {
public void test_0() throws Exception {
String sql = "CREATE TABLE `test1` (\n" +
" `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',\n" +
" `c_tinyint` tinyint(4) DEFAULT '1' COMMENT 'tinyint',\n" +
" `c_smallint` smallint(6) DEFAULT 0 COMMENT 'smallint',\n" +
" `c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',\n" +
" `c_int` int(11) DEFAULT NULL COMMENT 'int',\n" +
" `c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',\n" +
" `c_decimal` decimal(10,3) DEFAULT NULL COMMENT 'decimal',\n" +
" `c_date` date DEFAULT '0000-00-00' COMMENT 'date',\n" +
" `c_datetime` datetime DEFAULT '0000-00-00 00:00:00' COMMENT 'datetime',\n" +
" `c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp' ON UPDATE CURRENT_TIMESTAMP ,\n" +
" `c_time` time DEFAULT NULL COMMENT 'time',\n" +
" `c_char` char(10) DEFAULT NULL COMMENT 'char',\n" +
" `c_varchar` varchar(10) DEFAULT 'hello' COMMENT 'varchar',\n" +
" `c_blob` blob COMMENT 'blob',\n" +
" `c_text` text COMMENT 'text',\n" +
" `c_mediumtext` mediumtext COMMENT 'mediumtext',\n" +
" `c_longblob` longblob COMMENT 'longblob',\n" +
" PRIMARY KEY (`id`,`c_tinyint`),\n" +
" UNIQUE KEY `uk_a` (`c_varchar`,`c_mediumint`),\n" +
" KEY `k_c` (`c_tinyint`,`c_int`),\n" +
" KEY `k_d` (`c_char`,`c_bigint`)\n" +
") ENGINE=InnoDB AUTO_INCREMENT=1769503 DEFAULT CHARSET=utf8mb4 COMMENT='10000000'";
MySqlCreateTableStatement createTableStmt;
{
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
assertEquals(1, statementList.size());
createTableStmt = (MySqlCreateTableStatement) statementList.get(0);
}
assertEquals(21, createTableStmt.getTableElementList().size());
StringBuilder buf = new StringBuilder();
createTableStmt.showCoumns(buf);
assertEquals("+--------------+---------------+------+-----+---------------------+-----------------------------+\n" +
"| Field | Type | Null | Key | Default | Extra |\n" +
"+--------------+---------------+------+-----+---------------------+-----------------------------+\n" +
"| id | bigint(20) | YES | PRI | NULL | auto_increment |\n" +
"| c_tinyint | tinyint(4) | NO | PRI | 1 | |\n" +
"| c_smallint | smallint(6) | NO | | 0 | |\n" +
"| c_mediumint | mediumint(9) | NO | | NULL | |\n" +
"| c_int | int(11) | NO | | NULL | |\n" +
"| c_bigint | bigint(20) | NO | | NULL | |\n" +
"| c_decimal | decimal(10,3) | NO | | NULL | |\n" +
"| c_date | date | NO | | 0000-00-00 | |\n" +
"| c_datetime | datetime | NO | | 0000-00-00 00:00:00 | |\n" +
"| c_timestamp | timestamp | NO | | NULL | on update CURRENT_TIMESTAMP |\n" +
"| c_time | time | NO | | NULL | |\n" +
"| c_char | char(10) | NO | MUL | NULL | |\n" +
"| c_varchar | varchar(10) | NO | MUL | hello | |\n" +
"| c_blob | blob | NO | | NULL | |\n" +
"| c_text | text | NO | | NULL | |\n" +
"| c_mediumtext | mediumtext | NO | | NULL | |\n" +
"| c_longblob | longblob | NO | | NULL | |\n" +
"+--------------+---------------+------+-----+---------------------+-----------------------------+\n", buf.toString());
SQLAlterTableStatement stmtDropColumn;
{
List<SQLStatement> statementList = SQLUtils.parseStatements("alter table test1 drop column c_decimal;", JdbcConstants.MYSQL, true);
assertEquals(1, statementList.size());
stmtDropColumn = (SQLAlterTableStatement) statementList.get(0);
}
createTableStmt.apply(stmtDropColumn);
assertEquals(20, createTableStmt.getTableElementList().size());
buf.delete(0, buf.length());
createTableStmt.showCoumns(buf);
assertEquals("+--------------+--------------+------+-----+---------------------+-----------------------------+\n" +
"| Field | Type | Null | Key | Default | Extra |\n" +
"+--------------+--------------+------+-----+---------------------+-----------------------------+\n" +
"| id | bigint(20) | YES | PRI | NULL | auto_increment |\n" +
"| c_tinyint | tinyint(4) | NO | PRI | 1 | |\n" +
"| c_smallint | smallint(6) | NO | | 0 | |\n" +
"| c_mediumint | mediumint(9) | NO | | NULL | |\n" +
"| c_int | int(11) | NO | | NULL | |\n" +
"| c_bigint | bigint(20) | NO | | NULL | |\n" +
"| c_date | date | NO | | 0000-00-00 | |\n" +
"| c_datetime | datetime | NO | | 0000-00-00 00:00:00 | |\n" +
"| c_timestamp | timestamp | NO | | NULL | on update CURRENT_TIMESTAMP |\n" +
"| c_time | time | NO | | NULL | |\n" +
"| c_char | char(10) | NO | MUL | NULL | |\n" +
"| c_varchar | varchar(10) | NO | MUL | hello | |\n" +
"| c_blob | blob | NO | | NULL | |\n" +
"| c_text | text | NO | | NULL | |\n" +
"| c_mediumtext | mediumtext | NO | | NULL | |\n" +
"| c_longblob | longblob | NO | | NULL | |\n" +
"+--------------+--------------+------+-----+---------------------+-----------------------------+\n", buf.toString());
SQLAlterTableStatement stmtAddColumn;
{
List<SQLStatement> statementList = SQLUtils.parseStatements("alter table test1 add column c_decimal decimal(10,3) DEFAULT NULL COMMENT 'decimal';", JdbcConstants.MYSQL, true);
assertEquals(1, statementList.size());
stmtAddColumn = (SQLAlterTableStatement) statementList.get(0);
}
createTableStmt.apply(stmtAddColumn);
assertEquals(21, createTableStmt.getTableElementList().size());
buf.delete(0, buf.length());
createTableStmt.showCoumns(buf);
assertEquals("+--------------+---------------+------+-----+---------------------+-----------------------------+\n" +
"| Field | Type | Null | Key | Default | Extra |\n" +
"+--------------+---------------+------+-----+---------------------+-----------------------------+\n" +
"| id | bigint(20) | YES | PRI | NULL | auto_increment |\n" +
"| c_tinyint | tinyint(4) | NO | PRI | 1 | |\n" +
"| c_smallint | smallint(6) | NO | | 0 | |\n" +
"| c_mediumint | mediumint(9) | NO | | NULL | |\n" +
"| c_int | int(11) | NO | | NULL | |\n" +
"| c_bigint | bigint(20) | NO | | NULL | |\n" +
"| c_date | date | NO | | 0000-00-00 | |\n" +
"| c_datetime | datetime | NO | | 0000-00-00 00:00:00 | |\n" +
"| c_timestamp | timestamp | NO | | NULL | on update CURRENT_TIMESTAMP |\n" +
"| c_time | time | NO | | NULL | |\n" +
"| c_char | char(10) | NO | MUL | NULL | |\n" +
"| c_varchar | varchar(10) | NO | MUL | hello | |\n" +
"| c_blob | blob | NO | | NULL | |\n" +
"| c_text | text | NO | | NULL | |\n" +
"| c_mediumtext | mediumtext | NO | | NULL | |\n" +
"| c_longblob | longblob | NO | | NULL | |\n" +
"| c_decimal | decimal(10,3) | NO | | NULL | |\n" +
"+--------------+---------------+------+-----+---------------------+-----------------------------+\n", buf.toString());
//System.out.println(buf.toString());
}
}
@@ -0,0 +1,87 @@
/*
* Copyright 1999-2017 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class MySqlCreateTable_showColumns_test extends MysqlTest {
public void test_0() throws Exception {
String sql = "CREATE TABLE `test1` (\n" +
" `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',\n" +
" `c_tinyint` tinyint(4) DEFAULT '1' COMMENT 'tinyint',\n" +
" `c_smallint` smallint(6) DEFAULT 0 COMMENT 'smallint',\n" +
" `c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',\n" +
" `c_int` int(11) DEFAULT NULL COMMENT 'int',\n" +
" `c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',\n" +
" `c_decimal` decimal(10,3) DEFAULT NULL COMMENT 'decimal',\n" +
" `c_date` date DEFAULT '0000-00-00' COMMENT 'date',\n" +
" `c_datetime` datetime DEFAULT '0000-00-00 00:00:00' COMMENT 'datetime',\n" +
" `c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp' ON UPDATE CURRENT_TIMESTAMP ,\n" +
" `c_time` time DEFAULT NULL COMMENT 'time',\n" +
" `c_char` char(10) DEFAULT NULL COMMENT 'char',\n" +
" `c_varchar` varchar(10) DEFAULT 'hello' COMMENT 'varchar',\n" +
" `c_blob` blob COMMENT 'blob',\n" +
" `c_text` text COMMENT 'text',\n" +
" `c_mediumtext` mediumtext COMMENT 'mediumtext',\n" +
" `c_longblob` longblob COMMENT 'longblob',\n" +
" PRIMARY KEY (`id`,`c_tinyint`),\n" +
" UNIQUE KEY `uk_a` (`c_varchar`,`c_mediumint`),\n" +
" KEY `k_c` (`c_tinyint`,`c_int`),\n" +
" KEY `k_d` (`c_char`,`c_bigint`)\n" +
") ENGINE=InnoDB AUTO_INCREMENT=1769503 DEFAULT CHARSET=utf8mb4 COMMENT='10000000'";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
assertEquals(1, statementList.size());
MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) statementList.get(0);
StringBuilder buf = new StringBuilder();
stmt.showCoumns(buf);
assertEquals("+--------------+---------------+------+-----+---------------------+-----------------------------+\n" +
"| Field | Type | Null | Key | Default | Extra |\n" +
"+--------------+---------------+------+-----+---------------------+-----------------------------+\n" +
"| id | bigint(20) | YES | PRI | NULL | auto_increment |\n" +
"| c_tinyint | tinyint(4) | NO | PRI | 1 | |\n" +
"| c_smallint | smallint(6) | NO | | 0 | |\n" +
"| c_mediumint | mediumint(9) | NO | | NULL | |\n" +
"| c_int | int(11) | NO | | NULL | |\n" +
"| c_bigint | bigint(20) | NO | | NULL | |\n" +
"| c_decimal | decimal(10,3) | NO | | NULL | |\n" +
"| c_date | date | NO | | 0000-00-00 | |\n" +
"| c_datetime | datetime | NO | | 0000-00-00 00:00:00 | |\n" +
"| c_timestamp | timestamp | NO | | NULL | on update CURRENT_TIMESTAMP |\n" +
"| c_time | time | NO | | NULL | |\n" +
"| c_char | char(10) | NO | MUL | NULL | |\n" +
"| c_varchar | varchar(10) | NO | MUL | hello | |\n" +
"| c_blob | blob | NO | | NULL | |\n" +
"| c_text | text | NO | | NULL | |\n" +
"| c_mediumtext | mediumtext | NO | | NULL | |\n" +
"| c_longblob | longblob | NO | | NULL | |\n" +
"+--------------+---------------+------+-----+---------------------+-----------------------------+\n", buf.toString());
}
}
@@ -0,0 +1,82 @@
/*
* Copyright 1999-2017 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.mysql.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class MySqlSelectTest_49_for_update_no_wait extends MysqlTest {
public void test_0() throws Exception {
String sql = "select /*MS-ARCORE-AR-II-M-QUERY-IN-LOCK-BY-AR-NOS*/ /*+INDEX(AR_II_M_004 PRIMARY) */ tnt_inst_id, ar_no, ar_nm, ar_tp_code, ar_entity_type " +
"from tb_001 " +
"where tnt_inst_id = ?" +
" and ar_no in ( ? ) for update no_wait";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
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());
// assertEquals(1, visitor.getTables().size());
// assertEquals(1, visitor.getColumns().size());
// assertEquals(0, visitor.getConditions().size());
// assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT /*+INDEX(AR_II_M_004 PRIMARY) */ tnt_inst_id, ar_no, ar_nm, ar_tp_code, ar_entity_type\n" +
"FROM tb_001\n" +
"WHERE tnt_inst_id = ?\n" +
"\tAND ar_no IN (?)\n" +
"FOR UPDATE NOWAIT", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select /*+INDEX(AR_II_M_004 PRIMARY) */ tnt_inst_id, ar_no, ar_nm, ar_tp_code, ar_entity_type\n" +
"from tb_001\n" +
"where tnt_inst_id = ?\n" +
"\tand ar_no in (?)\n" +
"for update nowait", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT /*+INDEX(AR_II_M_004 PRIMARY) */ tnt_inst_id, ar_no, ar_nm, ar_tp_code, ar_entity_type\n" +
"FROM tb\n" +
"WHERE tnt_inst_id = ?\n" +
"\tAND ar_no IN (?)\n" +
"FOR UPDATE NOWAIT", //
output);
}
}
}