hhtokpinar / sqfEntity

SqfEntity ORM for Flutter/Dart lets you build and execute SQL commands on SQLite database easily and quickly with the help of fluent methods similar to .Net Entity Framework. SqfEntity also generates add/edit forms with validations and special controls (DropDown List, DateTime pickers, Checkboxes.. etc) for your table.
379 stars 100 forks source link

How to add custom methods to generated classes #43

Closed boskokg closed 5 years ago

boskokg commented 5 years ago

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

hhtokpinar commented 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}
boskokg commented 5 years ago

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 :-)

hhtokpinar commented 5 years ago

Ok. My job is already with the async side! So do you have any suggestions? :-]

boskokg commented 5 years ago

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?

hhtokpinar commented 5 years ago

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?

boskokg commented 5 years ago

Thanks a lot!!!

boskokg commented 5 years ago

I wrote new tickets. If I find something breaking I will report. Thanks again.

hhtokpinar commented 5 years ago

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';
     }
    ''');
hhtokpinar commented 5 years ago

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';
        }
    ''');
boskokg commented 5 years ago

Yes, this is all great. Just one suggestion... I think your parameter should be named customCode, not customCodes.

hhtokpinar commented 5 years ago

Alright.. 👍