Search in sources :

Example 1 with SupportSQLiteOpenHelper

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;
            }
    }
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) SupportSQLiteOpenHelper(androidx.sqlite.db.SupportSQLiteOpenHelper) SelectionBuilder(com.battlelancer.seriesguide.util.SelectionBuilder) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 2 with SupportSQLiteOpenHelper

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();
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) FrameworkSQLiteOpenHelperFactory(androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory) SqlDriver(app.cash.sqldelight.db.SqlDriver) AndroidSqliteDriver(app.cash.sqldelight.driver.android.AndroidSqliteDriver) SupportSQLiteOpenHelper(androidx.sqlite.db.SupportSQLiteOpenHelper) Test(org.junit.Test)

Aggregations

SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)2 SupportSQLiteOpenHelper (androidx.sqlite.db.SupportSQLiteOpenHelper)2 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 FrameworkSQLiteOpenHelperFactory (androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory)1 SqlDriver (app.cash.sqldelight.db.SqlDriver)1 AndroidSqliteDriver (app.cash.sqldelight.driver.android.AndroidSqliteDriver)1 SelectionBuilder (com.battlelancer.seriesguide.util.SelectionBuilder)1 Test (org.junit.Test)1