Skip to content

Commit f6230ad

Browse files
Explicitly allow specifying WAL journal mode with SQLiteOpenHelper constructor
1 parent e9013f3 commit f6230ad

File tree

8 files changed

+62
-13
lines changed

8 files changed

+62
-13
lines changed
8 KB
Binary file not shown.

sqlcipher/src/androidTest/java/net/zetetic/database/SQLCipherWALTestScenario.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MyHelper extends SQLiteOpenHelper {
2626
public static final String DATABASE_NAME = "mydb.db";
2727

2828
public MyHelper(Context ctx){
29-
super(ctx, ctx.getDatabasePath(DATABASE_NAME).getAbsolutePath(), "secret", null, 1, 1, null, null);
29+
super(ctx, ctx.getDatabasePath(DATABASE_NAME).getAbsolutePath(), "secret", null, 1, 1, null, null, false);
3030
}
3131
public void onConfigure(SQLiteDatabase db){
3232
db.enableWriteAheadLogging();

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherDatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public void shouldRetrieveLargeSingleRowResultFromCursor(){
481481
public void shouldAllowCursorWindowToResize(){
482482
try {
483483
Cursor cursor;
484-
int id = 1, extra = 100, size = 256;
484+
int id = 1, extra = 1024, size = 256;
485485
byte[] tooLargeQueriedData = null;
486486
SQLiteCursor.setCursorWindowSize(size);
487487
byte[] tooLargeData = generateRandomBytes(size + extra);

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherOpenHelperTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import org.junit.Test;
1717

18+
import java.io.File;
19+
1820
public class SQLCipherOpenHelperTest extends AndroidSQLCipherTestCase {
1921

2022
@Test
@@ -28,6 +30,43 @@ public void shouldAccessReadOnlyDatabaseFromOpenHelper(){
2830
assertThat(db, is(notNullValue()));
2931
}
3032

33+
@Test
34+
public void shouldSpecifyWALModeFromSQLiteOpenHelperConstructor(){
35+
database.close();
36+
File file = extractAssetToDatabaseDirectory("sqlcipher-4-wal-journal.db");
37+
SqlCipherOpenHelper helper = new SqlCipherOpenHelper(context, file, "test", true);
38+
SQLiteDatabase database = helper.getWritableDatabase();
39+
SQLiteStatement statement = database.compileStatement("PRAGMA journal_mode;");
40+
String mode = statement.simpleQueryForString();
41+
statement.close();
42+
assertThat(mode, is("wal"));
43+
}
44+
45+
@Test
46+
public void shouldIgnoreWALModeInDatabaseWhenExplicitlyDisableInConstructor(){
47+
database.close();
48+
File file = extractAssetToDatabaseDirectory("sqlcipher-4-wal-journal.db");
49+
SqlCipherOpenHelper helper = new SqlCipherOpenHelper(context, file, "test", false);
50+
SQLiteDatabase database = helper.getWritableDatabase();
51+
SQLiteStatement statement = database.compileStatement("PRAGMA journal_mode;");
52+
String mode = statement.simpleQueryForString();
53+
statement.close();
54+
assertThat(mode, is("delete"));
55+
}
56+
57+
@Test
58+
public void shouldAllowWALModeWhenEnabledViaSettingFromHelperInstance(){
59+
database.close();
60+
File file = extractAssetToDatabaseDirectory("sqlcipher-4-wal-journal.db");
61+
SqlCipherOpenHelper helper = new SqlCipherOpenHelper(context, file, "test", false);
62+
helper.setWriteAheadLoggingEnabled(true);
63+
SQLiteDatabase database = helper.getWritableDatabase();
64+
SQLiteStatement statement = database.compileStatement("PRAGMA journal_mode;");
65+
String mode = statement.simpleQueryForString();
66+
statement.close();
67+
assertThat(mode, is("wal"));
68+
}
69+
3170
private class SqlCipherOpenHelper extends SQLiteOpenHelper {
3271

3372
private final String TAG = getClass().getSimpleName();
@@ -43,7 +82,11 @@ public void preKey(SQLiteConnection sqLiteConnection) {
4382
public void postKey(SQLiteConnection sqLiteConnection) {
4483
Log.d(SQLCipherOpenHelperTest.this.TAG, "postKey()");
4584
}
46-
});
85+
}, false);
86+
}
87+
88+
public SqlCipherOpenHelper(Context context, File file, String password, boolean enableWriteAheadLogging){
89+
super(context, file.getAbsolutePath(), password, null, 1, 1, null, null, enableWriteAheadLogging);
4790
}
4891

4992
@Override

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherVersionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public void shouldExtractLibraryCipherVersion() {
1717
cipherVersion = cursor.getString(0);
1818
cursor.close();
1919
}
20-
assertThat(cipherVersion, containsString("4.5.0"));
20+
assertThat(cipherVersion, containsString("4.5.1"));
2121
}
2222
}

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteOpenHelper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int
140140
public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
141141
int minimumSupportedVersion, DatabaseErrorHandler errorHandler) {
142142
this(context, name, new byte[0], factory, version, minimumSupportedVersion,
143-
errorHandler, null);
143+
errorHandler, null, false);
144144
}
145145

