Search in sources :

Example 31 with SQLiteOpenHelper

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

the class DefaultStorIOSQLiteTest method shouldPassSQLWithoutArgsToExecSQL.

@Test
public void shouldPassSQLWithoutArgsToExecSQL() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    RawQuery rawQuery = RawQuery.builder().query("DROP TABLE IF EXISTS someTable").build();
    storIOSQLite.lowLevel().executeSQL(rawQuery);
    verify(sqLiteOpenHelper).getWritableDatabase();
    verify(sqLiteDatabase).execSQL(eq(rawQuery.query()));
    verifyNoMoreInteractions(sqLiteOpenHelper, sqLiteDatabase);
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) RawQuery(com.pushtorefresh.storio.sqlite.queries.RawQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 32 with SQLiteOpenHelper

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

the class DefaultStorIOSQLiteTest method observeChangesAndNotifyAboutChangesShouldWorkCorrectly.

@Test
public void observeChangesAndNotifyAboutChangesShouldWorkCorrectly() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChanges().subscribe(testSubscriber);
    testSubscriber.assertNoValues();
    Changes changes = Changes.newInstance("test_table");
    storIOSQLite.lowLevel().notifyAboutChanges(changes);
    testSubscriber.assertValue(changes);
    testSubscriber.assertNoErrors();
    testSubscriber.unsubscribe();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) Changes(com.pushtorefresh.storio.sqlite.Changes) TestSubscriber(rx.observers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 33 with SQLiteOpenHelper

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

the class DefaultStorIOSQLiteTest method defaultSchedulerReturnsNullIfSpecifiedSchedulerNull.

@Test
public void defaultSchedulerReturnsNullIfSpecifiedSchedulerNull() {
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).defaultScheduler(null).build();
    assertThat(storIOSQLite.defaultScheduler()).isNull();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 34 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.

the class HistoryManager method addHistoryItemDetails.

public void addHistoryItemDetails(String itemID, String itemDetails) {
    // As we're going to do an update only we don't need need to worry
    // about the preferences; if the item wasn't saved it won't be udpated
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_DETAIL_COL_PROJECTION, DBHelper.TEXT_COL + "=?", new String[] { itemID }, null, null, DBHelper.TIMESTAMP_COL + " DESC", "1");
        String oldID = null;
        String oldDetails = null;
        if (cursor.moveToNext()) {
            oldID = cursor.getString(0);
            oldDetails = cursor.getString(1);
        }
        if (oldID != null) {
            String newDetails;
            if (oldDetails == null) {
                newDetails = itemDetails;
            } else if (oldDetails.contains(itemDetails)) {
                newDetails = null;
            } else {
                newDetails = oldDetails + " : " + itemDetails;
            }
            if (newDetails != null) {
                ContentValues values = new ContentValues();
                values.put(DBHelper.DETAILS_COL, newDetails);
                db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
            }
        }
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Example 35 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project zxingfragmentlib by mitoyarzun.

the class HistoryManager method buildHistory.

/**
   * <p>Builds a text representation of the scanning history. Each scan is encoded on one
   * line, terminated by a line break (\r\n). The values in each line are comma-separated,
   * and double-quoted. Double-quotes within values are escaped with a sequence of two
   * double-quotes. The fields output are:</p>
   *
   * <ul>
   *  <li>Raw text</li>
   *  <li>Display text</li>
   *  <li>Format (e.g. QR_CODE)</li>
   *  <li>Timestamp</li>
   *  <li>Formatted version of timestamp</li>
   * </ul>
   */
CharSequence buildHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        StringBuilder historyText = new StringBuilder(1000);
        while (cursor.moveToNext()) {
            historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");
            // Add timestamp again, formatted
            long timestamp = cursor.getLong(3);
            historyText.append('"').append(massageHistoryField(format.format(new Date(timestamp)))).append("\",");
            // Above we're preserving the old ordering of columns which had formatted data in position 5
            historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
        }
        return historyText;
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DateFormat(java.text.DateFormat) Cursor(android.database.Cursor) Date(java.util.Date)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)49 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)38 Cursor (android.database.Cursor)21 Test (org.junit.Test)16 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)9 ContentValues (android.content.ContentValues)8 Result (com.google.zxing.Result)6 Context (android.content.Context)5 SQLException (android.database.SQLException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 SharedPreferences (android.content.SharedPreferences)3 DateFormat (java.text.DateFormat)3 ArrayList (java.util.ArrayList)3 SQLiteException (android.database.sqlite.SQLiteException)2 Changes (com.pushtorefresh.storio.sqlite.Changes)2 RawQuery (com.pushtorefresh.storio.sqlite.queries.RawQuery)2 Date (java.util.Date)2 Stream (com.annimon.stream.Stream)1 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)1 InsertQuery (com.pushtorefresh.storio.sqlite.queries.InsertQuery)1