Fixed ORM Query
This commit is contained in:
parent
e11f8f77e1
commit
0204977281
2 changed files with 13 additions and 13 deletions
|
@ -11,7 +11,7 @@ void main() async {
|
||||||
var query = EmployeeQuery()
|
var query = EmployeeQuery()
|
||||||
..where?.firstName.equals('Rich')
|
..where?.firstName.equals('Rich')
|
||||||
..where?.lastName.equals('Person')
|
..where?.lastName.equals('Person')
|
||||||
..orWhere((w) => w?.salary.greaterThanOrEqualTo(75000))
|
..orWhere((w) => w.salary.greaterThanOrEqualTo(75000))
|
||||||
..join('companies', 'company_id', 'id');
|
..join('companies', 'company_id', 'id');
|
||||||
|
|
||||||
var richPerson = await query.getOne(_FakeExecutor());
|
var richPerson = await query.getOne(_FakeExecutor());
|
||||||
|
@ -51,7 +51,7 @@ abstract class _Employee extends Model {
|
||||||
double? get salary;
|
double? get salary;
|
||||||
}
|
}
|
||||||
|
|
||||||
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere?> {
|
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
|
||||||
@override
|
@override
|
||||||
final QueryValues values = MapQueryValues();
|
final QueryValues values = MapQueryValues();
|
||||||
|
|
||||||
|
|
|
@ -34,12 +34,12 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
/// A reference to an abstract query builder.
|
/// A reference to an abstract query builder.
|
||||||
///
|
///
|
||||||
/// This is usually a generated class.
|
/// This is usually a generated class.
|
||||||
Where get where;
|
Where? get where;
|
||||||
|
|
||||||
/// A set of values, for an insertion or update.
|
/// A set of values, for an insertion or update.
|
||||||
///
|
///
|
||||||
/// This is usually a generated class.
|
/// This is usually a generated class.
|
||||||
QueryValues get values;
|
QueryValues? get values;
|
||||||
|
|
||||||
/// Preprends the [tableName] to the [String], [s].
|
/// Preprends the [tableName] to the [String], [s].
|
||||||
String adornWithTableName(String s) {
|
String adornWithTableName(String s) {
|
||||||
|
@ -86,21 +86,21 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
void andWhere(void Function(Where) f) {
|
void andWhere(void Function(Where) f) {
|
||||||
var w = newWhereClause();
|
var w = newWhereClause();
|
||||||
f(w);
|
f(w);
|
||||||
where.and(w);
|
where?.and(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shorthand for calling [where].or with a [Where] clause.
|
/// Shorthand for calling [where].or with a [Where] clause.
|
||||||
void notWhere(void Function(Where) f) {
|
void notWhere(void Function(Where) f) {
|
||||||
var w = newWhereClause();
|
var w = newWhereClause();
|
||||||
f(w);
|
f(w);
|
||||||
where.not(w);
|
where?.not(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shorthand for calling [where].or with a [Where] clause.
|
/// Shorthand for calling [where].or with a [Where] clause.
|
||||||
void orWhere(void Function(Where) f) {
|
void orWhere(void Function(Where) f) {
|
||||||
var w = newWhereClause();
|
var w = newWhereClause();
|
||||||
f(w);
|
f(w);
|
||||||
where.or(w);
|
where?.or(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Limit the number of rows to return.
|
/// Limit the number of rows to return.
|
||||||
|
@ -310,8 +310,8 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
var whereClause =
|
var whereClause =
|
||||||
where.compile(tableName: includeTableName ? tableName : null);
|
where?.compile(tableName: includeTableName ? tableName : null);
|
||||||
if (whereClause.isNotEmpty == true) {
|
if (whereClause?.isNotEmpty == true) {
|
||||||
b.write(' WHERE $whereClause');
|
b.write(' WHERE $whereClause');
|
||||||
}
|
}
|
||||||
if (_groupBy != null) b.write(' GROUP BY $_groupBy');
|
if (_groupBy != null) b.write(' GROUP BY $_groupBy');
|
||||||
|
@ -355,7 +355,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Optional<T>> insert(QueryExecutor executor) {
|
Future<Optional<T>> insert(QueryExecutor executor) {
|
||||||
var insertion = values.compileInsert(this, tableName);
|
var insertion = values?.compileInsert(this, tableName);
|
||||||
|
|
||||||
if (insertion == '') {
|
if (insertion == '') {
|
||||||
throw StateError('No values have been specified for update.');
|
throw StateError('No values have been specified for update.');
|
||||||
|
@ -373,14 +373,14 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
|
|
||||||
Future<List<T>> update(QueryExecutor executor) async {
|
Future<List<T>> update(QueryExecutor executor) async {
|
||||||
var updateSql = StringBuffer('UPDATE $tableName ');
|
var updateSql = StringBuffer('UPDATE $tableName ');
|
||||||
var valuesClause = values.compileForUpdate(this);
|
var valuesClause = values?.compileForUpdate(this);
|
||||||
|
|
||||||
if (valuesClause == '') {
|
if (valuesClause == '') {
|
||||||
throw StateError('No values have been specified for update.');
|
throw StateError('No values have been specified for update.');
|
||||||
} else {
|
} else {
|
||||||
updateSql.write(' $valuesClause');
|
updateSql.write(' $valuesClause');
|
||||||
var whereClause = where.compile();
|
var whereClause = where?.compile();
|
||||||
if (whereClause.isNotEmpty == true) {
|
if (whereClause?.isNotEmpty == true) {
|
||||||
updateSql.write(' WHERE $whereClause');
|
updateSql.write(' WHERE $whereClause');
|
||||||
}
|
}
|
||||||
if (_limit != null) updateSql.write(' LIMIT $_limit');
|
if (_limit != null) updateSql.write(' LIMIT $_limit');
|
||||||
|
|
Loading…
Reference in a new issue