Commit 6a35813d authored by kevin's avatar kevin

Merge branch 'refactor_add_sql_interface' into 'master'

refactor and add sql database interface

See merge request !9
parents c42ed8ee 0ad6c764
Pipeline #37753382 (#34) passed with stage
in 1 minute and 46 seconds
image: registry.gitlab.com/santetis/santetis_image:latest
image: registry.gitlab.com/santetis/open_source/santetis_image:latest
stages:
- analyze
......
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
exclude:
- '**/*.g.dart'
- .pub/**
- .dart_tool/**
- lib/**/*.jser.dart
- build/**
errors:
dead_code: warning
unused_element: warning
unused_import: warning
unused_local_variable: warning
uri_has_not_been_generated: ignore
linter:
rules:
- always_declare_return_types
# - always_put_control_body_on_new_line
- always_put_required_named_parameters_first
- always_require_non_null_named_parameters
# - always_specify_types
- annotate_overrides
- avoid_annotating_with_dynamic
# - avoid_as
# - avoid_catches_without_on_clauses
- avoid_catching_errors
- avoid_classes_with_only_static_members
- avoid_empty_else
- avoid_function_literals_in_foreach_calls
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_positional_boolean_parameters
- avoid_return_types_on_setters
# - avoid_returning_null
- avoid_returning_this
# - avoid_setters_without_getters
- avoid_slow_async_io
- avoid_types_on_closure_parameters
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
- cancel_subscriptions
- cascade_invocations
- close_sinks
- comment_references
- constant_identifier_names
- control_flow_in_finally
# - directives_ordering
- empty_catches
- empty_constructor_bodies
- empty_statements
- hash_and_equals
- implementation_imports
- invariant_booleans
- iterable_contains_unrelated_type
- join_return_with_assignment
- library_names
- library_prefixes
- list_remove_unrelated_type
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- no_duplicate_case_values
- hash_and_equals
- non_constant_identifier_names
- omit_local_variable_types
- one_member_abstracts
# - only_throw_errors
- overridden_fields
- package_api_docs
- package_names
- package_prefixed_library_names
- parameter_assignments
- prefer_adjacent_string_concatenation
- prefer_asserts_in_initializer_lists
- prefer_bool_in_asserts
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_const_constructors
- prefer_const_constructors_in_immutables
- prefer_const_declarations
- prefer_const_literals_to_create_immutables
- prefer_constructors_over_static_methods
- prefer_contains
- prefer_expression_function_bodies
- prefer_bool_in_asserts
- prefer_final_fields
- prefer_final_locals
- prefer_foreach
- prefer_function_declarations_over_variables
- prefer_initializing_formals
- prefer_interpolation_to_compose_strings
- prefer_is_empty
- prefer_is_not_empty
- prefer_single_quotes
- prefer_typing_uninitialized_variables
# - public_member_api_docs
- recursive_getters
- slash_for_doc_comments
- sort_constructors_first
- sort_unnamed_constructors_first
- super_goes_last
- test_types_in_equals
- throw_in_finally
# - type_annotate_public_apis
- type_init_formals
# - unawaited_futures
- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_null_aware_assignments
- unnecessary_null_in_if_null_operators
- unnecessary_overrides
- unnecessary_statements
- unnecessary_this
- unrelated_type_equality_checks
\ No newline at end of file
- use_rethrow_when_possible
- use_setters_to_change_properties
- use_string_buffers
- use_to_and_as_if_applicable
- valid_regexps
\ No newline at end of file
export 'src/common.dart';
export 'src/no_sql_interfaces.dart';
export 'src/sql_interface.dart';
import 'package:conn_pool/conn_pool.dart';
import 'package:quiver/core.dart';
class DatabaseException implements Exception {
......@@ -28,3 +29,43 @@ class DatabaseId {
@override
String toString() => 'DatabaseIdInterface("$id")';
}
/// Is an interface that all no sql database need to implement
abstract class DatabaseInterface<T> {
bool get isOpen;
Future<void> close();
T collection(String name);
Future<void> open();
}
/// A function that return a compatible no sql database
typedef DatabaseInterface<T> FactoryDatabase<T>();
class DatabaseManager<T extends DatabaseInterface>
implements ConnectionManager<T> {
DatabaseManager(this.factoryDatabase);
final FactoryDatabase factoryDatabase;
@override
Future<void> close(T connection) => connection.close();
@override
Future<T> open() async {
final database = factoryDatabase();
await database.open();
return database;
}
}
/// A generic database pool for no sql database
class DatabasePool<T extends DatabaseInterface> {
DatabasePool.fromManager(DatabaseManager<T> manager,
{int minPoolSize = 10, int maxPoolSize = 10})
: pool = SharedPool(manager, minSize: minPoolSize, maxSize: maxPoolSize);
final Pool<T> pool;
}
import 'dart:async';
import 'package:conn_pool/conn_pool.dart';
/// A function that return a compatible no sql database
typedef NoSqlDatabaseInterface FactoryDatabase();
/// Is an interface that all collection need to implement
abstract class CollectionInterface {
/// Allow you to delete an entry in the current collection
......@@ -37,30 +32,3 @@ abstract class NoSqlDatabaseInterface {
Future<void> open();
}
/// A generic no sql database manager
class NoSqlDatabaseManager
implements ConnectionManager<NoSqlDatabaseInterface> {
final FactoryDatabase factoryDatabase;
NoSqlDatabaseManager(this.factoryDatabase);
@override
Future<void> close(NoSqlDatabaseInterface connection) => connection.close();
@override
Future<NoSqlDatabaseInterface> open() async {
final database = factoryDatabase();
await database.open();
return database;
}
}
/// A generic database pool for no sql database
class NoSqlDatabasePool {
final Pool<NoSqlDatabaseInterface> pool;
NoSqlDatabasePool.fromManager(NoSqlDatabaseManager manager,
{int minPoolSize: 10, int maxPoolSize: 10})
: pool = SharedPool(manager, minSize: minPoolSize, maxSize: maxPoolSize);
}
import 'dart:async';
/// Is an interface that all collection need to implement
abstract class TableInterface {
Future<dynamic> query(String query);
}
/// Is an interface that all no sql database need to implement
abstract class SqlDatabaseInterface {
bool get isOpen;
Future<void> close();
TableInterface table(String name);
Future<void> open();
}
......@@ -10,6 +10,7 @@ environment:
dependencies:
quiver: ^2.0.0+1
conn_pool: ^2.1.8
pedantic: ^1.3.0
dev_dependencies:
test: ^1.0.0
import 'no_sql_database_interface_test.dart' as no_sql_database_interface;
import 'common_test.dart' as common_test;
import 'no_sql_database_interface_test.dart' as no_sql_database_interface_test;
import 'sql_database_interface_test.dart' as sql_database_interface_test;
void main() {
no_sql_database_interface.main();
common_test.main();
no_sql_database_interface_test.main();
sql_database_interface_test.main();
}
import 'package:database_interface/database_interface.dart';
import 'package:quiver/core.dart';
import 'package:test/test.dart';
void main() {
group('DatabaseIdInterface', () {
test('hashcode', () {
final id = DatabaseId('12');
expect(id.hashCode, hashObjects(<String>['12']));
});
test('toString', () {
final id = DatabaseId('12');
expect(id.toString(), 'DatabaseIdInterface("12")');
});
test('== with a DatabaseIdInterface', () {
final id1 = DatabaseId('12');
final id2 = DatabaseId('12');
expect(id1 == id2, isTrue);
});
test('== with a String', () {
final id1 = DatabaseId('12');
const Object id2 = '12';
expect(id1 == id2, isTrue);
});
});
group('DatabaseException', () {
test('message', () {
final exception = DatabaseException('Error');
expect(exception.message, 'Error');
});
});
}
import 'package:database_interface/database_interface.dart';
import 'package:quiver/core.dart';
import 'package:test/test.dart';
import 'src/fake_no_sql_database.dart';
void main() {
group('DatabaseIdInterface', () {
test('hashcode', () {
final id = DatabaseId('12');
expect(id.hashCode, hashObjects(<String>['12']));
});
test('toString', () {
final id = DatabaseId('12');
expect(id.toString(), 'DatabaseIdInterface("12")');
});
test('== with a DatabaseIdInterface', () {
final id1 = DatabaseId('12');
final id2 = DatabaseId('12');
expect(id1 == id2, isTrue);
});
test('== with a String', () {
final id1 = DatabaseId('12');
const Object id2 = '12';
expect(id1 == id2, isTrue);
});
});
group('DatabaseException', () {
test('message', () {
final exception = DatabaseException('Error');
expect(exception.message, 'Error');
});
});
group('no sql manager', () {
NoSqlDatabaseInterface fakeNoSqlDatabaseFactory() => FakeNoSqlDatabase();
FakeNoSqlDatabase fakeNoSqlDatabaseFactory() => FakeNoSqlDatabase();
test('open', () async {
final manager = NoSqlDatabaseManager(fakeNoSqlDatabaseFactory);
final manager = DatabaseManager(fakeNoSqlDatabaseFactory);
final db = await manager.open();
expect(db is NoSqlDatabaseInterface, isTrue);
expect(db is FakeNoSqlDatabase, isTrue);
expect(db.isOpen, isTrue);
});
test('close', () async {
final manager = NoSqlDatabaseManager(fakeNoSqlDatabaseFactory);
final manager = DatabaseManager(fakeNoSqlDatabaseFactory);
final db = await manager.open();
await manager.close(db);
expect(db.isOpen, isFalse);
......@@ -55,11 +23,11 @@ void main() {
});
group('no sql pool', () {
NoSqlDatabaseInterface fakeNoSqlDatabaseFactory() => FakeNoSqlDatabase();
FakeNoSqlDatabase fakeNoSqlDatabaseFactory() => FakeNoSqlDatabase();
test('open', () async {
final manager = NoSqlDatabaseManager(fakeNoSqlDatabaseFactory);
final pool = NoSqlDatabasePool.fromManager(manager);
final manager = DatabaseManager(fakeNoSqlDatabaseFactory);
final pool = DatabasePool.fromManager(manager);
expect(pool.pool, isNotNull);
final connection = await pool.pool.get();
expect(connection.connection.isOpen, isTrue);
......
import 'package:database_interface/database_interface.dart';
import 'package:test/test.dart';
import 'src/fake_sql_database.dart';
void main() {
group('sql manager', () {
FakeSqlDatabase fakeSqlDatabaseFactory() => FakeSqlDatabase();
test('open', () async {
final manager = DatabaseManager(fakeSqlDatabaseFactory);
final db = await manager.open();
expect(db is FakeSqlDatabase, isTrue);
expect(db.isOpen, isTrue);
});
test('close', () async {
final manager = DatabaseManager(fakeSqlDatabaseFactory);
final db = await manager.open();
await manager.close(db);
expect(db.isOpen, isFalse);
});
});
group('sql pool', () {
FakeSqlDatabase fakeSqlDatabaseFactory() => FakeSqlDatabase();
test('open', () async {
final manager = DatabaseManager(fakeSqlDatabaseFactory);
final pool = DatabasePool.fromManager(manager);
expect(pool.pool, isNotNull);
final connection = await pool.pool.get();
expect(connection.connection.isOpen, isTrue);
await pool.pool.manager.close(connection.connection);
expect(connection.connection.isOpen, isFalse);
});
});
}
......@@ -25,7 +25,7 @@ class FakeCollection implements CollectionInterface {
Map<String, dynamic> selector, Map<String, dynamic> document) async {}
}
class FakeNoSqlDatabase implements NoSqlDatabaseInterface {
class FakeNoSqlDatabase implements DatabaseInterface<CollectionInterface> {
bool _isOpen = false;
@override
......
import 'dart:async';
import 'package:database_interface/database_interface.dart';
class FakeTable implements TableInterface {
@override
Future<dynamic> query(String query) => null;
}
class FakeSqlDatabase implements DatabaseInterface<TableInterface> {
bool _isOpen = false;
@override
bool get isOpen => _isOpen;
@override
Future<void> close() async {
_isOpen = false;
}
@override
TableInterface collection(String name) => FakeTable();
@override
Future<void> open() async {
_isOpen = true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment