Skip to content

Commit

Permalink
Added changes over original PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoleitao committed Jan 13, 2024
1 parent 7acc5ad commit f5685f3
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 28 deletions.
87 changes: 71 additions & 16 deletions packages/stash_sqlite/lib/src/sqlite/sqlite_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:drift/drift.dart';
Expand Down Expand Up @@ -52,12 +53,16 @@ class SqliteMemoryAdapter<I extends Info, E extends Entry<I>>
/// * [builder]: Database builder
/// * [logStatements]: Generated sql statements will be printed before executing
/// * [setup]: Function that can be used to perform a setup just after the database is opened
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
static Future<SqliteAdapter<I, E>> build<I extends Info, E extends Entry<I>>(
SqliteBuilder<I, E> builder,
{bool? logStatements,
DatabaseSetup? setup}) {
DatabaseSetup? setup,
bool? cachePreparedStatements}) {
return Future.value(SqliteMemoryAdapter._(builder(NativeDatabase.memory(
logStatements: logStatements ?? false, setup: setup))));
logStatements: logStatements ?? false,
setup: setup,
cachePreparedStatements: cachePreparedStatements ?? false))));
}

@override
Expand All @@ -71,40 +76,90 @@ class SqliteMemoryAdapter<I extends Info, E extends Entry<I>>
}
}

/// The [SqliteFileAdapter] provides a bridge between the store and the
/// Sqlite backend
class SqliteFileAdapter<I extends Info, E extends Entry<I>>
/// The [SqliteFileBaseAdapter] provides a base abstraction for bridge between
/// the store and the Sqlite file backend
abstract class SqliteFileBaseAdapter<I extends Info, E extends Entry<I>>
extends SqliteAdapter<I, E> {
/// The database file
final File file;

/// [SqliteBaseFileAdapter] constructor.
///
/// * [db]: The database
/// * [file]: The [File] that store the Sqlite database
SqliteFileBaseAdapter(super.db, this.file);

@override
Future<void> delete(String name) {
return dao.clear(name);
}

@override
Future<void> deleteAll() {
return file.delete();
}
}

/// The [SqliteFileAdapter] provides a bridge between the store and the
/// Sqlite file backend
class SqliteFileAdapter<I extends Info, E extends Entry<I>>
extends SqliteFileBaseAdapter<I, E> {
/// [SqliteFileAdapter] constructor.
///
/// * [db]: The database
/// * [file]: The [File] that store the Sqlite database
SqliteFileAdapter._(super.db, this.file);
SqliteFileAdapter._(super.db, super.file);

/// Builds [SqliteFileAdapter].
///
/// * [builder]: Database builder
/// * [file]: The [File] that store the Sqlite database
/// * [logStatements]: Generated sql statements will be printed before executing
/// * [setup]: Function that can be used to perform a setup just after the database is opened
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
static Future<SqliteAdapter<I, E>> build<I extends Info, E extends Entry<I>>(
SqliteBuilder<I, E> builder, File file,
{bool? logStatements, DatabaseSetup? setup}) {
{bool? logStatements,
DatabaseSetup? setup,
bool? cachePreparedStatements}) {
return Future.value(SqliteFileAdapter._(
builder(NativeDatabase.createInBackground(file,
logStatements: logStatements ?? false, setup: setup)),
builder(NativeDatabase(file,
logStatements: logStatements ?? false,
setup: setup,
cachePreparedStatements: cachePreparedStatements ?? false)),
file));
}
}