146146
/**
@@ -170,9 +170,10 @@ public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int
170170
*/
171171
public SQLiteOpenHelper(Context context, String name, String password, CursorFactory factory,
172172
int version, int minimumSupportedVersion,
173-
DatabaseErrorHandler errorHandler, SQLiteDatabaseHook databaseHook){
173+
DatabaseErrorHandler errorHandler, SQLiteDatabaseHook databaseHook,
174+
boolean enableWriteAheadLogging) {
174175
this(context, name, getBytes(password), factory, version, minimumSupportedVersion,
175-
errorHandler, databaseHook);
176+
errorHandler, databaseHook, enableWriteAheadLogging);
176177
}
177178

178179
/**
@@ -202,7 +203,8 @@ public SQLiteOpenHelper(Context context, String name, String password, CursorFac
202203
*/
203204
public SQLiteOpenHelper(Context context, String name, byte[] password, CursorFactory factory,
204205
int version, int minimumSupportedVersion,
205-
DatabaseErrorHandler errorHandler, SQLiteDatabaseHook databaseHook) {
206+
DatabaseErrorHandler errorHandler, SQLiteDatabaseHook databaseHook,
207+
boolean enableWriteAheadLogging) {
206208
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
207209

208210
mContext = context;
@@ -212,6 +214,7 @@ public SQLiteOpenHelper(Context context, String name, byte[] password, CursorFac
212214
mNewVersion = version;
213215
mErrorHandler = errorHandler;
214216
mDatabaseHook = databaseHook;
217+
mEnableWriteAheadLogging = enableWriteAheadLogging;
215218
mMinimumSupportedVersion = Math.max(0, minimumSupportedVersion);
216219
}
217220

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SupportHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ public class SupportHelper implements SupportSQLiteOpenHelper {
88

99
private SQLiteOpenHelper openHelper;
1010

11-
public SupportHelper(final Configuration configuration, byte[] password, SQLiteDatabaseHook hook) {
11+
public SupportHelper(final Configuration configuration, byte[] password, SQLiteDatabaseHook hook,
12+
boolean enableWriteAheadLogging) {
1213
openHelper = new SQLiteOpenHelper(configuration.context, configuration.name, password,
1314
null, configuration.callback.version, configuration.callback.version,
14-
null, hook) {
15+
null, hook, enableWriteAheadLogging) {
1516
@Override
1617
public void onCreate(SQLiteDatabase db) {
1718
configuration.callback.onCreate(db);

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SupportOpenHelperFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ public class SupportOpenHelperFactory implements SupportSQLiteOpenHelper.Factory
77

88
private final byte[] password;
99
private final SQLiteDatabaseHook hook;
10+
private final boolean enableWriteAheadLogging;
1011

1112
public SupportOpenHelperFactory(byte[] password){
12-
this(password, null);
13+
this(password, null, false);
1314
}
1415

15-
public SupportOpenHelperFactory(byte[] password, SQLiteDatabaseHook hook){
16+
public SupportOpenHelperFactory(byte[] password, SQLiteDatabaseHook hook, boolean enableWriteAheadLogging) {
1617
this.password = password;
1718
this.hook = hook;
19+
this.enableWriteAheadLogging = enableWriteAheadLogging;
1820
}
1921

2022
@NonNull
2123
@Override
2224
public SupportSQLiteOpenHelper create(@NonNull SupportSQLiteOpenHelper.Configuration configuration) {
23-
return new SupportHelper(configuration, this.password, this.hook);
25+
return new SupportHelper(configuration, this.password, this.hook, enableWriteAheadLogging);
2426
}
2527
}

0 commit comments

Comments
 (0)