Search in sources :

Example 56 with SQLiteDatabase

use of android.database.sqlite.SQLiteDatabase in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method shouldPassArgsToInsertWithOnConflict.

@Test
public void shouldPassArgsToInsertWithOnConflict() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    InsertQuery insertQuery = InsertQuery.builder().table("test_table").nullColumnHack("custom_null_hack").build();
    ContentValues contentValues = mock(ContentValues.class);
    int conflictAlgorithm = SQLiteDatabase.CONFLICT_ROLLBACK;
    storIOSQLite.lowLevel().insertWithOnConflict(insertQuery, contentValues, conflictAlgorithm);
    verify(sqLiteDatabase).insertWithOnConflict(eq("test_table"), eq("custom_null_hack"), same(contentValues), eq(SQLiteDatabase.CONFLICT_ROLLBACK));
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 57 with SQLiteDatabase

use of android.database.sqlite.SQLiteDatabase in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method nestedTransactionShouldWorkNormally.

// See https://github.com/pushtorefresh/storio/issues/478
@Test
public void nestedTransactionShouldWorkNormally() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    // External transaction
    storIOSQLite.lowLevel().beginTransaction();
    try {
        try {
            // Nested transaction
            storIOSQLite.lowLevel().beginTransaction();
            storIOSQLite.lowLevel().notifyAboutChanges(Changes.newInstance("table1"));
            storIOSQLite.lowLevel().notifyAboutChanges(Changes.newInstance("table2"));
            // Finishing nested transaction
            storIOSQLite.lowLevel().setTransactionSuccessful();
        } finally {
            storIOSQLite.lowLevel().endTransaction();
        }
        // Marking external transaction as successful
        storIOSQLite.lowLevel().setTransactionSuccessful();
    } finally {
        // Finishing external transaction
        storIOSQLite.lowLevel().endTransaction();
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 58 with SQLiteDatabase

use of android.database.sqlite.SQLiteDatabase in project weiciyuan by qii.

the class DraftDBManager method getInstance.

public static DraftDBManager getInstance() {
    if (singleton == null) {
        DatabaseHelper databaseHelper = DatabaseHelper.getInstance();
        SQLiteDatabase wsd = databaseHelper.getWritableDatabase();
        SQLiteDatabase rsd = databaseHelper.getReadableDatabase();
        singleton = new DraftDBManager();
        singleton.wsd = wsd;
        singleton.rsd = rsd;
    }
    return singleton;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 59 with SQLiteDatabase

use of android.database.sqlite.SQLiteDatabase in project platform_frameworks_base by android.

the class RecentsProvider method insert.

@Override
public Uri insert(Uri uri, ContentValues values) {
    final SQLiteDatabase db = mHelper.getWritableDatabase();
    final ContentValues key = new ContentValues();
    switch(sMatcher.match(uri)) {
        case URI_RECENT:
            values.put(RecentColumns.TIMESTAMP, System.currentTimeMillis());
            db.insert(TABLE_RECENT, null, values);
            final long cutoff = System.currentTimeMillis() - MAX_HISTORY_IN_MILLIS;
            db.delete(TABLE_RECENT, RecentColumns.TIMESTAMP + "<" + cutoff, null);
            return uri;
        case URI_STATE:
            final String authority = uri.getPathSegments().get(1);
            final String rootId = uri.getPathSegments().get(2);
            final String documentId = uri.getPathSegments().get(3);
            key.put(StateColumns.AUTHORITY, authority);
            key.put(StateColumns.ROOT_ID, rootId);
            key.put(StateColumns.DOCUMENT_ID, documentId);
            // Ensure that row exists, then update with changed values
            db.insertWithOnConflict(TABLE_STATE, null, key, SQLiteDatabase.CONFLICT_IGNORE);
            db.update(TABLE_STATE, values, StateColumns.AUTHORITY + "=? AND " + StateColumns.ROOT_ID + "=? AND " + StateColumns.DOCUMENT_ID + "=?", new String[] { authority, rootId, documentId });
            return uri;
        case URI_RESUME:
            values.put(ResumeColumns.TIMESTAMP, System.currentTimeMillis());
            final String packageName = uri.getPathSegments().get(1);
            key.put(ResumeColumns.PACKAGE_NAME, packageName);
            // Ensure that row exists, then update with changed values
            db.insertWithOnConflict(TABLE_RESUME, null, key, SQLiteDatabase.CONFLICT_IGNORE);
            db.update(TABLE_RESUME, values, ResumeColumns.PACKAGE_NAME + "=?", new String[] { packageName });
            return uri;
        default:
            throw new UnsupportedOperationException("Unsupported Uri " + uri);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DocumentInfo.getCursorString(com.android.documentsui.model.DocumentInfo.getCursorString)

Example 60 with SQLiteDatabase

use of android.database.sqlite.SQLiteDatabase in project platform_frameworks_base by android.

the class Mapper method putDeviceDocument.

/**
     * Puts device information to database.
     *
     * @return If device is added to the database.
     * @throws FileNotFoundException
     */
synchronized boolean putDeviceDocument(MtpDeviceRecord device) throws FileNotFoundException {
    final SQLiteDatabase database = mDatabase.getSQLiteDatabase();
    database.beginTransaction();
    try {
        final ContentValues[] valuesList = new ContentValues[1];
        final ContentValues[] extraValuesList = new ContentValues[1];
        valuesList[0] = new ContentValues();
        extraValuesList[0] = new ContentValues();
        MtpDatabase.getDeviceDocumentValues(valuesList[0], extraValuesList[0], device);
        final boolean changed = putDocuments(null, valuesList, extraValuesList, COLUMN_PARENT_DOCUMENT_ID + " IS NULL", EMPTY_ARGS, strings(COLUMN_DEVICE_ID, COLUMN_MAPPING_KEY));
        database.setTransactionSuccessful();
        return changed;
    } finally {
        database.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1658 Cursor (android.database.Cursor)527 ContentValues (android.content.ContentValues)350 ArrayList (java.util.ArrayList)111 File (java.io.File)65 Test (org.junit.Test)59 SQLiteException (android.database.sqlite.SQLiteException)48 SQLException (android.database.SQLException)44 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)44 Uri (android.net.Uri)44 IOException (java.io.IOException)43 ServiceStatus (com.vodafone360.people.service.ServiceStatus)42 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)38 RemoteException (android.os.RemoteException)36 Pair (android.util.Pair)31 MediumTest (android.test.suitebuilder.annotation.MediumTest)30 Account (android.accounts.Account)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)22 HashMap (java.util.HashMap)21