SQLUtils support addCondition & addSelectItem

This commit is contained in:
wenshao
2015-06-07 18:47:41 +08:00
parent f616dbfebe
commit fc4cdfacbd
15 changed files with 450 additions and 7 deletions
@@ -0,0 +1,26 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddColumnTest extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT id, name" //
+ "\nFROM t", SQLUtils.addSelectItem("select id from t", "name", null, null));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT id, name AS XX" //
+ "\nFROM t", SQLUtils.addSelectItem("select id from t", "name", "XX", null));
}
public void test_select_2() throws Exception {
Assert.assertEquals("SELECT id, name AS \"XX W\"" //
+ "\nFROM t", SQLUtils.addSelectItem("select id from t", "name", "XX W", null));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", null));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", null));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", null));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", null));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", null));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", null));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_mysql extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "mysql"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "mysql"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "mysql"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "mysql"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "mysql"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "mysql"));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_oracle extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "oracle"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "oracle"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "oracle"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "oracle"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "oracle"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "oracle"));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_pg extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "postgresql"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "postgresql"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "postgresql"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "postgresql"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "postgresql"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "postgresql"));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_sqlserver extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "sqlserver"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "sqlserver"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "sqlserver"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "sqlserver"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "sqlserver"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "sqlserver"));
}
}
@@ -49,7 +49,7 @@ public class SQLServerUpdateTest extends TestCase {
"\n\t\tGROUP BY a.FId" +
"\n\t\t) b" +
"\nWHERE a.FId = b.FId" +
"\nAND a.FId = ?";
"\n\tAND a.FId = ?";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);