@override
Future<void> delete(String name) {
return dao.clear(name);
}
/// The [SqliteBackgroundFileAdapter] provides a bridge between the store and the
/// background Sqlite file backend
class SqliteBackgroundFileAdapter<I extends Info, E extends Entry<I>>
extends SqliteFileBaseAdapter<I, E> {
/// [SqliteBackgroundFileAdapter] constructor.
///
/// * [db]: The database
/// * [file]: The [File] that store the Sqlite database
SqliteBackgroundFileAdapter._(super.db, super.file);

@override
Future<void> deleteAll() {
return file.delete();
/// Builds [SqliteBackgroundFileAdapter].
///
/// * [builder]: Database builder
/// * [file]: The [File] that store the Sqlite database
/// * [logStatements]: Generated sql statements will be printed before executing
/// * [setup]: Function that can be used to perform a setup just after the database is opened
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
/// * [isolateSetup]: function that can perform setup work on the isolate before opening the database.
static Future<SqliteAdapter<I, E>> build<I extends Info, E extends Entry<I>>(
SqliteBuilder<I, E> builder, File file,
{bool? logStatements,
DatabaseSetup? setup,
bool? cachePreparedStatements,
FutureOr<void> Function()? isolateSetup}) {
return Future.value(SqliteBackgroundFileAdapter._(
builder(NativeDatabase.createInBackground(file,
logStatements: logStatements ?? false,
setup: setup,
cachePreparedStatements: cachePreparedStatements ?? false)),
file));
}
}
102 changes: 90 additions & 12 deletions packages/stash_sqlite/lib/stash_sqlite.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Provides a Sqlite implementation of the Stash caching API for Dart
library stash_sqlite;

import 'dart:async';
import 'dart:io';

import 'package:drift/drift.dart';
Expand All @@ -19,12 +20,17 @@ export 'src/sqlite/sqlite_store.dart';
/// * [codec]: The [StoreCodec] used to convert to/from a Map<String, dynamic>` representation to a binary representation
/// * [logStatements]: If [logStatements] is true (defaults to `false`), generated sql statements will be printed before executing
/// * [databaseSetup]: This optional function can be used to perform a setup just after the database is opened, before drift is fully ready
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
Future<SqliteVaultStore> newSqliteMemoryVaultStore(
{StoreCodec? codec, bool? logStatements, DatabaseSetup? databaseSetup}) {
{StoreCodec? codec,
bool? logStatements,
DatabaseSetup? databaseSetup,
bool? cachePreparedStatements}) {
return SqliteMemoryAdapter.build<VaultInfo, VaultEntry>(
(QueryExecutor executor) => VaultDatabase(executor),
logStatements: logStatements,
setup: databaseSetup)
setup: databaseSetup,
cachePreparedStatements: cachePreparedStatements)
.then((adapter) => SqliteVaultStore(adapter, codec: codec));
}

Expand All @@ -33,49 +39,121 @@ Future<SqliteVaultStore> newSqliteMemoryVaultStore(
/// * [codec]: The [StoreCodec] used to convert to/from a Map<String, dynamic>` representation to a binary representation
/// * [logStatements]: If [logStatements] is true (defaults to `false`), generated sql statements will be printed before executing
/// * [databaseSetup]: This optional function can be used to perform a setup just after the database is opened, before drift is fully ready
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
Future<SqliteCacheStore> newSqliteMemoryCacheStore(
{StoreCodec? codec, bool? logStatements, DatabaseSetup? databaseSetup}) {
{StoreCodec? codec,
bool? logStatements,
DatabaseSetup? databaseSetup,
bool? cachePreparedStatements}) {
return SqliteMemoryAdapter.build<CacheInfo, CacheEntry>(
(QueryExecutor executor) => CacheDatabase(executor),
logStatements: logStatements,
setup: databaseSetup)
setup: databaseSetup,
cachePreparedStatements: cachePreparedStatements)
.then((adapter) => SqliteCacheStore(adapter, codec: codec));
}

/// Creates a new [SqliteVaultStore] on a file
/// Provides the default vault file
///
/// * [file]: An optional file
File _defaultVaultFile(File? file) =>
file ?? File('${Directory.systemTemp.path}/vault.db');

/// Creates a file based [SqliteVaultStore]
///
/// * [file]: The path to the database file
/// * [codec]: The [StoreCodec] used to convert to/from a Map<String, dynamic>` representation to a binary representation
/// * [logStatements]: If [logStatements] is true (defaults to `false`), generated sql statements will be printed before executing
/// * [databaseSetup]: This optional function can be used to perform a setup just after the database is opened, before drift is fully ready
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
Future<SqliteVaultStore> newSqliteLocalVaultStore(
{File? file,
StoreCodec? codec,
bool? logStatements,
DatabaseSetup? databaseSetup}) {
DatabaseSetup? databaseSetup,
bool? cachePreparedStatements}) {
return SqliteFileAdapter.build<VaultInfo, VaultEntry>(
(QueryExecutor executor) => VaultDatabase(executor),
file ?? File('${Directory.systemTemp.path}/vault.db'),
_defaultVaultFile(file),
logStatements: logStatements,
setup: databaseSetup,
cachePreparedStatements: cachePreparedStatements)
.then((adapter) => SqliteVaultStore(adapter, codec: codec));
}

