rails / arel

A Relational Algebra
2.06k stars 390 forks source link

Add temporary tables #478

Closed alexmobo closed 6 years ago

alexmobo commented 7 years ago

With this we could do something like

clients = Arel::Subquery.new('SELECT * FROM users WHERE type="client"', as: 'clients')

or

users = Arel::Table.new(:users)
clients = Arel::Subquery.new(users.project(Arel.star).where(users[:type].eq('type')), as: 'clients')

to do something like

clients.project(Arel.star)

to produce the sql next output

SELECT * FROM (SELECT * FROM users WHERE type="client") clients

also if we write something like

clients = Arel::Subquery.new('SELECT * FROM users')

without the as keyword argument, we get the same as before but with a 10-length random name for the table (i.e.)

SELECT * FROM (SELECT * FROM users) htjdnyehrn
mstahl commented 7 years ago

👍

webandtech commented 7 years ago

👍

bdevel commented 7 years ago

Technically speaking, that SQL is not making a temporary table - it is a subquery. As such, I would recommend re-naming the class to Arel::Subquery and reserve Arel::TemporaryTable for true temporary tables to avoid confusion.

Further, theres more than one way to create temporary tables (depending on db provider). Examples:

Currently, I'm currently having trouble using AREL to create such a temporary table with AREL when common table expressions (CTE) have been applied.

alexmobo commented 7 years ago

I have changed TemporaryTable to Subquery as @bdevel commented.

matthewd commented 6 years ago

Per #523, Arel development is moving to rails/rails.

If this PR is still relevant, please consider reopening it over there.