Skip to content

Commit

Permalink
Updated example with a page opening an encrypted database
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmartos96 committed Nov 20, 2019
1 parent 981b8d9 commit 8f7a389
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
Binary file added sqflite/example/assets/example_pass_1234.db
Binary file not shown.
5 changes: 5 additions & 0 deletions sqflite/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:sqflite_example/deprecated_test_page.dart';
import 'package:sqflite_example/exception_test_page.dart';
import 'package:sqflite_example/exp_test_page.dart';
import 'package:sqflite_example/manual_test_page.dart';
import 'package:sqflite_example/sqlcipher_test_page.dart';
import 'package:sqflite_example/src/dev_utils.dart';

import 'model/main_item.dart';
Expand All @@ -29,6 +30,7 @@ class MyApp extends StatefulWidget {
_MyAppState createState() => _MyAppState();
}

const String testSqlCipherRoute = "/test/sqlcipher";
const String testRawRoute = "/test/simple";
const String testOpenRoute = "/test/open";
const String testSlowRoute = "/test/slow";
Expand All @@ -43,6 +45,7 @@ const String testDeprecatedRoute = "/test/deprecated";
class _MyAppState extends State<MyApp> {
var routes = <String, WidgetBuilder>{
'/test': (BuildContext context) => MyHomePage(),
testSqlCipherRoute: (BuildContext context) => SqlCipherTestPage(),
testRawRoute: (BuildContext context) => RawTestPage(),
testOpenRoute: (BuildContext context) => OpenTestPage(),
testSlowRoute: (BuildContext context) => SlowTestPage(),
Expand Down Expand Up @@ -78,6 +81,8 @@ class _MyAppState extends State<MyApp> {

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key) {
items.add(
MainItem("Sqlcipher tests", "Simple tests with an encrypted database", route: testSqlCipherRoute));
items.add(
MainItem("Raw tests", "Raw SQLite operations", route: testRawRoute));
items.add(MainItem("Open tests", "Open onCreate/onUpgrade/onDowngrade",
Expand Down
4 changes: 2 additions & 2 deletions sqflite/example/lib/open_test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class OpenCallbacks {
/// Check if a file is a valid database file
///
/// An empty file is a valid empty sqlite file
Future<bool> isDatabase(String path) async {
Future<bool> isDatabase(String path, {String password}) async {
Database db;
bool isDatabase = false;
try {
db = await openReadOnlyDatabase(path);
db = await openReadOnlyDatabase(path, password: password);
int version = await db.getVersion();
if (version != null) {
isDatabase = true;
Expand Down
79 changes: 79 additions & 0 deletions sqflite/example/lib/sqlcipher_test_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_example/open_test_page.dart';

import 'test_page.dart';

class SqlCipherTestPage extends TestPage {
SqlCipherTestPage() : super("SqlCipher tests") {
test('Open and query database', () async {
String path = await initDeleteDb("encrypted.db");

expect(await isDatabase(path), isFalse);

const String password = "1234";

Database db = await openDatabase(
path,
password: password,
version: 1,
onCreate: (db, version) async {
Batch batch = db.batch();

batch
.execute("CREATE TABLE Test (id INTEGER PRIMARY KEY, text NAME)");
await batch.commit();
},
);

try {
expect(
await db.rawInsert("INSERT INTO Test (text) VALUES (?)", ['test']),
1);
var result = await db.query("Test");
List expected = [
{'id': 1, 'text': 'test'}
];
expect(result, expected);

expect(await isDatabase(path, password: password), isTrue);
} finally {
await db?.close();
}
expect(await isDatabase(path, password: password), isTrue);
});

test("Open asset database", () async {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, "asset_example.db");

// delete existing if any
await deleteDatabase(path);

// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}

// Copy from asset
ByteData data = await rootBundle.load(join("assets", "example_pass_1234.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);

// open the database
Database db = await openDatabase(path, password: "1234");

// Our database as a single table with a single element
List<Map<String, dynamic>> list = await db.rawQuery("SELECT * FROM Test");
print("list $list");
expect(list.first["name"], "simple value");

await db.close();
});
}
}
7 changes: 3 additions & 4 deletions sqflite/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ description: Demonstrates how to use the sqflite plugin.
publish_to: none

environment:
sdk: ">=2.1.0-dev <3.0.0"
sdk: '>=2.5.0 <3.0.0'

dependencies:
path: any
collection: any
flutter:
sdk: flutter
sqflite:
git:
url: git://github.com/tekartik/sqflite
path: sqflite
path: ../


dev_dependencies:
Expand Down Expand Up @@ -44,6 +42,7 @@ flutter:
# - images/a_dot_ham.jpeg
assets:
- assets/example.db
- assets/example_pass_1234.db
- assets/issue_64.db

dependency_overrides:
Expand Down
1 change: 1 addition & 0 deletions sqflite/lib/sqlite_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ abstract class OpenDatabaseOptions {

/// Called after all other callbacks have been called.
OnDatabaseOpenFn onOpen;
/// Database password (optional)
String password;

/// Open the database in read-only mode (no callback called).
Expand Down
2 changes: 1 addition & 1 deletion sqflite/lib/src/constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const String paramVersion = "version";
/// The database id (int)
const String paramId = "id";

// When opening the database (String) (optional)
/// When opening the database (String) (optional)
const String paramPassword = "password";

/// True if the database is in a transaction
Expand Down

0 comments on commit 8f7a389

Please sign in to comment.