github.com/acoshift/pgsql@v0.15.3/pgstmt/update.go (about) 1 package pgstmt 2 3 // Update builds update statement 4 func Update(f func(b UpdateStatement)) *Result { 5 var st updateStmt 6 f(&st) 7 return newResult(build(st.make())) 8 } 9 10 type UpdateStatement interface { 11 Table(table string) 12 Set(col ...string) Set 13 From(table ...string) 14 Join(table string) Join 15 InnerJoin(table string) Join 16 FullOuterJoin(table string) Join 17 LeftJoin(table string) Join 18 RightJoin(table string) Join 19 Where(f func(b Cond)) 20 WhereCurrentOf(cursor string) 21 Returning(col ...string) 22 } 23 24 type Set interface { 25 To(value ...any) 26 ToRaw(rawValue ...any) 27 Select(f func(b SelectStatement)) 28 } 29 30 type updateStmt struct { 31 table string 32 sets group 33 from group 34 joins buffer 35 where cond 36 whereCurrentOf string 37 returning group 38 } 39 40 func (st *updateStmt) Table(table string) { 41 st.table = table 42 } 43 44 func (st *updateStmt) Set(col ...string) Set { 45 var x set 46 x.col.pushString(col...) 47 st.sets.push(&x) 48 return &x 49 } 50 51 func (st *updateStmt) From(table ...string) { 52 st.from.pushString(table...) 53 } 54 55 func (st *updateStmt) join(typ, table string) Join { 56 var b buffer 57 b.push(table) 58 x := join{ 59 typ: typ, 60 table: &b, 61 } 62 st.joins.push(&x) 63 return &x 64 } 65 66 func (st *updateStmt) Join(table string) Join { 67 return st.join("join", table) 68 } 69 70 func (st *updateStmt) InnerJoin(table string) Join { 71 return st.join("inner join", table) 72 } 73 74 func (st *updateStmt) FullOuterJoin(table string) Join { 75 return st.join("full outer join", table) 76 } 77 78 func (st *updateStmt) LeftJoin(table string) Join { 79 return st.join("left join", table) 80 } 81 82 func (st *updateStmt) RightJoin(table string) Join { 83 return st.join("right join", table) 84 } 85 86 func (st *updateStmt) Where(f func(b Cond)) { 87 f(&st.where) 88 } 89 90 func (st *updateStmt) WhereCurrentOf(cursor string) { 91 st.whereCurrentOf = cursor 92 } 93 94 func (st *updateStmt) Returning(col ...string) { 95 st.returning.pushString(col...) 96 } 97 98 func (st *updateStmt) make() *buffer { 99 var b buffer 100 b.push("update") 101 if st.table != "" { 102 b.push(st.table) 103 } 104 if !st.sets.empty() { 105 b.push("set", &st.sets) 106 } 107 if !st.from.empty() { 108 b.push("from", &st.from) 109 } 110 if !st.joins.empty() { 111 b.push(&st.joins) 112 } 113 if !st.where.empty() { 114 b.push("where", &st.where) 115 } 116 if st.whereCurrentOf != "" { 117 b.push("where current of", st.whereCurrentOf) 118 } 119 if !st.returning.empty() { 120 b.push("returning", &st.returning) 121 } 122 return &b 123 } 124 125 type set struct { 126 col group 127 to group 128 } 129 130 func (st *set) To(value ...any) { 131 for _, v := range value { 132 st.to.push(Arg(v)) 133 } 134 } 135 136 func (st *set) ToRaw(rawValue ...any) { 137 st.to.push(rawValue...) 138 } 139 140 func (st *set) Select(f func(b SelectStatement)) { 141 var x selectStmt 142 f(&x) 143 st.to.push(paren(x.make())) 144 } 145 146 func (st *set) build() []any { 147 var b buffer 148 if len(st.col.q) > 1 { 149 b.push(paren(&st.col)) 150 } else { 151 b.push(&st.col) 152 } 153 b.push("=") 154 if len(st.to.q) > 1 { 155 var p parenGroup 156 p.prefix = "row" 157 p.push(&st.to) 158 b.push(&p) 159 } else { 160 b.push(&st.to) 161 } 162 return b.q 163 }