Closed boskokg closed 5 years ago
you can merge two or more fields with custom sql query using by columnsToSelect parameter
I've tested this code in my model:
final productList = await Product().select(columnsToSelect: ['(name || \' \' || description) AS name, isActive,price',]).toList();
printList(productList,isMap: true);
Note: each product's name = Notebook 12" and description changes between 128-512GB
DEBUG OUTPUT:
flutter: {name: Notebook 12" 128 GB SSD i7, isActive: 1, price: 6899.0}
flutter: {name: Notebook 12" 256 GB SSD i7, isActive: 1, price: 8244.0}
flutter: {name: Notebook 12" 512 GB SSD i7, isActive: 1, price: 9214.0}
Thanks. This solution is ok, but not practical in all situations. I can use this method only in aync method, and we need this in the widget's init method that is not async... So I need a solution without using await :-)
Ok. My job is already with the async side! So do you have any suggestions? :-]
Maybe the easiest solution would be to add optional parameter to SqfEntityTable with custom code (as String) that would be added in the generated class?
Okay, I'm gonna think about it. But I need to fix all the issues you've reported before. Is there anything else like throws an exception?
Thanks a lot!!!
I wrote new tickets. If I find something breaking I will report. Thanks again.
is it enough to put your compiled custom code as a string const into the constructor body of your table?
this is your custom method(s):
void setDefaultValues() {
... }
//end of sqfentity methods
// CUSTOM METHODS
String fullName()
{
return '$firstName $lastName';
}
// END CUSTOM METHODS
and your table model is here:
const tableCategory = SqfEntityTable(
tableName: 'person',
// declare fields
fields: [
SqfEntityField('firstName', DbType.text, formIsRequired: true),
SqfEntityField('lastName', DbType.bool, defaultValue: true),
....
],
customCodes: '''
String fullName()
{
return '\$firstName \$lastName';
}
''');
1- I added equalsOrNull keyword for queries Example:
// this query lists only isActive=false
final productList = await Product().select().isActive.not.equals(true).toList();
// but this query lists isActive=false and isActive is null both
final productList = await Product().select().isActive.not.equalsOrNull(true).toList();
2- you can define customCodes property of your SqfEntityTable constant for ex:
const tablePerson = SqfEntityTable(
tableName: 'person',
primaryKeyName: 'id',
primaryKeyType: PrimaryKeyType.integer_auto_incremental,
fields: [
SqfEntityField('firstName', DbType.text),
SqfEntityField('lastName', DbType.text),
],
customCodes: '''
String fullName()
{
return '\$firstName \$lastName';
}
''');
Yes, this is all great. Just one suggestion... I think your parameter should be named customCode, not customCodes.
Alright.. 👍
I have a Person with firstName and lastName and I would like to add a method getFullName. How I can do that?
I have done that by creating a separate class with method getFullMethod(Person), but it would be great to somehow do that without a helper class.
Thanks