androidx.sqlite.db.SimpleSQLiteQuery Java Examples

The following examples show how to use androidx.sqlite.db.SimpleSQLiteQuery. 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: InvalidationTracker.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private Set<Integer> checkUpdatedTable() {
    HashSet<Integer> invalidatedTableIds = new HashSet<>();
    Cursor cursor = mDatabase.query(new SimpleSQLiteQuery(SELECT_UPDATED_TABLES_SQL));
    //noinspection TryFinallyCanBeTryWithResources
    try {
        while (cursor.moveToNext()) {
            final int tableId = cursor.getInt(0);
            invalidatedTableIds.add(tableId);
        }
    } finally {
        cursor.close();
    }
    if (!invalidatedTableIds.isEmpty()) {
        mCleanupStatement.executeUpdateDelete();
    }
    return invalidatedTableIds;
}
 
Example #2
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 #3
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 #4
Source File: RoomOpenHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private void checkIdentity(SupportSQLiteDatabase db) {
    if (hasRoomMasterTable(db)) {
        String identityHash = null;
        Cursor cursor = db.query(new SimpleSQLiteQuery(RoomMasterTable.READ_QUERY));
        //noinspection TryFinallyCanBeTryWithResources
        try {
            if (cursor.moveToFirst()) {
                identityHash = cursor.getString(0);
            }
        } finally {
            cursor.close();
        }
        if (!mIdentityHash.equals(identityHash) && !mLegacyHash.equals(identityHash)) {
            throw new IllegalStateException("Room cannot verify the data integrity. Looks like"
                    + " you've changed schema but forgot to update the version number. You can"
                    + " simply fix this by increasing the version number.");
        }
    } else {
        // No room_master_table, this might an a pre-populated DB, we must validate to see if
        // its suitable for usage.
        ValidationResult result = mDelegate.onValidateSchema(db);
        if (!result.isValid) {
            throw new IllegalStateException("Pre-packaged database has an invalid schema: "
                    + result.expectedFoundMsg);
        }
        mDelegate.onPostMigrate(db);
        updateIdentity(db);
    }
}
 
Example #5
Source File: Database.java    From cwac-saferoom with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor query(String sql) {
  return(query(new SimpleSQLiteQuery(sql)));
}
 
Example #6
Source File: Database.java    From cwac-saferoom with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor query(String sql, Object[] selectionArgs) {
  return(query(new SimpleSQLiteQuery(sql, selectionArgs)));
}
 
Example #7
Source File: Database.java    From cwac-saferoom with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("ThrowFromFinallyBlock")
@Override
public int update(String table, int conflictAlgorithm, ContentValues values,
                  String whereClause, Object[] whereArgs) {
  // taken from SQLiteDatabase class.
  if (values == null || values.size() == 0) {
    throw new IllegalArgumentException("Empty values");
  }
  StringBuilder sql = new StringBuilder(120);
  sql.append("UPDATE ");
  sql.append(CONFLICT_VALUES[conflictAlgorithm]);
  sql.append(table);
  sql.append(" SET ");

  // move all bind args to one array
  int setValuesSize = values.size();
  int bindArgsSize = (whereArgs == null) ? setValuesSize : (setValuesSize + whereArgs.length);
  Object[] bindArgs = new Object[bindArgsSize];
  int i = 0;
  for (String colName : values.keySet()) {
    sql.append((i > 0) ? "," : "");
    sql.append(colName);
    bindArgs[i++] = values.get(colName);
    sql.append("=?");
  }
  if (whereArgs != null) {
    for (i = setValuesSize; i < bindArgsSize; i++) {
      bindArgs[i] = whereArgs[i - setValuesSize];
    }
  }
  if (!TextUtils.isEmpty(whereClause)) {
    sql.append(" WHERE ");
    sql.append(whereClause);
  }
  SupportSQLiteStatement statement = compileStatement(sql.toString());

  try {
    SimpleSQLiteQuery.bind(statement, bindArgs);
    return statement.executeUpdateDelete();
  }
  finally {
    try {
      statement.close();
    }
    catch (Exception e) {
      throw new RuntimeException("Exception attempting to close statement", e);
    }
  }
}
 
Example #8
Source File: KriptonSQLiteDatabaseWrapperImpl.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor query(String sql) {
	return (query(new SimpleSQLiteQuery(sql)));
}
 
Example #9
Source File: KriptonSQLiteDatabaseWrapperImpl.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor query(String sql, Object[] selectionArgs) {
	return (query(new SimpleSQLiteQuery(sql, selectionArgs)));
}
 
Example #10
Source File: Database.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor query(String sql) {
	return (query(new SimpleSQLiteQuery(sql)));
}
 
Example #11
Source File: Database.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor query(String sql, Object[] selectionArgs) {
	return (query(new SimpleSQLiteQuery(sql, selectionArgs)));
}
 
Example #12
Source File: Database.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int update(String table, int conflictAlgorithm, ContentValues values, String whereClause,
		Object[] whereArgs) {
	// taken from SQLiteDatabase class.
	if (values == null || values.size() == 0) {
		throw new IllegalArgumentException("Empty values");
	}
	StringBuilder sql = new StringBuilder(120);
	sql.append("UPDATE ");
	sql.append(CONFLICT_VALUES[conflictAlgorithm]);
	sql.append(table);
	sql.append(" SET ");

	// move all bind args to one array
	int setValuesSize = values.size();
	int bindArgsSize = (whereArgs == null) ? setValuesSize : (setValuesSize + whereArgs.length);
	Object[] bindArgs = new Object[bindArgsSize];
	int i = 0;
	for (String colName : values.keySet()) {
		sql.append((i > 0) ? "," : "");
		sql.append(colName);
		bindArgs[i++] = values.get(colName);
		sql.append("=?");
	}
	if (whereArgs != null) {
		for (i = setValuesSize; i < bindArgsSize; i++) {
			bindArgs[i] = whereArgs[i - setValuesSize];
		}
	}
	if (!TextUtils.isEmpty(whereClause)) {
		sql.append(" WHERE ");
		sql.append(whereClause);
	}
	SupportSQLiteStatement statement = compileStatement(sql.toString());

	try {
		SimpleSQLiteQuery.bind(statement, bindArgs);
		return statement.executeUpdateDelete();
	} finally {
		try {
			statement.close();
		} catch (Exception e) {
			throw new RuntimeException("Exception attempting to close statement", e);
		}
	}
}