Skip to content

Commit

Permalink
Merge pull request #19 from a-givertzman/fixes-&-SchemaEntry-test
Browse files Browse the repository at this point in the history
fixes + SchemaEntry test
  • Loading branch information
a-givertzman authored Dec 30, 2023
2 parents 7bef023 + 0496208 commit 23ef8c1
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 126 deletions.
1 change: 1 addition & 0 deletions lib/ext_rw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export 'src/table_schema/field.dart';
export 'src/table_schema/relation.dart';
export 'src/table_schema/relation_schema.dart';
export 'src/table_schema/schema_entry.dart';
export 'src/table_schema/schema_entry_abstract.dart';
export 'src/table_schema/schema.dart';
export 'src/table_schema/table_schema.dart';
export 'src/table_schema/table_schema_abstract.dart';
Expand Down
39 changes: 18 additions & 21 deletions lib/src/api_client/access/sql_read.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_log.dart';
import 'package:hmi_core/hmi_core_result_new.dart';

class SqlRead<T extends SchemaEntry, P> implements SchemaRead<T, P> {
class SqlRead<T extends SchemaEntryAbstract, P> implements SchemaRead<T, P> {
late final Log _log;
final ApiAddress _address;
final String _authToken;
final String _database;
final bool _keepAlive;
final bool _debug;
final SqlBuilder<P> _sqlBuilder;
final Map<T, Function> _entryFromFactories;
final SqlBuilder<P?> _sqlBuilder;
final T Function(Map<String, dynamic> row) _entryBuilder;
Sql _sql = Sql(sql: '');
///
///
Expand All @@ -21,22 +21,22 @@ class SqlRead<T extends SchemaEntry, P> implements SchemaRead<T, P> {
required String database,
bool keepAlive = false,
bool debug = false,
required SqlBuilder<P> sqlBuilder,
required Map<T, Function> entryFromFactories,
required SqlBuilder<P?> sqlBuilder,
required T Function(Map<String, dynamic> row) entryBuilder,
}) :
_address = address,
_authToken = authToken,
_database = database,
_keepAlive = keepAlive,
_debug = debug,
_sqlBuilder = sqlBuilder,
_entryFromFactories = entryFromFactories {
_entryBuilder = entryBuilder {
_log = Log("$runtimeType");
}
//
//
@override
Future<Result<List<T>, Failure>> fetch(P params) {
Future<Result<List<T>, Failure>> fetch(P? params) {
_sql = _sqlBuilder(_sql, params);
return _fetch(_sql);
}
Expand All @@ -57,32 +57,29 @@ class SqlRead<T extends SchemaEntry, P> implements SchemaRead<T, P> {
return request.fetch().then((result) {
return switch (result) {
Ok(value :final reply) => () {
_log.debug("._fetch | reply: $reply");
if (reply.hasError) {
return Err<List<T>, Failure>(Failure(message: reply.error.message, stackTrace: StackTrace.current));
} else {
final List<T> entries = [];
final rows = reply.data;
final rowsLength = rows.length;
_log.debug("._fetch | reply rows ($rowsLength): $rows");
for (final row in rows) {
final entry = _makeEntry(row);
_log.debug("._fetch | row: $row");
final entry = _entryBuilder(row);
_log.debug("._fetch | entry: $entry");
entries.add(entry);
}
_log.debug("._fetch | entries: $entries");
return Ok<List<T>, Failure>(entries);
}
}(),
Err(:final error) => Err<List<T>, Failure>(error),
Err(:final error) => () {
_log.debug("._fetch | error: $error");
return Err<List<T>, Failure>(error);
}(),
};
});
}
///
///
T _makeEntry(Map<String, dynamic> row) {
final constructor = _entryFromFactories[T];
if (constructor != null) {
return constructor(row);
}
throw Failure(
message: "$runtimeType._makeEntry | Can't find constructor for $T",
stackTrace: StackTrace.current,
);
}
}
86 changes: 43 additions & 43 deletions lib/src/api_client/access/sql_write.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_log.dart';
import 'package:hmi_core/hmi_core_result_new.dart';

class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
class SqlWrite<T extends SchemaEntryAbstract> implements SchemaWrite<T> {
late final Log _log;
final ApiAddress _address;
final String _authToken;
Expand All @@ -13,8 +13,8 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
final SqlBuilder<T>? _insertSqlBuilder;
final SqlBuilder<T>? _updateSqlBuilder;
final SqlBuilder<T>? _deleteSqlBuilder;
final Map<T, Function> _entryFromFactories;
final Map<T, Function> _entryEmptyFactories;
// final T Function(Map<String, dynamic> row) _entryFromFactories;
final T Function() _emptyEntryBuilder;
///
///
SqlWrite({
Expand All @@ -26,8 +26,8 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
SqlBuilder<T>? insertSqlBuilder,
SqlBuilder<T>? updateSqlBuilder,
SqlBuilder<T>? deleteSqlBuilder,
required Map<T, Function> entryFromFactories,
required Map<T, Function> entryEmptyFactories,
// required T Function(Map<String, dynamic> row) entryFromFactories,
required T Function() emptyEntryBuilder,
}) :
_address = address,
_authToken = authToken,
Expand All @@ -37,8 +37,8 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
_insertSqlBuilder = insertSqlBuilder,
_updateSqlBuilder = updateSqlBuilder,
_deleteSqlBuilder = deleteSqlBuilder,
_entryFromFactories = entryFromFactories,
_entryEmptyFactories = entryEmptyFactories {
// _entryFromFactories = entryFromFactories,
_emptyEntryBuilder = emptyEntryBuilder {
_log = Log("$runtimeType");
}
//
Expand All @@ -49,7 +49,7 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
if (entry != null) {
entry_ = entry;
} else {
entry_ = _makeEmptyEntry();
entry_ = _emptyEntryBuilder();
}
final builder = _insertSqlBuilder;
if (builder != null) {
Expand Down Expand Up @@ -127,7 +127,7 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
}
///
/// Fetchs data with new [sql]
Future<Result<List<T>, Failure>> _fetchWith(Sql sql) {
Future<Result<void, Failure>> _fetchWith(Sql sql) {
final request = ApiRequest(
address: _address,
authToken: _authToken,
Expand All @@ -144,44 +144,44 @@ class SqlWrite<T extends SchemaEntry> implements SchemaWrite<T> {
Ok(:final value) => () {
final reply = value;
if (reply.hasError) {
return Err<List<T>, Failure>(Failure(message: reply.error.message, stackTrace: StackTrace.current));
return Err<void, Failure>(Failure(message: reply.error.message, stackTrace: StackTrace.current));
} else {
final List<T> entries = [];
final rows = reply.data;
for (final row in rows) {
final entry = _makeEntry(row);
entries.add(entry);
}
return Ok<List<T>, Failure>(entries);
// final List<T> entries = [];
// final rows = reply.data;
// for (final row in rows) {
// final entry = _makeEntry(row);
// entries.add(entry);
// }
return const Ok<void, Failure>(null);
}
}(),
Err(:final error) => Err<List<T>, Failure>(error),
Err(:final error) => Err<void, Failure>(error),
};
});
}
///
///
T _makeEmptyEntry() {
final constructor = _entryEmptyFactories[T];
if (constructor != null) {
return constructor();
} else {
throw Failure(
message: "$runtimeType._makeEntry | Can't find constructor for $T",
stackTrace: StackTrace.current,
);
}
}
///
///
T _makeEntry(Map<String, dynamic> row) {
final constructor = _entryFromFactories[T];
if (constructor != null) {
return constructor(row);
}
throw Failure(
message: "$runtimeType._makeEntry | Can't find constructor for $T",
stackTrace: StackTrace.current,
);
}
// ///
// ///
// T _makeEmptyEntry() {
// // final constructor = _entryEmptyFactories[T];
// return _entryEmptyFactories();
// // if (constructor != null) {
// // } else {
// // throw Failure(
// // message: "$runtimeType._makeEntry | Can't find constructor for $T",
// // stackTrace: StackTrace.current,
// // );
// // }
// }
// ///
// ///
// T _makeEntry(Map<String, dynamic> row) {
// return _entryFromFactories(row);
// // final constructor = _entryFromFactories[T];
// // if (_entryFromFactories != null) {
// // }
// // throw Failure(
// // message: "$runtimeType._makeEntry | Can't find constructor for $T",
// // stackTrace: StackTrace.current,
// // );
// }
}
8 changes: 4 additions & 4 deletions lib/src/table_schema/data_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:hmi_core/hmi_core_result_new.dart';
///
/// A collection of the SchameEntry,
/// abstruction on the SQL table rows
class DataSchema<T extends SchemaEntry, P> implements Schema<T, P> {
class DataSchema<T extends SchemaEntryAbstract, P> implements Schema<T, P> {
final SchemaRead<T, P> _read;
final SchemaWrite<T> _write;
///
Expand All @@ -26,21 +26,21 @@ class DataSchema<T extends SchemaEntry, P> implements Schema<T, P> {
return read.fetch(params);
}
///
/// Inserts new entry into the table scheme
/// Inserts new entry into the table schema
@override
Future<Result<void, Failure>> insert({T? entry}) {
final write = _write;
return write.insert(entry);
}
///
/// Updates entry of the table scheme
/// Updates entry of the table schema
@override
Future<Result<void, Failure>> update(T entry) {
final write = _write;
return write.update(entry);
}
///
/// Deletes entry of the table scheme
/// Deletes entry of the table schema
@override
Future<Result<void, Failure>> delete(T entry) {
final write = _write;
Expand Down
6 changes: 6 additions & 0 deletions lib/src/table_schema/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ class Field {
///
///
Relation get relation => _relation;
//
//
@override
String toString() {
return '$runtimeType{ key: $_key, name: $_name, hidden: $_hidden, editable: $_edit, relation: $_relation }';
}
}
52 changes: 45 additions & 7 deletions lib/src/table_schema/field_value.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class FieldValue<T> {
T _value;
final FieldType _type;
FieldType _type;
///
///
FieldValue(
T value, {
FieldType type = FieldType.string,
FieldType type = FieldType.unknown,
}) :
_value = value,
_type = type;
///
///
T get value => _value;
///
///
/// Returns a string representation of the inner value
String? get str {
if (_value == null) {
return null;
Expand All @@ -27,22 +27,59 @@ class FieldValue<T> {
return '$_value';
case FieldType.string:
return "'$_value'";
case FieldType.unknown:
return "'$_value'";
// default:
}
}

///
///
FieldType get type => _type;
FieldType get type {
if (_type != FieldType.unknown) {
return _type;
}
if (value.runtimeType == bool) {
_type = FieldType.bool;
} else if (value.runtimeType == int) {
_type = FieldType.int;
} else if (value.runtimeType == double) {
_type = FieldType.double;
} else if (value.runtimeType == String) {
_type = FieldType.string;
}
return _type;
}
///
/// Returns true if changed
bool update(T value) {
if (_value != value) {
_value = value;
return true;
switch (type) {
case FieldType.bool:
_value = value;
return true;
case FieldType.int:
_value = value;
return true;
case FieldType.double:
_value = value;
return true;
case FieldType.string:
_value = '$value' as T;
return true;
case FieldType.unknown:
_value = value;
return true;
}
}
return false;
}
//
//
@override
String toString() {
return '$runtimeType{type: $type, value: $_value}';
}
}


Expand All @@ -51,4 +88,5 @@ enum FieldType {
int,
double,
string,
}
unknown,
}
Loading

0 comments on commit 23ef8c1

Please sign in to comment.