androidx.sqlite.db.SupportSQLiteStatement Java Examples

The following examples show how to use androidx.sqlite.db.SupportSQLiteStatement. 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
Source File: VariableArgsOperationHelper.java    From sqlitemagic with 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 #2
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #3
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #4
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #5
Source File: Database.java    From kripton with 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 #6
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #7
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #8
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #9
Source File: Database.java    From cwac-saferoom with 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 #10
Source File: CompiledUpdate.java    From sqlitemagic with 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 #11
Source File: VariableArgsOperationHelper.java    From sqlitemagic with 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 #12
Source File: EntityDbManager.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement compileStatement(@NonNull String sql) {
  final DbConnectionImpl dbConnection = this.dbConnection;
  if (dbConnection == null) {
    throw new IllegalStateException("DB connection closed");
  }
  return dbConnection.compileStatement(sql);
}
 
Example #13
Source File: EntityDbManager.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement getUpdateStatement(@NonNull String updateSql) {
  SupportSQLiteStatement stm = updateStatement.get();
  if (stm == null) {
    if (dbConnection == null) {
      throw new IllegalStateException("DB connection closed");
    }
    stm = dbConnection.compileStatement(String.format(updateSql, ""));
    updateStatement.set(stm);
    return stm;
  }
  return stm;
}
 
Example #14
Source File: EntityDbManager.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement compileStatement(@NonNull String sql, @ConflictAlgorithm int conflictAlgorithm) {
  final DbConnectionImpl dbConnection = this.dbConnection;
  if (dbConnection == null) {
    throw new IllegalStateException("DB connection closed");
  }
  return dbConnection.compileStatement(String.format(sql, ConflictAlgorithm.CONFLICT_VALUES[conflictAlgorithm]));
}
 
Example #15
Source File: Utils.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public Short parseFromStatement(@NonNull SupportSQLiteStatement statement) {
  final String rawVal = statement.simpleQueryForString();
  if (rawVal != null) {
    return Short.valueOf(rawVal);
  }
  return null;
}
 
Example #16
Source File: Utils.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public Double parseFromStatement(@NonNull SupportSQLiteStatement statement) {
  final String rawVal = statement.simpleQueryForString();
  if (rawVal != null) {
    return Double.parseDouble(rawVal);
  }
  return null;
}
 
Example #17
Source File: Utils.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public Float parseFromStatement(@NonNull SupportSQLiteStatement statement) {
  final String rawVal = statement.simpleQueryForString();
  if (rawVal != null) {
    return Float.parseFloat(rawVal);
  }
  return null;
}
 
Example #18
Source File: Utils.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public Byte parseFromStatement(@NonNull SupportSQLiteStatement statement) {
  final String rawVal = statement.simpleQueryForString();
  if (rawVal != null) {
    return Byte.valueOf(rawVal);
  }
  return null;
}
 
Example #19
Source File: SqlUtil.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
static void bindAllArgsAsStrings(@NonNull SupportSQLiteStatement statement, @Nullable String[] args) {
  if (args != null) {
    for (int i = args.length; i != 0; i--) {
      statement.bindString(i, args[i - 1]);
    }
  }
}
 
Example #20
Source File: Utils.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Override
public Integer parseFromStatement(@NonNull SupportSQLiteStatement statement) {
  final String rawVal = statement.simpleQueryForString();
  if (rawVal != null) {
    return Integer.valueOf(rawVal);
  }
  return null;
}
 
Example #21
Source File: EntityDbManager.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
@CheckResult
SupportSQLiteStatement getInsertStatement(@NonNull String insertSql) {
  SupportSQLiteStatement stm = insertStatement.get();
  if (stm == null) {
    if (dbConnection == null) {
      throw new IllegalStateException("DB connection closed");
    }
    stm = dbConnection.compileStatement(String.format(insertSql, ""));
    insertStatement.set(stm);
    return stm;
  }
  return stm;
}
 
Example #22
Source File: Column.java    From sqlitemagic with 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);
    }
  };
}
 
Example #23
Source File: Column.java    From sqlitemagic with 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 #24
Source File: OperationHelper.java    From sqlitemagic with 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 #25
Source File: KriptonDatabaseHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Update delete.
 *
 * @param context the context
 * @param sql the sql
 * @param contentValues the content values
 * @return the int
 */
public static int updateDelete(SQLContext context, String sql, KriptonContentValues contentValues) {
	SupportSQLiteStatement ps = context.getDatabase().compileStatement(sql);
	try {
		contentValues.bind(ps);
		return ps.executeUpdateDelete();
	} finally {
		try {
			ps.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
Example #26
Source File: OperationHelper.java    From sqlitemagic with 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 #27
Source File: OperationHelper.java    From sqlitemagic with 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 #28
Source File: OperationHelper.java    From sqlitemagic with 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 #29
Source File: KriptonContentValues.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Clear.
 *
 * @param compiledStatement
 *            the compiled statement
 */
public void clear(SupportSQLiteStatement compiledStatement) {
	clear();
	this.compiledStatement = compiledStatement;
	if (compiledStatement != null) {
		this.compiledStatement.clearBindings();
	}

}
 
Example #30
Source File: EntityInsertionAdapter.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}