Skip to content

Commit 025673c

Browse files
committed
Resolve #14 Changes to support api level 9 (Gingerbread)
1 parent 63ab0d9 commit 025673c

File tree

8 files changed

+175
-62
lines changed

8 files changed

+175
-62
lines changed

sqlite-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ android {
2222
buildToolsVersion "23.0.3"
2323

2424
defaultConfig {
25-
minSdkVersion 15
25+
minSdkVersion 9
2626
targetSdkVersion 23
2727
versionCode 1
2828
versionName project.version

sqlite-android/src/main/java/io/requery/android/database/sqlite/SQLiteConnection.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
package io.requery.android.database.sqlite;
2323

2424
import android.annotation.SuppressLint;
25+
import android.annotation.TargetApi;
2526
import android.database.Cursor;
26-
import android.database.DatabaseUtils;
2727
import android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException;
2828
import android.database.sqlite.SQLiteDatabaseLockedException;
2929
import android.database.sqlite.SQLiteException;
30+
import android.os.Build;
31+
import android.os.Looper;
3032
import android.os.ParcelFileDescriptor;
3133
import android.support.v4.os.CancellationSignal;
3234
import android.support.v4.os.OperationCanceledException;
35+
import android.support.v4.util.LruCache;
3336
import android.util.Log;
34-
import android.util.LruCache;
3537
import android.util.Printer;
3638
import io.requery.android.database.CursorWindow;
3739

@@ -89,7 +91,7 @@
8991
*
9092
* @hide
9193
*/
92-
@SuppressWarnings({"unused", "ForLoopReplaceableByForEach", "TryFinallyCanBeTryWithResources"})
94+
@SuppressWarnings("TryFinallyCanBeTryWithResources")
9395
public final class SQLiteConnection implements CancellationSignal.OnCancelListener {
9496
private static final String TAG = "SQLiteConnection";
9597
private static final boolean DEBUG = false;
@@ -325,12 +327,13 @@ private void setSyncMode(String newValue) {
325327
}
326328

327329
private static String canonicalizeSyncMode(String value) {
328-
if (value.equals("0")) {
329-
return "OFF";
330-
} else if (value.equals("1")) {
331-
return "NORMAL";
332-
} else if (value.equals("2")) {
333-
return "FULL";
330+
switch (value) {
331+
case "0":
332+
return "OFF";
333+
case "1":
334+
return "NORMAL";
335+
case "2":
336+
return "FULL";
334337
}
335338
return value;
336339
}
@@ -345,10 +348,14 @@ private void setJournalMode(String newValue) {
345348
}
346349
// PRAGMA journal_mode silently fails and returns the original journal
347350
// mode in some cases if the journal mode could not be changed.
348-
} catch (SQLiteDatabaseLockedException ex) {
351+
} catch (SQLiteException ex) {
349352
// This error (SQLITE_BUSY) occurs if one connection has the database
350353
// open in WAL mode and another tries to change it to non-WAL.
354+
if (!(ex instanceof SQLiteDatabaseLockedException)) {
355+
throw ex;
356+
}
351357
}
358+
352359
// Because we always disable WAL mode when a database is first opened
353360
// (even if we intend to re-enable it), we can encounter problems if
354361
// there is another open connection to the database somewhere.
@@ -475,14 +482,6 @@ boolean isPreparedStatementInCache(String sql) {
475482
return mPreparedStatementCache.get(sql) != null;
476483
}
477484

478-
/**
479-
* Gets the unique id of this connection.
480-
* @return The connection id.
481-
*/
482-
public int getConnectionId() {
483-
return mConnectionId;
484-
}
485-
486485
/**
487486
* Returns true if this is the primary database connection.
488487
* @return True if this is the primary database connection.
@@ -711,7 +710,11 @@ public ParcelFileDescriptor executeForBlobFileDescriptor(String sql, Object[] bi
711710
try {
712711
int fd = nativeExecuteForBlobFileDescriptor(
713712
mConnectionPtr, statement.mStatementPtr);
714-
return fd >= 0 ? ParcelFileDescriptor.adoptFd(fd) : null;
713+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
714+
return fd >= 0 ? ParcelFileDescriptor.adoptFd(fd) : null;
715+
} else {
716+
throw new UnsupportedOperationException();
717+
}
715718
} finally {
716719
detachCancellationSignal(cancellationSignal);
717720
}
@@ -918,7 +921,7 @@ private PreparedStatement acquirePreparedStatement(String sql) {
918921
final long statementPtr = nativePrepareStatement(mConnectionPtr, sql);
919922
try {
920923
final int numParameters = nativeGetParameterCount(mConnectionPtr, statementPtr);
921-
final int type = DatabaseUtils.getSqlStatementType(sql);
924+
final int type = SQLiteStatementType.getSqlStatementType(sql);
922925
final boolean readOnly = nativeIsReadOnly(mConnectionPtr, statementPtr);
923926
statement = obtainPreparedStatement(sql, statementPtr, numParameters, type, readOnly);
924927
if (!skipCache && isCacheable(type)) {
@@ -1009,9 +1012,13 @@ public void onCancel() {
10091012
private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
10101013
final int count = bindArgs != null ? bindArgs.length : 0;
10111014
if (count != statement.mNumParameters) {
1012-
throw new SQLiteBindOrColumnIndexOutOfRangeException(
1013-
"Expected " + statement.mNumParameters + " bind arguments but "
1014-
+ count + " were provided.");
1015+
String message = "Expected " + statement.mNumParameters + " bind arguments but "
1016+
+ count + " were provided.";
1017+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
1018+
throw new SQLiteBindOrColumnIndexOutOfRangeException(message);
1019+
} else {
1020+
throw new SQLiteException(message);
1021+
}
10151022
}
10161023
if (count == 0) {
10171024
return;
@@ -1065,6 +1072,7 @@ private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
10651072
* @param obj the object whose value type is to be returned
10661073
* @return object value type
10671074
*/
1075+
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
10681076
private static int getTypeOfObject(Object obj) {
10691077
if (obj == null) {
10701078
return Cursor.FIELD_TYPE_NULL;
@@ -1088,15 +1096,21 @@ private void throwIfStatementForbidden(PreparedStatement statement) {
10881096
}
10891097

10901098
private static boolean isCacheable(int statementType) {
1091-
if (statementType == DatabaseUtils.STATEMENT_UPDATE
1092-
|| statementType == DatabaseUtils.STATEMENT_SELECT) {
1093-
return true;
1094-
}
1095-
return false;
1099+
return statementType == SQLiteStatementType.STATEMENT_UPDATE
1100+
|| statementType == SQLiteStatementType.STATEMENT_SELECT;
10961101
}
10971102

10981103
private void applyBlockGuardPolicy(PreparedStatement statement) {
1099-
1104+
if (!mConfiguration.isInMemoryDb() && SQLiteDebug.DEBUG_SQL_LOG) {
1105+
// don't have access to the policy, so just log
1106+
if (Looper.myLooper() == Looper.getMainLooper()) {
1107+
if (statement.mReadOnly) {
1108+
Log.w(TAG, "Reading from disk on main thread");
1109+
} else {
1110+
Log.w(TAG, "Writing to disk on main thread");
1111+
}
1112+
}
1113+
}
11001114
}
11011115

11021116
/**
@@ -1369,8 +1383,7 @@ public int beginOperation(String kind, String sql, Object[] bindArgs) {
13691383
} else {
13701384
operation.mBindArgs.clear();
13711385
}
1372-
for (int i = 0; i < bindArgs.length; i++) {
1373-
final Object arg = bindArgs[i];
1386+
for (final Object arg : bindArgs) {
13741387
if (arg != null && arg instanceof byte[]) {
13751388
// Don't hold onto the real byte array longer than necessary.
13761389
operation.mBindArgs.add(EMPTY_BYTE_ARRAY);
@@ -1427,6 +1440,9 @@ private boolean endOperationDeferLogLocked(int cookie) {
14271440

14281441
private void logOperationLocked(int cookie, String detail) {
14291442
final Operation operation = getOperationLocked(cookie);
1443+
if (operation == null) {
1444+
return;
1445+
}
14301446
StringBuilder msg = new StringBuilder();
14311447
operation.describe(msg, false);
14321448
if (detail != null) {

sqlite-android/src/main/java/io/requery/android/database/sqlite/SQLiteConnectionPool.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
*
7373
* @hide
7474
*/
75-
@SuppressWarnings("ForLoopReplaceableByForEach")
7675
public final class SQLiteConnectionPool implements Closeable {
7776
private static final String TAG = "SQLiteConnectionPool";
7877

@@ -123,7 +122,7 @@ enum AcquiredConnectionStatus {
123122
* perform read-only operations.
124123
* </p>
125124
*/
126-
public static final int CONNECTION_FLAG_READ_ONLY = 1 << 0;
125+
public static final int CONNECTION_FLAG_READ_ONLY = 1;
127126

128127
/**
129128
* Connection flag: Primary connection affinity.
@@ -510,9 +509,8 @@ private void closeAvailableConnectionsAndLogExceptionsLocked() {
510509

511510
// Can't throw.
512511
private void closeAvailableNonPrimaryConnectionsAndLogExceptionsLocked() {
513-
final int count = mAvailableNonPrimaryConnections.size();
514-
for (int i = 0; i < count; i++) {
515-
closeConnectionAndLogExceptionsLocked(mAvailableNonPrimaryConnections.get(i));
512+
for (SQLiteConnection connection : mAvailableNonPrimaryConnections) {
513+
closeConnectionAndLogExceptionsLocked(connection);
516514
}
517515
mAvailableNonPrimaryConnections.clear();
518516
}
@@ -585,9 +583,8 @@ private void markAcquiredConnectionsLocked(AcquiredConnectionStatus status) {
585583
keysToUpdate.add(entry.getKey());
586584
}
587585
}
588-
final int updateCount = keysToUpdate.size();
589-
for (int i = 0; i < updateCount; i++) {
590-
mAcquiredConnections.put(keysToUpdate.get(i), status);
586+
for (SQLiteConnection key : keysToUpdate) {
587+
mAcquiredConnections.put(key, status);
591588
}
592589
}
593590
}
@@ -1030,9 +1027,8 @@ public void dump(Printer printer, boolean verbose) {
10301027

10311028
printer.println(" Available non-primary connections:");
10321029
if (!mAvailableNonPrimaryConnections.isEmpty()) {
1033-
final int count = mAvailableNonPrimaryConnections.size();
1034-
for (int i = 0; i < count; i++) {
1035-
mAvailableNonPrimaryConnections.get(i).dump(printer, verbose);
1030+
for (SQLiteConnection connection : mAvailableNonPrimaryConnections) {
1031+
connection.dump(printer, verbose);
10361032
}
10371033
} else {
10381034
printer.println("<none>");

sqlite-android/src/main/java/io/requery/android/database/sqlite/SQLiteDatabase.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.annotation.SuppressLint;
2525
import android.content.ContentValues;
2626
import android.database.Cursor;
27-
import android.database.DatabaseUtils;
2827
import android.database.SQLException;
2928
import android.database.sqlite.SQLiteDatabaseCorruptException;
3029
import android.database.sqlite.SQLiteException;
@@ -49,6 +48,7 @@
4948
import java.util.ArrayList;
5049
import java.util.List;
5150
import java.util.Locale;
51+
import java.util.Map;
5252
import java.util.WeakHashMap;
5353

5454
/**
@@ -1472,10 +1472,10 @@ public long insertWithOnConflict(String table, String nullColumnHack,
14721472
if (size > 0) {
14731473
bindArgs = new Object[size];
14741474
int i = 0;
1475-
for (String colName : initialValues.keySet()) {
1475+
for (Map.Entry<String, Object> entry : initialValues.valueSet()) {
14761476
sql.append((i > 0) ? "," : "");
1477-
sql.append(colName);
1478-
bindArgs[i++] = initialValues.get(colName);
1477+
sql.append(entry.getKey());
1478+
bindArgs[i++] = entry.getValue();
14791479
}
14801480
sql.append(')');
14811481
sql.append(" VALUES (");
@@ -1576,10 +1576,10 @@ public int updateWithOnConflict(String table, ContentValues values,
15761576
int bindArgsSize = (whereArgs == null) ? setValuesSize : (setValuesSize + whereArgs.length);
15771577
Object[] bindArgs = new Object[bindArgsSize];
15781578
int i = 0;
1579-
for (String colName : values.keySet()) {
1579+
for (Map.Entry<String, Object> entry : values.valueSet()) {
15801580
sql.append((i > 0) ? "," : "");
1581-
sql.append(colName);
1582-
bindArgs[i++] = values.get(colName);
1581+
sql.append(entry.getKey());
1582+
bindArgs[i++] = entry.getValue();
15831583
sql.append("=?");
15841584
}
15851585
if (whereArgs != null) {
@@ -1679,7 +1679,7 @@ public void execSQL(String sql, Object[] bindArgs) throws SQLException {
16791679
private int executeSql(String sql, Object[] bindArgs) throws SQLException {
16801680
acquireReference();
16811681
try {
1682-
if (DatabaseUtils.getSqlStatementType(sql) == DatabaseUtils.STATEMENT_ATTACH) {
1682+
if (SQLiteStatementType.getSqlStatementType(sql) == SQLiteStatementType.STATEMENT_ATTACH) {
16831683
boolean disableWal = false;
16841684
synchronized (mLock) {
16851685
if (!mHasAttachedDbsLocked) {

sqlite-android/src/main/java/io/requery/android/database/sqlite/SQLiteProgram.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package io.requery.android.database.sqlite;
1919

20-
import android.database.DatabaseUtils;
2120
import android.support.v4.os.CancellationSignal;
2221

2322
import java.util.Arrays;
@@ -44,18 +43,18 @@ public abstract class SQLiteProgram extends SQLiteClosable {
4443
mDatabase = db;
4544
mSql = sql.trim();
4645

47-
int n = DatabaseUtils.getSqlStatementType(mSql);
46+
int n = SQLiteStatementType.getSqlStatementType(mSql);
4847
switch (n) {
49-
case DatabaseUtils.STATEMENT_BEGIN:
50-
case DatabaseUtils.STATEMENT_COMMIT:
51-
case DatabaseUtils.STATEMENT_ABORT:
48+
case SQLiteStatementType.STATEMENT_BEGIN:
49+
case SQLiteStatementType.STATEMENT_COMMIT:
50+
case SQLiteStatementType.STATEMENT_ABORT:
5251
mReadOnly = false;
5352
mColumnNames = EMPTY_STRING_ARRAY;
5453
mNumParameters = 0;
5554
break;
5655

5756
default:
58-
boolean assumeReadOnly = (n == DatabaseUtils.STATEMENT_SELECT);
57+
boolean assumeReadOnly = (n == SQLiteStatementType.STATEMENT_SELECT);
5958
SQLiteStatementInfo info = new SQLiteStatementInfo();
6059
db.getThreadSession().prepare(mSql,
6160
db.getThreadDefaultConnectionFlags(assumeReadOnly),

sqlite-android/src/main/java/io/requery/android/database/sqlite/SQLiteSession.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
package io.requery.android.database.sqlite;
2323

2424
import android.annotation.SuppressLint;
25-
import android.database.DatabaseUtils;
2625
import android.database.sqlite.SQLiteException;
2726
import android.database.sqlite.SQLiteTransactionListener;
2827
import android.os.ParcelFileDescriptor;
@@ -881,19 +880,19 @@ private boolean executeSpecial(String sql, Object[] bindArgs, int connectionFlag
881880
cancellationSignal.throwIfCanceled();
882881
}
883882

884-
final int type = DatabaseUtils.getSqlStatementType(sql);
883+
final int type = SQLiteStatementType.getSqlStatementType(sql);
885884
switch (type) {
886-
case DatabaseUtils.STATEMENT_BEGIN:
885+
case SQLiteStatementType.STATEMENT_BEGIN:
887886
beginTransaction(TRANSACTION_MODE_EXCLUSIVE, null, connectionFlags,
888887
cancellationSignal);
889888
return true;
890889

891-
case DatabaseUtils.STATEMENT_COMMIT:
890+
case SQLiteStatementType.STATEMENT_COMMIT:
892891
setTransactionSuccessful();
893892
endTransaction(cancellationSignal);
894893
return true;
895894

896-
case DatabaseUtils.STATEMENT_ABORT:
895+
case SQLiteStatementType.STATEMENT_ABORT:
897896
endTransaction(cancellationSignal);
898897
return true;
899898
}

0 commit comments

Comments
 (0)