github.com/acoshift/pgsql@v0.15.3/pgstmt/union.go (about) 1 package pgstmt 2 3 func Union(f func(b UnionStatement)) *Result { 4 var st unionStmt 5 f(&st) 6 return newResult(build(st.make())) 7 } 8 9 type UnionStatement interface { 10 Select(f func(b SelectStatement)) 11 AllSelect(f func(b SelectStatement)) 12 Union(f func(b UnionStatement)) 13 AllUnion(f func(b UnionStatement)) 14 OrderBy(col string) OrderBy 15 Limit(n int64) 16 Offset(n int64) 17 } 18 19 type unionStmt struct { 20 b buffer 21 orderBy group 22 limit *int64 23 offset *int64 24 } 25 26 func (st *unionStmt) Select(f func(b SelectStatement)) { 27 var x selectStmt 28 f(&x) 29 30 if st.b.empty() { 31 st.b.push(paren(x.make())) 32 } else { 33 st.b.push("union", paren(x.make())) 34 } 35 } 36 37 func (st *unionStmt) AllSelect(f func(b SelectStatement)) { 38 var x selectStmt 39 f(&x) 40 41 if st.b.empty() { 42 st.b.push(paren(x.make())) 43 } else { 44 st.b.push("union all", paren(x.make())) 45 } 46 } 47 48 func (st *unionStmt) Union(f func(b UnionStatement)) { 49 var x unionStmt 50 f(&x) 51 52 if st.b.empty() { 53 st.b.push(paren(x.make())) 54 } else { 55 st.b.push("union", paren(x.make())) 56 } 57 } 58 59 func (st *unionStmt) AllUnion(f func(b UnionStatement)) { 60 var x unionStmt 61 f(&x) 62 63 if st.b.empty() { 64 st.b.push(paren(x.make())) 65 } else { 66 st.b.push("union all", paren(x.make())) 67 } 68 } 69 70 func (st *unionStmt) OrderBy(col string) OrderBy { 71 p := orderBy{ 72 col: col, 73 } 74 st.orderBy.push(&p) 75 return &p 76 } 77 78 func (st *unionStmt) Limit(n int64) { 79 st.limit = &n 80 } 81 82 func (st *unionStmt) Offset(n int64) { 83 st.offset = &n 84 } 85 86 func (st *unionStmt) make() *buffer { 87 var b buffer 88 b.push(&st.b) 89 if !st.orderBy.empty() { 90 b.push("order by", &st.orderBy) 91 } 92 if st.limit != nil { 93 b.push("limit", *st.limit) 94 } 95 if st.offset != nil { 96 b.push("offset", *st.offset) 97 } 98 return &b 99 }