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,
SQLite
}
// The connection
private Connection conn = null;
private Connection conn;
// The pool that this connection belongs to
private DBConnectionPool pool;
@ -57,7 +58,7 @@ public class DBConnection implements Closeable{
public DBConnection(String jndi) throws NamingException, SQLException{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/"+jndi);
conn = ds.getConnection();
this.conn = ds.getConnection();
}
/**
@ -110,7 +111,7 @@ public class DBConnection implements Closeable{
* @param db is the DB type
* @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){
case MySQL:
Class.forName("com.mysql.jdbc.Driver").newInstance();
@ -129,12 +130,35 @@ public class DBConnection implements Closeable{
*/
public long getLastInsertID(){
try{
return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult<BigInteger>()).longValue();
return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult<Long>());
}catch(SQLException e){
logger.log(Level.WARNING, null, e);
}
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>

View file

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