use of androidx.sqlite.db.SupportSQLiteOpenHelper in project SeriesGuide by UweTrottmann.
the class SeriesGuideProvider method query.
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if (LOGV) {
Timber.v("query(uri=%s, proj=%s)", uri, Arrays.toString(projection));
}
SupportSQLiteOpenHelper databaseHelper = SgRoomDatabase.getInstance(getContext()).getOpenHelper();
final int match = sUriMatcher.match(uri);
// support close op for legacy database import tool, will reopen on next op
if (match == CLOSE) {
databaseHelper.close();
return null;
}
// always get writable database, might have to be upgraded
final SupportSQLiteDatabase db = databaseHelper.getWritableDatabase();
switch(match) {
default:
{
// Most cases are handled with simple SelectionBuilder
final SelectionBuilder builder = buildSelection(uri, match);
Cursor query = null;
try {
query = builder.map(BaseColumns._COUNT, // support count base column
"count(*)").where(selection, selectionArgs).query(db, projection, sortOrder);
} catch (SQLiteException e) {
Timber.e(e, "Failed to query with uri=%s", uri);
}
if (query != null) {
// noinspection ConstantConditions
query.setNotificationUri(getContext().getContentResolver(), uri);
}
return query;
}
}
}
use of androidx.sqlite.db.SupportSQLiteOpenHelper in project sqldelight by square.
the class MigrationTest method testMigrationWorks.
@Test
public void testMigrationWorks() throws IOException {
// Set up version 1 db.
SupportSQLiteOpenHelper.Callback callback = new SupportSQLiteOpenHelper.Callback(1) {
@Override
public void onCreate(SupportSQLiteDatabase db) {
db.execSQL("CREATE TABLE person (\n" + " _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" + " first_name TEXT NOT NULL\n" + ");");
db.execSQL("CREATE TABLE `group` (\n" + " _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" + " 'where' INTEGER NOT NULL,\n" + " [having] INTEGER NOT NULL\n" + ");");
db.execSQL("INSERT INTO person VALUES (1, 'alec');");
}
@Override
public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
}
};
SupportSQLiteOpenHelper.Configuration configuration = SupportSQLiteOpenHelper.Configuration.builder(InstrumentationRegistry.getTargetContext()).callback(callback).name("test.db").build();
SupportSQLiteOpenHelper helper = new FrameworkSQLiteOpenHelperFactory().create(configuration);
// Creates the db.
helper.getWritableDatabase().execSQL("DELETE FROM person WHERE first_name NOT IN ('alec')");
helper.close();
// Migrate the db with a queryWrapper
SqlDriver database = new AndroidSqliteDriver(QueryWrapper.Companion.getSchema(), InstrumentationRegistry.getTargetContext(), "test.db");
QueryWrapper queryWrapper = QueryWrapper.Companion.invoke(database);
// Assert info is correct
Person person = queryWrapper.getPersonQueries().nameIn(Arrays.asList("alec")).executeAsOne();
assertThat(person).isEqualTo(new Person(1, "alec", "sup"));
database.close();
}
Aggregations