compile method
String
compile(
- Set<
String> trampoline, { - bool includeTableName = false,
- String? preamble,
- bool withFields = true,
- String? fromQuery,
override
Implementation
@override
String compile(Set<String> trampoline,
{bool includeTableName = false,
String? preamble,
bool withFields = true,
String? fromQuery}) {
// One table MAY appear multiple times in a query.
if (!canCompile(trampoline)) {
//return null;
//throw Exception('One table appear multiple times in a query');
return '';
}
includeTableName = includeTableName || _joins.isNotEmpty;
var b = StringBuffer(preamble ?? 'SELECT');
b.write(' ');
List<String> f;
var compiledJoins = <JoinBuilder, String?>{};
//if (fields == null) {
if (fields.isEmpty) {
f = ['*'];
} else {
f = List<String>.from(fields.map((s) {
String? ss = includeTableName ? '$tableName.$s' : s;
if (expressions.containsKey(s)) {
ss = '( ${expressions[s]} )';
//ss = expressions[s];
}
var cast = casts[s];
if (cast != null) ss = 'CAST ($ss AS $cast)';
if (aliases.containsKey(s)) {
if (cast != null) {
ss = '($ss) AS ${aliases[s]}';
} else {
ss = '$ss AS ${aliases[s]}';
}
if (expressions.containsKey(s)) {
// ss = '($ss)';
}
} else if (expressions.containsKey(s)) {
if (cast != null) {
ss = '($ss) AS $s';
// ss = '(($ss) AS $s)';
} else {
ss = '$ss AS $s';
// ss = '($ss AS $s)';
}
}
return ss;
}));
for (var j in _joins) {
var c = compiledJoins[j] = j.compile(trampoline);
//if (c != null) {
if (c != '') {
var additional = j.additionalFields.map(j.nameFor).toList();
f.addAll(additional);
} else {
// If compilation failed, fill in NULL placeholders.
for (var i = 0; i < j.additionalFields.length; i++) {
f.add('NULL');
}
}
}
}
if (withFields) b.write(f.join(', '));
fromQuery ??= tableName;
b.write(' FROM $fromQuery');
// No joins if it's not a select.
if (preamble == null) {
if (_crossJoin != null) b.write(' CROSS JOIN $_crossJoin');
for (var join in _joins) {
var c = compiledJoins[join];
if (c != null) b.write(' $c');
}
}
var whereClause =
where?.compile(tableName: includeTableName ? tableName : null);
if (whereClause?.isNotEmpty == true) {
b.write(' WHERE $whereClause');
}
if (_groupBy != null) b.write(' GROUP BY $_groupBy');
var orderByClause = _orderBy.map((order) => order.compile()).join(', ');
if (orderByClause.isNotEmpty) {
b.write(' ORDER BY $orderByClause');
}
if (_limit != null) b.write(' LIMIT $_limit');
if (_offset != null) b.write(' OFFSET $_offset');
return b.toString();
}