Skip to content

Commit

Permalink
Merge pull request #14 from udsm-dhis2-lab/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rajey authored Dec 10, 2022
2 parents 9c5ed3b + c0a5548 commit e2c38b8
Show file tree
Hide file tree
Showing 94 changed files with 1,300 additions and 389 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ jobs:
with:
java-version: "12.x"
- uses: subosito/flutter-action@v1
with:
with:
channel: "stable"
- run: flutter pub get
- run: flutter analyze
- run: flutter test
- run: sh database_builder.sh
- run: flutter test
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "dhis2_flutter_sdk",
"request": "launch",
"type": "dart"
},
{
"name": "dhis2_flutter_sdk (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "dhis2_flutter_sdk (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## [1.0.2] - 17/09/2022

## [1.0.0-beta.13] - 09/09/2022

- Add attribute column on attribute option table
Expand Down
9 changes: 9 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# include: package:lints/recommended.yaml

analyzer:
exclude:
- "test/*"
- "sample/*"
# linter:
# rules:
# use_key_in_widget_constructors: false
14 changes: 14 additions & 0 deletions lib/core/annotations/entity.annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ class Entity {
static List<Column> getEntityColumns(
ClassMirror entityClassMirror, bool ignoreRelationColumns) {
List<Column> columns = [];

for (String key
in entityClassMirror.superclass!.superclass!.declarations.keys) {
var value = entityClassMirror.superclass!.superclass!.declarations[key];

if (value is VariableMirror) {
VariableMirror variableMirror = value;
Column column =
Column.getColumn(variableMirror, key, ignoreRelationColumns)
as Column;
columns.add(column);
}
}

for (String key in entityClassMirror.superclass!.declarations.keys) {
var value = entityClassMirror.superclass!.declarations[key];

Expand Down
3 changes: 2 additions & 1 deletion lib/core/annotations/reflectable.annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class AnnotationReflection extends Reflectable {
metadataCapability,
staticInvokeCapability,
typeRelationsCapability,
typeCapability);
typeCapability,
superclassQuantifyCapability);
}

const AnnotationReflectable = AnnotationReflection();
12 changes: 10 additions & 2 deletions lib/core/database/database_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ class DatabaseManager {
String path = join(documentDirectory.path, databaseName + '.db');

var database = inMemory
? await openDatabase(inMemoryDatabasePath)
: await openDatabase(path, version: version, onCreate: _createDatabase);
? await openDatabase(inMemoryDatabasePath, onConfigure: _onConfigure)
: await openDatabase(path,
version: version,
onCreate: _createDatabase,
onConfigure: _onConfigure);
return database;
}

_onConfigure(Database db) async {
// Add support for cascade delete
await db.execute("PRAGMA foreign_keys = OFF");
}

closeDatabase() {}

void _createDatabase(Database database, int version) async {}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utilities/query_expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class QueryExpression {
{required String foreignColumn,
required String referencedTable,
required String referencedColumn}) {
return 'FOREIGN KEY ($foreignColumn) REFERENCES $referencedTable ($referencedColumn)';
return 'FOREIGN KEY ($foreignColumn) REFERENCES $referencedTable ($referencedColumn) ON DELETE CASCADE';
}

static getSelectExpression(
Expand Down
51 changes: 42 additions & 9 deletions lib/core/utilities/repository.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:d2_touch/core/annotations/index.dart';
import 'package:d2_touch/core/database/database_manager.dart';
import 'package:d2_touch/core/utilities/repository_util.dart';
import 'package:d2_touch/shared/entities/base_entity.dart';
import 'package:d2_touch/shared/entities/base.entity.dart';
import 'package:d2_touch/shared/utilities/merge_mode.util.dart';
import 'package:d2_touch/shared/utilities/query_filter.util.dart';
import 'package:d2_touch/shared/utilities/query_filter_condition.util.dart';
Expand All @@ -10,41 +10,61 @@ import 'package:queue/queue.dart';
import 'package:reflectable/reflectable.dart';
import 'package:sqflite/sqflite.dart';

import '../../shared/utilities/save_option.util.dart';
import 'query_expression.dart';

abstract class BaseRepository<T extends BaseEntity> {
List<Column> get columns;

List<Column> get oneToManyColumns;

Entity get entity;

String get createQuery;

Future<Database> get database;

Future<dynamic> create({Database database});

Future<int> count({Database database});

Future<List<T>> find(
{String? id,
List<QueryFilter>? filters,
List<String>? fields,
Map<String, SortOrder>? sortOrder,
Database? database});

Future<T?> findById(
{required String id, List<String>? fields, Database? database});

Future<List<T>> findAll(
{List<QueryFilter> filters,
List<String> fields,
Map<String, SortOrder> sortOrder});

Future<int> insertOne({required T entity, Database? database});

Future<int> insertMany({required List<T> entities, Database? database});

Future<int> updateOne({required T entity, Database database});

Future<int> updateMany({required List<T> entities, Database database});

Future<int> saveMany(
{required List<T> entities,
Database database,
required MergeMode mergeMode});

Future<int> saveOne(
{required T entity, Database database, required MergeMode mergeMode});

Future<int> deleteById({required String id, Database database});

Future<int> deleteByIds({required List<String> ids, Database database});

Future<int> deleteAll({Database database});

Map<String, dynamic> sanitizeIncomingData(
{required Map<String, dynamic> entity, required List<Column> columns});
}
Expand Down Expand Up @@ -305,7 +325,7 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {
.sanitizeIncomingData(entity: entity.toJson(), columns: this.columns);
final Database db = database != null ? database : await this.database;

final saveDataResponse = db.insert(this.entity.tableName, data);
final saveDataResponse = await db.insert(this.entity.tableName, data);

if (this.oneToManyColumns.isEmpty) {
return saveDataResponse;
Expand Down Expand Up @@ -456,7 +476,8 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {
{required List<T> entities,
Database? database,
int? chunk,
required MergeMode mergeMode}) async {
required MergeMode mergeMode,
SaveOptions? saveOptions}) async {
final Database db = database != null ? database : await this.database;

if (entities.isEmpty) {
Expand All @@ -466,8 +487,11 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {
final queue = Queue(parallel: chunk ?? 500);

entities.forEach((T entity) {
queue.add(
() => saveOne(entity: entity, database: db, mergeMode: mergeMode));
queue.add(() => saveOne(
entity: entity,
database: db,
mergeMode: mergeMode,
saveOptions: saveOptions));
});

await queue.onComplete;
Expand All @@ -479,7 +503,8 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {
Future<int> saveOne(
{required T entity,
Database? database,
required MergeMode mergeMode}) async {
required MergeMode mergeMode,
SaveOptions? saveOptions}) async {
final Database db = database != null ? database : await this.database;

var result = await this.findById(id: entity.id as String, database: db);
Expand All @@ -500,10 +525,10 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {
Map<String, dynamic> localData = result.toJson();
Map<String, dynamic> entityMap = entity.toJson();

if(!localData['synced']){
if (saveOptions?.skipLocalSyncStatus == null ||
saveOptions?.skipLocalSyncStatus == false) {
entityMap['synced'] = localData['synced'];
}


localData.keys.forEach((key) {
if (entityMap[key] == null) {
Expand Down Expand Up @@ -555,7 +580,7 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {
Map<String, dynamic> data = this
.sanitizeIncomingData(entity: entity.toJson(), columns: this.columns);
final Database db = database != null ? database : await this.database;
final saveDataResponse = db.update(
final saveDataResponse = await db.update(
this.entity.tableName,
data,
where: "id = ?",
Expand Down Expand Up @@ -679,4 +704,12 @@ class Repository<T extends BaseEntity> extends BaseRepository<T> {

return countResult[0]['count'] as int;
}

Future<List<Map>> rawQuery({required String query}) async {
final Database db = await this.database;

final List<Map> queryResult = await db.rawQuery(query.toString());

return queryResult;
}
}
Loading

0 comments on commit e2c38b8

Please sign in to comment.