/** * Executes a batch on <code>java.sql.PreparedStatement</code>. * * @param ps <code>java.sql.PreparedStatement</code> to execute batch on. * * @return an array of update counts containing one element for each command in the batch. * The elements of the array are ordered according to the order in which commands were added to the batch. * * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeBatch()">Statement#executeBatch()</a> */ public int[] executeBatch(PreparedStatement ps){ try { int[] counters = ps.executeBatch(); ps.clearParameters(); return counters; } catch (SQLException e) { throw new DBException(e); } }
/** * * @param query SQL query * @param params - array of parameters, can be null * @param cause real cause. */ public DBException(String query, Object[] params, Throwable cause) { this(getMessage(query, params, cause), cause); }
/** * * @param query SQL query * @param params - array of parameters, can be null * @param cause real cause. */ public DBException(String query, Object[] params, Throwable cause) { StringBuilder sb = new StringBuilder(cause.toString()).append(", query: ").append(query); if (params != null && params.length > 0) { sb.append(", params: "); join(sb, params, ", "); } message = sb.toString(); setStackTrace(cause.getStackTrace()); initCause(cause); }
public DBException(String message, Throwable cause) { super(message, cause); this.setStackTrace(cause.getStackTrace()); }
public static final void dbInit() { try { new DB("ttc").open("org.sqlite.JDBC", "jdbc:sqlite:" + DataSources.DB_FILE(), "root", "p@ssw0rd"); } catch (DBException e) { e.printStackTrace(); dbClose(); dbInit(); } }
public DBException(Throwable cause) { super(cause); this.setStackTrace(cause.getStackTrace()); }
/** * Provides connection from current thread. * * @return connection from current thread. */ public Connection connection() { Connection connection = ConnectionsAccess.getConnection(name); if (connection == null) { throw new DBException("there is no connection '" + name + "' on this thread, are you sure you opened it?"); } return connection; }
/** * * @param query SQL query * @param params - array of parameters, can be null * @param cause real cause. */ public DBException(String query, Object[] params, Throwable cause) { this(getMessage(query, params, cause), cause); }
/** * Creates a <code>java.sql.PreparedStatement</code> to be used in batch executions later. * * @param parametrizedStatement Example of a statement: <code>INSERT INTO employees VALUES (?, ?)</code>. * @return instance of <code>java.sql.PreparedStatement</code> with compiled query. */ public PreparedStatement startBatch(String parametrizedStatement){ try { return connection().prepareStatement(parametrizedStatement); } catch (SQLException e) { throw new DBException(e); } }
/** * Adds a batch statement using given <code>java.sql.PreparedStatement</code> and parameters. * @param ps <code>java.sql.PreparedStatement</code> to add batch to. * @param params parameters for the query in <code>java.sql.PreparedStatement</code>. Parameters will be * set on the statement in the same order as provided here. */ public void addBatch(PreparedStatement ps, Object... params) { try { setParameters(ps, params); ps.addBatch(); } catch (SQLException e) { throw new DBException(e); } }
/** * Commits local transaction. */ public void commitTransaction() { try { Connection c = ConnectionsAccess.getConnection(name); if (c == null) { throw new DBException("Cannot commit transaction, connection '" + name + "' not available"); } c.commit(); LogFilter.log(LOGGER, LogLevel.DEBUG, "Transaction committed"); } catch (SQLException ex) { throw new DBException(ex.getMessage(), ex); } }
/** * Opens local transaction. */ public void openTransaction() { try { Connection c = ConnectionsAccess.getConnection(name); if (c == null) { throw new DBException("Cannot open transaction, connection '" + name + "' not available"); } c.setAutoCommit(false); LogFilter.log(LOGGER, LogLevel.DEBUG, "Transaction opened"); } catch (SQLException ex) { throw new DBException(ex.getMessage(), ex); } }
/** * Rolls back local transaction. */ public void rollbackTransaction() { try { Connection c = ConnectionsAccess.getConnection(name); if (c == null) { throw new DBException("Cannot rollback transaction, connection '" + name + "' not available"); } c.rollback(); LogFilter.log(LOGGER, LogLevel.DEBUG, "Transaction rolled back"); } catch (SQLException ex) { throw new DBException(ex.getMessage(), ex); } }
private void checkExistingConnection(String name) { if (null != ConnectionsAccess.getConnection(name)) { throw new DBException("Cannot open a new connection because existing connection is still on current thread, name: " + name + ", connection instance: " + connection() + ". This might indicate a logical error in your application."); } }
/** * Applies additional criteria to scopes defined in the model. * * @param subquery additional criteria. * @param params dynamic parameters for the subquery. Similar to {@link Model#where(String, Object...)}. * @return a list of records filtered by all supplied scopes as well as additional criteria. */ public <T extends Model> LazyList<T> where(String subquery, Object... params) { StringBuilder query; if(subquery.equals("*")){ query = new StringBuilder(); }else { query = new StringBuilder(subquery); query.append(" AND "); } for (int i = 0; i < scopes.size(); i++) { String scope = scopes.get(i); if(!ModelDelegate.getScopes(modelClass.getName()).containsKey(scope)){ throw new DBException(String.format("Scope '%s' is not defined in model '%s'.", scope, modelClass.getName())); } String scopeQuery = ModelDelegate.getScopes(modelClass.getName()).get(scope); query.append(scopeQuery); if (i < (scopes.size() - 1)) { query.append(" AND "); } } return ModelDelegate.where((Class<T>) modelClass, query.toString(), params); }
/** * This method exists to force immediate load from DB. Example; * <code> Person.find("name = ?", "Smith").load();</code>. * It is not possible to call other methods after load(). The load() method should be the last to be called in the chain: * <code> Person.find("name = ?", "Smith").limit(10).load();</code>. * This: will generate exception: <code> Person.find("name = ?", "Smith").load().limit();</code>. * * @return fully loaded list. */ //TODO: write test, and also test for exception. public <E extends Model> LazyList<E> load() { if (hydrated()) { throw new DBException("load() must be the last on the chain of methods"); } hydrate(); return (LazyList<E>) this; }
case 2: if (!schema.equalsIgnoreCase(names[0])) { throw new DBException("invalid schema name : " + names[0] + ", current scheme: " + schema); throw new DBException("invalid table name : " + table); throw new DBException("invalid table name: " + table);
protected String getTableName(Class<? extends Model> modelClass) { init(MetaModel.getDbName(modelClass)); String tableName = metaModels.getTableName(modelClass); if (tableName == null) { throw new DBException("failed to find metamodel for " + modelClass + ". Are you sure that a corresponding table exists in DB?"); } return tableName; }
public void with(RowListener listener){ try { processRS(listener); } catch(SQLException e) { throw new DBException(e); } finally { //TODO: shouldn't these be closed in the same scope they were created? closeQuietly(rs); closeQuietly(s); } }
throw new DBException("MSSQL offset queries require an order by column.");