mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve mapping support
This commit is contained in:
@@ -3,6 +3,7 @@ package com.alibaba.druid.mapping;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -40,11 +41,11 @@ public class MappingEngine {
|
||||
|
||||
public Entity getFirstEntity(MappingContext context) {
|
||||
Entity entity = context.getDefaultEntity();
|
||||
|
||||
|
||||
if (entity != null) {
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
|
||||
return entry.getValue();
|
||||
}
|
||||
@@ -95,7 +96,7 @@ public class MappingEngine {
|
||||
public SQLASTOutputVisitor createOutputVisitor(Appendable out) {
|
||||
return provider.createOutputVisitor(this, out);
|
||||
}
|
||||
|
||||
|
||||
public String explain(String sql) {
|
||||
return explain(sql);
|
||||
}
|
||||
@@ -116,7 +117,7 @@ public class MappingEngine {
|
||||
|
||||
return toSQL(stmt);
|
||||
}
|
||||
|
||||
|
||||
public SQLSelectQueryBlock explainToSelectSQLObject(String sql) {
|
||||
return explainToSelectSQLObject(sql, new MappingContext());
|
||||
}
|
||||
@@ -147,11 +148,11 @@ public class MappingEngine {
|
||||
public void afterResole(MappingVisitor visitor) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public SQLDeleteStatement explainToDeleteSQLObject(String sql, MappingContext context) {
|
||||
return provider.explainToDeleteSQLObject(this, sql, context);
|
||||
}
|
||||
|
||||
|
||||
public String explainToDeleteSQL(String sql) {
|
||||
return explainToDeleteSQL(sql, new MappingContext());
|
||||
}
|
||||
@@ -178,7 +179,7 @@ public class MappingEngine {
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(String sql, MappingContext context) {
|
||||
return provider.explainToUpdateSQLObject(this, sql, context);
|
||||
}
|
||||
|
||||
|
||||
public String explainToUpdateSQL(String sql) {
|
||||
return explainToUpdateSQL(sql, new MappingContext());
|
||||
}
|
||||
@@ -197,7 +198,7 @@ public class MappingEngine {
|
||||
public SQLInsertStatement explainToInsertSQLObject(String sql, MappingContext context) {
|
||||
return provider.explainToInsertSQLObject(this, sql, context);
|
||||
}
|
||||
|
||||
|
||||
public String explainToInsertSQL(String sql) {
|
||||
return explainToInsertSQL(sql, new MappingContext());
|
||||
}
|
||||
@@ -239,7 +240,7 @@ public class MappingEngine {
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
|
||||
public List<Map<String, Object>> select(Connection conn, String sql, List<Object> parameters) throws SQLException {
|
||||
return select(conn, sql, new MappingContext(parameters));
|
||||
}
|
||||
@@ -250,7 +251,7 @@ public class MappingEngine {
|
||||
String rawSql = this.toSQL(sqlObject);
|
||||
return JdbcUtils.executeQuery(conn, rawSql, context.getParameters());
|
||||
}
|
||||
|
||||
|
||||
public int delete(Connection conn, String sql, List<Object> parameters) throws SQLException {
|
||||
return delete(conn, sql, new MappingContext(parameters));
|
||||
}
|
||||
@@ -262,7 +263,7 @@ public class MappingEngine {
|
||||
int updateCount = JdbcUtils.executeUpdate(conn, rawSql, context.getParameters());
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
|
||||
public int update(Connection conn, String sql, List<Object> parameters) throws SQLException {
|
||||
return update(conn, sql, new MappingContext(parameters));
|
||||
}
|
||||
@@ -275,6 +276,10 @@ public class MappingEngine {
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
public void insert(Connection conn, String sql, Object... parameters) throws SQLException {
|
||||
insert(conn, sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public void insert(Connection conn, String sql, List<Object> parameters) throws SQLException {
|
||||
insert(conn, sql, new MappingContext(parameters));
|
||||
}
|
||||
@@ -293,6 +298,10 @@ public class MappingEngine {
|
||||
return this.dataSource.getConnection();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> select(String sql, Object... parameters) throws SQLException {
|
||||
return select(sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> select(String sql, List<Object> parameters) throws SQLException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
@@ -302,6 +311,10 @@ public class MappingEngine {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
public int delete(String sql, Object... parameters) throws SQLException {
|
||||
return delete(sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public int delete(String sql, List<Object> parameters) throws SQLException {
|
||||
Connection conn = null;
|
||||
@@ -322,6 +335,10 @@ public class MappingEngine {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
public void insert(String sql, Object... parameters) throws SQLException {
|
||||
insert(sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public void insert(String sql, List<Object> parameters) throws SQLException {
|
||||
Connection conn = null;
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -497,7 +498,7 @@ public final class JdbcUtils {
|
||||
} catch (ClassNotFoundException e) {
|
||||
// skip
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
return (Driver) Thread.currentThread().getContextClassLoader().loadClass(driverClassName).newInstance();
|
||||
} catch (IllegalAccessException e) {
|
||||
@@ -536,6 +537,10 @@ public final class JdbcUtils {
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
public static void execute(DataSource dataSource, String sql, Object... parameters) throws SQLException {
|
||||
execute(dataSource, sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public static void execute(DataSource dataSource, String sql, List<Object> parameters) throws SQLException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
@@ -610,7 +615,7 @@ public final class JdbcUtils {
|
||||
stmt.setObject(i + 1, parameters.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Class<?> loadDriverClass(String className) {
|
||||
Class<?> clazz = null;
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.alibaba.druid.bvt.mapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class MySqlMappingEngineTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
private MappingEngine mapping = new MappingEngine();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setUrl("jdbc:h2:mem:test;");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
|
||||
JdbcUtils.execute(dataSource, "CREATE TABLE user (id INT, name VARCHAR(40))");
|
||||
|
||||
mapping.setDataSource(dataSource);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.execute(dataSource, "DROP TABLE user");
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_curd() throws Exception {
|
||||
{
|
||||
List<Map<String, Object>> list = mapping.select("select * from user");
|
||||
Assert.assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
mapping.insert("insert into user (id, name) values (?, ?)", 123, "wenshao");
|
||||
|
||||
{
|
||||
List<Map<String, Object>> list = mapping.select("select * from user");
|
||||
Assert.assertEquals(1, list.size());
|
||||
|
||||
Map<String, Object> data = list.get(0);
|
||||
Assert.assertEquals(123, data.get("ID"));
|
||||
Assert.assertEquals("wenshao", data.get("NAME"));
|
||||
}
|
||||
|
||||
{
|
||||
List<Map<String, Object>> list = mapping.select("select id \"id\", name \"name\" from user");
|
||||
Assert.assertEquals(1, list.size());
|
||||
|
||||
Map<String, Object> data = list.get(0);
|
||||
Assert.assertEquals(123, data.get("id"));
|
||||
Assert.assertEquals("wenshao", data.get("name"));
|
||||
}
|
||||
|
||||
mapping.delete("delete from user");
|
||||
|
||||
{
|
||||
List<Map<String, Object>> list = mapping.select("select * from user");
|
||||
Assert.assertEquals(0, list.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user