encodeConditionValuesWithOperator method
FutureOr<EncodingContext>
encodeConditionValuesWithOperator(
- EncodingContext context,
- Type keyType,
- String? fieldKey,
- String tableKey,
- String operator,
- dynamic values,
- bool valueAsList, {
- bool negated = false,
- bool nullable = true,
- Condition? condition,
- bool nullsAsAbsent = false,
})
Implementation
FutureOr<EncodingContext> encodeConditionValuesWithOperator(
EncodingContext context,
Type keyType,
String? fieldKey,
String tableKey,
String operator,
dynamic values,
bool valueAsList, {
bool negated = false,
bool nullable = true,
Condition? condition,
bool nullsAsAbsent = false,
}) {
// The encoding value rather than only its text: a null supplied as a
// parameter encodes to a placeholder, so the text alone cannot say whether
// the comparison is against null.
var valueRet = valueToParameterValue(
context,
values,
fieldKey: fieldKey,
fieldType: keyType,
valueAsList: valueAsList,
);
return valueRet.resolveMapped((value) {
// A `null` in an `IN` list: as `IS NULL` (as when matched in memory).
// In SQL `x IN (1, NULL)` doesn't match a `NULL`, and
// `x NOT IN (1, NULL)` matches nothing.
var listWithNull = false;
if (value is EncodingValueList<String> &&
(operator == 'IN' || operator == 'NOT IN')) {
var elements = value.asList;
// An empty list (`x =~ []`, or a parameter's `[]`): `IN` matches
// nothing and `NOT IN` everything, also a `NULL` (as in memory). It
// is not a `null` (`x =~ null` and `[null]` are `IS NULL`, and arrive
// as a list with a `null`), except for a relationship
// ([nullsAsAbsent], below).
if (elements.isEmpty) {
return _writeEmptySetMatch(context, operator);
}
var nonNulls = elements
.where((e) => !_isNullListElement(e, context))
.toList();
if (nonNulls.length < elements.length) {
for (var e in elements) {
if (!nonNulls.contains(e)) {
_markPlaceholderInlinedAsNull(e, context);
}
}
if (nullsAsAbsent) {
// A relationship's set of IDs ([_isRelationshipKey]): its `null`s
// aren't IDs. Without any other, an empty set (as `[]`).
if (nonNulls.isEmpty) {
return _writeEmptySetMatch(context, operator);
}
value = EncodingValueList(
value.key,
value.type,
nonNulls,
encodeEncodingValueList,
);
} else if (nonNulls.isEmpty) {
value = EncodingValueNull(
value.key,
value.type,
encodeEncodingValueNull,
);
} else {
listWithNull = true;
value = EncodingValueList(
value.key,
value.type,
nonNulls,
encodeEncodingValueList,
);
}
}
}
if (isNullEncodingValue(value, context)) {
switch (operator) {
case '=':
case 'IN':
{
context.write(' $tableKey IS NULL ');
_markPlaceholderInlinedAsNull(value, context);
_markOuterCondition(condition, context);
return context;
}
case '!=':
case 'NOT IN':
{
context.write(' $tableKey IS NOT NULL ');
_markPlaceholderInlinedAsNull(value, context);
return context;
}
}
}
// A negated operator also matches a `null` column (as when matched in
// memory): `( x != v OR x IS NULL )`; unless the list has a `null`:
// `( x NOT IN (v) AND x IS NOT NULL )`. And `( x IN (v) OR x IS NULL )`.
String? nullSQL;
if (listWithNull) {
nullSQL = negated
? 'AND $tableKey IS NOT NULL'
: 'OR $tableKey IS NULL';
if (!negated) _markOuterCondition(condition, context);
} else if (negated && nullable) {
nullSQL = 'OR $tableKey IS NULL';
}
context.write(nullSQL != null ? ' ( ' : ' ');
context.write(tableKey);
context.write(' ');
context.write(operator);
context.write(' ');
context.write(value.encode);
context.write(' ');
if (nullSQL != null) {
context.write(nullSQL);
context.write(' ) ');
}
return context;
});
}