mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. for issue #1637
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.alter;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class MySqlAlterTableTest27 extends TestCase {
|
||||
|
||||
public void test_alter_add_key() throws Exception {
|
||||
String sql = "ALTER TABLE app_key ROW_FORMAT = DEFAULT";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("ALTER TABLE app_key" +
|
||||
"\n\tROW_FORMAT = DEFAULT", output);
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql.param;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/8/23.
|
||||
*/
|
||||
public class MySqlParameterizedOutputVisitorTest_35 extends TestCase {
|
||||
public void test_for_parameterize() throws Exception {
|
||||
final String dbType = JdbcConstants.MYSQL;
|
||||
|
||||
|
||||
String sql = "/*+TDDL({'extra':{'SOCKET_TIMEOUT':'3600000'}})*/\n" +
|
||||
"select sample_table_schema, \n" +
|
||||
"sample_table_name,\n" +
|
||||
"sample_table_orig_size,\n" +
|
||||
"sample_table_sample_size,\n" +
|
||||
"col.id as id,\n" +
|
||||
"sample_column_name, \n" +
|
||||
"sample_column_type,\n" +
|
||||
"sample_string, \n" +
|
||||
"sample_column_highkey, \n" +
|
||||
"sample_column_high2key, \n" +
|
||||
"sample_column_lowkey,\n" +
|
||||
"sample_column_low2key,\n" +
|
||||
"sample_column_cardinality,\n" +
|
||||
"sample_avg_length,\n" +
|
||||
"sample_column_dist_type as type, \n" +
|
||||
"sample_column_dist_quantileno as quantileno,\n" +
|
||||
"sample_column_dist_highkey,\n" +
|
||||
"sample_column_dist_lowkey, \n" +
|
||||
"sample_column_dist_value,\n" +
|
||||
"sample_column_dist_cardinality,\n" +
|
||||
"sample_column_dist_count,\n" +
|
||||
"col.gmt_create as time\n" +
|
||||
"from sample_tables tab, sample_columns col, sample_column_distribution dist\n" +
|
||||
"where \n" +
|
||||
"tab.id = col.sample_column_table_id and \n" +
|
||||
"col.id = dist.sample_column_dist_column_id and \n" +
|
||||
"col.id = ( \n" +
|
||||
" SELECT id FROM sample_columns col \n" +
|
||||
" WHERE sample_column_name = 'gmt_modified'\n" +
|
||||
" AND sample_column_table_schema = 'SC_PRODUCT_03'\n" +
|
||||
" AND sample_column_table_name = 'product_0096'\n" +
|
||||
" ORDER BY id DESC LIMIT 1 \n" +
|
||||
" order by type, quantileno)";
|
||||
|
||||
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
SQLStatement statement = stmtList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
// List<Object> parameters = new ArrayList<Object>();
|
||||
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
|
||||
visitor.setParameterized(true);
|
||||
visitor.setParameterizedMergeInList(true);
|
||||
// visitor.setParameters(parameters);
|
||||
visitor.setExportTables(true);
|
||||
visitor.setPrettyFormat(false);
|
||||
statement.accept(visitor);
|
||||
System.out.println(out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.postgresql.select;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGSelectTest47 extends PGTest {
|
||||
private String dbType = JdbcConstants.POSTGRESQL;
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT\n" +
|
||||
"u.userid,\n" +
|
||||
"u.phonenumber,\n" +
|
||||
"u.cdate,\n" +
|
||||
"u.udate,\n" +
|
||||
"u.wechatid,\n" +
|
||||
"u.wechatnickname,\n" +
|
||||
"u.wechattime,\n" +
|
||||
"b. ID,\n" +
|
||||
"b.username serviceStaffName,\n" +
|
||||
"u.appid,\n" +
|
||||
"s. ID servicegroupid,\n" +
|
||||
"n.total_amount blackNumber\n" +
|
||||
"FROM\n" +
|
||||
"users u\n" +
|
||||
"LEFT JOIN user_servicegroup s ON u.userid = s.userid\n" +
|
||||
"LEFT JOIN jcs_user_base b ON s.servicestaffid = b. ID\n" +
|
||||
"LEFT JOIN jcs_user_purchase_record r ON u.userid = r.user_id\n" +
|
||||
"LEFT JOIN user_black_article_number n ON u.userid = n.userid\n" +
|
||||
"WHERE\n" +
|
||||
"u.isactive = TRUE\n" +
|
||||
"ORDER BY\n" +
|
||||
"n.total_amount DESC NULLS LAST";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("SELECT u.userid, u.phonenumber, u.cdate, u.udate, u.wechatid\n" +
|
||||
"\t, u.wechatnickname, u.wechattime, b.ID, b.username AS serviceStaffName, u.appid\n" +
|
||||
"\t, s.ID AS servicegroupid, n.total_amount AS blackNumber\n" +
|
||||
"FROM users u\n" +
|
||||
"\tLEFT JOIN user_servicegroup s ON u.userid = s.userid\n" +
|
||||
"\tLEFT JOIN jcs_user_base b ON s.servicestaffid = b.ID\n" +
|
||||
"\tLEFT JOIN jcs_user_purchase_record r ON u.userid = r.user_id\n" +
|
||||
"\tLEFT JOIN user_black_article_number n ON u.userid = n.userid\n" +
|
||||
"WHERE u.isactive = true\n" +
|
||||
"ORDER BY n.total_amount DESC NULLS LAST", SQLUtils.toPGString(stmt));
|
||||
|
||||
Assert.assertEquals("select u.userid, u.phonenumber, u.cdate, u.udate, u.wechatid\n" +
|
||||
"\t, u.wechatnickname, u.wechattime, b.ID, b.username as serviceStaffName, u.appid\n" +
|
||||
"\t, s.ID as servicegroupid, n.total_amount as blackNumber\n" +
|
||||
"from users u\n" +
|
||||
"\tleft join user_servicegroup s on u.userid = s.userid\n" +
|
||||
"\tleft join jcs_user_base b on s.servicestaffid = b.ID\n" +
|
||||
"\tleft join jcs_user_purchase_record r on u.userid = r.user_id\n" +
|
||||
"\tleft join user_black_article_number n on u.userid = n.userid\n" +
|
||||
"where u.isactive = true\n" +
|
||||
"order by n.total_amount desc NULLS LAST", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
|
||||
Assert.assertEquals(17, visitor.getColumns().size());
|
||||
Assert.assertEquals(5, visitor.getTables().size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user