avram icon indicating copy to clipboard operation
avram copied to clipboard

Allow creating QueryBuilder with "from" as result of subselect

Open matthewmcgarvey opened this issue 4 years ago • 1 comments

Overview

Right now the "from" is set by the passed in table name in the initializer https://github.com/luckyframework/avram/blob/72ac1d684fb4624defe65495e4854818f1041306/src/avram/query_builder.cr#L18-L19

That's not the only way to specify a from in sql, though.

I would like to update the QueryBuilder to keep track of a from instead of table and remove the setting of table in the initialize.

I believe all that would need to change for existing usage to work is to call .from instead of passing the table name into the initialize in the Queryable here https://github.com/luckyframework/avram/blob/72ac1d684fb4624defe65495e4854818f1041306/src/avram/queryable.cr#L48

I would like to add an overloaded from that takes in a QueryBuilder and an alias which would translate to the SQL from (query_builder statement) as ALIAS

Why?

I would like to see Avram have robust layers. You don't need a full model and queryable for this particular sql query? Fine, just use the QueryBuilder for a bit of a nicer experience. And that would be without sacrificing any usability of the higher level constructs.

Ultimately, I would like to be able to experiment with different higher level patterns and need this lower layer to be more flexible.

matthewmcgarvey avatar Jun 07 '21 20:06 matthewmcgarvey

A blocker for this change is that update and insert don't use "from" so a table_name makes sense. For my goals, I think there would have to be a way to break up the different types of sql statements. I'll keep looking into it

matthewmcgarvey avatar Jun 08 '21 20:06 matthewmcgarvey