Java Code Examples for androidx.sqlite.db.SupportSQLiteStatement

The following examples show how to use androidx.sqlite.db.SupportSQLiteStatement. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Inserts the given entities into the database and returns the row ids.
 *
 * @param entities Entities to insert
 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1
 */
public final long[] insertAndReturnIdsArray(Collection<? extends T> entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        final long[] result = new long[entities.size()];
        int index = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            result[index] = stmt.executeInsert();
            index++;
        }
        return result;
    } finally {
        release(stmt);
    }
}
 
Example 2
/**
 * Inserts the given entities into the database and returns the row ids.
 *
 * @param entities Entities to insert
 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1
 */
public final long[] insertAndReturnIdsArray(T[] entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        final long[] result = new long[entities.length];
        int index = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            result[index] = stmt.executeInsert();
            index++;
        }
        return result;
    } finally {
        release(stmt);
    }
}
 
Example 3
/**
 * Inserts the given entities into the database and returns the row ids.
 *
 * @param entities Entities to insert
 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1
 */
public final Long[] insertAndReturnIdsArrayBox(Collection<? extends T> entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        final Long[] result = new Long[entities.size()];
        int index = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            result[index] = stmt.executeInsert();
            index++;
        }
        return result;
    } finally {
        release(stmt);
    }
}
 
Example 4
/**
 * Inserts the given entities into the database and returns the row ids.
 *
 * @param entities Entities to insert
 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1
 */
public final Long[] insertAndReturnIdsArrayBox(T[] entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        final Long[] result = new Long[entities.length];
        int index = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            result[index] = stmt.executeInsert();
            index++;
        }
        return result;
    } finally {
        release(stmt);
    }
}
 
Example 5
/**
 * Inserts the given entities into the database and returns the row ids.
 *
 * @param entities Entities to insert
 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1
 */
public final List<Long> insertAndReturnIdsList(T[] entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        final List<Long> result = new ArrayList<>(entities.length);
        int index = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            result.add(index, stmt.executeInsert());
            index++;
        }
        return result;
    } finally {
        release(stmt);
    }
}
 
Example 6
/**
 * Inserts the given entities into the database and returns the row ids.
 *
 * @param entities Entities to insert
 * @return The SQLite row ids, for entities that are not inserted the row id returned will be -1
 */
public final List<Long> insertAndReturnIdsList(Collection<? extends T> entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        final List<Long> result = new ArrayList<>(entities.size());
        int index = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            result.add(index, stmt.executeInsert());
            index++;
        }
        return result;
    } finally {
        release(stmt);
    }
}
 
Example 7
Source Project: cwac-saferoom   Source File: Database.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("ThrowFromFinallyBlock")
@Override
public int delete(String table, String whereClause, Object[] whereArgs) {
  String query = "DELETE FROM " + table
    + (TextUtils.isEmpty(whereClause) ? "" : " WHERE " + whereClause);
  SupportSQLiteStatement statement = compileStatement(query);

  try {
    SimpleSQLiteQuery.bind(statement, whereArgs);
    return statement.executeUpdateDelete();
  }
  finally {
    try {
      statement.close();
    }
    catch (Exception e) {
      throw new RuntimeException("Exception attempting to close statement", e);
    }
  }
}
 
Example 8
Source Project: sqlitemagic   Source File: CompiledUpdate.java    License: Apache License 2.0 6 votes vote down vote up
@NonNull
@CheckResult
CompiledUpdate build() {
  final String sql = SqlCreator.getSql(sqlTreeRoot, sqlNodeCount);
  final SupportSQLiteStatement stm = dbConnection.compileStatement(sql);
  final ArrayList<String> args = this.args;
  for (int i = args.size(); i != 0; i--) {
    final String arg = args.get(i - 1);
    if (arg != null) {
      stm.bindString(i, arg);
    } else {
      stm.bindNull(i);
    }
  }
  return new CompiledUpdate(stm, tableNode.tableName, dbConnection);
}
 
Example 9
Source Project: sqlitemagic   Source File: VariableArgsOperationHelper.java    License: Apache License 2.0 6 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement compileStatement(@OperationHelper.Op int operation,
                                        @NonNull String tableName,
                                        int maxColumns,
                                        @NonNull SimpleArrayMap<String, Object> values,
                                        @NonNull String resolutionColumn,
                                        @NonNull EntityDbManager manager) {
  StringBuilder sqlBuilder = this.sqlBuilder;
  if (sqlBuilder == null) {
    sqlBuilder = new StringBuilder(7 + conflictAlgorithm.length() + maxColumns * 22);
    this.sqlBuilder = sqlBuilder;
  }
  final int lastCompiledOperation = this.lastCompiledOperation;
  this.lastCompiledOperation = operation;

  return compileStatement(
      operation == lastCompiledOperation,
      operation,
      sqlBuilder,
      conflictAlgorithm,
      tableName,
      values,
      resolutionColumn,
      manager);
}
 
