Added compatibility for SQLite to DBBean

This commit is contained in:
Ziver Koc 2015-12-12 01:34:26 +01:00
parent 0fae09d687
commit 47cfb895fb
3 changed files with 68 additions and 28 deletions

BIN
Zutil.jar

Binary file not shown.

View file

@ -43,8 +43,9 @@ public class DBConnection implements Closeable{
MySQL, MySQL,
SQLite SQLite
} }
// The connection // The connection
private Connection conn = null; private Connection conn;
// The pool that this connection belongs to // The pool that this connection belongs to
private DBConnectionPool pool; private DBConnectionPool pool;
@ -57,8 +58,8 @@ public class DBConnection implements Closeable{
public DBConnection(String jndi) throws NamingException, SQLException{ public DBConnection(String jndi) throws NamingException, SQLException{
InitialContext ctx = new InitialContext(); InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/"+jndi); DataSource ds = (DataSource)ctx.lookup("java:comp/env/"+jndi);
conn = ds.getConnection(); this.conn = ds.getConnection();
} }
/** /**
* Creates an Connection to a MySQL server * Creates an Connection to a MySQL server
@ -110,7 +111,7 @@ public class DBConnection implements Closeable{
* @param db is the DB type * @param db is the DB type
* @return the protocol name of the DBMS * @return the protocol name of the DBMS
*/ */
public String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ public static String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
switch(db){ switch(db){
case MySQL: case MySQL:
Class.forName("com.mysql.jdbc.Driver").newInstance(); Class.forName("com.mysql.jdbc.Driver").newInstance();
@ -129,12 +130,35 @@ public class DBConnection implements Closeable{
*/ */
public long getLastInsertID(){ public long getLastInsertID(){
try{ try{
return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult<BigInteger>()).longValue(); return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult<Long>());
}catch(SQLException e){ }catch(SQLException e){
logger.log(Level.WARNING, null, e); logger.log(Level.WARNING, null, e);
} }
return -1; return -1;
} }
/**
* @return the last inserted id or -1 if there was an error
*/
public long getLastInsertID(Statement stmt){
ResultSet result = null;
try{
result = stmt.getGeneratedKeys();
if(result != null){
return new SimpleSQLResult<Integer>().handleQueryResult(stmt, result);
}
}catch(SQLException e){
logger.log(Level.WARNING, null, e);
} finally {
if(result != null) {
try {
result.close();
} catch (SQLException e) {
logger.log(Level.WARNING, null, e);
}
}
}
return -1;
}
/** /**
* Runs a Prepared Statement.<br> * Runs a Prepared Statement.<br>

View file

@ -212,28 +212,44 @@ public abstract class DBBean {
Long id = this.getId(); Long id = this.getId();
// Generate the SQL // Generate the SQL
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
if( id == null ) if( id == null ) {
query.append("INSERT INTO "); query.append("INSERT INTO ").append( config.tableName );
else query.append("UPDATE "); query.append( " (" );
query.append( config.tableName ); for( Field field : config.fields ){
if( !List.class.isAssignableFrom(field.getType()) ){
query.append(" ");
query.append(field.getName());
query.append(",");
}
}
if(query.charAt(query.length()-1) == ',')
query.deleteCharAt(query.length()-1);
query.append( ") VALUES(" );
for( Field field : config.fields ){
query.append( "?," );
}
if(query.charAt(query.length()-1) == ',')
query.deleteCharAt(query.length()-1);
query.append( ")" );
}
else{
query.append("UPDATE ").append( config.tableName );
query.append( " SET" );
for( Field field : config.fields ){
if( !List.class.isAssignableFrom(field.getType()) ){
query.append(" ");
query.append(field.getName());
query.append("=?,");
}
}
if(query.charAt(query.length()-1) == ',')
query.deleteCharAt(query.length()-1);
query.append(" WHERE ").append(config.idColumn).append("=?");
}
StringBuilder params = new StringBuilder(); String sql = query.toString();
for( Field field : config.fields ){ logger.finest("Save query("+c.getName()+" id:"+this.getId()+"): "+ sql);
if( !List.class.isAssignableFrom(field.getType()) ){ PreparedStatement stmt = db.getPreparedStatement( sql );
params.append(" ");
params.append(field.getName());
params.append("=?,");
}
}
if( params.length() > 0 ){
params.delete( params.length()-1, params.length());
query.append( " SET" );
query.append( params );
if( id != null )
query.append(" WHERE ").append(config.idColumn).append("=?");
}
logger.finest("Save query("+c.getName()+" id:"+this.getId()+"): "+query.toString());
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
// Put in the variables in the SQL // Put in the variables in the SQL
int index = 1; int index = 1;
for(Field field : config.fields){ for(Field field : config.fields){
@ -253,7 +269,7 @@ public abstract class DBBean {
// A list of DBBeans // A list of DBBeans
else if( List.class.isAssignableFrom( field.getType() ) && else if( List.class.isAssignableFrom( field.getType() ) &&
field.getAnnotation( DBLinkTable.class ) != null){ field.getAnnotation( DBLinkTable.class ) != null){
// DO NOTING // Du stuff later
} }
// Normal field // Normal field
else{ else{
@ -268,7 +284,7 @@ public abstract class DBBean {
// Execute the SQL // Execute the SQL
DBConnection.exec(stmt); DBConnection.exec(stmt);
if( id == null ) if( id == null )
this.id = db.getLastInsertID(); this.id = db.getLastInsertID(stmt);
// Save the list, after we get the object id // Save the list, after we get the object id
for(Field field : config.fields){ for(Field field : config.fields){