com.aoindustries.dbc.AbstractDatabaseAccess Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ao-dbc Show documentation
Show all versions of ao-dbc Show documentation
Simplified JDBC access for simplified code.
/*
* ao-dbc - Simplified JDBC access for simplified code.
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014, 2015, 2016, 2018 AO Industries, Inc.
* [email protected]
* 7262 Bull Pen Cir
* Mobile, AL 36695
*
* This file is part of ao-dbc.
*
* ao-dbc is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ao-dbc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ao-dbc. If not, see .
*/
package com.aoindustries.dbc;
import com.aoindustries.util.AoCollections;
import com.aoindustries.util.IntList;
import com.aoindustries.util.LongList;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Wraps and simplifies access to a JDBC database.
*
* @author AO Industries, Inc.
*/
abstract public class AbstractDatabaseAccess implements DatabaseAccess {
private static final ObjectFactory bigDecimalObjectFactory = new ObjectFactory() {
@Override
public BigDecimal createObject(ResultSet result) throws SQLException {
return result.getBigDecimal(1);
}
};
@Override
final public BigDecimal executeBigDecimalQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, bigDecimalObjectFactory, sql, params);
}
@Override
final public BigDecimal executeBigDecimalUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, bigDecimalObjectFactory, sql, params);
}
@Override
final public BigDecimal executeBigDecimalQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, bigDecimalObjectFactory, sql, params);
}
@Override
final public boolean executeBooleanQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeBooleanQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, sql, params);
}
@Override
final public boolean executeBooleanUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeBooleanQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, sql, params);
}
@Override
abstract public boolean executeBooleanQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException;
private static final ObjectFactory byteArrayObjectFactory = new ObjectFactory() {
@Override
public byte[] createObject(ResultSet result) throws SQLException {
return result.getBytes(1);
}
};
@Override
final public byte[] executeByteArrayQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, byteArrayObjectFactory, sql, params);
}
@Override
final public byte[] executeByteArrayUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, byteArrayObjectFactory, sql, params);
}
@Override
final public byte[] executeByteArrayQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, byteArrayObjectFactory, sql, params);
}
private static final ObjectFactory dateObjectFactory = new ObjectFactory() {
@Override
public Date createObject(ResultSet result) throws SQLException {
return result.getDate(1);
}
};
@Override
final public Date executeDateQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, dateObjectFactory, sql, params);
}
@Override
final public Date executeDateUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, dateObjectFactory, sql, params);
}
@Override
final public Date executeDateQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, dateObjectFactory, sql, params);
}
@Override
final public IntList executeIntListQuery(String sql, Object ... params) throws SQLException {
return executeIntListQuery(Connection.TRANSACTION_READ_COMMITTED, true, sql, params);
}
@Override
final public IntList executeIntListUpdate(String sql, Object ... params) throws SQLException {
return executeIntListQuery(Connection.TRANSACTION_READ_COMMITTED, false, sql, params);
}
@Override
abstract public IntList executeIntListQuery(int isolationLevel, boolean readOnly, String sql, Object ... params) throws SQLException;
@Override
final public int executeIntQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeIntQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, sql, params);
}
@Override
final public int executeIntUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeIntQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, sql, params);
}
@Override
abstract public int executeIntQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException;
@Override
final public LongList executeLongListQuery(String sql, Object ... params) throws SQLException {
return executeLongListQuery(Connection.TRANSACTION_READ_COMMITTED, true, sql, params);
}
@Override
final public LongList executeLongListUpdate(String sql, Object ... params) throws SQLException {
return executeLongListQuery(Connection.TRANSACTION_READ_COMMITTED, false, sql, params);
}
@Override
abstract public LongList executeLongListQuery(int isolationLevel, boolean readOnly, String sql, Object ... params) throws SQLException;
@Override
final public long executeLongQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeLongQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, sql, params);
}
@Override
final public long executeLongUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeLongQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, sql, params);
}
@Override
abstract public long executeLongQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException;
private static class ClassObjectFactory implements ObjectFactory {
private final Class clazz;
private final Constructor constructor;
private ClassObjectFactory(Class clazz) throws SQLException {
this.clazz = clazz;
try {
this.constructor = clazz.getConstructor(ResultSet.class);
} catch(NoSuchMethodException err) {
throw new SQLException("Unable to find constructor: "+clazz.getName()+"(java.sql.ResultSet)", err);
}
}
@Override
public T createObject(ResultSet result) throws SQLException {
try {
return constructor.newInstance(result);
} catch(InstantiationException err) {
throw new SQLException("Unable to instantiate object: "+clazz.getName()+"(java.sql.ResultSet)", err);
} catch(IllegalAccessException err) {
throw new SQLException("Illegal access on constructor: "+clazz.getName()+"(java.sql.ResultSet)", err);
} catch(InvocationTargetException err) {
throw new SQLException("Invocation exception on constructor: "+clazz.getName()+"(java.sql.ResultSet)", err);
}
}
};
@Override
final public T executeObjectQuery(Class clazz, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params);
}
@Override
final public T executeObjectUpdate(Class clazz, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params);
}
@Override
final public T executeObjectQuery(int isolationLevel, boolean readOnly, boolean rowRequired, Class clazz, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params);
}
@Override
final public T executeObjectQuery(ObjectFactory objectFactory, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, objectFactory, sql, params);
}
@Override
final public T executeObjectUpdate(ObjectFactory objectFactory, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, objectFactory, sql, params);
}
@Override
final public T executeObjectQuery(int isolationLevel, boolean readOnly, boolean rowRequired, ObjectFactory objectFactory, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, objectFactory, sql, params);
}
@Override
final public T executeObjectQuery(Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws NoRowException, SQLException, E {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, eClass, objectFactory, sql, params);
}
@Override
final public T executeObjectUpdate(Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws NoRowException, SQLException, E {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, eClass, objectFactory, sql, params);
}
@Override
abstract public T executeObjectQuery(int isolationLevel, boolean readOnly, boolean rowRequired, Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws NoRowException, SQLException, E;
@Override
final public List executeObjectListQuery(Class clazz, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, new ArrayList(), RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params)
);
}
@Override
final public List executeObjectListUpdate(Class clazz, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, new ArrayList(), RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params)
);
}
@Override
final public List executeObjectListQuery(int isolationLevel, boolean readOnly, Class clazz, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(isolationLevel, readOnly, new ArrayList(), RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params)
);
}
@Override
final public List executeObjectListQuery(ObjectFactory objectFactory, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, new ArrayList(), RuntimeException.class, objectFactory, sql, params)
);
}
@Override
final public List executeObjectListUpdate(ObjectFactory objectFactory, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, new ArrayList(), RuntimeException.class, objectFactory, sql, params)
);
}
@Override
final public List executeObjectListQuery(int isolationLevel, boolean readOnly, ObjectFactory objectFactory, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(isolationLevel, readOnly, new ArrayList(), RuntimeException.class, objectFactory, sql, params)
);
}
@Override
final public List executeObjectListQuery(Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws SQLException, E {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, new ArrayList(), eClass, objectFactory, sql, params)
);
}
@Override
final public List executeObjectListUpdate(Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws SQLException, E {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, new ArrayList(), eClass, objectFactory, sql, params)
);
}
@Override
final public List executeObjectListQuery(int isolationLevel, boolean readOnly, Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws SQLException, E {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(isolationLevel, readOnly, new ArrayList(), eClass, objectFactory, sql, params)
);
}
@Override
final public > C executeObjectCollectionQuery(C collection, Class clazz, String sql, Object ... params) throws SQLException {
return executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, collection, RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params);
}
@Override
final public > C executeObjectCollectionUpdate(C collection, Class clazz, String sql, Object ... params) throws SQLException {
return executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, collection, RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params);
}
@Override
final public > C executeObjectCollectionQuery(int isolationLevel, boolean readOnly, C collection, Class clazz, String sql, Object ... params) throws SQLException {
return executeObjectCollectionQuery(isolationLevel, readOnly, collection, RuntimeException.class, new ClassObjectFactory<>(clazz), sql, params);
}
@Override
final public > C executeObjectCollectionQuery(C collection, ObjectFactory objectFactory, String sql, Object ... params) throws SQLException {
return executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, collection, RuntimeException.class, objectFactory, sql, params);
}
@Override
final public > C executeObjectCollectionUpdate(C collection, ObjectFactory objectFactory, String sql, Object ... params) throws SQLException {
return executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, collection, RuntimeException.class, objectFactory, sql, params);
}
@Override
final public > C executeObjectCollectionQuery(int isolationLevel, boolean readOnly, C collection, ObjectFactory objectFactory, String sql, Object ... params) throws SQLException {
return executeObjectCollectionQuery(isolationLevel, readOnly, collection, RuntimeException.class, objectFactory, sql, params);
}
@Override
final public ,E extends Exception> C executeObjectCollectionQuery(C collection, Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws SQLException, E {
return executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, collection, eClass, objectFactory, sql, params);
}
@Override
final public ,E extends Exception> C executeObjectCollectionUpdate(C collection, Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws SQLException, E {
return executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, collection, eClass, objectFactory, sql, params);
}
@Override
abstract public ,E extends Exception> C executeObjectCollectionQuery(int isolationLevel, boolean readOnly, C collection, Class eClass, ObjectFactoryE objectFactory, String sql, Object ... params) throws SQLException, E;
@Override
final public T executeQuery(ResultSetHandler resultSetHandler, String sql, Object ... params) throws SQLException {
return executeQuery(Connection.TRANSACTION_READ_COMMITTED, true, RuntimeException.class, resultSetHandler, sql, params);
}
@Override
final public T executeUpdate(ResultSetHandler resultSetHandler, String sql, Object ... params) throws SQLException {
return executeQuery(Connection.TRANSACTION_READ_COMMITTED, false, RuntimeException.class, resultSetHandler, sql, params);
}
@Override
final public T executeQuery(int isolationLevel, boolean readOnly, ResultSetHandler resultSetHandler, String sql, Object ... params) throws SQLException {
return executeQuery(isolationLevel, readOnly, RuntimeException.class, resultSetHandler, sql, params);
}
@Override
final public T executeQuery(Class eClass, ResultSetHandlerE resultSetHandler, String sql, Object ... params) throws SQLException, E {
return executeQuery(Connection.TRANSACTION_READ_COMMITTED, true, eClass, resultSetHandler, sql, params);
}
@Override
final public T executeUpdate(Class eClass, ResultSetHandlerE resultSetHandler, String sql, Object ... params) throws SQLException, E {
return executeQuery(Connection.TRANSACTION_READ_COMMITTED, false, eClass, resultSetHandler, sql, params);
}
@Override
abstract public T executeQuery(int isolationLevel, boolean readOnly, Class eClass, ResultSetHandlerE resultSetHandler, String sql, Object ... params) throws SQLException, E;
private static final ObjectFactory shortObjectFactory = new ObjectFactory() {
@Override
public Short createObject(ResultSet result) throws SQLException {
return result.getShort(1);
}
};
@Override
final public List executeShortListQuery(String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, new ArrayList(), RuntimeException.class, shortObjectFactory, sql, params)
);
}
@Override
final public List executeShortListUpdate(String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, new ArrayList(), RuntimeException.class, shortObjectFactory, sql, params)
);
}
@Override
final public List executeShortListQuery(int isolationLevel, boolean readOnly, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(isolationLevel, readOnly, new ArrayList(), RuntimeException.class, shortObjectFactory, sql, params)
);
}
@Override
final public short executeShortQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeShortQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, sql, params);
}
@Override
final public short executeShortUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeShortQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, sql, params);
}
@Override
abstract public short executeShortQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException;
private static final ObjectFactory stringObjectFactory = new ObjectFactory() {
@Override
public String createObject(ResultSet result) throws SQLException {
return result.getString(1);
}
};
@Override
final public String executeStringQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, stringObjectFactory, sql, params);
}
@Override
final public String executeStringUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, stringObjectFactory, sql, params);
}
@Override
final public String executeStringQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, stringObjectFactory, sql, params);
}
@Override
final public List executeStringListQuery(String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, true, new ArrayList(), RuntimeException.class, stringObjectFactory, sql, params)
);
}
@Override
final public List executeStringListUpdate(String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(Connection.TRANSACTION_READ_COMMITTED, false, new ArrayList(), RuntimeException.class, stringObjectFactory, sql, params)
);
}
@Override
final public List executeStringListQuery(int isolationLevel, boolean readOnly, String sql, Object ... params) throws SQLException {
return AoCollections.optimalUnmodifiableList(
executeObjectCollectionQuery(isolationLevel, readOnly, new ArrayList(), RuntimeException.class, stringObjectFactory, sql, params)
);
}
private static final ObjectFactory timestampObjectFactory = new ObjectFactory() {
@Override
public Timestamp createObject(ResultSet result) throws SQLException {
return result.getTimestamp(1);
}
};
@Override
final public Timestamp executeTimestampQuery(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, true, true, RuntimeException.class, timestampObjectFactory, sql, params);
}
@Override
final public Timestamp executeTimestampUpdate(String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(Connection.TRANSACTION_READ_COMMITTED, false, true, RuntimeException.class, timestampObjectFactory, sql, params);
}
@Override
final public Timestamp executeTimestampQuery(int isolationLevel, boolean readOnly, boolean rowRequired, String sql, Object ... params) throws NoRowException, SQLException {
return executeObjectQuery(isolationLevel, readOnly, rowRequired, RuntimeException.class, timestampObjectFactory, sql, params);
}
@Override
abstract public int executeUpdate(String sql, Object ... params) throws SQLException;
}