Example 10
Source Project: sqlitemagic   Source File: VariableArgsOperationHelper.java    License: Apache License 2.0 6 votes vote down vote up
private static void bindValue(@NonNull SupportSQLiteStatement statement, int pos, Object value) {
  if (value instanceof String) {
    statement.bindString(pos, (String) value);
  } else if (value instanceof Number) {
    if (value instanceof Float || value instanceof Double) {
      statement.bindDouble(pos, ((Number) value).doubleValue());
    } else {
      statement.bindLong(pos, ((Number) value).longValue());
    }
  } else if (value instanceof byte[]) {
    statement.bindBlob(pos, (byte[]) value);
  } else if (value instanceof Byte[]) {
    statement.bindBlob(pos, Utils.toByteArray((Byte[]) value));
  } else {
    statement.bindString(pos, value.toString());
  }
}
 
Example 11
Source Project: kripton   Source File: Database.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int delete(String table, String whereClause, Object[] whereArgs) {
	String query = "DELETE FROM " + table + (TextUtils.isEmpty(whereClause) ? "" : " WHERE " + whereClause);
	SupportSQLiteStatement statement = compileStatement(query);

	try {
		SimpleSQLiteQuery.bind(statement, whereArgs);
		return statement.executeUpdateDelete();
	} finally {
		try {
			statement.close();
		} catch (Exception e) {
			throw new RuntimeException("Exception attempting to close statement", e);
		}
	}
}
 
Example 12
/**
 * Inserts the entity into the database.
 *
 * @param entity The entity to insert
 */
public final void insert(T entity) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        bind(stmt, entity);
        stmt.executeInsert();
    } finally {
        release(stmt);
    }
}
 
Example 13
/**
 * Inserts the given entities into the database.
 *
 * @param entities Entities to insert
 */
public final void insert(T[] entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        for (T entity : entities) {
            bind(stmt, entity);
            stmt.executeInsert();
        }
    } finally {
        release(stmt);
    }
}
 
Example 14
/**
 * Inserts the given entities into the database.
 *
 * @param entities Entities to insert
 */
public final void insert(Iterable<? extends T> entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        for (T entity : entities) {
            bind(stmt, entity);
            stmt.executeInsert();
        }
    } finally {
        release(stmt);
    }
}
 
Example 15
/**
 * Inserts the given entity into the database and returns the row id.
 *
 * @param entity The entity to insert
 * @return The SQLite row id or -1 if no row is inserted
 */
public final long insertAndReturnId(T entity) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        bind(stmt, entity);
        return stmt.executeInsert();
    } finally {
        release(stmt);
    }
}
 
Example 16
private SupportSQLiteStatement getStmt(boolean canUseCached) {
    final SupportSQLiteStatement stmt;
    if (canUseCached) {
        if (mStmt == null) {
            mStmt = createNewStatement();
        }
        stmt = mStmt;
    } else {
        // it is in use, create a one off statement
        stmt = createNewStatement();
    }
    return stmt;
}
 
Example 17
/**
 * Deletes or updates the given entities in the database and returns the affected row count.
 *
 * @param entity The entity to delete or update
 * @return The number of affected rows
 */
public final int handle(T entity) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        bind(stmt, entity);
        return stmt.executeUpdateDelete();
    } finally {
        release(stmt);
    }
}
 
Example 18
/**
 * Deletes or updates the given entities in the database and returns the affected row count.
 *
 * @param entities Entities to delete or update
 * @return The number of affected rows
 */
public final int handleMultiple(Iterable<? extends T> entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        int total = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            total += stmt.executeUpdateDelete();
        }
        return total;
    } finally {
        release(stmt);
    }
}
 
Example 19
/**
 * Deletes or updates the given entities in the database and returns the affected row count.
 *
 * @param entities Entities to delete or update
 * @return The number of affected rows
 */
public final int handleMultiple(T[] entities) {
    final SupportSQLiteStatement stmt = acquire();
    try {
        int total = 0;
        for (T entity : entities) {
            bind(stmt, entity);
            total += stmt.executeUpdateDelete();
        }
        return total;
    } finally {
        release(stmt);
    }
}
 
Example 20
Source Project: sqlitemagic   Source File: CompiledDelete.java    License: Apache License 2.0 5 votes vote down vote up
CompiledDelete(@NonNull SupportSQLiteStatement deleteStm,
               @NonNull String tableName,
               @NonNull DbConnectionImpl dbConnection) {
  this.deleteStm = deleteStm;
  this.tableName = tableName;
  this.dbConnection = dbConnection;
}
 
Example 21
Source Project: sqlitemagic   Source File: CompiledDelete.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
CompiledDelete build() {
  final String sql = SqlCreator.getSql(sqlTreeRoot, sqlNodeCount);
  final SupportSQLiteStatement stm = dbConnection.compileStatement(sql);
  bindAllArgsAsStrings(stm, args.toArray(new String[args.size()]));
  return new CompiledDelete(stm, from.tableName, dbConnection);
}
 
Example 22
Source Project: sqlitemagic   Source File: CompiledUpdate.java    License: Apache License 2.0 5 votes vote down vote up
CompiledUpdate(@NonNull SupportSQLiteStatement updateStm,
               @NonNull String tableName,
               @NonNull DbConnectionImpl dbConnection) {
  this.updateStm = updateStm;
  this.tableName = tableName;
  this.dbConnection = dbConnection;
}
 
