Search in sources :

Example 6 with SQLiteDoneException

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

the class DatabaseStatementTest method testSimpleQuery.

@MediumTest
public void testSimpleQuery() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);");
    mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');");
    SQLiteStatement statement1 = mDatabase.compileStatement("SELECT num FROM test WHERE str = ?");
    SQLiteStatement statement2 = mDatabase.compileStatement("SELECT str FROM test WHERE num = ?");
    try {
        statement1.bindString(1, "hello");
        long value = statement1.simpleQueryForLong();
        assertEquals(1234, value);
        statement1.bindString(1, "world");
        statement1.simpleQueryForLong();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    try {
        statement2.bindLong(1, 1234);
        String value = statement1.simpleQueryForString();
        assertEquals("hello", value);
        statement2.bindLong(1, 5678);
        statement1.simpleQueryForString();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    statement1.close();
    statement2.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteDoneException(android.database.sqlite.SQLiteDoneException) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 7 with SQLiteDoneException

use of android.database.sqlite.SQLiteDoneException in project android_frameworks_base by AOSPA.

the class DatabaseStatementTest method testSimpleQuery.

@MediumTest
public void testSimpleQuery() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);");
    mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');");
    SQLiteStatement statement1 = mDatabase.compileStatement("SELECT num FROM test WHERE str = ?");
    SQLiteStatement statement2 = mDatabase.compileStatement("SELECT str FROM test WHERE num = ?");
    try {
        statement1.bindString(1, "hello");
        long value = statement1.simpleQueryForLong();
        assertEquals(1234, value);
        statement1.bindString(1, "world");
        statement1.simpleQueryForLong();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    try {
        statement2.bindLong(1, 1234);
        String value = statement1.simpleQueryForString();
        assertEquals("hello", value);
        statement2.bindLong(1, 5678);
        statement1.simpleQueryForString();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    statement1.close();
    statement2.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteDoneException(android.database.sqlite.SQLiteDoneException) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 8 with SQLiteDoneException

use of android.database.sqlite.SQLiteDoneException in project android_frameworks_base by ResurrectionRemix.

the class DatabaseStatementTest method testSimpleQuery.

@MediumTest
public void testSimpleQuery() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);");
    mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');");
    SQLiteStatement statement1 = mDatabase.compileStatement("SELECT num FROM test WHERE str = ?");
    SQLiteStatement statement2 = mDatabase.compileStatement("SELECT str FROM test WHERE num = ?");
    try {
        statement1.bindString(1, "hello");
        long value = statement1.simpleQueryForLong();
        assertEquals(1234, value);
        statement1.bindString(1, "world");
        statement1.simpleQueryForLong();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    try {
        statement2.bindLong(1, 1234);
        String value = statement1.simpleQueryForString();
        assertEquals("hello", value);
        statement2.bindLong(1, 5678);
        statement1.simpleQueryForString();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    statement1.close();
    statement2.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteDoneException(android.database.sqlite.SQLiteDoneException) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 9 with SQLiteDoneException

use of android.database.sqlite.SQLiteDoneException in project XPrivacy by M66B.

the class PrivacyService method getSetting.

@Override
public PSetting getSetting(PSetting setting) throws RemoteException {
    long start = System.currentTimeMillis();
    // Translate isolated uid
    setting.uid = getIsolatedUid(setting.uid);
    int userId = Util.getUserId(setting.uid);
    // Special case
    if (Meta.cTypeAccountHash.equals(setting.type))
        try {
            setting.type = Meta.cTypeAccount;
            setting.name = Util.sha1(setting.name);
        } catch (Throwable ex) {
            Util.bug(null, ex);
        }
    // Default result
    PSetting result = new PSetting(setting.uid, setting.type, setting.name, setting.value);
    // Disable strict mode
    ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
    ThreadPolicy newPolicy = new ThreadPolicy.Builder(oldPolicy).permitDiskReads().permitDiskWrites().build();
    StrictMode.setThreadPolicy(newPolicy);
    try {
        // No permissions enforced
        // Check cache
        CSetting key = new CSetting(setting.uid, setting.type, setting.name);
        synchronized (mSettingCache) {
            if (mSettingCache.containsKey(key)) {
                result.value = mSettingCache.get(key).getValue();
                if (result.value == null)
                    // default value
                    result.value = setting.value;
                return result;
            }
        }
        // No persmissions required
        SQLiteDatabase db = getDb();
        if (db == null)
            return result;
        // Fallback
        if (!PrivacyManager.cSettingMigrated.equals(setting.name) && !getSettingBool(userId, PrivacyManager.cSettingMigrated, false)) {
            if (setting.uid == 0)
                result.value = PrivacyProvider.getSettingFallback(setting.name, null, false);
            if (result.value == null) {
                result.value = PrivacyProvider.getSettingFallback(String.format("%s.%d", setting.name, setting.uid), setting.value, false);
                return result;
            }
        }
        // Precompile statement when needed
        if (stmtGetSetting == null) {
            String sql = "SELECT value FROM " + cTableSetting + " WHERE uid=? AND type=? AND name=?";
            stmtGetSetting = db.compileStatement(sql);
        }
        // Execute statement
        boolean found = false;
        mLock.readLock().lock();
        try {
            db.beginTransaction();
            try {
                try {
                    synchronized (stmtGetSetting) {
                        stmtGetSetting.clearBindings();
                        stmtGetSetting.bindLong(1, setting.uid);
                        stmtGetSetting.bindString(2, setting.type);
                        stmtGetSetting.bindString(3, setting.name);
                        result.value = stmtGetSetting.simpleQueryForString();
                        found = true;
                    }
                } catch (SQLiteDoneException ignored) {
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        } finally {
            mLock.readLock().unlock();
        }
        // Add to cache
        key.setValue(found ? result.value : null);
        synchronized (mSettingCache) {
            if (mSettingCache.containsKey(key))
                mSettingCache.remove(key);
            mSettingCache.put(key, key);
        }
        // Default value
        if (result.value == null)
            result.value = setting.value;
    } catch (SQLiteException ex) {
        notifyException(ex);
    } catch (Throwable ex) {
        Util.bug(null, ex);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }
    long ms = System.currentTimeMillis() - start;
    Util.log(null, ms < PrivacyManager.cWarnServiceDelayMs ? Log.INFO : Log.WARN, String.format("Get service %s %d ms", setting, ms));
    return result;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ThreadPolicy(android.os.StrictMode.ThreadPolicy) SQLiteDoneException(android.database.sqlite.SQLiteDoneException) SQLiteException(android.database.sqlite.SQLiteException) SuppressLint(android.annotation.SuppressLint)

Example 10 with SQLiteDoneException

use of android.database.sqlite.SQLiteDoneException in project XPrivacy by M66B.

the class PrivacyService method isRestrictionSet.

@Override
public boolean isRestrictionSet(PRestriction restriction) throws RemoteException {
    try {
        // No permissions required
        boolean set = false;
        SQLiteDatabase db = getDb();
        if (db != null) {
            // Precompile statement when needed
            if (stmtGetRestriction == null) {
                String sql = "SELECT restricted FROM " + cTableRestriction + " WHERE uid=? AND restriction=? AND method=?";
                stmtGetRestriction = db.compileStatement(sql);
            }
            // Execute statement
            mLock.readLock().lock();
            try {
                db.beginTransaction();
                try {
                    try {
                        synchronized (stmtGetRestriction) {
                            stmtGetRestriction.clearBindings();
                            stmtGetRestriction.bindLong(1, restriction.uid);
                            stmtGetRestriction.bindString(2, restriction.restrictionName);
                            stmtGetRestriction.bindString(3, restriction.methodName);
                            stmtGetRestriction.simpleQueryForLong();
                            set = true;
                        }
                    } catch (SQLiteDoneException ignored) {
                    }
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            } finally {
                mLock.readLock().unlock();
            }
        }
        return set;
    } catch (Throwable ex) {
        Util.bug(null, ex);
        throw new RemoteException(ex.toString());
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteDoneException(android.database.sqlite.SQLiteDoneException) RemoteException(android.os.RemoteException)

Aggregations

SQLiteDoneException (android.database.sqlite.SQLiteDoneException)12 SQLiteStatement (android.database.sqlite.SQLiteStatement)7 MediumTest (android.test.suitebuilder.annotation.MediumTest)6 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)5 SuppressLint (android.annotation.SuppressLint)3 SQLiteException (android.database.sqlite.SQLiteException)2 RemoteException (android.os.RemoteException)2 ThreadPolicy (android.os.StrictMode.ThreadPolicy)2 Date (java.util.Date)1