db icon indicating copy to clipboard operation
db copied to clipboard

Query builder for .Union() and .UnionAll()

Open VojtechVitek opened this issue 8 years ago • 4 comments

Feature request for the query builder:

res1 := col.Find(db.Cond{"name": "Max"}).Limit(10).Sort("-last_name")
res2 := col.Find(db.Cond{"name": "Rex", "age >": "21"}).Limit(15).Sort("-last_name")

err := db.Union(res1, res2).All(&people)

Related: #105

VojtechVitek avatar Sep 28 '15 19:09 VojtechVitek

I think Union() is a good thing to add to the SQL query builder, but I think in upper the generic builder via .Find() should be reserved to a subset of features just for doing the most basic conditions and using the sql query builder for all advanced queries, which is where Union would go.

pkieltyka avatar Sep 28 '15 22:09 pkieltyka

Now that the query builder supports query expansion we can add Union()/UnionAll() methods to the builder and do something like:

sel1 := sess.SelectFrom()...
sel2 := sess.SelectFrom()...
sess.Union(sel1, sel2)

xiam avatar Dec 16 '16 18:12 xiam

I think we can make this work with db.Result too, as you suggested @VojtechVitek. Let's do both.

xiam avatar Dec 16 '16 19:12 xiam

@xiam Would be interesting to see if this feature can be implemented in the near future. I think it is worth to add Union()/UnionAll() support to SQL-builder as it seems to be low level/custom SQL manipulation. The result would be the same Selector interface with all cool things like pagination.

Also, seems to be logical to add Intersect/IntersectAll and Minus/MinusAll (aka EXCEPT in postgres) to have all result set operation in SQL-builder.

dencoded avatar Dec 13 '19 15:12 dencoded