com.j256.ormlite.dao.RuntimeExceptionDao Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ormlite-core Show documentation
Show all versions of ormlite-core Show documentation
Lightweight Object Relational Model (ORM) for persisting objects to SQL databases.
package com.j256.ormlite.dao;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.logger.Log.Level;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.logger.LoggerFactory;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.GenericRowMapper;
import com.j256.ormlite.stmt.PreparedDelete;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.PreparedUpdate;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.support.DatabaseResults;
import com.j256.ormlite.table.DatabaseTableConfig;
import com.j256.ormlite.table.ObjectFactory;
import com.j256.ormlite.table.TableInfo;
/**
* Proxy to a {@link Dao} that wraps each Exception and rethrows it as RuntimeException. You can use this if your usage
* pattern is to ignore all exceptions. That's not a pattern that I like so it's not the default.
*
*
* RuntimeExceptionDao<Account, String> accountDao = RuntimeExceptionDao.createDao(connectionSource, Account.class);
*
*
* @author graywatson
*/
public class RuntimeExceptionDao implements Dao {
/*
* We use debug here because we don't want these messages to be logged by default. The user will need to turn on
* logging for this class to DEBUG to see the messages.
*/
private static final Level LOG_LEVEL = Level.DEBUG;
private Dao dao;
private static final Logger logger = LoggerFactory.getLogger(RuntimeExceptionDao.class);
public RuntimeExceptionDao(Dao dao) {
this.dao = dao;
}
/**
* Call through to {@link DaoManager#createDao(ConnectionSource, Class)} with the returned DAO wrapped in a
* RuntimeExceptionDao.
*/
public static RuntimeExceptionDao createDao(ConnectionSource connectionSource, Class clazz)
throws SQLException {
@SuppressWarnings("unchecked")
Dao castDao = (Dao) DaoManager.createDao(connectionSource, clazz);
return new RuntimeExceptionDao(castDao);
}
/**
* Call through to {@link DaoManager#createDao(ConnectionSource, DatabaseTableConfig)} with the returned DAO wrapped
* in a RuntimeExceptionDao.
*/
public static RuntimeExceptionDao createDao(ConnectionSource connectionSource,
DatabaseTableConfig tableConfig) throws SQLException {
@SuppressWarnings("unchecked")
Dao castDao = (Dao) DaoManager.createDao(connectionSource, tableConfig);
return new RuntimeExceptionDao(castDao);
}
/**
* @see Dao#queryForId(Object)
*/
@Override
public T queryForId(ID id) {
try {
return dao.queryForId(id);
} catch (SQLException e) {
logMessage(e, "queryForId threw exception on: " + id);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForFirst(PreparedQuery)
*/
@Override
public T queryForFirst(PreparedQuery preparedQuery) {
try {
return dao.queryForFirst(preparedQuery);
} catch (SQLException e) {
logMessage(e, "queryForFirst threw exception on: " + preparedQuery);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForAll()
*/
@Override
public List queryForAll() {
try {
return dao.queryForAll();
} catch (SQLException e) {
logMessage(e, "queryForAll threw exception");
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForEq(String, Object)
*/
@Override
public List queryForEq(String fieldName, Object value) {
try {
return dao.queryForEq(fieldName, value);
} catch (SQLException e) {
logMessage(e, "queryForEq threw exception on: " + fieldName);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForMatching(Object)
*/
@Override
public List queryForMatching(T matchObj) {
try {
return dao.queryForMatching(matchObj);
} catch (SQLException e) {
logMessage(e, "queryForMatching threw exception on: " + matchObj);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForMatchingArgs(Object)
*/
@Override
public List queryForMatchingArgs(T matchObj) {
try {
return dao.queryForMatchingArgs(matchObj);
} catch (SQLException e) {
logMessage(e, "queryForMatchingArgs threw exception on: " + matchObj);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForFieldValues(Map)
*/
@Override
public List queryForFieldValues(Map fieldValues) {
try {
return dao.queryForFieldValues(fieldValues);
} catch (SQLException e) {
logMessage(e, "queryForFieldValues threw exception");
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForFieldValuesArgs(Map)
*/
@Override
public List queryForFieldValuesArgs(Map fieldValues) {
try {
return dao.queryForFieldValuesArgs(fieldValues);
} catch (SQLException e) {
logMessage(e, "queryForFieldValuesArgs threw exception");
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryForSameId(Object)
*/
@Override
public T queryForSameId(T data) {
try {
return dao.queryForSameId(data);
} catch (SQLException e) {
logMessage(e, "queryForSameId threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryBuilder()
*/
@Override
public QueryBuilder queryBuilder() {
return dao.queryBuilder();
}
/**
* @see Dao#updateBuilder()
*/
@Override
public UpdateBuilder updateBuilder() {
return dao.updateBuilder();
}
/**
* @see Dao#deleteBuilder()
*/
@Override
public DeleteBuilder deleteBuilder() {
return dao.deleteBuilder();
}
/**
* @see Dao#query(PreparedQuery)
*/
@Override
public List query(PreparedQuery preparedQuery) {
try {
return dao.query(preparedQuery);
} catch (SQLException e) {
logMessage(e, "query threw exception on: " + preparedQuery);
throw new RuntimeException(e);
}
}
/**
* @see Dao#create(Object)
*/
@Override
public int create(T data) {
try {
return dao.create(data);
} catch (SQLException e) {
logMessage(e, "create threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#create(Collection)
*/
@Override
public int create(Collection datas) {
try {
return dao.create(datas);
} catch (SQLException e) {
logMessage(e, "create threw exception on: " + datas);
throw new RuntimeException(e);
}
}
/**
* @see Dao#createIfNotExists(Object)
*/
@Override
public T createIfNotExists(T data) {
try {
return dao.createIfNotExists(data);
} catch (SQLException e) {
logMessage(e, "createIfNotExists threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#createOrUpdate(Object)
*/
@Override
public CreateOrUpdateStatus createOrUpdate(T data) {
try {
return dao.createOrUpdate(data);
} catch (SQLException e) {
logMessage(e, "createOrUpdate threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#update(Object)
*/
@Override
public int update(T data) {
try {
return dao.update(data);
} catch (SQLException e) {
logMessage(e, "update threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#updateId(Object, Object)
*/
@Override
public int updateId(T data, ID newId) {
try {
return dao.updateId(data, newId);
} catch (SQLException e) {
logMessage(e, "updateId threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#update(PreparedUpdate)
*/
@Override
public int update(PreparedUpdate preparedUpdate) {
try {
return dao.update(preparedUpdate);
} catch (SQLException e) {
logMessage(e, "update threw exception on: " + preparedUpdate);
throw new RuntimeException(e);
}
}
/**
* @see Dao#refresh(Object)
*/
@Override
public int refresh(T data) {
try {
return dao.refresh(data);
} catch (SQLException e) {
logMessage(e, "refresh threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#delete(Object)
*/
@Override
public int delete(T data) {
try {
return dao.delete(data);
} catch (SQLException e) {
logMessage(e, "delete threw exception on: " + data);
throw new RuntimeException(e);
}
}
/**
* @see Dao#deleteById(Object)
*/
@Override
public int deleteById(ID id) {
try {
return dao.deleteById(id);
} catch (SQLException e) {
logMessage(e, "deleteById threw exception on: " + id);
throw new RuntimeException(e);
}
}
/**
* @see Dao#delete(Collection)
*/
@Override
public int delete(Collection datas) {
try {
return dao.delete(datas);
} catch (SQLException e) {
logMessage(e, "delete threw exception on: " + datas);
throw new RuntimeException(e);
}
}
/**
* @see Dao#deleteIds(Collection)
*/
@Override
public int deleteIds(Collection ids) {
try {
return dao.deleteIds(ids);
} catch (SQLException e) {
logMessage(e, "deleteIds threw exception on: " + ids);
throw new RuntimeException(e);
}
}
/**
* @see Dao#delete(PreparedDelete)
*/
@Override
public int delete(PreparedDelete preparedDelete) {
try {
return dao.delete(preparedDelete);
} catch (SQLException e) {
logMessage(e, "delete threw exception on: " + preparedDelete);
throw new RuntimeException(e);
}
}
/**
* @see Dao#iterator()
*/
@Override
public CloseableIterator iterator() {
return dao.iterator();
}
@Override
public CloseableIterator closeableIterator() {
return dao.closeableIterator();
}
/**
* @see Dao#iterator(int)
*/
@Override
public CloseableIterator iterator(int resultFlags) {
return dao.iterator(resultFlags);
}
/**
* @see Dao#getWrappedIterable()
*/
@Override
public CloseableWrappedIterable getWrappedIterable() {
return dao.getWrappedIterable();
}
/**
* @see Dao#getWrappedIterable(PreparedQuery)
*/
@Override
public CloseableWrappedIterable getWrappedIterable(PreparedQuery preparedQuery) {
return dao.getWrappedIterable(preparedQuery);
}
/**
* @see Dao#closeLastIterator()
*/
@Override
public void closeLastIterator() {
try {
dao.closeLastIterator();
} catch (IOException e) {
logMessage(e, "closeLastIterator threw exception");
throw new RuntimeException(e);
}
}
/**
* @see Dao#iterator(PreparedQuery)
*/
@Override
public CloseableIterator iterator(PreparedQuery preparedQuery) {
try {
return dao.iterator(preparedQuery);
} catch (SQLException e) {
logMessage(e, "iterator threw exception on: " + preparedQuery);
throw new RuntimeException(e);
}
}
/**
* @see Dao#iterator(PreparedQuery, int)
*/
@Override
public CloseableIterator iterator(PreparedQuery preparedQuery, int resultFlags) {
try {
return dao.iterator(preparedQuery, resultFlags);
} catch (SQLException e) {
logMessage(e, "iterator threw exception on: " + preparedQuery);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryRaw(String, String...)
*/
@Override
public GenericRawResults queryRaw(String query, String... arguments) {
try {
return dao.queryRaw(query, arguments);
} catch (SQLException e) {
logMessage(e, "queryRaw threw exception on: " + query);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryRawValue(String, String...)
*/
@Override
public long queryRawValue(String query, String... arguments) {
try {
return dao.queryRawValue(query, arguments);
} catch (SQLException e) {
logMessage(e, "queryRawValue threw exception on: " + query);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryRaw(String, RawRowMapper, String...)
*/
@Override
public GenericRawResults queryRaw(String query, RawRowMapper mapper, String... arguments) {
try {
return dao.queryRaw(query, mapper, arguments);
} catch (SQLException e) {
logMessage(e, "queryRaw threw exception on: " + query);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryRaw(String, DataType[], RawRowObjectMapper, String...)
*/
@Override
public GenericRawResults queryRaw(String query, DataType[] columnTypes, RawRowObjectMapper mapper,
String... arguments) {
try {
return dao.queryRaw(query, columnTypes, mapper, arguments);
} catch (SQLException e) {
logMessage(e, "queryRaw threw exception on: " + query);
throw new RuntimeException(e);
}
}
/**
* @see Dao#queryRaw(String, DataType[], String...)
*/
@Override
public GenericRawResults