sqlColumns function
Converts a list of fields
to a SQL fragment separated by comma.
Note: if fields
is null, "*"
is returned, i.e., all fields are assumed.
if fields
is empty, 1
is returned (so it is easier to construct
a SQL statement).
Note: if a field starts with '(', or a number, we don't encode it with a double quotation. It is used to retrieve a constant, an expression, or anything you prefer not to encode.
For example, you can pass a field as
("assignee" is not null or "due" is null)
.
Furthermore, you can name it (aka., virtual column or calculated column):
("assignee" is not null or "due" is null) alive
Here is an example of use:
access.query('select ${sqlColumns(fields)} from "Foo"');
shortcut
- the table shortcut to prefix the field (column name). If specified, the result will beT."field1",T."field2"
ifshortcut
isT
. Note:shortcut
is case insensitive.
Implementation
String sqlColumns(Iterable<String>? fields, [String? shortcut]) {
if (fields == null)
return "*";
if (fields.isEmpty)
return '1';
assert(fields is Set || fields.toSet().length == fields.length, "Dup? $fields");
final sql = StringBuffer();
bool first = true;
for (final field in fields) {
if (first) first = false;
else sql.write(',');
if (_reExpr.hasMatch(field)) {
sql.write(field);
} else {
if (shortcut != null)
sql..write(shortcut)..write('.');
sql..write('"')..write(field)..write('"');
}
}
return sql.toString();
}