Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return last inserted ID/s #44

Closed
3 tasks done
vitusortner opened this issue Feb 12, 2019 · 7 comments
Closed
3 tasks done

Return last inserted ID/s #44

vitusortner opened this issue Feb 12, 2019 · 7 comments
Assignees
Milestone

Comments

@vitusortner
Copy link
Collaborator

vitusortner commented Feb 12, 2019

Whenever inserting objects open up the possibility to return the inserted ID or IDs in case a list is getting inserted.
Investigate what Room returns on update and delete and decide if support these as well.

  • Insert
  • Update
  • Delete
@vitusortner vitusortner modified the milestones: Version 1.0, 1 Feb 12, 2019
@vitusortner vitusortner self-assigned this Feb 15, 2019
vitusortner added a commit that referenced this issue Feb 15, 2019
vitusortner added a commit that referenced this issue Mar 9, 2019
* Add configuration files

* Add submodules example and floor generator

* Add license (#14)

* Add code generation for database creation (#13)

Resolves #2

* Add static function to instantiate database (#15)

* Add code generator for query methods (#17)

* Singularize directory names (#18)

* Database writer improvements (#19)

* Add insert methods code generation (#20)

* Add update methods code generation (#21)

* Add delete methods code generation and fix update methods (#22)

* Improve change method generators (#24)

* Add test setup and test for type utils (#26)

* Add Travis CI config (#27)

* Update README with note to not use the library (#28)

* Cleanup code and add simple string escaping in SQL statements (#29)

* Return empty list of empty list query result (#32)

* Add database tests (#34)

* Fix grammar in README (#35)

* Exclude hashCode from mapping (#38)

* Enable custom column name mapping (#39)

* Enable NOT NULL columns (#40)

* Support custom entity name (#41)

* Add support for changing (insert, update, delete) lists (#42)

* Add LICENSE file (#43)

* Add support for transactions (#49)

* Return ID/s of inserted item/s (#51)

#44

* Return updated rows count on update (#52)

* Return deleted row count on delete (#53)

* Only use async modifier when necessary (#54)

* Make all method/constructor parameters final (#55)

* Cleanup example app (#56)

* Add parameter verification for query methods (#57)

Resolves #50.

* Add support for foreign keys (#59)

* Add documentation on how to use the library (#60)

* Add support for running queries that return void (#61)

* Generate CREATE TABLE statements in models instead of writer (#63)

* Update README (#64)

* Add release drafter configuration (#65)

* Update dependencies (#66)

* Support conflict strategies when inserting or updating records (#67)

*  Move annotations to separate module to make the generator testable (#68)

* Enable CI (analyzer, dartfmt) for annotations module (#69)

* Add more tests for insert method writer, database writer (#71)

* Update version number to 0.1.0 (#72)

* Update README (#73)

* Add support for migrations (#74)

Resolves #8.

* Add database adapters (#75)

* Run Flutter tests on CI (#76)

* Add support for returning Streams as query result (#77)

* Add CHANGELOG (#78)

*  Support accessing data from Data Access Objects (#82)

* Move value objects to value_objects directory (#83)

* Map elements into value objects in processors (#85)

* Add entity classes to database annotation (#86)

* Add files for release (#89)
@vitusortner vitusortner modified the milestones: 1, Version 1.0 Jun 16, 2019
@cassioseffrin
Copy link
Contributor

This feature is already available in last version 0.12.0? If yes, how can I perform it?

@mqus
Copy link
Collaborator

mqus commented May 15, 2020

You can use Future<int> or Future<List<int>> as a return type for @insert DAO functions, e.g.

@insert
Future<int> insertPerson(Person person);

will return a primary id which was generated and used by sqlite.
Of course, this will only work if your primary key is of type integer, but I think that only integer primary keys can be generated anyway. The id(or whatever you named it) field of those Person objects will not have changed, you would have to do that yourself if you want.

See also the README

@cassioseffrin
Copy link
Contributor

cassioseffrin commented May 18, 2020

In the previous version if I am not wrong the returned int value was 0 or 1 (true or false), just to represent if the insert/update were done/committed without errors. Now it's pretty better providing the accuracy ID information. But seems to be an issue in @update annotation.

In @update the floor API seems to returning always 1 instead of the ID. In @insert it's Ok returning the correct Id.

#personDao.dart
@update
Future updatePerson(Person person);

#instantiating the dao
id = personDao.updatePerson(_person);

In that case, the id is always == 1, instead of the committed _person.id

@mqus
Copy link
Collaborator

mqus commented May 18, 2020

In the case of @update and @delete it will return the count of changed rows, not the id. This last inserted ID in this ticket only applies to @insert. And as the (admittedly short) issue description says is

Investigate what Room returns on update and delete and decide if support these as well.

And this is (at least from my PoV) the amount of rows that were changed by the operation, just like the titles of the referenced PRs indicate.

If you want the id of your updated row(s), look inside the id field of the Entity you provided to the update function, as this will not be changed(in the db and in your app) by either update nor delete operations.

@cassioseffrin
Copy link
Contributor

@mqus ok, it was very clear. Really this way similar to Room got better, thks!

@mqus
Copy link
Collaborator

mqus commented May 18, 2020

And honestly, if you had read the README link I gave you, this would have been obvious:

@update marks a method as an update method. When using the capitalized @update you can specify a conflict strategy. Else it just defaults to aborting the update. These methods can return a Future of either void or int.

void return nothing
int return number of changed rows

@delete marks a method as a deletion method. These methods can return a Future of either void or int.

void return nothing
int return number of deleted rows

@cassioseffrin
Copy link
Contributor

Yeap indeed. I didn't understand correctly. It's very clear in docs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants