From c7f80a985fd2e3325e958ddb5dcb298238bc617f Mon Sep 17 00:00:00 2001 From: biezhi Date: Mon, 12 Sep 2016 11:56:35 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=84=20fix=20https://github.com/bladejava/?= =?UTF-8?q?blade-jdbc/issues/6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/blade/jdbc/Base.java | 33 +++++++++++++++---- src/main/java/com/blade/jdbc/Model.java | 27 ++++++++++----- src/main/java/org/sql2o/Connection.java | 5 ++- src/main/java/org/sql2o/Query.java | 4 +-- .../java/org/sql2o/converters/Convert.java | 2 +- .../converters/EnumConverterFactory.java | 2 +- 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/blade/jdbc/Base.java b/src/main/java/com/blade/jdbc/Base.java index b894791..1f36550 100644 --- a/src/main/java/com/blade/jdbc/Base.java +++ b/src/main/java/com/blade/jdbc/Base.java @@ -1,23 +1,42 @@ package com.blade.jdbc; +import java.util.HashMap; +import java.util.Map; + import javax.sql.DataSource; import org.sql2o.Sql2o; public final class Base { - - public static Sql2o sql2o; - + + public static Map sql2oMap = new HashMap(8); + public static void open(String url, String user, String password) { - sql2o = new Sql2o(url, user, password); + sql2oMap.put(Const.DEFAULT_DB_NAME, new Sql2o(url, user, password)); } public static void open(DataSource dataSource) { - sql2o = new Sql2o(dataSource); + sql2oMap.put(Const.DEFAULT_DB_NAME, new Sql2o(dataSource)); + } + + public static void open(String dbName, DataSource dataSource) { + sql2oMap.put(dbName, new Sql2o(dataSource)); + } + + public static Sql2o database() { + return sql2oMap.get(Const.DEFAULT_DB_NAME); + } + + public static Sql2o database(String dbName) { + return sql2oMap.get(dbName); + } + + public static void execute(String sql) { + database().open().createQuery(sql).executeUpdate(); } - public static void execute(String sql){ - sql2o.open().createQuery(sql).executeUpdate(); + public static void execute(String dbName, String sql) { + database(dbName).open().createQuery(sql).executeUpdate(); } } diff --git a/src/main/java/com/blade/jdbc/Model.java b/src/main/java/com/blade/jdbc/Model.java index 809ab5f..36b1577 100644 --- a/src/main/java/com/blade/jdbc/Model.java +++ b/src/main/java/com/blade/jdbc/Model.java @@ -13,6 +13,7 @@ import org.slf4j.LoggerFactory; import org.sql2o.Connection; import org.sql2o.Query; +import org.sql2o.Sql2o; import com.blade.jdbc.annotation.Table; import com.blade.jdbc.dialect.DefaultDialect; @@ -29,7 +30,9 @@ public class Model extends HashMap { private static final Logger LOGGER = LoggerFactory.getLogger(Model.class); private Class clazz; - + + private Sql2o sql2o; + private Connection connection; private Query query; @@ -46,6 +49,12 @@ public class Model extends HashMap { public Model() { this.clazz = this.getClass(); + this.sql2o = Base.database(); + } + + public Model db(String name){ + this.sql2o = Base.database(); + return this; } public Model where(String name, Object value) { @@ -108,7 +117,7 @@ public K save() { } if(null == this.connection){ - this.connection = Base.sql2o.open(); + this.connection = sql2o.open(); } this.query = connection.createQuery(sql); @@ -123,7 +132,7 @@ public K save() { public void addToBatch(){ if(null == query){ String sql = dialect.getSaveSql(this); - query = Base.sql2o.beginTransaction().createQuery(sql); + query = sql2o.beginTransaction().createQuery(sql); } Collection vlaues = this.values(); Object[] paramValues = vlaues.toArray(new Object[vlaues.size()]); @@ -162,7 +171,7 @@ public int update() { } if(null == this.connection){ - this.connection = Base.sql2o.open(); + this.connection = sql2o.open(); } Query query = this.connection.createQuery(sql); List vlaues = new ArrayList(this.values()); @@ -190,7 +199,7 @@ public int delete(){ } if(null == this.connection){ - this.connection = Base.sql2o.open(); + this.connection = sql2o.open(); } Query query = this.connection.createQuery(sql); @@ -207,7 +216,7 @@ public int delete(){ } public void tx(AtomTx atomTx){ - this.connection = Base.sql2o.beginTransaction(); + this.connection = sql2o.beginTransaction(); try { atomTx.execute(); this.connection.commit(); @@ -239,7 +248,7 @@ private List list(String sql) { LOGGER.debug("Preparing\t=> {}", querySql); - Query query = Base.sql2o.open().createQuery(querySql); + Query query = sql2o.open().createQuery(querySql); if (!this.params.isEmpty()) { Object[] paramValues = this.params.values().toArray(); @@ -273,7 +282,7 @@ public T findById(Serializable pk) { public T findOne() { String sql = dialect.getQueryOneSql(this.sql, this); LOGGER.debug("Preparing\t=> {}", sql); - Query query = Base.sql2o.open().createQuery(sql); + Query query = sql2o.open().createQuery(sql); if (!this.params.isEmpty()) { Object[] paramValues = this.params.values().toArray(); @@ -298,7 +307,7 @@ public int count(){ public int count(boolean clear){ String sql = dialect.getQueryCountSql(this.sql, this); LOGGER.debug("Preparing\t=> {}", sql); - Query query = Base.sql2o.open().createQuery(sql); + Query query = sql2o.open().createQuery(sql); if (!this.params.isEmpty()) { Object[] paramValues = this.params.values().toArray(); diff --git a/src/main/java/org/sql2o/Connection.java b/src/main/java/org/sql2o/Connection.java index ab6e912..16066d1 100644 --- a/src/main/java/org/sql2o/Connection.java +++ b/src/main/java/org/sql2o/Connection.java @@ -192,9 +192,8 @@ public Object getKey(){ } return null; } - - @SuppressWarnings("unchecked") // need to change Convert - public V getKey(Class returnType){ + + public V getKey(Class returnType){ final Quirks quirks = this.sql2o.getQuirks(); Object key = getKey(); try { diff --git a/src/main/java/org/sql2o/Query.java b/src/main/java/org/sql2o/Query.java index 7ba96e5..a31f934 100644 --- a/src/main/java/org/sql2o/Query.java +++ b/src/main/java/org/sql2o/Query.java @@ -35,8 +35,8 @@ /** * Represents a sql2o statement. With sql2o, all statements are instances of the Query class. */ -public class Query implements AutoCloseable { - +public class Query { + private final static Logger logger = LocalLoggerFactory.getLogger(Query.class); private Connection connection; diff --git a/src/main/java/org/sql2o/converters/Convert.java b/src/main/java/org/sql2o/converters/Convert.java index b3e72ed..1e4f74b 100644 --- a/src/main/java/org/sql2o/converters/Convert.java +++ b/src/main/java/org/sql2o/converters/Convert.java @@ -18,7 +18,7 @@ public class Convert { private static final ReentrantReadWriteLock rrwl = new ReentrantReadWriteLock(); private static final ReentrantReadWriteLock.ReadLock rl = rrwl.readLock(); - private static final ReentrantReadWriteLock.WriteLock wl = rrwl.writeLock(); +// private static final ReentrantReadWriteLock.WriteLock wl = rrwl.writeLock(); private static volatile EnumConverterFactory registeredEnumConverterFactory = new DefaultEnumConverterFactory(); private static Map, Converter> registeredConverters = new HashMap, Converter>(); diff --git a/src/main/java/org/sql2o/converters/EnumConverterFactory.java b/src/main/java/org/sql2o/converters/EnumConverterFactory.java index 76da53d..120ee95 100644 --- a/src/main/java/org/sql2o/converters/EnumConverterFactory.java +++ b/src/main/java/org/sql2o/converters/EnumConverterFactory.java @@ -4,5 +4,5 @@ * Used by sql2o to convert a value from the database into an {@link Enum}. */ public interface EnumConverterFactory { - Converter newConverter(Class enumClass); + Converter newConverter(Class enumClass); }