Skip to content

Commit

Permalink
Scheme Read/Write
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed Nov 30, 2023
1 parent 8b474a4 commit 564c221
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 221 deletions.
104 changes: 9 additions & 95 deletions lib/src/table_schema/data_schema.dart
Original file line number Diff line number Diff line change
@@ -1,135 +1,49 @@
import 'package:ext_rw/ext_rw.dart';
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';

///
/// A collection of the SchameEntry,
/// abstruction on the SQL table rows
class DataSchema<T extends SchemaEntry, P> implements Schema<T, P> {
late final Log _log;
final List<Field> _fields;
final Map<String, T> _entries = {};
final SchemaRead<T, P> _read;
final SchemaWrite<T> _write;
final Map<String, Schema> _relations;
///
/// A collection of the SchameEntry,
/// abstruction on the SQL table rows
/// - [keys] - list of table field names
DataSchema({
required List<Field> fields,
SchemaRead<T, P> read = const SchemaRead.empty(),
SchemaWrite<T> write = const SchemaWrite.empty(),
Map<String, Schema> relations = const {},
}) :
_fields = fields,
_read = read,
_write = write,
_relations = relations {
_log = Log("$runtimeType");
}
///
/// Returns a list of table field names
List<Field> get fields {
return _fields;
}
///
/// Returns a list of table field keys
List<String> get keys {
return _fields.map((field) => field.key).toList();
}
///
///
List<T> get entries => _entries.values.toList();
_write = write;
///
/// Fetchs data with new sql built from [values]
@override
Future<Result<List<T>, Failure>> fetch(P params) async {
await fetchRelations();
final read = _read;
return read.fetch(params).then((result) {
return switch(result) {
Ok<List<T>, Failure>(:final value) => () {
_entries.clear();
for (final entry in value) {
if (_entries.containsKey(entry.key)) {
throw Failure(
message: "$runtimeType.fetchWith | dublicated entry key: ${entry.key}",
stackTrace: StackTrace.current,
);
}
_entries[entry.key] = entry;
}
return Ok<List<T>, Failure>(_entries.values.toList());
}(),
Err<List<T>, Failure>(:final error) => () {
return Err<List<T>, Failure>(error);
}(),
};
});
}
///
/// Returns relation Result<Scheme> if exists else Result<Failure>
Result<Schema, Failure> relation(String id) {
final rel = _relations[id];
if (rel != null) {
return Ok(rel);
} else {
return Err(Failure(
message: "$runtimeType.relation | id: $id - not found",
stackTrace: StackTrace.current,
));
}
return read.fetch(params);
}
///
/// Inserts new entry into the table scheme
@override
Future<Result<void, Failure>> insert({T? entry}) {
final write = _write;
return write.insert(entry).then((result) {
return switch (result) {
Ok(:final value) => () {
final entry_ = value;
_entries[entry_.key] = entry_;
return const Ok<void, Failure>(null);
}(),
Err(:final error) => Err(error),
};
});
return write.insert(entry);
}
///
/// Updates entry of the table scheme
@override
Future<Result<void, Failure>> update(T entry) {
final write = _write;
return write.update(entry).then((result) {
if (result is Ok) {
_entries[entry.key] = entry;
}
return result;
});
return write.update(entry);
}
///
/// Deletes entry of the table scheme
@override
Future<Result<void, Failure>> delete(T entry) {
final write = _write;
return write.delete(entry).then((result) {
if (result is Ok) {
_entries.remove(entry.key);
}
return result;
});
}
///
/// Fetchs data of the relation schemes only (with existing sql)
Future<void> fetchRelations() async {
for (final field in _fields) {
if (field.relation.isNotEmpty) {
switch (relation(field.relation.id)) {
case Ok(:final value):
await value.fetch(null);
case Err(:final error):
_log.warning(".fetchRelations | relation '${field.relation}' - not found\n\terror: $error");
}
}
}
return write.delete(entry);
}
}
133 changes: 9 additions & 124 deletions lib/src/table_schema/schema.dart
Original file line number Diff line number Diff line change
@@ -1,135 +1,20 @@
import 'package:ext_rw/ext_rw.dart';
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';

