Merge remote-tracking branch 'upstream/master'

This commit is contained in:
DigitalSonic
2019-02-03 00:04:15 +08:00
58 changed files with 1038 additions and 253 deletions
@@ -12,10 +12,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.LayoutCharacters;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.Token;
import com.alibaba.druid.sql.parser.*;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
@@ -36,6 +33,40 @@ public class SqlHolder {
private boolean parsed;
public SQLStatement ast;
private boolean isParam;
public static SQLSelectListCache selectListCache = new SQLSelectListCache(JdbcConstants.MYSQL);
static {
selectListCache.add("select id as id, gmt_create as gmtCreate, gmt_modified as gmtModified, name as name, owner as owner, type as type, statement as statement, datasource as datasource, meta as meta, param_file as paramFile, sharable as sharable, data_type as dataType, status as status, config as config, project_id as projectId, plugins as plugins, field_compare as fieldCompare, field_ext as fieldExt, openx as openx from");
selectListCache.add("SELECT id, dispute_id, buyer_id, seller_id, total_fee, refund_fee, max_apply_goods_fee, apply_goods_fee, apply_carriage_fee, refund_goods_fee, refund_carriage_fee, refund_point, refund_coupon, refund_return_point, refund_cash, real_deduct_refund_point, real_refund_return_point, refund_return_commission, gmt_create, gmt_modified, attributes, attributes_cc FROM");
// selectListCache.add("select `auction_relation`.`id`,`auction_relation`.`item_id`,`auction_relation`.`sku_id`,`auction_relation`.`user_id`,`auction_relation`.`target_id`,`auction_relation`.`extra_id`,`auction_relation`.`type`,`auction_relation`.`target_type`,`auction_relation`.`type_attr`,`auction_relation`.`status`,`auction_relation`.`target_user_id`,`auction_relation`.`options`,`auction_relation`.`features`,`auction_relation`.`version`,`auction_relation`.`sub_type`,`auction_relation`.`gmt_create`,`auction_relation`.`gmt_modified` from");
// selectListCache.add("SELECT biz_order_id, value_type, key_value, gmt_create, gmt_modified\n" +
// "\t, attribute_cc, buyer_id\n" +
// "FROM");
// selectListCache.add("SELECT biz_order_id, value_type, key_value, gmt_create, gmt_modified\n" +
// "\t, attribute_cc, buyer_id\n" +
// "FROM");
// selectListCache.add("SELECT sub_logistics_order_id, consign_time, attribute_cc, attributes, out_logistics_id\n" +
// "\t, parent_id, gmt_create, gmt_modified, detail_order_id, is_last\n" +
// "\t, ship_amount, buyer_id, seller_id, ship_status, step_order_id\n" +
// "FROM");
selectListCache.add("SELECT biz_order_id, out_order_id, seller_nick, buyer_nick, seller_id\n" +
"\t, buyer_id, auction_id, auction_title, auction_price, buy_amount\n" +
"\t, biz_type, sub_biz_type, fail_reason, pay_status, logistics_status\n" +
"\t, out_trade_status, snap_path, gmt_create, status\n" +
"\t, ifnull(buyer_rate_status, 4) AS buyer_rate_status\n" +
"\t, ifnull(seller_rate_status, 4) AS seller_rate_status, auction_pict_url\n" +
"\t, seller_memo, buyer_memo, seller_flag, buyer_flag, buyer_message_path\n" +
"\t, refund_status, attributes, attributes_cc, gmt_modified, ip\n" +
"\t, end_time, pay_time, is_main, is_detail, point_rate\n" +
"\t, parent_id, adjust_fee, discount_fee, refund_fee, confirm_paid_fee\n" +
"\t, cod_status, trade_tag, shop_id, sync_version, options\n" +
"\t, ignore_sold_quantity, from_group, attribute1, attribute2, attribute3\n" +
"\t, attribute4, attribute11\n" +
"FROM");
selectListCache.add("select `member_cart`.`CART_ID`,`member_cart`.`SKU_ID`,`member_cart`.`ITEM_ID`,`member_cart`.`QUANTITY`,`member_cart`.`USER_ID`,`member_cart`.`SELLER_ID`,`member_cart`.`STATUS`,`member_cart`.`EXT_STATUS`,`member_cart`.`TYPE`,`member_cart`.`SUB_TYPE`,`member_cart`.`GMT_CREATE`,`member_cart`.`GMT_MODIFIED`,`member_cart`.`ATTRIBUTE`,`member_cart`.`ATTRIBUTE_CC`,`member_cart`.`EX2` from");
}
public static SqlHolder of(String sql) {
return new SqlHolder(sql);
}
@@ -44,6 +75,11 @@ public class SqlHolder {
this(text, JdbcConstants.MYSQL);
}
public SqlHolder(String text, String dbType, boolean isParam) {
this(text, dbType);
this.isParam = isParam;
}
SqlHolder(String text, String dialect) {
if (!"mysql".equalsIgnoreCase(dialect)) {
throw new IllegalArgumentException("only mysql is");
@@ -78,9 +114,21 @@ public class SqlHolder {
// }
// ast = SQLUtils.parseStatements(text, dialect).get(0);
SQLParserFeature[] features = {SQLParserFeature.EnableSQLBinaryOpExprGroup, SQLParserFeature.OptimizedForParameterized};
SQLParserFeature[] features;
if (isParam) {
features = new SQLParserFeature[]{SQLParserFeature.EnableSQLBinaryOpExprGroup, SQLParserFeature.OptimizedForParameterized};
} else {
features = new SQLParserFeature[]{SQLParserFeature.EnableSQLBinaryOpExprGroup
, SQLParserFeature.OptimizedForParameterized
, SQLParserFeature.UseInsertColumnsCache
};
}
try {
ast = new SqlHolderParser(text, features).parseStatement();
MySqlStatementParser parser = new MySqlStatementParser(text, features);
parser.setSelectListCache(selectListCache);
ast = parser.parseStatement();
} catch (ParserException e) {
throw new UnsupportedOperationException(e);
}
@@ -238,75 +286,4 @@ public class SqlHolder {
}
return tableName;
}
public static class SqlHolderParser extends MySqlStatementParser {
public SqlHolderParser(String sql, SQLParserFeature... features) {
super(sql, features);
}
public void parseStatementList(List<SQLStatement> statementList, int max, SQLObject parent) {
// special optimized
if (lexer.token() == Token.SELECT) {
String source = lexer.getSource();
if ("select @@session.tx_read_only".equals(source)) {
SQLSelect select = new SQLSelect();
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
queryBlock.addSelectItem(new SQLPropertyExpr(new SQLVariantRefExpr("@@session"), "tx_read_only"));
select.setQuery(queryBlock);
SQLSelectStatement stmt = new SQLSelectStatement(select);
statementList.add(stmt);
lexer.reset(29, '\u001A', Token.EOF);
return;
}
int pos = lexer.pos();
if (source.startsWith(" id, dispute_id, buyer_id, seller_id, total_fee, refund_fee, max_apply_goods_fee, apply_goods_fee, apply_carriage_fee, refund_goods_fee, refund_carriage_fee, refund_point, refund_coupon, refund_return_point, refund_cash, real_deduct_refund_point, real_refund_return_point, refund_return_commission, gmt_create, gmt_modified, attributes, attributes_cc FROM", pos)) {
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
queryBlock.addSelectItem(new SQLIdentifierExpr("id"));
queryBlock.addSelectItem(new SQLIdentifierExpr("dispute_id"));
queryBlock.addSelectItem(new SQLIdentifierExpr("buyer_id"));
queryBlock.addSelectItem(new SQLIdentifierExpr("seller_id"));
queryBlock.addSelectItem(new SQLIdentifierExpr("total_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("max_apply_goods_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("apply_goods_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("apply_carriage_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_goods_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_carriage_fee"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_point"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_coupon"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_return_point"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_cash"));
queryBlock.addSelectItem(new SQLIdentifierExpr("real_deduct_refund_point"));
queryBlock.addSelectItem(new SQLIdentifierExpr("real_refund_return_point"));
queryBlock.addSelectItem(new SQLIdentifierExpr("refund_return_commission"));
queryBlock.addSelectItem(new SQLIdentifierExpr("gmt_create"));
queryBlock.addSelectItem(new SQLIdentifierExpr("gmt_modified"));
queryBlock.addSelectItem(new SQLIdentifierExpr("attributes"));
queryBlock.addSelectItem(new SQLIdentifierExpr("attributes_cc"));
lexer.reset(430, ' ', Token.FROM);
MySqlSelectParser selectParser = new MySqlSelectParser(this.exprParser);
selectParser.parseFrom(queryBlock);
selectParser.parseWhere(queryBlock);
SQLSelect select = new SQLSelect(queryBlock);
SQLSelectStatement stmt = new SQLSelectStatement(select);
stmt.setDbType(JdbcConstants.MYSQL);
statementList.add(stmt);
return;
}
}
super.parseStatementList(statementList, max, parent);
}
}
}
@@ -0,0 +1,31 @@
package com.alibaba.druid.bvt.bug;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.List;
public class Issue2876 extends TestCase {
public void test_0() throws Exception {
String sql = "SELECT CONCAT(\"'\",b.PRIMARY_ID,\"'\") \n" +
"FROM s_user_session_attributes a \n" +
"LEFT JOIN s_user_session b ON a.SESSION_PRIMARY_ID=b.PRIMARY_ID \n" +
"WHERE a.ATTRIBUTE_NAME='KAPTCHA_SESSION_KEY' AND b.LAST_ACCESS_TIME <= 1540429945459";
System.out.println(sql);
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
SQLSelectStatement stmt = (SQLSelectStatement) stmtList.get(0);
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
SQLJoinTableSource joinTableSource = (SQLJoinTableSource) queryBlock.getFrom();
assertEquals("a", joinTableSource.getLeft().getAlias());
assertEquals("b", joinTableSource.getRight().getAlias());
}
}
@@ -20,7 +20,6 @@ public class DruidDataSourceTest4 extends TestCase {
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setInitialSize(1);
}
protected void tearDown() throws Exception {
@@ -21,6 +21,18 @@ public class DruidDataSourceTest9_phyMaxUseCount extends TestCase {
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setInitialSize(1);
dataSource.setPhyMaxUseCount(10);
assertEquals(10, dataSource.getPhyMaxUseCount());
Properties properties = new Properties();
properties.put("druid.phyMaxUseCount", "20");
dataSource.configFromPropety(properties);
assertEquals(20, dataSource.getPhyMaxUseCount());
properties.put("druid.phyMaxUseCount", "10");
dataSource.configFromPropety(properties);
assertEquals(10, dataSource.getPhyMaxUseCount());
dataSource.setFilters("log4j");
}
@@ -853,7 +853,7 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SHOW FULL PROCESSLIST", output);
}
public void test_show_profiles() throws Exception {
String sql = "SHOW profiles";
MySqlStatementParser parser = new MySqlStatementParser(sql);
@@ -0,0 +1,13 @@
package com.alibaba.druid.bvt.sql.derby;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
public class DerbySelectTest extends TestCase {
public void test_for_derby() throws Exception {
String sql = "select * from sys_user offset ? rows fetch next ? rows only";
SQLUtils.parseSingleStatement(sql, JdbcConstants.DERBY);
}
}
@@ -0,0 +1,78 @@
/*
* 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.hive;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.List;
public class HiveSelectTest_41 extends TestCase {
public void test_0() throws Exception {
String sql = "select\n" +
"field['id'].string_type as banner_id\n" +
",field['index'].string_type as banner_index\n" +
",field['pageName'].string_type as pageName\n" +
"from\n" +
"bdm_book.midu_xcx_web_main_log\n" +
"where day = '2019-01-09'\n" +
"and eventid = '151'\n" +
"and cmd in ('26002','26003')\n" +
"group by field['id'].string_type\n" +
",field['index'].string_type\n" +
",field['pageName'].string_type ;";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.HIVE);
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals("SELECT field['id'].string_type AS banner_id, field['index'].string_type AS banner_index, field['pageName'].string_type AS pageName\n" +
"FROM bdm_book.midu_xcx_web_main_log\n" +
"WHERE day = '2019-01-09'\n" +
"\tAND eventid = '151'\n" +
"\tAND cmd IN ('26002', '26003')\n" +
"GROUP BY field['id'].string_type, field['index'].string_type, field['pageName'].string_type;", stmt.toString());
assertEquals("select field['id'].string_type as banner_id, field['index'].string_type as banner_index, field['pageName'].string_type as pageName\n" +
"from bdm_book.midu_xcx_web_main_log\n" +
"where day = '2019-01-09'\n" +
"\tand eventid = '151'\n" +
"\tand cmd in ('26002', '26003')\n" +
"group by field['id'].string_type, field['index'].string_type, field['pageName'].string_type;", stmt.toLowerCaseString());
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.HIVE);
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());
System.out.println("groupBy : " + visitor.getGroupByColumns());
assertEquals(1, visitor.getTables().size());
assertEquals(4, visitor.getColumns().size());
assertEquals(3, visitor.getConditions().size());
assertEquals(0, visitor.getGroupByColumns().size());
// assertTrue(visitor.containsColumn("source", "*"));
}
}
@@ -0,0 +1,47 @@
package com.alibaba.druid.bvt.sql.mysql.createTable;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import java.util.List;
public class MySqlCreateTableTest134 extends MysqlTest {
public void test_0() throws Exception {
String sql = "create table xuhan3(\n" +
"id int not null AUTO_INCREMENT primary key, \n" +
"name char(40), SimpleDate date, \n" +
"SimpleDate_dayofweek tinyint(4) GENERATED ALWAYS AS (dayofweek(SimpleDate)) VIRTUAL, \n" +
"KEY SimpleDate_dayofweek (SimpleDate_dayofweek));";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
MySqlCreateTableStatement stmt = (MySqlCreateTableStatement)statementList.get(0);
assertEquals(1, statementList.size());
assertEquals("CREATE TABLE xuhan3 (\n" +
"\tid int NOT NULL PRIMARY KEY AUTO_INCREMENT,\n" +
"\tname char(40),\n" +
"\tSimpleDate date,\n" +
"\tSimpleDate_dayofweek tinyint(4) GENERATED ALWAYS AS (dayofweek(SimpleDate)) VIRTUAL,\n" +
"\tKEY SimpleDate_dayofweek (SimpleDate_dayofweek)\n" +
");", stmt.toString());
assertEquals("create table xuhan3 (\n" +
"\tid int not null primary key auto_increment,\n" +
"\tname char(40),\n" +
"\tSimpleDate date,\n" +
"\tSimpleDate_dayofweek tinyint(4) generated always as (dayofweek(SimpleDate)) virtual,\n" +
"\tkey SimpleDate_dayofweek (SimpleDate_dayofweek)\n" +
");", stmt.toLowerCaseString());
}
}
@@ -12,7 +12,8 @@ import java.util.List;
public class MySqlInsertTest_17 extends MysqlTest {
public void test_insert_rollback_on_fail() throws Exception {
String sql = "INSERT INTO dashboard_crowd_analysis(cname,cvalue,orders,users,dt,tagid)\n"
String sql = "/*+engine=MPP, mppNativeInsertFromSelect=true*/\n"
+ "INSERT INTO dashboard_crowd_analysis(cname,cvalue,orders,users,dt,tagid)\n"
+ "SELECT x.cname as cname,\n" + " x.cvalue as cvalue,\n" + " x.orders as orders,\n"
+ " x.users as users,\n" + " 20171211 as dt,\n" + " 91 as tagid\n"
+ "FROM (WITH h AS\n" + " (SELECT a.userid,\n" + " a.col_1,\n"
@@ -40,22 +41,44 @@ public class MySqlInsertTest_17 extends MysqlTest {
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
// System.out.println(SQLUtils.toMySqlString(insertStmt));
String formatSql = "INSERT INTO dashboard_crowd_analysis (cname, cvalue, orders, users, dt\n" + "\t, tagid)\n"
+ "SELECT x.cname AS cname, x.cvalue AS cvalue, x.orders AS orders, x.users AS users, 20171211 AS dt\n"
+ "\t, 91 AS tagid\n" + "FROM (\n" + "\tWITH h AS (\n"
+ "\t\t\tSELECT a.userid, a.col_1, a.col_2, a.col_3, a.col_4\n"
+ "\t\t\t\t, a.col_5, a.col_6, c.orders_week\n" + "\t\t\tFROM (\n"
+ "\t\t\t\tSELECT userid, col_1, col_2, col_3, col_4\n" + "\t\t\t\t\t, col_5, col_6\n"
+ "\t\t\t\tFROM ofo_personas_dimensions\n" + "\t\t\t\tWHERE dt = 20171211\n" + "\t\t\t) a\n"
+ "\t\t\t\tJOIN (\n" + "\t\t\t\t\tSELECT userid\n" + "\t\t\t\t\tFROM personas_user_tag\n"
+ "\t\t\t\t\tWHERE tagid = 91\n" + "\t\t\t\t) b\n" + "\t\t\t\tON a.userid = b.userid\n"
+ "\t\t\t\tJOIN (\n" + "\t\t\t\t\tSELECT userid, orders_week\n"
+ "\t\t\t\t\tFROM ofo_personas_metrics\n" + "\t\t\t\t\tWHERE dt = 20171211\n"
+ "\t\t\t\t) c\n" + "\t\t\t\tON a.userid = c.userid\n" + "\t\t)\n"
+ "\tSELECT 'col_1' AS cname, col_1 AS cvalue, SUM(orders_week) AS orders\n"
+ "\t\t, COUNT(userid) AS users\n" + "\tFROM h\n" + "\tGROUP BY col_1\n" + "\tUNION ALL\n"
+ "\tSELECT 'col_2' AS cname, col_2 AS cvalue, SUM(orders_week) AS orders\n"
+ "\t\t, COUNT(userid) AS users\n" + "\tFROM h\n" + "\tGROUP BY col_2\n" + ") x";
String formatSql = "/*+engine=MPP, mppNativeInsertFromSelect=true*/\n" +
"INSERT INTO dashboard_crowd_analysis (cname, cvalue, orders, users, dt\n" +
"\t, tagid)\n" +
"SELECT x.cname AS cname, x.cvalue AS cvalue, x.orders AS orders, x.users AS users, 20171211 AS dt\n" +
"\t, 91 AS tagid\n" +
"FROM (\n" +
"\tWITH h AS (\n" +
"\t\t\tSELECT a.userid, a.col_1, a.col_2, a.col_3, a.col_4\n" +
"\t\t\t\t, a.col_5, a.col_6, c.orders_week\n" +
"\t\t\tFROM (\n" +
"\t\t\t\tSELECT userid, col_1, col_2, col_3, col_4\n" +
"\t\t\t\t\t, col_5, col_6\n" +
"\t\t\t\tFROM ofo_personas_dimensions\n" +
"\t\t\t\tWHERE dt = 20171211\n" +
"\t\t\t) a\n" +
"\t\t\t\tJOIN (\n" +
"\t\t\t\t\tSELECT userid\n" +
"\t\t\t\t\tFROM personas_user_tag\n" +
"\t\t\t\t\tWHERE tagid = 91\n" +
"\t\t\t\t) b\n" +
"\t\t\t\tON a.userid = b.userid\n" +
"\t\t\t\tJOIN (\n" +
"\t\t\t\t\tSELECT userid, orders_week\n" +
"\t\t\t\t\tFROM ofo_personas_metrics\n" +
"\t\t\t\t\tWHERE dt = 20171211\n" +
"\t\t\t\t) c\n" +
"\t\t\t\tON a.userid = c.userid\n" +
"\t\t)\n" +
"\tSELECT 'col_1' AS cname, col_1 AS cvalue, SUM(orders_week) AS orders\n" +
"\t\t, COUNT(userid) AS users\n" +
"\tFROM h\n" +
"\tGROUP BY col_1\n" +
"\tUNION ALL\n" +
"\tSELECT 'col_2' AS cname, col_2 AS cvalue, SUM(orders_week) AS orders\n" +
"\t\t, COUNT(userid) AS users\n" +
"\tFROM h\n" +
"\tGROUP BY col_2\n" +
") x";
Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
@@ -0,0 +1,125 @@
/*
* 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.insert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.List;
public class MySqlInsertTest_43 extends TestCase {
public void test_insert_0() throws Exception {
String sql = "insert into adl_indication_ums_warehouse_ebax_a (sys_pk,sys_ds,sys_biztime,sys_gmt_modified,business_type,parcelled_order_num,warehouse_id)\n" +
"select\n" +
" concat(20190115,'_',ifnull(businessType, '#null#') ,'_',ifnull(warehouseId, '#null#') ) as sys_pk,\n" +
" cast(20190115 as bigint) as sys_ds,\n" +
" str_to_date('2019-01-15 21:51:00', '%Y-%m-%d %H:%i:%s') as sys_biztime,\n" +
" now() as sys_gmt_modified,\n" +
" cast(businessType as varchar) as business_type,\n" +
" cast(parcelledOrderNum as bigint) as parcelled_order_num,\n" +
" cast(warehouseId as varchar) as warehouse_id\n" +
"from (\n" +
"\n" +
"\n" +
"select cast(t1.warehouse_id as varchar) as warehouseId\n" +
" , t2.businessType\n" +
" , count(1) as parcelledOrderNum\n" +
"from sales_order t1\n" +
"join (\n" +
" select t2_1.warehouse_id\n" +
" , t2_1.external_batch_code\n" +
" ,(case\n" +
" when t2_2.operation_type = 2 or t2_2.operation_type is null then '2'\n" +
" when t2_2.operation_type = 3 then '800'\n" +
" when t2_2.operation_type = 1 then '900'\n" +
" end) as businessType\n" +
" from batch_order t2_1\n" +
" join wave_order t2_2\n" +
" on t2_1.wave_id = t2_2.id\n" +
" and t2_1.warehouse_id = t2_2.warehouse_id\n" +
" and t2_2.wave_status in (5,6)\n" +
" and t2_1.ds >= (201901 - 1)\n" +
" and t2_2.ds >= (201901 - 1)\n" +
" and t2_1.gmt_create >= date_add(now(), interval -3 day)\n" +
" and t2_1.gmt_create <= now()\n" +
" and t2_2.gmt_create >= date_add(now(), interval -3 day)\n" +
" and t2_2.gmt_create <= now()\n" +
"\n" +
") t2 on t1.out_batch_code = t2.external_batch_code\n" +
" and t1.warehouse_id = t2.warehouse_id\n" +
" and t1.out_operation_mode = 3\n" +
" and t1.gmt_create >= CURDATE()\n" +
" and t1.gmt_create <= now()\n" +
" and t1.ds >= 201901\n" +
" and t2.businessType is not null\n" +
"group by t1.warehouse_id, t2.businessType\n" +
") a\n";
MySqlStatementParser parser = new MySqlStatementParser(sql, false, true);
parser.config(SQLParserFeature.KeepInsertValueClauseOriginalString, true);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
assertEquals("INSERT INTO adl_indication_ums_warehouse_ebax_a (sys_pk, sys_ds, sys_biztime, sys_gmt_modified, business_type\n" +
"\t, parcelled_order_num, warehouse_id)\n" +
"SELECT concat(20190115, '_', ifnull(businessType, '#null#'), '_', ifnull(warehouseId, '#null#')) AS sys_pk\n" +
"\t, CAST(20190115 AS bigint) AS sys_ds, str_to_date('2019-01-15 21:51:00', '%Y-%m-%d %H:%i:%s') AS sys_biztime, now() AS sys_gmt_modified\n" +
"\t, CAST(businessType AS varchar) AS business_type, CAST(parcelledOrderNum AS bigint) AS parcelled_order_num, CAST(warehouseId AS varchar) AS warehouse_id\n" +
"FROM (\n" +
"\tSELECT CAST(t1.warehouse_id AS varchar) AS warehouseId, t2.businessType, COUNT(1) AS parcelledOrderNum\n" +
"\tFROM sales_order t1\n" +
"\t\tJOIN (\n" +
"\t\t\tSELECT t2_1.warehouse_id, t2_1.external_batch_code\n" +
"\t\t\t\t, CASE \n" +
"\t\t\t\t\tWHEN t2_2.operation_type = 2\n" +
"\t\t\t\t\tOR t2_2.operation_type IS NULL THEN '2'\n" +
"\t\t\t\t\tWHEN t2_2.operation_type = 3 THEN '800'\n" +
"\t\t\t\t\tWHEN t2_2.operation_type = 1 THEN '900'\n" +
"\t\t\t\tEND AS businessType\n" +
"\t\t\tFROM batch_order t2_1\n" +
"\t\t\t\tJOIN wave_order t2_2\n" +
"\t\t\t\tON t2_1.wave_id = t2_2.id\n" +
"\t\t\t\t\tAND t2_1.warehouse_id = t2_2.warehouse_id\n" +
"\t\t\t\t\tAND t2_2.wave_status IN (5, 6)\n" +
"\t\t\t\t\tAND t2_1.ds >= 201901 - 1\n" +
"\t\t\t\t\tAND t2_2.ds >= 201901 - 1\n" +
"\t\t\t\t\tAND t2_1.gmt_create >= date_add(now(), INTERVAL -3 DAY)\n" +
"\t\t\t\t\tAND t2_1.gmt_create <= now()\n" +
"\t\t\t\t\tAND t2_2.gmt_create >= date_add(now(), INTERVAL -3 DAY)\n" +
"\t\t\t\t\tAND t2_2.gmt_create <= now()\n" +
"\t\t) t2\n" +
"\t\tON t1.out_batch_code = t2.external_batch_code\n" +
"\t\t\tAND t1.warehouse_id = t2.warehouse_id\n" +
"\t\t\tAND t1.out_operation_mode = 3\n" +
"\t\t\tAND t1.gmt_create >= CURDATE()\n" +
"\t\t\tAND t1.gmt_create <= now()\n" +
"\t\t\tAND t1.ds >= 201901\n" +
"\t\t\tAND t2.businessType IS NOT NULL\n" +
"\tGROUP BY t1.warehouse_id, t2.businessType\n" +
") a", insertStmt.toString());
ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, null, null);
}
}
@@ -0,0 +1,35 @@
/*
* 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;
public class MySqlSelectTest_227 extends MysqlTest {
public void test_0() throws Exception {
String sql = "select id, distinct name from a";
Exception error = null;
try {
SQLUtils.parseSingleMysqlStatement(sql);
} catch (Exception ex) {
error = ex;
}
assertNotNull(error);
}
}
@@ -0,0 +1,65 @@
/*
* 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.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.List;
public class MySqlSelectTest_229_interval extends MysqlTest {
public void test_0() throws Exception {
String sql = "select DATE_ADD(utc_date(), INTERVAL -'5' DAY)";
// System.out.println(sql);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLStatement stmt = statementList.get(0);
assertEquals("SELECT DATE_ADD(utc_date(), INTERVAL -5 DAY)", stmt.toString());
assertEquals("select DATE_ADD(utc_date(), interval -5 day)", stmt.clone().toLowerCaseString());
}
public void test_1() throws Exception {
String sql = "select DATE_ADD(utc_date(), INTERVAL +'5' DAY)";
// System.out.println(sql);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLStatement stmt = statementList.get(0);
assertEquals("SELECT DATE_ADD(utc_date(), INTERVAL +5 DAY)", stmt.toString());
assertEquals("select DATE_ADD(utc_date(), interval +5 day)", stmt.clone().toLowerCaseString());
}
}
@@ -0,0 +1,59 @@
/*
* 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.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLNotExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import java.util.List;
public class MySqlSelectTest_ads_keywords extends MysqlTest {
public void test_0() throws Exception {
String sql = "select label from a where not id > 50";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
assertEquals("SELECT label\n" +
"FROM a\n" +
"WHERE NOT id > 50", //
stmt.toString());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
assertEquals(1, visitor.getTables().size());
assertEquals(2, visitor.getColumns().size());
assertEquals(1, visitor.getConditions().size());
assertEquals(0, visitor.getOrderByColumns().size());
SQLNotExpr notExpr = (SQLNotExpr) ((SQLSelectStatement) stmt).getSelect().getQueryBlock().getWhere();
assertNotNull(notExpr.getParent());
assertNotNull(notExpr.getExpr().getParent());
}
}
@@ -0,0 +1,62 @@
/*
* 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.oracle.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.List;
public class OracleSelectTest133 extends TestCase {
public void test_0() throws Exception {
String sql = "SELECT to_char(sysdate-interval '1' hour,'hh24:mi:ss') as num FROM DUAL;";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
assertEquals(1, statementList.size());
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals("SELECT to_char(SYSDATE - INTERVAL '1' HOUR, 'hh24:mi:ss') AS num\n" +
"FROM DUAL;", stmt.toString());
assertEquals("select to_char(sysdate - interval '1' HOUR, 'hh24:mi:ss') as num\n" +
"from DUAL;", stmt.toLowerCaseString());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
assertEquals(0, visitor.getTables().size());
assertEquals(0, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
// assertTrue(visitor.containsColumn("srm1.CONSIGNEE_ADDRESS", "id"));
}
}
@@ -2,6 +2,7 @@ package com.alibaba.druid.postgresql;
import com.alibaba.druid.DbTestCase;
import com.alibaba.druid.benckmark.proxy.BenchmarkExecutor;
import com.alibaba.druid.pool.vendor.PGValidConnectionChecker;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
@@ -22,6 +23,10 @@ public class PG_getCreateTableScriptTest extends DbTestCase {
public void test_oracle() throws Exception {
Connection conn = getConnection();
PGValidConnectionChecker checker = new PGValidConnectionChecker();
Connection raw = conn.unwrap(Connection.class);
checker.isValidConnection(raw, "select 1", 100);
// 从Oracle通过DBMS_METADATA.GET_DDL获取CreateTable语句列表
//String createTableScript = JdbcUtils.getCreateTableScript(conn, JdbcConstants.ORACLE);
//System.out.println(createTableScript);