Skip to content

Commit 0b11805

Browse files
committed
Implemented SQLiteDatabase#{begin,end}Transaction().
1 parent ebabfe7 commit 0b11805

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [0.1.1] - 2018-11-23
1010
### Added
11+
- `SQLiteDatabase#beginTransaction()` method
12+
- `SQLiteDatabase#beginTransactionNonExclusive()` method
13+
- `SQLiteDatabase#endTransaction()` method
1114

1215
## [0.1.0] - 2018-11-22
1316
### Added

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ Reference
100100
- [`SQLiteDatabase.openOrCreateDatabase()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/openOrCreateDatabase.html)
101101
- [`SQLiteDatabase#path`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/path.html)
102102
- [`SQLiteDatabase#version`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/version.html)
103+
- [`SQLiteDatabase#beginTransaction()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/beginTransaction.html)
104+
- [`SQLiteDatabase#beginTransactionNonExclusive()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/beginTransactionNonExclusive.html)
105+
- [`SQLiteDatabase#endTransaction()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/endTransaction.html)
103106
- [`SQLiteDatabase#execSQL()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/execSQL.html)
104107
- [`SQLiteDatabase#getPath()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/getPath.html)
105108
- [`SQLiteDatabase#getVersion()`](https://pub.dartlang.org/documentation/flutter_sqlcipher/latest/sqlite/SQLiteDatabase/getVersion.html)

android/src/main/java/com/github/drydart/flutter_sqlcipher/SQLiteDatabaseMethodHandler.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ class SQLiteDatabaseMethodHandler implements MethodCallHandler {
5656
break;
5757
}
5858

59+
case "beginTransaction": {
60+
final SQLiteDatabase db = this.getDatabaseArgument(call);
61+
final String mode = getOptionalArgument(call, "mode", "exclusive");
62+
switch (mode) {
63+
case "exclusive":
64+
db.beginTransaction();
65+
break;
66+
case "immediate":
67+
//db.beginTransactionNonExclusive(); // TODO: this is missing in SQLCipher 3.5.9
68+
db.beginTransaction();
69+
break;
70+
default:
71+
assert(false); // unreachable
72+
throw new AssertionError();
73+
}
74+
result.success(null);
75+
break;
76+
}
77+
78+
case "endTransaction": {
79+
final SQLiteDatabase db = this.getDatabaseArgument(call);
80+
db.endTransaction();
81+
result.success(null);
82+
break;
83+
}
84+
5985
case "execSQL": {
6086
final SQLiteDatabase db = this.getDatabaseArgument(call);
6187
final String sql = getRequiredArgument(call, "sql");
@@ -90,7 +116,7 @@ class SQLiteDatabaseMethodHandler implements MethodCallHandler {
90116

91117
case "isWriteAheadLoggingEnabled": {
92118
final SQLiteDatabase db = this.getDatabaseArgument(call);
93-
//result.success(db.isWriteAheadLoggingEnabled()); // TODO: this is missing in SQLCipher
119+
//result.success(db.isWriteAheadLoggingEnabled()); // TODO: this is missing in SQLCipher 3.5.9
94120
result.success(false);
95121
break;
96122
}
@@ -197,9 +223,16 @@ class SQLiteDatabaseMethodHandler implements MethodCallHandler {
197223
private static <T> T
198224
getOptionalArgument(final MethodCall call,
199225
final String name) {
226+
return getOptionalArgument(call, name, (T)null);
227+
}
228+
229+
private static <T> T
230+
getOptionalArgument(final MethodCall call,
231+
final String name,
232+
final T defaultValue) {
200233
assert(call != null);
201234
assert(name != null);
202235

203-
return call.hasArgument(name) ? (T)call.argument(name) : (T)null;
236+
return call.hasArgument(name) ? (T)call.argument(name) : defaultValue;
204237
}
205238
}

lib/src/database.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ abstract class SQLiteDatabase {
126126
/// This is simply a Dart-idiomatic getter alias for [getVersion()].
127127
Future<int> get version => getVersion();
128128

129+
/// Begins a transaction in `EXCLUSIVE` mode.
130+
///
131+
/// See: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#beginTransaction()
132+
Future<void> beginTransaction() {
133+
final Map<String, dynamic> request = <String, dynamic>{'id': id, 'mode': 'exclusive'};
134+
return _channel.invokeMethod('beginTransaction', request);
135+
}
136+
137+
/// Begins a transaction in `IMMEDIATE` mode.
138+
///
139+
/// See: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#beginTransactionNonExclusive()
140+
Future<void> beginTransactionNonExclusive() {
141+
final Map<String, dynamic> request = <String, dynamic>{'id': id, 'mode': 'immediate'};
142+
return _channel.invokeMethod('beginTransaction', request);
143+
}
144+
145+
/// Ends a transaction.
146+
///
147+
/// See: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#endTransaction()
148+
Future<void> endTransaction() {
149+
final Map<String, dynamic> request = <String, dynamic>{'id': id};
150+
return _channel.invokeMethod('endTransaction', request);
151+
}
152+
129153
/// Executes a single SQL statement that is *not* a `SELECT` or any other SQL
130154
/// statement that returns data.
131155
///

0 commit comments

Comments
 (0)