Skip to content

Commit

Permalink
[sqflite_ffi_async] get more pub points
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Dec 17, 2023
1 parent 5794cd0 commit 9406831
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/sqflite_common_ffi_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.1.1
## 0.1.1+1

- Allow concurrent read.

Expand Down
124 changes: 101 additions & 23 deletions packages/sqflite_common_ffi_async/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,108 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
include: package:lints/recommended.yaml

# Until there are meta linter rules, each desired lint must be explicitly enabled.
# See: https://github.com/dart-lang/linter/issues/288
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
# For a list of lints, see: http://dart-lang.github.io/linter/lints/
# See the configuration guide for more
# https://github.com/dart-lang/sdk/tree/master/pkg/analyzer#configuring-the-analyzer
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml
# NOTE: Please keep this file in sync with
# https://github.com/flutter/flutter/blob/master/analysis_options.yaml

# Uncomment the following section to specify additional rules.
analyzer:
# Strong mode is sometimes harder to keep
language:
strict-casts: true
strict-inference: true

# linter:
# rules:
# - camel_case_types
errors:
# treat missing required parameters as a warning (not a hint)
missing_required_param: warning
# treat missing returns as a warning (not a hint)
missing_return: warning
# allow having TODOs in the code
todo: ignore
# Ignore errors like
# 'super_goes_last' is a deprecated lint rule and should not be used • included_file_warning
included_file_warning: ignore

# analyzer:
# exclude:
# - path/to/excluded/files/**
linter:
rules:
- always_declare_return_types
- avoid_dynamic_calls
- avoid_empty_else
- avoid_relative_lib_imports
- avoid_shadowing_type_parameters
- avoid_slow_async_io
- avoid_types_as_parameter_names
- await_only_futures
- camel_case_extensions
- camel_case_types
- cancel_subscriptions
- curly_braces_in_flow_control_structures
# - depend_on_referenced_packages
- directives_ordering
- empty_catches
- hash_and_equals
- collection_methods_unrelated_type
- no_adjacent_strings_in_list
- no_duplicate_case_values
- non_constant_identifier_names
- omit_local_variable_types
- package_api_docs
- package_prefixed_library_names
- prefer_generic_function_type_aliases
- prefer_is_empty
- prefer_is_not_empty
- prefer_iterable_whereType
- prefer_single_quotes
- prefer_typing_uninitialized_variables
- sort_child_properties_last
- test_types_in_equals
- throw_in_finally
- unawaited_futures
- unnecessary_null_aware_assignments
- unnecessary_statements
- unrelated_type_equality_checks
- unsafe_html
- use_full_hex_values_for_flutter_colors
- valid_regexps

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
- constant_identifier_names
- control_flow_in_finally
- empty_statements
- implementation_imports
- overridden_fields
- package_names
- prefer_const_constructors
- prefer_initializing_formals
- prefer_void_to_null
#
- annotate_overrides
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_return_types_on_setters
- empty_constructor_bodies
- library_names
- library_prefixes
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_contains
- slash_for_doc_comments
- type_init_formals
- unnecessary_const
- unnecessary_new
- unnecessary_null_in_if_null_operators
- use_rethrow_when_possible

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
- dangling_library_doc_comments
- deprecated_member_use_from_same_package
- implicit_reopen
- invalid_case_patterns
- no_literal_bool_comparisons
- no_self_assignments
- no_wildcard_variable_uses
- type_literal_in_constant_pattern
# === doc rules ===
- public_member_api_docs
23 changes: 23 additions & 0 deletions packages/sqflite_common_ffi_async/example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite_common_ffi_async/sqflite_ffi_async.dart';

