Skip to content

Commit 261b760

Browse files
Merge pull request #82 from brodyspark/cursor-api-fix-2
Support Cursor.getType() for all Android API versions
2 parents e5068b2 + 855e4cb commit 261b760

10 files changed

+114
-17
lines changed

src/net/sqlcipher/AbstractCursor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* This is an abstract cursor class that handles a lot of the common code
3737
* that all cursors need to deal with and is provided for convenience reasons.
3838
*/
39-
public abstract class AbstractCursor implements android.database.CrossProcessCursor {
39+
public abstract class AbstractCursor implements android.database.CrossProcessCursor, net.sqlcipher.Cursor {
4040
private static final String TAG = "Cursor";
4141

4242
DataSetObservable mDataSetObservable = new DataSetObservable();
@@ -56,9 +56,7 @@ public abstract class AbstractCursor implements android.database.CrossProcessCur
5656
abstract public double getDouble(int column);
5757
abstract public boolean isNull(int column);
5858

59-
public int getType(int column) {
60-
throw new UnsupportedOperationException();
61-
}
59+
abstract public int getType(int column);
6260

6361
// TODO implement getBlob in all cursor types
6462
public byte[] getBlob(int column) {

src/net/sqlcipher/CrossProcessCursorWrapper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package net.sqlcipher;
22

3-
import android.database.Cursor;
43
import android.database.CrossProcessCursor;
54
import android.database.CursorWindow;
6-
import android.database.CursorWrapper;
75

86
public class CrossProcessCursorWrapper extends CursorWrapper implements CrossProcessCursor {
97

src/net/sqlcipher/Cursor.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2006 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.sqlcipher;
18+
19+
/**
20+
* Extension of android.database.Cursor to support getType() for API < 11.
21+
*/
22+
public interface Cursor extends android.database.Cursor {
23+
/*
24+
* Values returned by {@link #getType(int)}.
25+
* These should be consistent with the corresponding types defined in CursorWindow.h
26+
*/
27+
/** Value returned by {@link #getType(int)} if the specified column is null */
28+
static final int FIELD_TYPE_NULL = 0;
29+
30+
/** Value returned by {@link #getType(int)} if the specified column type is integer */
31+
static final int FIELD_TYPE_INTEGER = 1;
32+
33+
/** Value returned by {@link #getType(int)} if the specified column type is float */
34+
static final int FIELD_TYPE_FLOAT = 2;
35+
36+
/** Value returned by {@link #getType(int)} if the specified column type is string */
37+
static final int FIELD_TYPE_STRING = 3;
38+
39+
/** Value returned by {@link #getType(int)} if the specified column type is blob */
40+
static final int FIELD_TYPE_BLOB = 4;
41+
42+
/**
43+
* Returns data type of the given column's value.
44+
* The preferred type of the column is returned but the data may be converted to other types
45+
* as documented in the get-type methods such as {@link #getInt(int)}, {@link #getFloat(int)}
46+
* etc.
47+
*<p>
48+
* Returned column types are
49+
* <ul>
50+
* <li>{@link #FIELD_TYPE_NULL}</li>
51+
* <li>{@link #FIELD_TYPE_INTEGER}</li>
52+
* <li>{@link #FIELD_TYPE_FLOAT}</li>
53+
* <li>{@link #FIELD_TYPE_STRING}</li>
54+
* <li>{@link #FIELD_TYPE_BLOB}</li>
55+
*</ul>
56+
*</p>
57+
*
58+
* @param columnIndex the zero-based index of the target column.
59+
* @return column value type
60+
*/
61+
int getType(int columnIndex);
62+
}

src/net/sqlcipher/CursorWindow.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package net.sqlcipher;
1818

1919
import android.database.CharArrayBuffer;
20-
import android.database.Cursor;
2120

2221
import android.content.res.Resources;
2322
import android.database.sqlite.SQLiteClosable;

src/net/sqlcipher/CursorWrapper.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2006 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.sqlcipher;
18+
19+
/**
20+
* Extension of android.database.CursorWrapper to support getType() for API < 11.
21+
*/
22+
public class CursorWrapper extends android.database.CursorWrapper implements Cursor {
23+
24+
private final Cursor mCursor;
25+
26+
public CursorWrapper(Cursor cursor) {
27+
super(cursor);
28+
mCursor = cursor;
29+
}
30+
31+
public int getType(int columnIndex) {
32+
return mCursor.getType(columnIndex);
33+
}
34+
}
35+

src/net/sqlcipher/database/SQLiteCursorDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface SQLiteCursorDriver {
3131
* null if standard SQLiteCursors should be returned.
3232
* @return a Cursor over the result set
3333
*/
34-
android.database.Cursor query(CursorFactory factory, String[] bindArgs);
34+
Cursor query(CursorFactory factory, String[] bindArgs);
3535

3636
/**
3737
* Called by a SQLiteCursor when it is released.

src/net/sqlcipher/database/SQLiteDatabase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package net.sqlcipher.database;
1818

19+
import net.sqlcipher.Cursor;
1920
import net.sqlcipher.CrossProcessCursorWrapper;
2021
import net.sqlcipher.DatabaseUtils;
2122
import net.sqlcipher.SQLException;
@@ -43,7 +44,7 @@
4344
import android.content.ContentValues;
4445

4546
import android.content.Context;
46-
import android.database.Cursor;
47+
4748
import android.os.Debug;
4849
import android.os.SystemClock;
4950
import android.text.TextUtils;

src/net/sqlcipher/database/SQLiteDirectCursorDriver.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package net.sqlcipher.database;
1818

19+
import net.sqlcipher.Cursor;
1920
import net.sqlcipher.database.SQLiteDatabase.CursorFactory;
20-
import android.database.Cursor;
2121

2222
/**
2323
* A cursor driver that uses the given query directly.
@@ -27,7 +27,7 @@
2727
public class SQLiteDirectCursorDriver implements SQLiteCursorDriver {
2828
private String mEditTable;
2929
private SQLiteDatabase mDatabase;
30-
private android.database.Cursor mCursor;
30+
private Cursor mCursor;
3131
private String mSql;
3232
private SQLiteQuery mQuery;
3333

@@ -37,7 +37,7 @@ public SQLiteDirectCursorDriver(SQLiteDatabase db, String sql, String editTable)
3737
mSql = sql;
3838
}
3939

40-
public android.database.Cursor query(CursorFactory factory, String[] selectionArgs) {
40+
public Cursor query(CursorFactory factory, String[] selectionArgs) {
4141
// Compile the query
4242
SQLiteQuery query = new SQLiteQuery(mDatabase, mSql, 0, selectionArgs);
4343

@@ -76,11 +76,13 @@ public void setBindArguments(String[] bindArgs) {
7676
}
7777
}
7878

79+
@Override
7980
public void cursorDeactivated() {
8081
// Do nothing
8182
}
8283

83-
public void cursorRequeried(Cursor cursor) {
84+
@Override
85+
public void cursorRequeried(android.database.Cursor cursor) {
8486
// Do nothing
8587
}
8688

src/net/sqlcipher/database/SQLiteQueryBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
package net.sqlcipher.database;
18+
1819
import net.sqlcipher.*;
1920

2021
import android.provider.BaseColumns;
@@ -273,7 +274,7 @@ public static void appendColumns(StringBuilder s, String[] columns) {
273274
* @see android.content.ContentResolver#query(android.net.Uri, String[],
274275
* String, String[], String)
275276
*/
276-
public android.database.Cursor query(SQLiteDatabase db, String[] projectionIn,
277+
public Cursor query(SQLiteDatabase db, String[] projectionIn,
277278
String selection, String[] selectionArgs, String groupBy,
278279
String having, String sortOrder) {
279280
return query(db, projectionIn, selection, selectionArgs, groupBy, having, sortOrder,
@@ -312,7 +313,7 @@ public android.database.Cursor query(SQLiteDatabase db, String[] projectionIn,
312313
* @see android.content.ContentResolver#query(android.net.Uri, String[],
313314
* String, String[], String)
314315
*/
315-
public android.database.Cursor query(SQLiteDatabase db, String[] projectionIn,
316+
public Cursor query(SQLiteDatabase db, String[] projectionIn,
316317
String selection, String[] selectionArgs, String groupBy,
317318
String having, String sortOrder, String limit) {
318319
if (mTables == null) {

src/net/sqlcipher/database/SqliteWrapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
import android.content.ContentResolver;
2121
import android.content.ContentValues;
2222
import android.content.Context;
23-
import android.database.Cursor;
23+
2424
import net.sqlcipher.*;
25+
2526
import android.net.Uri;
2627
import android.util.Log;
2728
import android.widget.Toast;
@@ -64,7 +65,7 @@ public static Cursor query(Context context, ContentResolver resolver, Uri uri,
6465
}
6566
}
6667

67-
public static boolean requery(Context context, Cursor cursor) {
68+
public static boolean requery(Context context, android.database.Cursor cursor) {
6869
try {
6970
return cursor.requery();
7071
} catch (SQLiteException e) {

0 commit comments

Comments
 (0)