standalone and belongs to work

This commit is contained in:
Tobe O 2018-12-07 21:14:14 -05:00
parent e647663fad
commit b6b9c7148d

View file

@ -230,8 +230,13 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
} }
if (withFields) b.write(f.join(', ')); if (withFields) b.write(f.join(', '));
b.write(' FROM $tableName'); b.write(' FROM $tableName');
// No joins if it's not a select.
if (preamble == null) {
if (_crossJoin != null) b.write(' CROSS JOIN $_crossJoin'); if (_crossJoin != null) b.write(' CROSS JOIN $_crossJoin');
for (var join in _joins) b.write(' ${join.compile()}'); for (var join in _joins) b.write(' ${join.compile()}');
}
var whereClause = var whereClause =
where.compile(tableName: includeTableName ? tableName : null); where.compile(tableName: includeTableName ? tableName : null);
if (whereClause.isNotEmpty) b.write(' WHERE $whereClause'); if (whereClause.isNotEmpty) b.write(' WHERE $whereClause');
@ -249,13 +254,21 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
} }
Future<List<T>> delete(QueryExecutor executor) { Future<List<T>> delete(QueryExecutor executor) {
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 { return executor.transaction(() async {
// TODO: Can this be done with just *one* query?
var existing = await get(executor); var existing = await get(executor);
//var sql = compile(preamble: 'SELECT $tableName.id', withFields: false); //var sql = compile(preamble: 'SELECT $tableName.id', withFields: false);
var sql = compile(preamble: 'DELETE', withFields: false);
return executor.query(sql).then((_) => existing); return executor.query(sql).then((_) => existing);
}); });
} }
}
Future<T> deleteOne(QueryExecutor executor) { Future<T> deleteOne(QueryExecutor executor) {
return delete(executor).then((it) => it.isEmpty ? null : it.first); return delete(executor).then((it) => it.isEmpty ? null : it.first);
@ -284,9 +297,17 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
var whereClause = where.compile(); var whereClause = where.compile();
if (whereClause.isNotEmpty) sql.write(' WHERE $whereClause'); if (whereClause.isNotEmpty) sql.write(' WHERE $whereClause');
if (_limit != null) sql.write(' LIMIT $_limit'); if (_limit != null) sql.write(' LIMIT $_limit');
if (_joins.isEmpty) {
return executor return executor
.query(sql.toString(), fields.map(adornWithTableName).toList()) .query(sql.toString(), fields.map(adornWithTableName).toList())
.then((it) => it.map(deserialize).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));
}
} }
} }