Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Commit

Permalink
fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Sep 12, 2016
1 parent a48e675 commit c7f80a9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
33 changes: 26 additions & 7 deletions src/main/java/com/blade/jdbc/Base.java
Original file line number Diff line number Diff line change
@@ -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<String, Sql2o> sql2oMap = new HashMap<String, Sql2o>(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();
}

}
27 changes: 18 additions & 9 deletions src/main/java/com/blade/jdbc/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +30,9 @@ public class Model extends HashMap<String, Object> {
private static final Logger LOGGER = LoggerFactory.getLogger(Model.class);

private Class<? extends Model> clazz;


private Sql2o sql2o;

private Connection connection;

private Query query;
Expand All @@ -46,6 +49,12 @@ public class Model extends HashMap<String, Object> {

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) {
Expand Down Expand Up @@ -108,7 +117,7 @@ public <K> K save() {
}

if(null == this.connection){
this.connection = Base.sql2o.open();
this.connection = sql2o.open();
}

this.query = connection.createQuery(sql);
Expand All @@ -123,7 +132,7 @@ public <K> 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<Object> vlaues = this.values();
Object[] paramValues = vlaues.toArray(new Object[vlaues.size()]);
Expand Down Expand Up @@ -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<Object> vlaues = new ArrayList<Object>(this.values());
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -239,7 +248,7 @@ private <T extends Model> List<T> 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();
Expand Down Expand Up @@ -273,7 +282,7 @@ public <T extends Model> T findById(Serializable pk) {
public <T extends Model> 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();
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/sql2o/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ public Object getKey(){
}
return null;
}

@SuppressWarnings("unchecked") // need to change Convert
public <V> V getKey(Class returnType){

public <V> V getKey(Class<V> returnType){
final Quirks quirks = this.sql2o.getQuirks();
Object key = getKey();
try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/sql2o/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sql2o/converters/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>, Converter<?>> registeredConverters = new HashMap<Class<?>, Converter<?>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* Used by sql2o to convert a value from the database into an {@link Enum}.
*/
public interface EnumConverterFactory {
<E extends Enum> Converter<E> newConverter(Class<E> enumClass);
<E extends Enum> Converter<E> newConverter(Class<E> enumClass);
}

0 comments on commit c7f80a9

Please sign in to comment.