Future main() async {
// Init ffi loader if needed.
sqfliteFfiInit();

var databaseFactory = databaseFactoryFfiAsync;
var db = await databaseFactory.openDatabase('example.db');
await db.execute('''
CREATE TABLE Product (
id INTEGER PRIMARY KEY,
title TEXT
)
''');
await db.insert('Product', <String, Object?>{'title': 'Product 1'});
await db.insert('Product', <String, Object?>{'title': 'Product 1'});

var result = await db.query('Product');
print(result);
// prints [{id: 1, title: Product 1}, {id: 2, title: Product 1}]
await db.close();
}
3 changes: 2 additions & 1 deletion packages/sqflite_common_ffi_async/lib/sqflite_ffi_async.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// sqflite on top of sqlite_async
library;

export 'package:sqflite_common/sqflite.dart';

export 'src/platform.dart'
show databaseFactoryFfiAsync, databaseFactoryFfiAsyncTest;
export 'package:sqflite_common/sqflite.dart';
14 changes: 7 additions & 7 deletions packages/sqflite_common_ffi_async/lib/src/import.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// ignore_for_file: depend_on_referenced_packages

export 'package:sqflite_common/src/batch.dart';
export 'package:sqflite_common/src/constant.dart';
export 'package:sqflite_common/src/cursor.dart';
export 'package:sqflite_common/src/database.dart';
export 'package:sqflite_common/src/database_mixin.dart';
export 'package:sqflite_common/src/factory_mixin.dart';
export 'package:sqflite_common/src/dev_utils.dart';
export 'package:sqflite_common/src/exception.dart';
export 'package:sqflite_common/src/constant.dart';
export 'package:sqflite_common/src/factory_mixin.dart';
export 'package:sqflite_common/src/internals.dart';
export 'package:sqflite_common/src/sql_command.dart';
export 'package:sqflite_common/src/transaction.dart';
export 'package:sqflite_common_ffi/src/mixin/handler_mixin.dart';
export 'package:sqflite_common/src/dev_utils.dart';
export 'package:sqflite_common/src/batch.dart';
export 'package:sqflite_common/src/cursor.dart';
export 'package:sqflite_common/src/sql_command.dart';
export 'package:sqflite_common/src/internals.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:collection';
import 'dart:io';

import 'package:path/path.dart';

import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite_common_ffi_async/src/sqflite_ffi_async_factory.dart';
import 'package:sqflite_common_ffi_async/src/sqflite_ffi_async_transaction.dart';
Expand All @@ -11,12 +10,14 @@ import 'package:sqlite_async/sqlite_async.dart' as sqlite_async;

import 'import.dart';