Example 23
Source Project: sqlitemagic   Source File: CompiledSelect1Impl.java    License: Apache License 2.0 5 votes vote down vote up
CompiledFirstSelect1Impl(@NonNull CompiledSelect1Impl<T, S> compiledSelect,
                         @NonNull DbConnectionImpl dbConnection) {
  super(dbConnection, null);
  final String sql = addTakeFirstLimitClauseIfNeeded(compiledSelect.sql);
  final String[] args = compiledSelect.args;
  final SupportSQLiteStatement selectStm = dbConnection.compileStatement(sql);
  bindAllArgsAsStrings(selectStm, args);
  this.selectStm = selectStm;
  this.sql = sql;
  this.args = args;
  this.selectedColumn = compiledSelect.selectedColumn;
  this.observedTables = compiledSelect.observedTables;
}
 
Example 24
Source Project: sqlitemagic   Source File: CompiledSelectImpl.java    License: Apache License 2.0 5 votes vote down vote up
CompiledCountSelectImpl(@NonNull String parentSql,
                        @Nullable String[] args,
                        @NonNull DbConnectionImpl dbConnection,
                        @NonNull String[] observedTables) {
  super(dbConnection, null);
  final String sql = addCountFunction(parentSql);
  final SupportSQLiteStatement countStm = dbConnection.compileStatement(sql);
  bindAllArgsAsStrings(countStm, args);
  this.countStm = countStm;
  this.sql = sql;
  this.observedTables = observedTables;
  this.args = args;
}
 
Example 25
Source Project: sqlitemagic   Source File: OperationHelper.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement getInsertStatement(@NonNull String tableName,
                                          @NonNull String sql,
                                          @NonNull EntityDbManager manager) {
  if (customSqlNeededForConflictAlgorithm) {
    SupportSQLiteStatement insert = inserts.get(tableName);
    if (insert == null) {
      insert = manager.compileStatement(sql, conflictAlgorithm);
      inserts.put(tableName, insert);
    }
    return insert;
  }
  return manager.getInsertStatement(sql);
}
 
Example 26
Source Project: sqlitemagic   Source File: OperationHelper.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement getUpdateStatement(@NonNull String tableName,
                                          @NonNull String sql,
                                          @NonNull EntityDbManager manager) {
  if (customSqlNeededForConflictAlgorithm || customSqlNeededForUpdates) {
    SupportSQLiteStatement update = updates.get(tableName);
    if (update == null) {
      update = manager.compileStatement(opByColumnSql(sql, tableName, operationByColumns), conflictAlgorithm);
      updates.put(tableName, update);
    }
    return update;
  }
  return manager.getUpdateStatement(sql);
}
 
Example 27
Source Project: sqlitemagic   Source File: OperationHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
  final SimpleArrayMap<String, SupportSQLiteStatement> inserts = this.inserts;
  if (inserts != null) {
    this.inserts = null;
    closeStatements(inserts);
  }
  final SimpleArrayMap<String, SupportSQLiteStatement> updates = this.updates;
  if (updates != null) {
    this.updates = null;
    closeStatements(updates);
  }
}
 
Example 28
Source Project: sqlitemagic   Source File: OperationHelper.java    License: Apache License 2.0 5 votes vote down vote up
private static void closeStatements(@NonNull SimpleArrayMap<String, SupportSQLiteStatement> statements) {
  try {
    final int size = statements.size();
    for (int i = 0; i < size; i++) {
      statements.valueAt(i).close();
    }
  } catch (Exception ignore) {}
}
 
Example 29
Source Project: sqlitemagic   Source File: Column.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@androidx.annotation.Nullable
<V> V getFromStatement(@NonNull SupportSQLiteStatement stm) {
  try {
    return (V) valueParser.parseFromStatement(stm);
  } catch (SQLiteDoneException e) {
    // query returned no results
  }
  return null;
}
 
Example 30
Source Project: sqlitemagic   Source File: Column.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
static <T, R, ET, P, N> Column<T, R, ET, P, N> internalCopy(@NonNull Table<P> newTable,
                                                            @NonNull final Column<T, R, ET, ?, N> column) {
  return new Column<T, R, ET, P, N>(newTable, column.name, column.allFromTable, column.valueParser, column.nullable, column.alias) {
    @NonNull
    @Override
    public String toSqlArg(@NonNull T val) {
      return column.toSqlArg(val);
    }

    @androidx.annotation.Nullable
    @Override
    <V> V getFromCursor(@NonNull Cursor cursor) {
      return column.getFromCursor(cursor);
    }

    @androidx.annotation.Nullable
    @Override
    <V> V getFromStatement(@NonNull SupportSQLiteStatement stm) {
      return column.getFromStatement(stm);
    }

    @Override
    void appendSql(@NonNull StringBuilder sb, @NonNull SimpleArrayMap<String, LinkedList<String>> systemRenamedTables) {
      super.appendSql(sb);
    }
  };
}