Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aceaa225 authored by Vasu Nori's avatar Vasu Nori Committed by Android (Google) Code Review
Browse files

Merge "bug:4090903 allow bindargs on attach database statement"

parents 54aa1477 cc1eaf6f
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -1885,10 +1885,6 @@ public class SQLiteDatabase extends SQLiteClosable {
     * @throws SQLException if the SQL string is invalid
     */
    public void execSQL(String sql) throws SQLException {
        if (DatabaseUtils.getSqlStatementType(sql) == DatabaseUtils.STATEMENT_ATTACH) {
            disableWriteAheadLogging();
            mHasAttachedDbs = true;
        }
        executeSql(sql, null);
    }

@@ -1943,6 +1939,10 @@ public class SQLiteDatabase extends SQLiteClosable {
    }

    private int executeSql(String sql, Object[] bindArgs) throws SQLException {
        if (DatabaseUtils.getSqlStatementType(sql) == DatabaseUtils.STATEMENT_ATTACH) {
            disableWriteAheadLogging();
            mHasAttachedDbs = true;
        }
        SQLiteStatement statement = new SQLiteStatement(this, sql, bindArgs);
        try {
            return statement.executeUpdateDelete();
+3 −9
Original line number Diff line number Diff line
@@ -105,12 +105,9 @@ public abstract class SQLiteProgram extends SQLiteClosable {
            case DatabaseUtils.STATEMENT_SELECT:
                mStatementType = n | STATEMENT_CACHEABLE | STATEMENT_USE_POOLED_CONN;
                break;
            case DatabaseUtils.STATEMENT_ATTACH:
            case DatabaseUtils.STATEMENT_BEGIN:
            case DatabaseUtils.STATEMENT_COMMIT:
            case DatabaseUtils.STATEMENT_ABORT:
            case DatabaseUtils.STATEMENT_DDL:
            case DatabaseUtils.STATEMENT_UNPREPARED:
                mStatementType = n | STATEMENT_DONT_PREPARE;
                break;
            default:
@@ -353,13 +350,10 @@ public abstract class SQLiteProgram extends SQLiteClosable {

    /* package */ void compileAndbindAllArgs() {
        if ((mStatementType & STATEMENT_DONT_PREPARE) > 0) {
            // no need to prepare this SQL statement
            if (SQLiteDebug.DEBUG_SQL_STATEMENTS) {
            if (mBindArgs != null) {
                    throw new IllegalArgumentException("no need to pass bindargs for this sql :" +
                            mSql);
                }
                throw new IllegalArgumentException("Can't pass bindargs for this sql :" + mSql);
            }
            // no need to prepare this SQL statement
            return;
        }
        if (nStatement == 0) {
+45 −0
Original line number Diff line number Diff line
@@ -31,9 +31,11 @@ import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;
import android.util.Pair;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SQLiteDatabaseTest extends AndroidTestCase {
    private static final String TAG = "DatabaseGeneralTest";
@@ -892,6 +894,49 @@ public class SQLiteDatabaseTest extends AndroidTestCase {
        c.close();
    }

    @SmallTest
    public void testAttachDb() {
        String newDb = "/sdcard/mydata.db";
        File f = new File(newDb);
        if (f.exists()) {
            f.delete();
        }
        assertFalse(f.exists());
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(newDb, null);
        db.execSQL("create table test1 (i int);");
        db.execSQL("insert into test1 values(1);");
        db.execSQL("insert into test1 values(11);");
        Cursor c = null;
        try {
            c = db.rawQuery("select * from test1", null);
            int count = c.getCount();
            Log.i(TAG, "count: " + count);
            assertEquals(2, count);
        } finally {
            c.close();
            db.close();
            c = null;
        }

        mDatabase.execSQL("attach database ? as newDb" , new String[]{newDb});
        Cursor c1 = null;
        try {
            c1 = mDatabase.rawQuery("select * from newDb.test1", null);
            assertEquals(2, c1.getCount());
        } catch (Exception e) {
            fail("unexpected exception: " + e.getMessage());
        } finally {
            if (c1 != null) {
                c1.close();
            }
        }
        List<Pair<String, String>> dbs = mDatabase.getAttachedDbs();
        for (Pair<String, String> p: dbs) {
            Log.i(TAG, "attached dbs: " + p.first + " : " + p.second);
        }
        assertEquals(2, dbs.size());
     }

    /**
     * http://b/issue?id=2943028
     * SQLiteOpenHelper maintains a Singleton even if it is in bad state.