github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/query_gen/micro_builders.go (about) 1 package qgen 2 3 type dateCutoff struct { 4 Column string 5 Quantity int 6 Unit string 7 Type int 8 } 9 10 type prebuilder struct { 11 adapter Adapter 12 } 13 14 func (b *prebuilder) Select(nlist ...string) *selectPrebuilder { 15 name := optString(nlist, "") 16 return &selectPrebuilder{name, "", "", "", "", "", nil, nil, "", b.adapter} 17 } 18 19 func (b *prebuilder) Count(nlist ...string) *selectPrebuilder { 20 name := optString(nlist, "") 21 return &selectPrebuilder{name, "", "COUNT(*)", "", "", "", nil, nil, "", b.adapter} 22 } 23 24 func (b *prebuilder) Insert(nlist ...string) *insertPrebuilder { 25 name := optString(nlist, "") 26 return &insertPrebuilder{name, "", "", "", b.adapter} 27 } 28 29 func (b *prebuilder) Update(nlist ...string) *updatePrebuilder { 30 name := optString(nlist, "") 31 return &updatePrebuilder{name, "", "", "", nil, nil, b.adapter} 32 } 33 34 func (b *prebuilder) Delete(nlist ...string) *deletePrebuilder { 35 name := optString(nlist, "") 36 return &deletePrebuilder{name, "", "", nil, b.adapter} 37 } 38 39 type deletePrebuilder struct { 40 name string 41 table string 42 where string 43 dateCutoff *dateCutoff 44 45 build Adapter 46 } 47 48 func (b *deletePrebuilder) Table(table string) *deletePrebuilder { 49 b.table = table 50 return b 51 } 52 53 func (b *deletePrebuilder) Where(where string) *deletePrebuilder { 54 if b.where != "" { 55 b.where += " AND " 56 } 57 b.where += where 58 return b 59 } 60 61 // TODO: We probably want to avoid the double allocation of two builders somehow 62 func (b *deletePrebuilder) FromAcc(acc *accDeleteBuilder) *deletePrebuilder { 63 b.table = acc.table 64 b.where = acc.where 65 b.dateCutoff = acc.dateCutoff 66 return b 67 } 68 69 func (b *deletePrebuilder) Text() (string, error) { 70 return b.build.SimpleDelete(b.name, b.table, b.where) 71 } 72 73 func (b *deletePrebuilder) Parse() { 74 b.build.SimpleDelete(b.name, b.table, b.where) 75 } 76 77 type updatePrebuilder struct { 78 name string 79 table string 80 set string 81 where string 82 dateCutoff *dateCutoff // We might want to do this in a slightly less hacky way 83 whereSubQuery *selectPrebuilder 84 85 build Adapter 86 } 87 88 func qUpdate(table string, set string, where string) *updatePrebuilder { 89 return &updatePrebuilder{table: table, set: set, where: where} 90 } 91 92 func (b *updatePrebuilder) Table(table string) *updatePrebuilder { 93 b.table = table 94 return b 95 } 96 97 func (b *updatePrebuilder) Set(set string) *updatePrebuilder { 98 b.set = set 99 return b 100 } 101 102 func (b *updatePrebuilder) Where(where string) *updatePrebuilder { 103 if b.where != "" { 104 b.where += " AND " 105 } 106 b.where += where 107 return b 108 } 109 110 func (b *updatePrebuilder) WhereQ(sel *selectPrebuilder) *updatePrebuilder { 111 b.whereSubQuery = sel 112 return b 113 } 114 115 func (b *updatePrebuilder) Text() (string, error) { 116 return b.build.SimpleUpdate(b) 117 } 118 119 func (b *updatePrebuilder) Parse() { 120 b.build.SimpleUpdate(b) 121 } 122 123 type selectPrebuilder struct { 124 name string 125 table string 126 columns string 127 where string 128 orderby string 129 limit string 130 dateCutoff *dateCutoff 131 inChain *selectPrebuilder 132 inColumn string // for inChain 133 134 build Adapter 135 } 136 137 func (b *selectPrebuilder) Table(table string) *selectPrebuilder { 138 b.table = table 139 return b 140 } 141 142 func (b *selectPrebuilder) Columns(columns string) *selectPrebuilder { 143 b.columns = columns 144 return b 145 } 146 147 func (b *selectPrebuilder) Where(where string) *selectPrebuilder { 148 if b.where != "" { 149 b.where += " AND " 150 } 151 b.where += where 152 return b 153 } 154 155 func (b *selectPrebuilder) InQ(subBuilder *selectPrebuilder) *selectPrebuilder { 156 b.inChain = subBuilder 157 return b 158 } 159 160 func (b *selectPrebuilder) Orderby(orderby string) *selectPrebuilder { 161 b.orderby = orderby 162 return b 163 } 164 165 func (b *selectPrebuilder) Limit(limit string) *selectPrebuilder { 166 b.limit = limit 167 return b 168 } 169 170 // TODO: We probably want to avoid the double allocation of two builders somehow 171 func (b *selectPrebuilder) FromAcc(acc *AccSelectBuilder) *selectPrebuilder { 172 b.table = acc.table 173 if acc.columns != "" { 174 b.columns = acc.columns 175 } 176 b.where = acc.where 177 b.orderby = acc.orderby 178 b.limit = acc.limit 179 180 b.dateCutoff = acc.dateCutoff 181 if acc.inChain != nil { 182 b.inChain = &selectPrebuilder{"", acc.inChain.table, acc.inChain.columns, acc.inChain.where, acc.inChain.orderby, acc.inChain.limit, acc.inChain.dateCutoff, nil, "", b.build} 183 b.inColumn = acc.inColumn 184 } 185 return b 186 } 187 188 func (b *selectPrebuilder) FromCountAcc(acc *accCountBuilder) *selectPrebuilder { 189 b.table = acc.table 190 b.where = acc.where 191 b.limit = acc.limit 192 193 b.dateCutoff = acc.dateCutoff 194 if acc.inChain != nil { 195 b.inChain = &selectPrebuilder{"", acc.inChain.table, acc.inChain.columns, acc.inChain.where, acc.inChain.orderby, acc.inChain.limit, acc.inChain.dateCutoff, nil, "", b.build} 196 b.inColumn = acc.inColumn 197 } 198 return b 199 } 200 201 // TODO: Add support for dateCutoff 202 func (b *selectPrebuilder) Text() (string, error) { 203 return b.build.SimpleSelect(b.name, b.table, b.columns, b.where, b.orderby, b.limit) 204 } 205 206 // TODO: Add support for dateCutoff 207 func (b *selectPrebuilder) Parse() { 208 b.build.SimpleSelect(b.name, b.table, b.columns, b.where, b.orderby, b.limit) 209 } 210 211 type insertPrebuilder struct { 212 name string 213 table string 214 columns string 215 fields string 216 217 build Adapter 218 } 219 220 func (b *insertPrebuilder) Table(table string) *insertPrebuilder { 221 b.table = table 222 return b 223 } 224 225 func (b *insertPrebuilder) Columns(columns string) *insertPrebuilder { 226 b.columns = columns 227 return b 228 } 229 230 func (b *insertPrebuilder) Fields(fields string) *insertPrebuilder { 231 b.fields = fields 232 return b 233 } 234 235 func (b *insertPrebuilder) Text() (string, error) { 236 return b.build.SimpleInsert(b.name, b.table, b.columns, b.fields) 237 } 238 239 func (b *insertPrebuilder) Parse() { 240 b.build.SimpleInsert(b.name, b.table, b.columns, b.fields) 241 } 242 243 /*type countPrebuilder struct { 244 name string 245 table string 246 where string 247 limit string 248 249 build Adapter 250 } 251 252 func (b *countPrebuilder) Table(table string) *countPrebuilder { 253 b.table = table 254 return b 255 } 256 257 func b *countPrebuilder) Where(where string) *countPrebuilder { 258 if b.where != "" { 259 b.where += " AND " 260 } 261 b.where += where 262 return b 263 } 264 265 func (b *countPrebuilder) Limit(limit string) *countPrebuilder { 266 b.limit = limit 267 return b 268 } 269 270 func (b *countPrebuilder) Text() (string, error) { 271 return b.build.SimpleCount(b.name, b.table, b.where, b.limit) 272 } 273 274 func (b *countPrebuilder) Parse() { 275 b.build.SimpleCount(b.name, b.table, b.where, b.limit) 276 }*/ 277 278 func optString(nlist []string, defaultStr string) string { 279 if len(nlist) == 0 { 280 return defaultStr 281 } 282 return nlist[0] 283 }