Skip to content

Commit

Permalink
use default toColumnMap for query result mapping (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion authored Dec 27, 2023
1 parent 9c950a3 commit 060ecb7
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/dox-app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
postgres: ^3.0.4
postgres: ^3.0.5
ioredis: ^1.0.2
short_uuids: ^2.0.0
dox_auth: ^2.0.0-beta.2
Expand Down
3 changes: 1 addition & 2 deletions packages/dox-query-builder/lib/src/drivers/db_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ abstract class DBDriver {
{Map<String, dynamic>? substitutionValues});

/// run query and return map result
Future<List<Map<String, Map<String, dynamic>>>> mappedResultsQuery(
String query,
Future<List<Map<String, dynamic>>> mappedResultsQuery(String query,
{Map<String, dynamic>? substitutionValues});

/// run query, this function do not return any value
Expand Down
36 changes: 3 additions & 33 deletions packages/dox-query-builder/lib/src/drivers/postgres_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PostgresDriver extends DBDriver {

/// run query and return map result
@override
Future<List<Map<String, Map<String, dynamic>>>> mappedResultsQuery(
Future<List<Map<String, dynamic>>> mappedResultsQuery(
String query, {
Map<String, dynamic>? substitutionValues,
}) async {
Expand All @@ -44,37 +44,7 @@ class PostgresDriver extends DBDriver {

/// extension on postgres
extension ToMapList on Result {
List<Map<String, Map<String, dynamic>>> toMapList() {
List<Map<String, Map<String, dynamic>>> data =
<Map<String, Map<String, dynamic>>>[];
forEach((ResultRow element) {
data.add(element.toMappedColumns());
});
return data;
}
}

extension ToMappedColumns on ResultRow {
Map<String, Map<String, dynamic>> toMappedColumns() {
Map<String, Map<String, dynamic>> data = <String, Map<String, dynamic>>{};

for (var (int i, ResultSchemaColumn col) in schema.columns.indexed) {
String tableId = col.tableOid.toString();
if (col.columnName case String name) {
if (data[tableId] == null) {
data[tableId] = <String, dynamic>{};
}
Map<String, dynamic> table = data[tableId]!;
table[name] = this[i];
} else {
if (data[tableId] == null) {
data[tableId] = <String, dynamic>{};
}
Map<String, dynamic> table = data[tableId]!;
String name = '[$i]';
table[name] = this[i];
}
}
return data;
List<Map<String, dynamic>> toMapList() {
return map((ResultRow element) => element.toColumnMap()).toList();
}
}
9 changes: 4 additions & 5 deletions packages/dox-query-builder/lib/src/insert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ mixin Insert<T> implements SharedMixin<T> {
// ignore: always_specify_types
Future insert(Map<String, dynamic> data) async {
// ignore: always_specify_types
List<Map<String, Map<String, dynamic>>> result =
List<Map<String, dynamic>> result =
await insertMultiple(<Map<String, dynamic>>[data]);
if (result.isNotEmpty) {
Map<String, Map<String, dynamic>> insertedData = result.first;
String tableId = insertedData.keys.first;
int id = insertedData[tableId]?[primaryKey] ?? 0;
Map<String, dynamic> insertedData = result.first;
int id = insertedData[primaryKey] ?? 0;
resetSubstitutionValues();
return await queryBuilder.find(id);
}
Expand All @@ -45,7 +44,7 @@ mixin Insert<T> implements SharedMixin<T> {
/// {"title" : "Another blog title"},
/// ]);
/// ```
Future<List<Map<String, Map<String, dynamic>>>> insertMultiple(
Future<List<Map<String, dynamic>>> insertMultiple(
List<Map<String, dynamic>> list) async {
List<String> columns = <String>[];
List<String> values = <String>[];
Expand Down
11 changes: 4 additions & 7 deletions packages/dox-query-builder/lib/src/schema/table.update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,13 @@ mixin TableUpdate implements TableSharedMixin {
Future<List<String>> getTableColumns() async {
String query =
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$tableName'";
List<Map<String, Map<String, dynamic>>> result =
await db.mappedResultsQuery(query);
List<Map<String, dynamic>> result = await db.mappedResultsQuery(query);

List<String> columns = <String>[];

for (Map<String, Map<String, dynamic>> element in result) {
element.forEach((String key, Map<String, dynamic> value) {
value.forEach((String key2, dynamic value2) {
columns.add(value2.toString());
});
for (Map<String, dynamic> element in result) {
element.forEach((String key2, dynamic value2) {
columns.add(value2.toString());
});
}
return columns;
Expand Down
21 changes: 8 additions & 13 deletions packages/dox-query-builder/lib/src/utils/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QueryBuilderHelper<T> {
return query;
}

Future<List<Map<String, Map<String, dynamic>>>> runQuery(String query) async {
Future<List<Map<String, dynamic>>> runQuery(String query) async {
Map<String, dynamic> values = queryBuilder.substitutionValues;
if (queryBuilder.shouldDebug) queryBuilder.logger.log(query, values);
DBDriver db = queryBuilder.db;
Expand All @@ -33,30 +33,25 @@ class QueryBuilderHelper<T> {
}

List<Map<String, dynamic>> getMapResult(
List<Map<String, Map<String, dynamic>>> queryResult,
List<Map<String, dynamic>> queryResult,
) {
List<Map<String, dynamic>> result = <Map<String, dynamic>>[];
// setting key values format from query result
for (Map<String, Map<String, dynamic>> row in queryResult) {
for (Map<String, dynamic> data in queryResult) {
Map<String, dynamic> ret = <String, dynamic>{};
(row).forEach((String mainKey, Map<String, dynamic> data) {
(data).forEach((String key, dynamic value) {
if (ret[key] == null) {
ret[key] = value is DateTime ? value.toIso8601String() : value;
} else {
ret["${mainKey}_$key"] =
value is DateTime ? value.toIso8601String() : value;
}
});

(data).forEach((String key, dynamic value) {
ret[key] = value is DateTime ? value.toIso8601String() : value;
});

result.add(ret);
}
return result;
}

// ignore: always_specify_types
Future<List> formatResult(
List<Map<String, Map<String, dynamic>>> queryResult,
List<Map<String, dynamic>> queryResult,
) async {
List<Map<String, dynamic>> result = getMapResult(queryResult);

Expand Down
2 changes: 1 addition & 1 deletion packages/dox-query-builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
postgres: ^3.0.4
postgres: ^3.0.5
dox_annotation: 1.0.5-alpha2.1

dev_dependencies:
Expand Down

0 comments on commit 060ecb7

Please sign in to comment.