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);
@@ -51,7 +51,9 @@ public class DruidStatServiceTest extends TestCase {
private DruidDataSource dataSource;
@Override
protected void setUp() throws Exception {
DruidStatService.getInstance().setResetEnable(true);
// DruidStatService is singleton, reset all for other testcase.
DruidStatService.getInstance().service("/reset-all.json");
// clear web app.
@@ -127,6 +129,10 @@ public class DruidStatServiceTest extends TestCase {
}
public void test_statService_getDataSourceList() throws Exception {
{
DruidStatService.getInstance().service("/reset-all.json");
}
String sql = "select 1";
Connection conn = dataSource.getConnection();
@@ -155,6 +155,7 @@ public class DruidStatServiceTest2 extends TestCase {
}
public void test_statService_getDataSourceList() throws Exception {
DruidStatService.getInstance().service("/reset-all.json");
String result = DruidStatService.getInstance().service("/datasource.json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
List<Map<String, Object>> dataSourceList = (List<Map<String, Object>>) resultMap.get("Content");