where method
Adds a WHERE condition. Pass exactly one named argument per call.
.where('age', greaterThan: 18)
.where('email', like: '%@corp.com')
.where('role', whereIn: ['admin', 'editor'])
.where('deleted_at', isNull: true)
Implementation
QueryBuilder where(
String column, {
Object? equals,
Object? notEquals,
Object? greaterThan,
Object? lessThan,
Object? greaterThanOrEqual,
Object? lessThanOrEqual,
String? like,
List<Object?>? whereIn,
bool isNull = false,
bool isNotNull = false,
}) {
if (isNull) {
_conditions.add(_Condition(column, 'IS NULL', null));
} else if (isNotNull) {
_conditions.add(_Condition(column, 'IS NOT NULL', null));
} else if (equals != null) {
_conditions.add(_Condition(column, '=', equals));
} else if (notEquals != null) {
_conditions.add(_Condition(column, '!=', notEquals));
} else if (greaterThan != null) {
_conditions.add(_Condition(column, '>', greaterThan));
} else if (lessThan != null) {
_conditions.add(_Condition(column, '<', lessThan));
} else if (greaterThanOrEqual != null) {
_conditions.add(_Condition(column, '>=', greaterThanOrEqual));
} else if (lessThanOrEqual != null) {
_conditions.add(_Condition(column, '<=', lessThanOrEqual));
} else if (like != null) {
_conditions.add(_Condition(column, 'LIKE', like));
} else if (whereIn != null) {
if (whereIn.isEmpty) {
throw ArgumentError.value(
whereIn,
'whereIn',
'whereIn list must not be empty — an empty IN () clause is invalid SQL',
);
}
if (whereIn.length > 1000) {
// ignore: avoid_print
print(
'[dartapi_db] WARNING: whereIn on "$column" has ${whereIn.length} '
'values (>1000). Large IN clauses can degrade query performance — '
'consider a JOIN or a temporary table instead.',
);
}
_conditions.add(_Condition(column, 'IN', whereIn));
}
return this;
}