///
/// A collection of the SchameEntry,
/// abstruction on the SQL table rows
/// An abstruction on the data access
abstract interface class Schema<T extends SchemaEntry, P> {
// late final Log _log;
// final List<Field> _fields;
// final Map<String, T> _entries = {};
// final SchemaRead<T, P> _read;
// final SchemaWrite<T> _write;
// final Map<String, Schema> _relations;
///
/// A collection of the SchameEntry,
/// abstruction on the SQL table rows
/// - [keys] - list of table field names
// Schema({
// required List<Field> fields,
// SchemaRead<T, P> read = const SchemaRead.empty(),
// SchemaWrite<T> write = const SchemaWrite.empty(),
// Map<String, Schema> relations = const {},
// }) :
// _fields = fields,
// _read = read,
// _write = write,
// _relations = relations {
// _log = Log("$runtimeType");
// }
/// Fetchs data from the data source using [params]
Future<Result<List<T>, Failure>> fetch(P params);
///
/// Returns a list of table field names
// List<Field> get fields {
// return _fields;
// }
/// Inserts new entry into the data source
Future<Result<void, Failure>> insert({T? entry});
///
/// Returns a list of table field keys
// List<String> get keys {
// return _fields.map((field) => field.key).toList();
// }
/// Updates entry of the data source
Future<Result<void, Failure>> update(T entry);
///
///
// List<T> get entries => _entries.values.toList();
///
/// Fetchs data with new sql built from [values]
Future<Result<List<T>, Failure>> fetch(P params); // async {
// await fetchRelations();
// final read = _read;
// return read.fetch(params).then((result) {
// return switch(result) {
// Ok<List<T>, Failure>(:final value) => () {
// _entries.clear();
// for (final entry in value) {
// if (_entries.containsKey(entry.key)) {
// throw Failure(
// message: "$runtimeType.fetchWith | dublicated entry key: ${entry.key}",
// stackTrace: StackTrace.current,
// );
// }
// _entries[entry.key] = entry;
// }
// return Ok<List<T>, Failure>(_entries.values.toList());
// }(),
// Err<List<T>, Failure>(:final error) => () {
// return Err<List<T>, Failure>(error);
// }(),
// };
// });
// }
///
/// Returns relation Result<Scheme> if exists else Result<Failure>
// Result<Schema, Failure> relation(String id) {
// final rel = _relations[id];
// if (rel != null) {
// return Ok(rel);
// } else {
// return Err(Failure(
// message: "$runtimeType.relation | id: $id - not found",
// stackTrace: StackTrace.current,
// ));
// }
// }
///
/// Inserts new entry into the table scheme
Future<Result<void, Failure>> insert({T? entry}); // {
// final write = _write;
// return write.insert(entry).then((result) {
// return switch (result) {
// Ok(:final value) => () {
// final entry_ = value;
// _entries[entry_.key] = entry_;
// return const Ok<void, Failure>(null);
// }(),
// Err(:final error) => Err(error),
// };
// });
// }
///
/// Updates entry of the table scheme
Future<Result<void, Failure>> update(T entry); // {
// final write = _write;
// return write.update(entry).then((result) {
// if (result is Ok) {
// _entries[entry.key] = entry;
// }
// return result;
// });
// }
///
/// Deletes entry of the table scheme
Future<Result<void, Failure>> delete(T entry); // {
// final write = _write;
// return write.delete(entry).then((result) {
// if (result is Ok) {
// _entries.remove(entry.key);
// }
// return result;
// });
// }
///
/// Fetchs data of the relation schemes only (with existing sql)
// Future<void> fetchRelations() async {
// for (final field in _fields) {
// if (field.relation.isNotEmpty) {
// switch (relation(field.relation.id)) {
// case Ok(:final value):
// await value.fetch(null);
// case Err(:final error):
// _log.warning(".fetchRelations | relation '${field.relation}' - not found\n\terror: $error");
// }
// }
// }
// }
/// Deletes entry from the data source
Future<Result<void, Failure>> delete(T entry);
}
2 changes: 1 addition & 1 deletion lib/src/table_schema/schema_read.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_result_new.dart';

///
/// Abstraction on read data access
/// An abstraction on read data access
abstract interface class SchemaRead<T extends SchemaEntry, P> {
///
/// Empty instance implements SchemaRead
Expand Down
2 changes: 1 addition & 1 deletion lib/src/table_schema/schema_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_result_new.dart';

///
/// Abstraction on write data access
/// An abstraction on write data access
abstract interface class SchemaWrite<T extends SchemaEntry> {
///
/// Empty instance implements SchemaRead
Expand Down

0 comments on commit 564c221

Please sign in to comment.