/// Creates a file based [SqliteVaultStore] on a background isolate
///
/// * [file]: The path to the database file
/// * [codec]: The [StoreCodec] used to convert to/from a Map<String, dynamic>` representation to a binary representation
/// * [logStatements]: If [logStatements] is true (defaults to `false`), generated sql statements will be printed before executing
/// * [databaseSetup]: This optional function can be used to perform a setup just after the database is opened, before drift is fully ready
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
/// * [isolateSetup]: function that can perform setup work on the isolate before opening the database.
Future<SqliteVaultStore> newSqliteBackgroundVaultStore(
{File? file,
StoreCodec? codec,
bool? logStatements,
DatabaseSetup? databaseSetup,
bool? cachePreparedStatements,
FutureOr<void> Function()? isolateSetup}) {
return SqliteBackgroundFileAdapter.build<VaultInfo, VaultEntry>(
(QueryExecutor executor) => VaultDatabase(executor),
_defaultVaultFile(file),
logStatements: logStatements,
setup: databaseSetup)
setup: databaseSetup,
cachePreparedStatements: cachePreparedStatements,
isolateSetup: isolateSetup)
.then((adapter) => SqliteVaultStore(adapter, codec: codec));
}

/// Creates a new [SqliteCacheStore] on a file
/// Provides the default cache file
///
/// * [file]: An optional file
File _defaultCacheFile(File? file) =>
file ?? File('${Directory.systemTemp.path}/cache.db');

/// Creates a file based [SqliteCacheStore]
///
/// * [file]: The path to the database file
/// * [codec]: The [StoreCodec] used to convert to/from a Map<String, dynamic>` representation to a binary representation
/// * [logStatements]: If [logStatements] is true (defaults to `false`), generated sql statements will be printed before executing
/// * [databaseSetup]: This optional function can be used to perform a setup just after the database is opened, before drift is fully ready
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
Future<SqliteCacheStore> newSqliteLocalCacheStore(
{File? file,
StoreCodec? codec,
bool? logStatements,
DatabaseSetup? databaseSetup}) {
DatabaseSetup? databaseSetup,
bool? cachePreparedStatements}) {
return SqliteFileAdapter.build<CacheInfo, CacheEntry>(
(QueryExecutor executor) => CacheDatabase(executor),
file ?? File('${Directory.systemTemp.path}/cache.db'),
_defaultCacheFile(file),
logStatements: logStatements,
setup: databaseSetup,
cachePreparedStatements: cachePreparedStatements)
.then((adapter) => SqliteCacheStore(adapter, codec: codec));
}

/// Creates a file based [SqliteCacheStore] on a background isolate
///
/// * [file]: The path to the database file
/// * [codec]: The [StoreCodec] used to convert to/from a Map<String, dynamic>` representation to a binary representation
/// * [logStatements]: If [logStatements] is true (defaults to `false`), generated sql statements will be printed before executing
/// * [databaseSetup]: This optional function can be used to perform a setup just after the database is opened, before drift is fully ready
/// * [cachePreparedStatements]: controls whether drift will cache prepared statement objects
/// * [isolateSetup]: function that can perform setup work on the isolate before opening the database.
Future<SqliteCacheStore> newSqliteBackgroundCacheStore(
{File? file,
StoreCodec? codec,
bool? logStatements,
DatabaseSetup? databaseSetup,
bool? cachePreparedStatements,
FutureOr<void> Function()? isolateSetup}) {
return SqliteBackgroundFileAdapter.build<CacheInfo, CacheEntry>(
(QueryExecutor executor) => CacheDatabase(executor),
_defaultCacheFile(file),
logStatements: logStatements,
setup: databaseSetup)
setup: databaseSetup,
cachePreparedStatements: cachePreparedStatements)
.then((adapter) => SqliteCacheStore(adapter, codec: codec));
}

0 comments on commit f5685f3

Please sign in to comment.