/** * @return the current SQLite version as a {@link VersionCode} * @throws RuntimeException if the version could not be read */ public VersionCode getSqliteVersion() { VersionCode toReturn = sqliteVersion; if (toReturn == null) { acquireNonExclusiveLock(); try { synchronized (databaseInstanceLock) { getDatabase(); // Opening the database will populate the sqliteVersion field return sqliteVersion; } } finally { releaseNonExclusiveLock(); } } return toReturn; }
private void ensureSqlCompiles(String sql) { acquireNonExclusiveLock(); try { getDatabase().ensureSqlCompiles(sql); } finally { releaseNonExclusiveLock(); } }
/** * Execute a raw SQL statement. May throw a runtime exception if there is an error parsing the SQL or some other * error * * @param sql the statement to execute * @see ISQLiteDatabase#execSQL(String) */ public void execSqlOrThrow(String sql) { acquireNonExclusiveLock(); try { getDatabase().execSQL(sql); } finally { releaseNonExclusiveLock(); } }
/** * Execute a raw SQL statement with optional arguments. The sql string may contain '?' placeholders for the * arguments. May throw a runtime exception if there is an error parsing the SQL or some other error * * @param sql the statement to execute * @param bindArgs the arguments to bind to the statement * @see ISQLiteDatabase#execSQL(String, Object[]) */ public void execSqlOrThrow(String sql, Object[] bindArgs) { acquireNonExclusiveLock(); try { getDatabase().execSQL(sql, bindArgs); } finally { releaseNonExclusiveLock(); } }
/** * Execute a statement that returns a 1x1 long result. If you know your result set will only have one row and * column, this is much more efficient than calling {@link #rawQuery(String, Object[])} and parsing the cursor. * <br> * Note: This will throw an exception if the given SQL query returns a result that is not a single column * * @param sql a sql statement * @param sqlArgs arguments to bind to the sql statement * @return the long result of the query */ public long simpleQueryForLong(String sql, Object[] sqlArgs) { acquireNonExclusiveLock(); try { return getDatabase().simpleQueryForLong(sql, sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * Execute a statement that returns a 1x1 String result. If you know your result set will only have one row and * column, this is much more efficient than calling {@link #rawQuery(String, Object[])} and parsing the cursor. * <br> * Note: This will throw an exception if the given SQL query returns a result that is not a single column * * @param sql a sql statement * @param sqlArgs arguments to bind to the sql statement * @return the String result of the query */ public String simpleQueryForString(String sql, Object[] sqlArgs) { acquireNonExclusiveLock(); try { return getDatabase().simpleQueryForString(sql, sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * End the current transaction * * @see ISQLiteDatabase#endTransaction() */ public void endTransaction() { TransactionSuccessState successState = transactionSuccessState.get(); try { getDatabase().endTransaction(); } catch (RuntimeException e) { successState.unsetTransactionSuccessful(); throw e; } finally { releaseNonExclusiveLock(); successState.endTransaction(); if (!successState.inTransaction()) { flushAccumulatedNotifications(successState.outerTransactionSuccess); successState.reset(); } } }
/** * Execute a raw SQLite query. This method takes an Object[] for the arguments because Android's default behavior * of binding all arguments as strings can have unexpected bugs, particularly when working with functions. For * example: * * <pre> * select * from t where _id = '1'; // Returns the first row * select * from t where abs(_id) = '1'; // Always returns empty set * </pre> * * To eliminate this class of bugs, we bind all arguments as their native types, not as strings. Any object in the * array that is not a basic type (Number, String, Boolean, etc.) will be converted to a sanitized string before * binding. * * @param sql a sql statement * @param sqlArgs arguments to bind to the sql statement * @return a {@link ICursor} containing results of the query */ public ICursor rawQuery(String sql, Object[] sqlArgs) { acquireNonExclusiveLock(); try { return getDatabase().rawQuery(sql, sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * Execute a SQL {@link com.yahoo.squidb.sql.Insert} statement * * @return the row id of the last row inserted on success, -1 on failure */ private long insertInternal(Insert insert) { CompiledStatement compiled = insert.compile(getCompileContext()); acquireNonExclusiveLock(); try { return getDatabase().executeInsert(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
return getDatabase().prepareStatement(sql); } finally { releaseNonExclusiveLock();
} catch (RuntimeException e) { releaseNonExclusiveLock(); throw e;
} catch (RuntimeException e) { releaseNonExclusiveLock(); throw e;
/** * Execute a SQL {@link com.yahoo.squidb.sql.Delete} statement * * @return the number of rows deleted on success, -1 on failure */ private int deleteInternal(Delete delete) { CompiledStatement compiled = delete.compile(getCompileContext()); acquireNonExclusiveLock(); try { return getDatabase().executeUpdateDelete(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * Execute a raw SQL statement * * @param sql the statement to execute * @return true if the statement executed without an error * @see ISQLiteDatabase#execSQL(String) */ public boolean tryExecSql(String sql) { acquireNonExclusiveLock(); try { getDatabase().execSQL(sql); return true; } catch (RuntimeException e) { onError("Failed to execute statement: " + sql, e); return false; } finally { releaseNonExclusiveLock(); } }
/** * Execute a raw SQL statement with optional arguments. The sql string may contain '?' placeholders for the * arguments. * * @param sql the statement to execute * @param bindArgs the arguments to bind to the statement * @return true if the statement executed without an error * @see ISQLiteDatabase#execSQL(String, Object[]) */ public boolean tryExecSql(String sql, Object[] bindArgs) { acquireNonExclusiveLock(); try { getDatabase().execSQL(sql, bindArgs); return true; } catch (RuntimeException e) { onError("Failed to execute statement: " + sql, e); return false; } finally { releaseNonExclusiveLock(); } }
} catch (RuntimeException e) { releaseNonExclusiveLock(); throw e;
/** * Execute a SQL {@link com.yahoo.squidb.sql.Update} statement * * @return the number of rows updated on success, -1 on failure */ private int updateInternal(Update update) { CompiledStatement compiled = update.compile(getCompileContext()); acquireNonExclusiveLock(); try { return getDatabase().executeUpdateDelete(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
} catch (RuntimeException e) { releaseNonExclusiveLock(); throw e;
newRow = preparedStatement.executeInsert(); } finally { releaseNonExclusiveLock();
/** * Count the number of rows matching a given {@link Criterion}. Use null to count all rows. * * @param modelClass the model class corresponding to the table * @param criterion the criterion to match * @return the number of rows matching the given criterion */ public int count(Class<? extends AbstractModel> modelClass, Criterion criterion) { Property.IntegerProperty countProperty = Property.IntegerProperty.countProperty(); Query query = Query.select(countProperty); if (criterion != null) { query.where(criterion); } query = inferTableForQuery(modelClass, query); CompiledStatement compiled = query.compile(getCompileContext()); acquireNonExclusiveLock(); try { return (int) getDatabase().simpleQueryForLong(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }