Describe the bug
I created a fetch function that receives a custom expression content:
Future<List<D>> fetch<T extends Table, D extends DataClass>(
{TableInfo table, String whereExp}) async {
final statement = select<T, D>(table);
if (whereExp != null) {
statement..where((Table u) {
return CustomExpression(whereExp);
});
}
return statement.get();
}
if I pass whereExp as 'columnName = valueName'
then I get this exception:
Dart Unhandled Exception: "SqliteException(1): no such column: valueName, SQL logic error (code 1)"
What am I doing wrong here?
Thanks in advance!
Well, does the table have a column named valueName? Judging by your other issue it might just be called value_name?
Nono... when I say valueName I mean the value that I'm trying to compare the column to...
I'm basically trying to create a select statement like this:
column_list
FROM
table
WHERE
columnName = valueName; // e.g total_people = 100
Oh, I see! But passing whereExp: 'total_people = 100' should work then? If you want to pass the column and the target value as two different parameters, you could also use this:
Future<List<D>> fetch<T extends Table, D extends DataClass>({
required TableInfo<T, D> table,
String? column,
Object? value,
}) async {
final statement = select<T, D>(table);
if (column != null) {
statement.where((T row) {
final col = table.columnsByName[column]!;
return col.equals(value);
});
}
return statement.get();
}
Yes that works perfect! I made a change that works best for my use case:
Future<List<D>> fetch<T extends Table, D extends DataClass>({
@required TableInfo<T, D> table,
Map<String, dynamic> params
}) async {
final statement = select<T, D>(table);
params.keys.forEach((key) {
statement.where((T row) {
final col = table.columnsByName[key];
return col.equals(params[key]);
});
});
return statement.get();
}
But I was not aware of the columnsByName getter thanks!
Most helpful comment
Oh, I see! But passing
whereExp: 'total_people = 100'should work then? If you want to pass the column and the target value as two different parameters, you could also use this: