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

Allow query to return objects not entities #130

Closed
megasoft78 opened this issue May 4, 2019 · 9 comments
Closed

Allow query to return objects not entities #130

megasoft78 opened this issue May 4, 2019 · 9 comments

Comments

@megasoft78
Copy link

Hi,
I'm using your amazing package for my first app on Flutter and I'm having an issue.
I want to query the data and return the result of these queries into some objects that are not table.

For instance if I want to have the result of a group by query like this one:

Select R.roomName, count(*) as numberOfStudents
from tblRooms R
Inner Join tblStudents S on (S.roomId=R.roomId)
Group By R.roomName

I need the possibility to setup a class like this:

@View()
class RoomStudentsCountView {
  String roomName;
  int numberOfStudents;

  RoomStudentsCountView (this.roomName, this.numberOfStudents);
}

I replaced the @entity with @view as alternative to describe this type of entity.

I also find sometime that I need the possibility to define field that are not column of the table but just calculated field.
Here's an example:

@Entity(tableName: "tblRooms")
class RoomEntity {
  @PrimaryKey(autoGenerate: true)
  int roomId;
  String roomName;

  @CalculatedField()
  int numberOfStudents;

  RoomEntity (this.roomId, this.roomName, this.numberOfStudents);
}

I can try to help by looking at the code and try to adapt it but I'm not sure I have enough experience to make it right.

I hope you can help on this. :)

@vitusortner
Copy link
Collaborator

Hi @megasoft78! I'm happy to see your interest in the package. To your first request: It's planned to support returning objects for queries that are not entities. #33 holds some more information about this feature request. To your second request: This behavior is also planned. #12 describes the feature.

I've seen you forked the repository. Do you plan on implementing the behavior? Would be great to see a PR!

@megasoft78
Copy link
Author

Hi @vitusortner,
I'm new to Flutter and Dart as I mainly work on C# but I'm looking to implement #12.
My idea is to add a new annotation to ColumnInfo : readonly. I did that but I'm learning where to change the code in order to achieve this feature.
I'm looking for all the CRUD operations and skip the field with readonly annotation for Create, Update and Delete.
Could you give me some info on where to look and how is structured?

@mqus
Copy link
Collaborator

mqus commented Feb 12, 2020

Hi! the current state of android development pushed me to flutter and yesterday I found your library which seems to perfectly replace room. I would really like to see the @DatabaseViews in floor like I had them in room:

@DatabaseView("SELECT collection, artist AS name, group_concat(DISTINCT genre) AS genres, count(*) AS count FROM files GROUP BY artist, collection")
public class Artist {
	String collection;
	String name;
	String genres;
	int count;
}

I just got into flutter/dart yesterday, so I can't really help in implementing this but I would really like to see some progress here!

(I know, this is just a wordy +1, but I really want to motivate it!)

mqus added a commit to mqus/floor that referenced this issue Feb 15, 2020
This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements pinchbv#130.
mqus added a commit to mqus/floor that referenced this issue Feb 23, 2020
This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements pinchbv#130.
mqus added a commit to mqus/floor that referenced this issue Mar 11, 2020
This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements pinchbv#130.

fix linting error
mqus added a commit to mqus/floor that referenced this issue Mar 14, 2020
This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements pinchbv#130.

fix linting error
mqus added a commit to mqus/floor that referenced this issue Mar 14, 2020
This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements pinchbv#130.

fix linting error
vitusortner pushed a commit that referenced this issue Mar 15, 2020
* Feature: Add support for @DatabaseView annotations

This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements #130.

fix linting error

* Clean up Uint8List/Blob<->View interop

* Add unit tests

* Add integration tests for view

* Fix issues from review

- Do not allow queries from views to be streamed
	- throw error if a user tries to do that and test that error
	- remove Stream<> queries from DAO in integration test
	- getStreamEntities will only return entities again

- Create new Superclass for EntityProcessor and ViewProcessor named QueryableProcessor to deduplicate common functions

- Code cleanup
	- clean up integration tests
	- improve code layout and comment wording in annotations, DatabaseProcessor, QueryMethodProcessor
	- fix toString and hashCode methods in Database (value object)
	- improve error message wording in DatabaseViewError

* Adapt to upstream changes,split integrations tests

* Fix from review part 2 (tests & documentation)

- Clean up small details in code, according to the review

- Improve tests
 - Simplify integration tests
 - split off and merge common QueryableProcessor tests into a separate file
 - move createClassElement function, which is now used in three test files to testutils

- Add documentation to README.md
@mqus
Copy link
Collaborator

mqus commented Mar 15, 2020

I think this can be closed now, as 90% of the DatabaseView features are implemented and the remaining 10% (Streams on Views) open a whole new can of worms :)

@vitusortner vitusortner mentioned this issue May 3, 2020
9 tasks
@mqus
Copy link
Collaborator

mqus commented May 5, 2020

I'm closing this as this seems to be adressed now, with Streams of DatabaseView queries also coming soon.

@mqus mqus closed this as completed May 5, 2020
@mqus
Copy link
Collaborator

mqus commented May 5, 2020

The general issue (parsing the results of arbitrary queries into objects) is tracked in #94

@moak13
Copy link

moak13 commented Aug 3, 2020

Hi, I have a question. Am not totally sound on this but need assistance. I have entities [A, B] created and would like to know how to implement a Join by id from B (child) to A (parent), where both hold a list of items.

@mqus
Copy link
Collaborator

mqus commented Aug 4, 2020

You could create a database view which has the join as the query and its result columbs as fields. You can then make a query on that view to return the results of that join.

@ulugbekusmanov
Copy link

Hi! the current state of android development pushed me to flutter and yesterday I found your library which seems to perfectly replace room. I would really like to see the @DatabaseViews in floor like I had them in room:

@DatabaseView("SELECT collection, artist AS name, group_concat(DISTINCT genre) AS genres, count(*) AS count FROM files GROUP BY artist, collection")
public class Artist {
	String collection;
	String name;
	String genres;
	int count;
}

I just got into flutter/dart yesterday, so I can't really help in implementing this but I would really like to see some progress here!

(I know, this is just a wordy +1, but I really want to motivate it!)

do yo have full example for left join

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

5 participants