-
Notifications
You must be signed in to change notification settings - Fork 88
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
Support for raw SQL queries #184
Comments
@noahlh this is something I have been wanting to add. I recommend adding a new base class that is a View Only object and supports a select that provides a mapping to the fields. A view model. This is a common pattern that will support any type of results including aggregate group by results like count. We would remove the transactional module to remove save and delete from the object. Let me know what u think. |
@noahlh What about adding a
That way, you can still perform queries:
This will only need to override the default generated |
@drujensen I'm punching slightly above my weight class here so apologies if any of this is malformed, as it were. So are you suggesting something like a new (replying to your other comment shortly) |
That's a very clever idea -- I like it a lot. The only limitation I see is that it (effectively) restricts you to one alternate SELECT per model, but I suppose that's probably a good thing? If you're getting into multiple different methods of populating a given model, you probably need multiple models. Or, perhaps building on what you suggested -- a View Model that sits between the Base model. (In reading that over - I perhaps answered my own question and misunderstood your original suggestion). Thoughts? |
Isn't select
when channel1.receive
puts "Channel1!!!"
when channel2.receive
puts "Channel2!!!"
end |
Right. I think this is a valid restriction for an ORM.
Correct. we will want a View Model that removes the
@faustinoaq yeah, your right. maybe we could use |
Cool - all great thoughts - thanks. I'm going to work on the initial portion of this (the |
Update: Initial PR coming soon. Just writing up final new tests & README update and will submit as soon as complete. |
Done & done with #215 |
Thanks @noahlh |
Proposal for discussion:
One of the things I really like about Granite is the philosophy that you should be able to get into the nitty gritty of SQL and let the ORM handle only the task of mapping the results to the object. The
all
method does a great job of this.I'd like to propose abstracting one level further and adding a method called
sql
(or mayberaw
?) which allows a full SQL query to be passed in and simply maps the results to the appropriate fields of a model.An example based on an issue I ran into the the other night dealing with a legacy database in a project I'm building:
Assume I have a table called
vehicles
as such:makeid
andmodelid
are both references to foreign keys in themakes
andmodels
tables respectively. In those tables are naming columns (calledmakename
andmodelname
).If I want to build a single JOIN query to pull in a vehicle's year, make, and model names, I can construct the following SQL to do so:
There's no way currently to have Granite construct that query -- the JOIN part could be done manually, but by default the SELECT portion can only be constructed to access the fields from a single table (the model's table).
In my proposal, you could have a model as such:
And submit a query as such:
With the resulting
vehicles
object, you could then access the respective fields as you would normally, i.e.I've implemented a monkey-patched version of this in my own application as follows (PG only, for starters)
Thoughts on the above? If this is of interest, I'd be happy to take a stab at a proper PR w/ tests, etc.
The text was updated successfully, but these errors were encountered: