Skip to content

Commit

Permalink
partial useDefault for insert and _makeWhere utility
Browse files Browse the repository at this point in the history
  • Loading branch information
juancastillo0 committed Dec 7, 2023
1 parent ef2afbe commit 854e18e
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions packages/wasm_packages/sql_parser/typesql/lib/src/sql_executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,11 @@ class SqlExec<T> {
}
}

static SqlExec<SqlExecution> insert(SqlInsertModel model) {
final fields = model.dataClassProps.fields;
final args = SqlExecArgs(
"INSERT INTO ${model.table}('${fields.keys.join("','")}')"
" VALUES (${Iterable.generate(fields.length, (i) => '?').join(',')})",
fields.values.map(toSqlValue).toList(growable: false),
);
return SqlExec(args, args.execute);
}
static SqlExec<SqlExecution> insert<T extends SqlReturnModel>(
SqlTypeData<T, dynamic> ty,
SqlInsertModel model,
) =>
insertMany(ty, List.filled(1, model), useDefault: false);

static SqlExec<SqlExecution>
update<T extends SqlReturnModel, U extends SqlUpdateModel<T>>(
Expand All @@ -124,7 +120,7 @@ class SqlExec<T> {
model.dataClassProps.fields.entries.where((e) => e.value != null);
final set = fields.map((v) => '${v.key} = ?').join(",");
final args = SqlExecArgs(
"UPDATE ${model.table} SET $set WHERE ${items.where}",
"UPDATE ${model.table} SET $set ${_makeWhere([items.where])}",
[...fields.map((e) => e.value).map(toSqlValue), ...items.params],
);
return SqlExec(args, args.execute);
Expand All @@ -133,7 +129,7 @@ class SqlExec<T> {
static SqlExec<SqlExecution> delete(SqlUniqueKeyModel<dynamic, dynamic> key) {
final items = sqlItemsKey(key);
final args = SqlExecArgs(
"DELETE FROM ${key.table} WHERE ${items.where}",
"DELETE FROM ${key.table} ${_makeWhere([items.where])}",
items.params,
);
return SqlExec(args, args.execute);
Expand All @@ -145,16 +141,17 @@ class SqlExec<T> {
) {
final items = mergeSqlItems(keys.map(sqlItemsKey));
final args = SqlExecArgs(
"DELETE FROM ${keys.first.table} WHERE (${items.where.join(") OR (")})",
"DELETE FROM ${keys.first.table} ${_makeWhere(items.where)}",
items.params,
);
return SqlExec(args, args.execute);
}

static SqlExec<SqlExecution> insertMany<T extends SqlReturnModel>(
SqlTypeData<T, dynamic> ty,
List<SqlInsertModel<T>> models,
) {
List<SqlInsertModel<T>> models, {
bool useDefault = true,
}) {
final table = models.first.table;
final fields = models.map((e) => e.dataClassProps).toList();
final keys = fields.expand((e) => e.fields.keys).toSet();
Expand All @@ -163,6 +160,12 @@ class SqlExec<T> {
return value == null && t.type is! BTypeNullable && t.hasDefault;
}

if (!useDefault) {
for (final f in fields) {
keys.removeWhere((k) => isDefault(k, f.fields[k]));
}
}

final values = fields
.map(
(f) =>
Expand Down Expand Up @@ -196,8 +199,8 @@ abstract class SqlExecutor {

Future<T?> transaction<T>(Future<T> Function() transact);

Future<SqlExecution> insert(SqlInsertModel model) =>
SqlExec.insert(model).run(this);
// Future<SqlExecution> insert(SqlInsertModel model) =>
// SqlExec.insert(model).run(this);

Future<SqlExecution>
update<T extends SqlReturnModel, U extends SqlUpdateModel<T>>(
Expand All @@ -224,7 +227,7 @@ abstract class SqlExecutor {
) async {
final items = sqlItemsKey(key);
final rows = await query(
"SELECT * FROM ${key.table} WHERE ${items.where}",
"SELECT * FROM ${key.table} ${_makeWhere([items.where])}",
items.params,
);
return rows.firstOrNull;
Expand All @@ -236,7 +239,7 @@ abstract class SqlExecutor {
if (keys.isEmpty) return [];
final items = mergeSqlItems(keys.map(sqlItemsKey));
final rows = await query(
"SELECT * FROM ${keys.first.table} WHERE (${items.where.join(") OR (")})",
"SELECT * FROM ${keys.first.table} ${_makeWhere(items.where)})",
items.params,
);
return rows;
Expand All @@ -247,13 +250,18 @@ abstract class SqlExecutor {
) async {
final items = filter.sqlItems();
final rows = await query(
"SELECT * FROM ${filter.table} WHERE ${items.where}",
"SELECT * FROM ${filter.table} ${_makeWhere([items.where])}",
items.params,
);
return rows;
}
}

String _makeWhere(List<String> where) {
final conditions = where.where((a) => a.trim().isNotEmpty).join(') OR (');
return conditions.isEmpty ? '' : 'WHERE ($conditions)';
}

typedef SqlTypeDataField = ({String name, BaseType type, bool hasDefault});

abstract class SqlTypeData<T extends SqlReturnModel,
Expand Down Expand Up @@ -312,10 +320,11 @@ class SqlTypedController<T extends SqlReturnModel,
Future<List<T>> selectMany(SqlModelFilter<T, U> filter) =>
executor.selectMany(filter);

Future<T> insertReturning(SqlInsertModel<T> model) => SqlExec.insert(model)
.addReturning(type)
.run(executor.executor)
.then(extractFirst);
Future<T> insertReturning(SqlInsertModel<T> model) =>
SqlExec.insert(type, model)
.addReturning(type)
.run(executor.executor)
.then(extractFirst);

Future<SqlExecution> insertMany(List<SqlInsertModel<T>> models) =>
SqlExec.insertMany(type, models).run(executor.executor);
Expand Down

0 comments on commit 854e18e

Please sign in to comment.