class SqfliteDatabaseAsync extends SqfliteDatabaseBase {
/// Ffi async database implementation.
class SqfliteDatabaseFfiAsync extends SqfliteDatabaseBase {
static int _id = 0;
late final asyncId = ++_id;
late final _asyncId = ++_id;
late sqlite_async.SqliteDatabase _database;

SqfliteDatabaseAsync(super.openHelper, super.path);
/// Ffi async database implementation.
SqfliteDatabaseFfiAsync(super.openHelper, super.path);

@override
Future<T> transaction<T>(Future<T> Function(Transaction txn) action,
Expand Down Expand Up @@ -55,18 +56,18 @@ class SqfliteDatabaseAsync extends SqfliteDatabaseBase {
@override
Future<int> openDatabase() async {
sqlite_async.SqliteOptions sqliteOptions;
int maxReaders = sqlite_async.SqliteDatabase.defaultMaxReaders;
var maxReaders = sqlite_async.SqliteDatabase.defaultMaxReaders;
var path = this.path;
if (path == inMemoryDatabasePath) {
var dir = await Directory.systemTemp.createTemp();
path = this.path = join(dir.path, 'in_memory_$asyncId.db');
path = this.path = join(dir.path, 'in_memory_$_asyncId.db');
}
if (options?.readOnly ?? false) {
if (!await factoryFfi.databaseExists(path)) {
throw SqfliteDatabaseException(
'read-only Database not found: $path', null);
}
sqliteOptions = sqlite_async.SqliteOptions(
sqliteOptions = const sqlite_async.SqliteOptions(
journalMode: null, journalSizeLimit: null, synchronous: null);
} else {
var dir = Directory(dirname(path));
Expand All @@ -78,15 +79,15 @@ class SqfliteDatabaseAsync extends SqfliteDatabaseBase {
} catch (e) {
print('error checking directory $dir: $e');
}
sqliteOptions = sqlite_async.SqliteOptions.defaults();
sqliteOptions = const sqlite_async.SqliteOptions.defaults();
}
final factory = sqlite_async.DefaultSqliteOpenFactory(
path: path, sqliteOptions: sqliteOptions);

_database = sqlite_async.SqliteDatabase.withFactory(factory,
maxReaders: maxReaders);
//_database = sqlite_async.SqliteDatabase(path: path);
return asyncId;
return _asyncId;
}

Future<List<Map<String, Object?>>> _select(sqlite_async.SqliteReadContext wc,
Expand Down Expand Up @@ -286,12 +287,15 @@ class SqfliteDatabaseAsync extends SqfliteDatabaseBase {
}
}

/// Result set implementation.
class SqfliteResultSet extends ListBase<Map<String, Object?>> {
/// Result set data.
final sqlite3.ResultSet resultSet;

@override
int get length => resultSet.length;

/// Result set implementation.
SqfliteResultSet(this.resultSet);

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import 'dart:typed_data';
import 'dart:io' as io;
import 'dart:typed_data';

import 'package:sqflite_common_ffi/sqflite_ffi.dart';

import 'import.dart';
import 'sqflite_ffi_async_database.dart';

/// The Ffi database factory, to use when needed.
var factoryFfi = databaseFactoryFfi; //.debugQuickLoggerWrapper();

/// The Ffi database factory.
var databaseFactoryFfiAsyncImpl =
SqfliteDatabaseFactoryFfiAsync(tag: 'ffi_async');

/// The Ffi database factory for tests.
var databaseFactoryFfiAsyncTestImpl =
SqfliteDatabaseFactoryFfiAsync(tag: 'ffi_async_test');

/// The Ffi async database factory.
class SqfliteDatabaseFactoryFfiAsync with SqfliteDatabaseFactoryMixin {
/// The Ffi async database factory.
SqfliteDatabaseFactoryFfiAsync({String? tag}) {
this.tag = tag;
}
Expand Down Expand Up @@ -41,7 +47,7 @@ class SqfliteDatabaseFactoryFfiAsync with SqfliteDatabaseFactoryMixin {
@override
SqfliteDatabase newDatabase(
SqfliteDatabaseOpenHelper openHelper, String path) {
return SqfliteDatabaseAsync(openHelper, path);
return SqfliteDatabaseFfiAsync(openHelper, path);
}

/// Invoke delete database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import 'package:sqflite_common_ffi_async/src/import.dart';
import 'package:sqlite_async/sqlite_async.dart' as sqlite_async;

/// Write transaction.
class SqfliteFfiAsyncTransaction extends SqfliteTransaction {
/// Write context.
final sqlite_async.SqliteWriteContext writeContext;

/// Write transaction.
SqfliteFfiAsyncTransaction(super.database, this.writeContext);
}

/// Read transaction.
class SqfliteFfiAsyncReadTransaction extends SqfliteTransaction {
/// Read context.
final sqlite_async.SqliteReadContext readContext;

/// Read transaction.
SqfliteFfiAsyncReadTransaction(super.database, this.readContext);
}
4 changes: 2 additions & 2 deletions packages/sqflite_common_ffi_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: sqflite_common_ffi_async
homepage: https://github.com/tekartik/sqflite/tree/master/packages/sqflite_common_ffi_async
description: Sqlite API on top on sqlite_async
version: 0.1.1
description: Sqflite API on top on sqlite_async from PowerSync, ffi based implementation.
version: 0.1.1+1

environment:
sdk: ^3.2.0
Expand Down

0 comments on commit 9406831

Please sign in to comment.