From b6b9c7148db6c16893e87a7add20276e57126dd5 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 7 Dec 2018 21:14:14 -0500 Subject: [PATCH] standalone and belongs to work --- angel_orm/lib/src/query.dart | 43 +++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/angel_orm/lib/src/query.dart b/angel_orm/lib/src/query.dart index 4b28525d..dbde9da2 100644 --- a/angel_orm/lib/src/query.dart +++ b/angel_orm/lib/src/query.dart @@ -230,8 +230,13 @@ abstract class Query extends QueryBase { } if (withFields) b.write(f.join(', ')); b.write(' FROM $tableName'); - if (_crossJoin != null) b.write(' CROSS JOIN $_crossJoin'); - for (var join in _joins) b.write(' ${join.compile()}'); + + // No joins if it's not a select. + if (preamble == null) { + if (_crossJoin != null) b.write(' CROSS JOIN $_crossJoin'); + for (var join in _joins) b.write(' ${join.compile()}'); + } + var whereClause = where.compile(tableName: includeTableName ? tableName : null); if (whereClause.isNotEmpty) b.write(' WHERE $whereClause'); @@ -249,12 +254,20 @@ abstract class Query extends QueryBase { } Future> delete(QueryExecutor executor) { - return executor.transaction(() async { - var existing = await get(executor); - //var sql = compile(preamble: 'SELECT $tableName.id', withFields: false); - var sql = compile(preamble: 'DELETE', withFields: false); - return executor.query(sql).then((_) => existing); - }); + var sql = compile(preamble: 'DELETE', withFields: false); + + if (_joins.isEmpty) { + return executor + .query(sql, fields.map(adornWithTableName).toList()) + .then((it) => it.map(deserialize).toList()); + } else { + return executor.transaction(() async { + // TODO: Can this be done with just *one* query? + var existing = await get(executor); + //var sql = compile(preamble: 'SELECT $tableName.id', withFields: false); + return executor.query(sql).then((_) => existing); + }); + } } Future deleteOne(QueryExecutor executor) { @@ -284,9 +297,17 @@ abstract class Query extends QueryBase { var whereClause = where.compile(); if (whereClause.isNotEmpty) sql.write(' WHERE $whereClause'); if (_limit != null) sql.write(' LIMIT $_limit'); - return executor - .query(sql.toString(), fields.map(adornWithTableName).toList()) - .then((it) => it.map(deserialize).toList()); + + if (_joins.isEmpty) { + return executor + .query(sql.toString(), fields.map(adornWithTableName).toList()) + .then((it) => it.map(deserialize).toList()); + } else { + // TODO: Can this be done with just *one* query? + return executor + .query(sql.toString(), fields.map(adornWithTableName).toList()) + .then((it) => get(executor)); + } } }