mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
@@ -16,6 +16,27 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
public enum SQLObjectType {
|
||||
TABLE, FUNCTION, PROCEDURE, USER, DATABASE, //
|
||||
ROLE, PROJECT, PACKAGE, RESOURCE, INSTANCE, JOB, VOLUME, OfflineModel, XFLOW // for odps
|
||||
TABLE("TABLE"), //
|
||||
FUNCTION("FUNCTION"), //
|
||||
PROCEDURE("PROCEDURE"), //
|
||||
USER("USER"), //
|
||||
DATABASE("DATABASE"), //
|
||||
ROLE("ROLE"), //
|
||||
PROJECT("PROJECT"), //
|
||||
PACKAGE("PACKAGE"), //
|
||||
RESOURCE("RESOURCE"), //
|
||||
INSTANCE("INSTANCE"), //
|
||||
JOB("JOB"), //
|
||||
VOLUME("VOLUME"), //
|
||||
OfflineModel("OFFLINEMODEL"), //
|
||||
XFLOW("XFLOW") // for odps
|
||||
;
|
||||
|
||||
public final String name;
|
||||
public final String name_lcase;
|
||||
|
||||
SQLObjectType(String name) {
|
||||
this.name = name;
|
||||
this.name_lcase = name.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,23 +98,44 @@ public class MysqlForeignKey extends SQLForeignKeyImpl {
|
||||
|
||||
RESTRICT("RESTRICT"), CASCADE("CASCADE"), SET_NULL("SET NULL"), NO_ACTION("NO ACTION");
|
||||
|
||||
private String text;
|
||||
public final String name;
|
||||
public final String name_lcase;
|
||||
|
||||
Option(String text){
|
||||
this.text = text;
|
||||
Option(String name){
|
||||
this.name = name;
|
||||
this.name_lcase = name.toLowerCase();
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static enum Match {
|
||||
FULL, PARTIAL, SIMPLE;
|
||||
FULL("FULL"),
|
||||
PARTIAL("PARTIAL"),
|
||||
SIMPLE("SIMPLE");
|
||||
|
||||
public final String name;
|
||||
public final String name_lcase;
|
||||
|
||||
Match(String name) {
|
||||
this.name = name;
|
||||
this.name_lcase = name.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
public static enum On {
|
||||
DELETE, UPDATE;
|
||||
DELETE("DELETE"), //
|
||||
UPDATE("UPDATE");
|
||||
|
||||
public final String name;
|
||||
public final String name_lcase;
|
||||
|
||||
On(String name) {
|
||||
this.name = name;
|
||||
this.name_lcase = name.toLowerCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Option;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
@@ -605,7 +606,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
if (x.getIndexType() != null) {
|
||||
print0(ucase ? " USING " : " using ");
|
||||
;
|
||||
print0(x.getIndexType());
|
||||
}
|
||||
|
||||
@@ -2810,17 +2810,20 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
printAndAccept(x.getReferencedColumns(), ", ");
|
||||
print(')');
|
||||
|
||||
if (x.getReferenceMatch() != null) {
|
||||
MysqlForeignKey.Match match = x.getReferenceMatch();
|
||||
if (match != null) {
|
||||
print0(ucase ? " MATCH " : " match ");
|
||||
print0(x.getReferenceMatch().name());
|
||||
print0(ucase ? match.name : match.name_lcase);
|
||||
}
|
||||
|
||||
if (x.getReferenceOn() != null) {
|
||||
MysqlForeignKey.On on = x.getReferenceOn();
|
||||
if (on != null) {
|
||||
print0(ucase ? " ON " : " on ");
|
||||
print0(x.getReferenceOn().name());
|
||||
print0(ucase ? on.name : on.name_lcase);
|
||||
print(' ');
|
||||
if (x.getReferenceOption() != null) {
|
||||
print0(x.getReferenceOption().getText());
|
||||
Option option = x.getReferenceOption();
|
||||
if (option != null) {
|
||||
print0(ucase ? option.name : option.name_lcase);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -2055,8 +2055,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
if (x.getOn() != null) {
|
||||
print0(ucase ? " ON " : " on ");
|
||||
|
||||
if (x.getObjectType() != null) {
|
||||
print0(x.getObjectType().name());
|
||||
SQLObjectType objectType = x.getObjectType();
|
||||
if (objectType != null) {
|
||||
print0(ucase ? objectType.name : objectType.name_lcase);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
|
||||
@@ -93,8 +93,9 @@ public class DDLParserTest extends TestCase {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TEMPORARY TABLE IF NOT EXISTS tb_name (\n\tfid int\n)", output);
|
||||
|
||||
Assert.assertEquals("CREATE TEMPORARY TABLE IF NOT EXISTS tb_name (\n\tfid int\n)", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("create temporary table if not exists tb_name (\n\tfid int\n)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
public void test_createTable_1() throws Exception {
|
||||
@@ -102,8 +103,9 @@ public class DDLParserTest extends TestCase {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE IF NOT EXISTS tb_name (\n\tfid int\n)", output);
|
||||
|
||||
Assert.assertEquals("CREATE TABLE IF NOT EXISTS tb_name (\n\tfid int\n)", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("create table if not exists tb_name (\n\tfid int\n)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
public void test_createIndex_0() throws Exception {
|
||||
|
||||
@@ -18,13 +18,15 @@ package com.alibaba.druid.bvt.sql.mysql;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
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.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class CALL_Test extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
@@ -55,9 +57,10 @@ public class CALL_Test extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("EXECUTE s USING @version, @increment;", text);
|
||||
Assert.assertEquals("EXECUTE s USING @version, @increment", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("execute s using @version, @increment", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
@@ -66,9 +69,10 @@ public class CALL_Test extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("EXECUTE s;", text);
|
||||
Assert.assertEquals("EXECUTE s", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("execute s", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
private String output(List<SQLStatement> stmtList) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
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.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
@@ -33,9 +34,9 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE FROM somelog\nWHERE user = 'jcole'\nORDER BY timestamp_column\nLIMIT 1;", text);
|
||||
Assert.assertEquals("DELETE FROM somelog\nWHERE user = 'jcole'\nORDER BY timestamp_column\nLIMIT 1", SQLUtils.toMySqlString(stmt));
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
@@ -44,12 +45,12 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE t1" + //
|
||||
"\nFROM t1" + //
|
||||
"\n\tLEFT JOIN t2 ON t1.id = t2.id" + //
|
||||
"\nWHERE t2.id IS NULL;", text);
|
||||
"\nWHERE t2.id IS NULL", SQLUtils.toMySqlString(stmt));
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
@@ -58,11 +59,11 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE a1, a2\nFROM t1 a1" + //
|
||||
"\n\tINNER JOIN t2 a2" + //
|
||||
"\nWHERE a1.id = a2.id;", text);
|
||||
"\nWHERE a1.id = a2.id", SQLUtils.toMySqlString(stmt));
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
@@ -71,10 +72,12 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE FROM a1, a2 USING (t1) AS a1" + //
|
||||
"\n\tINNER JOIN t2 a2\nWHERE a1.id = a2.id;", text);
|
||||
"\n\tINNER JOIN t2 a2\nWHERE a1.id = a2.id", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete from a1, a2 using (t1) as a1" + //
|
||||
"\n\tinner join t2 a2\nwhere a1.id = a2.id", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
public void test_4() throws Exception {
|
||||
@@ -83,19 +86,10 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
String text = output(stmtList);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE LOW_PRIORITY QUICK IGNORE FROM T;", text);
|
||||
Assert.assertEquals("DELETE LOW_PRIORITY QUICK IGNORE FROM T", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete low_priority quick ignore from T", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
|
||||
private String output(List<SQLStatement> stmtList) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
stmt.accept(new MySqlOutputVisitor(out));
|
||||
out.append(";");
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
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.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
@@ -41,13 +42,35 @@ public class MySqlCreateTableTest14 extends MysqlTest {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("CREATE TABLE `OptionList` ("
|
||||
+ "\n\t`ID` int(11) NOT NULL AUTO_INCREMENT, "
|
||||
+ "\n\t`OptionID` int(11) NULL COMMENT '选项ID', "
|
||||
+ "\n\t`QuizID` int(11) NULL COMMENT '竞猜题目ID', "
|
||||
+ "\n\t`OptionName` varchar(500) NULL COMMENT '选项名称', "
|
||||
+ "\n\t`OptionCount` int(11) NULL COMMENT '选择的人数', "
|
||||
+ "\n\tPRIMARY KEY (`ID`), "
|
||||
+ "\n\tKEY `quizId` USING BTREE (`QuizID`), "
|
||||
+ "\n\tKEY `optionId` USING BTREE (`OptionID`)"
|
||||
+ "\n) ENGINE = InnoDB CHARSET = gbk COMMENT = ''", //
|
||||
SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("create table `OptionList` ("
|
||||
+ "\n\t`ID` int(11) not null auto_increment, "
|
||||
+ "\n\t`OptionID` int(11) null comment '选项ID', "
|
||||
+ "\n\t`QuizID` int(11) null comment '竞猜题目ID', "
|
||||
+ "\n\t`OptionName` varchar(500) null comment '选项名称', "
|
||||
+ "\n\t`OptionCount` int(11) null comment '选择的人数', "
|
||||
+ "\n\tprimary key (`ID`), "
|
||||
+ "\n\tkey `quizId` using BTREE (`QuizID`), "
|
||||
+ "\n\tkey `optionId` using BTREE (`OptionID`)"
|
||||
+ "\n) engine = InnoDB charset = gbk comment = ''", //
|
||||
SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
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.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
@@ -32,13 +33,17 @@ public class MySqlCreateTableTest19 extends MysqlTest {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("CREATE TABLE new_tbl LIKE orig_tbl", //
|
||||
SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("create table new_tbl like orig_tbl", //
|
||||
SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -96,8 +96,9 @@ public class MySqlCreateTableTest54 extends MysqlTest {
|
||||
Assert.assertEquals(24, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE `t_activity_node` ("
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE `t_activity_node` ("
|
||||
+ "\n\t`id` bigint(20) NOT NULL, "
|
||||
+ "\n\t`sellerId` bigint(20) NULL, "
|
||||
+ "\n\t`canvas_id` bigint(20) NOT NULL COMMENT '画布ID', "
|
||||
@@ -126,7 +127,39 @@ public class MySqlCreateTableTest54 extends MysqlTest {
|
||||
+ "\n\tKEY `sid_ty_time` (`sellerId`, `node_type`, `start_time`), "
|
||||
+ "\n\tCONSTRAINT `t_activity_node_ibfk_1` FOREIGN KEY (`canvas_id`) REFERENCES `t_activity_canvas` (`id`) ON DELETE CASCADE"
|
||||
+ "\n) ENGINE = InnoDB CHARSET = utf8", output);
|
||||
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
Assert.assertEquals("create table `t_activity_node` ("
|
||||
+ "\n\t`id` bigint(20) not null, "
|
||||
+ "\n\t`sellerId` bigint(20) null, "
|
||||
+ "\n\t`canvas_id` bigint(20) not null comment '画布ID', "
|
||||
+ "\n\t`view_node_id` bigint(20) not null comment '对应显示的节点id', "
|
||||
+ "\n\t`activity_type` int(11) not null comment '活动类型', "
|
||||
+ "\n\t`node_type` int(11) not null comment '节点类型', "
|
||||
+ "\n\t`node_title` varchar(200) not null comment '节点标题', "
|
||||
+ "\n\t`status` int(11) not null default '0' comment '页面的显示状态', "
|
||||
+ "\n\t`update_status` int(11) null comment '节点创建后的修改状态', "
|
||||
+ "\n\t`execute_status` int(11) null comment '节点当前的执行状态', "
|
||||
+ "\n\t`start_time` datetime null comment '该节点活动的开始时间', "
|
||||
+ "\n\t`end_time` datetime null comment '该节点活动的结束时间', "
|
||||
+ "\n\t`activity_start_time` datetime null comment '营销活动的开始时间', "
|
||||
+ "\n\t`activity_end_time` datetime null comment '营销活动的结束时间', "
|
||||
+ "\n\t`report_start_time` datetime null comment '该节点活动效果报告的开始时间', "
|
||||
+ "\n\t`report_end_time` datetime null comment '该节点活动效果报告的结束时间', "
|
||||
+ "\n\t`cron_rule` varchar(100) null comment '周期性营销的时间表达式', "
|
||||
+ "\n\t`split_rule` varchar(1000) null comment '节点拆分规则', "
|
||||
+ "\n\t`search_json` varchar(5000) null comment '索引查找的条件字符串', "
|
||||
+ "\n\t`filter_json` varchar(1000) null comment '各种过滤条件的设置', "
|
||||
+ "\n\t`buyer_count` int(11) null comment '营销的会员数量', "
|
||||
+ "\n\t`gmt_modified` datetime not null comment '活动最后修改时间', "
|
||||
+ "\n\t`gmt_create` datetime not null comment '活动创建时间', "
|
||||
+ "\n\tprimary key (`id`), "
|
||||
+ "\n\tkey `canvas_id` (`canvas_id`), "
|
||||
+ "\n\tkey `sid_ty_time` (`sellerId`, `node_type`, `start_time`), "
|
||||
+ "\n\tconstraint `t_activity_node_ibfk_1` foreign key (`canvas_id`) references `t_activity_canvas` (`id`) on delete cascade"
|
||||
+ "\n) engine = InnoDB charset = utf8", output);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
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.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
@@ -33,13 +34,19 @@ public class MySqlDeleteTest_1 extends MysqlTest {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("DELETE FROM a1, a2 USING (t1) AS a1"
|
||||
+ "\n\tINNER JOIN t2 a2"
|
||||
+ "\nWHERE a1.id = a2.id", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete from a1, a2 using (t1) as a1"
|
||||
+ "\n\tinner join t2 a2"
|
||||
+ "\nwhere a1.id = a2.id", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
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.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
@@ -33,13 +34,20 @@ public class MySqlDeleteTest_2 extends MysqlTest {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
Assert.assertEquals("DELETE t1"
|
||||
+ "\nFROM t1"
|
||||
+ "\n\tLEFT JOIN t2 ON t1.id = t2.id"
|
||||
+ "\nWHERE t2.id IS NULL", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete t1"
|
||||
+ "\nfrom t1"
|
||||
+ "\n\tleft join t2 on t1.id = t2.id"
|
||||
+ "\nwhere t2.id is null", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
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.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
@@ -40,13 +41,44 @@ public class MySqlDeleteTest_3 extends MysqlTest {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
Assert.assertEquals("DELETE FROM t1"
|
||||
+ "\nWHERE s11 > ANY (SELECT COUNT(*)"
|
||||
+ "\n\t\tFROM t2"
|
||||
+ "\n\t\tWHERE NOT EXISTS ("
|
||||
+ "\n\t\t\tSELECT *"
|
||||
+ "\n\t\t\tFROM t3"
|
||||
+ "\n\t\t\tWHERE ROW(5 * t2.s1, 77) = ("
|
||||
+ "\n\t\t\t\tSELECT 50, 11 * s1"
|
||||
+ "\n\t\t\t\tFROM t4"
|
||||
+ "\n\t\t\t\tUNION"
|
||||
+ "\n\t\t\t\tSELECT 50, 77"
|
||||
+ "\n\t\t\t\tFROM (SELECT *"
|
||||
+ "\n\t\t\t\t\tFROM t5"
|
||||
+ "\n\t\t\t\t\t) t5"
|
||||
+ "\n\t\t\t\t)"
|
||||
+ "\n\t\t\t))", SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("delete from t1"
|
||||
+ "\nwhere s11 > any (select count(*)"
|
||||
+ "\n\t\tfrom t2"
|
||||
+ "\n\t\twhere not exists ("
|
||||
+ "\n\t\t\tselect *"
|
||||
+ "\n\t\t\tfrom t3"
|
||||
+ "\n\t\t\twhere ROW(5 * t2.s1, 77) = ("
|
||||
+ "\n\t\t\t\tselect 50, 11 * s1"
|
||||
+ "\n\t\t\t\tfrom t4"
|
||||
+ "\n\t\t\t\tunion"
|
||||
+ "\n\t\t\t\tselect 50, 77"
|
||||
+ "\n\t\t\t\tfrom (select *"
|
||||
+ "\n\t\t\t\t\tfrom t5"
|
||||
+ "\n\t\t\t\t\t) t5"
|
||||
+ "\n\t\t\t\t)"
|
||||
+ "\n\t\t\t))", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -40,9 +40,10 @@ public class MySqlGrantTest_12 extends MysqlTest {
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("GRANT CREATE TEMPORARY TABLE ON mydb.* TO 'someuser'@'somehost'", //
|
||||
output);
|
||||
SQLUtils.toMySqlString(stmt));
|
||||
Assert.assertEquals("grant CREATE TEMPORARY TABLE on mydb.* to 'someuser'@'somehost'", //
|
||||
SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -54,5 +54,13 @@ public class MySqlInsertTest_6 extends MysqlTest {
|
||||
"\n\t, ?)" + //
|
||||
"\nON DUPLICATE KEY UPDATE Gmt_modify = VALUES(Gmt_modify), Expired_time = VALUES(Expired_time), the_value = VALUES(the_value)",
|
||||
SQLUtils.toMySqlString(insertStmt));
|
||||
|
||||
|
||||
Assert.assertEquals("insert into document (the_key, the_namespace, Gmt_create, Gmt_modify, Expired_time" + //
|
||||
"\n\t, the_value)" + //
|
||||
"\nvalues (?, ?, now(), now(), date_add(now(), interval ? second)" + //
|
||||
"\n\t, ?)" + //
|
||||
"\non duplicate key update Gmt_modify = VALUES(Gmt_modify), Expired_time = VALUES(Expired_time), the_value = VALUES(the_value)",
|
||||
SQLUtils.toMySqlString(insertStmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user