| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package dbx |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"reflect" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type BuildHookFunc func(q *Query) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type SelectQuery struct { |
| 18 |
|
| 19 |
FieldMapper FieldMapFunc |
| 20 |
|
| 21 |
TableMapper TableMapFunc |
| 22 |
|
| 23 |
builder Builder |
| 24 |
ctx context.Context |
| 25 |
buildHook BuildHookFunc |
| 26 |
|
| 27 |
preFragment string |
| 28 |
postFragment string |
| 29 |
selects []string |
| 30 |
distinct bool |
| 31 |
selectOption string |
| 32 |
from []string |
| 33 |
where Expression |
| 34 |
join []JoinInfo |
| 35 |
orderBy []string |
| 36 |
groupBy []string |
| 37 |
having Expression |
| 38 |
union []UnionInfo |
| 39 |
limit int64 |
| 40 |
offset int64 |
| 41 |
params Params |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type JoinInfo struct { |
| 46 |
Join string |
| 47 |
Table string |
| 48 |
On Expression |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
type UnionInfo struct { |
| 53 |
All bool |
| 54 |
Query *Query |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func NewSelectQuery(builder Builder, db *DB) *SelectQuery { |
| 59 |
return &SelectQuery{ |
| 60 |
builder: builder, |
| 61 |
selects: []string{}, |
| 62 |
from: []string{}, |
| 63 |
join: []JoinInfo{}, |
| 64 |
orderBy: []string{}, |
| 65 |
groupBy: []string{}, |
| 66 |
union: []UnionInfo{}, |
| 67 |
limit: -1, |
| 68 |
params: Params{}, |
| 69 |
ctx: db.ctx, |
| 70 |
FieldMapper: db.FieldMapper, |
| 71 |
TableMapper: db.TableMapper, |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
func (q *SelectQuery) WithBuildHook(fn BuildHookFunc) *SelectQuery { |
| 77 |
q.buildHook = fn |
| 78 |
return q |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func (q *SelectQuery) Context() context.Context { |
| 83 |
return q.ctx |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (q *SelectQuery) WithContext(ctx context.Context) *SelectQuery { |
| 88 |
q.ctx = ctx |
| 89 |
return q |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func (s *SelectQuery) PreFragment(fragment string) *SelectQuery { |
| 94 |
s.preFragment = fragment |
| 95 |
return s |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func (s *SelectQuery) PostFragment(fragment string) *SelectQuery { |
| 103 |
s.postFragment = fragment |
| 104 |
return s |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func (s *SelectQuery) Select(cols ...string) *SelectQuery { |
| 110 |
s.selects = cols |
| 111 |
return s |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
func (s *SelectQuery) AndSelect(cols ...string) *SelectQuery { |
| 117 |
s.selects = append(s.selects, cols...) |
| 118 |
return s |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
func (s *SelectQuery) Distinct(v bool) *SelectQuery { |
| 124 |
s.distinct = v |
| 125 |
return s |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (s *SelectQuery) SelectOption(option string) *SelectQuery { |
| 130 |
s.selectOption = option |
| 131 |
return s |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
func (s *SelectQuery) From(tables ...string) *SelectQuery { |
| 137 |
s.from = tables |
| 138 |
return s |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (s *SelectQuery) Where(e Expression) *SelectQuery { |
| 143 |
s.where = e |
| 144 |
return s |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func (s *SelectQuery) AndWhere(e Expression) *SelectQuery { |
| 149 |
s.where = And(s.where, e) |
| 150 |
return s |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (s *SelectQuery) OrWhere(e Expression) *SelectQuery { |
| 155 |
s.where = Or(s.where, e) |
| 156 |
return s |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
func (s *SelectQuery) Join(typ string, table string, on Expression) *SelectQuery { |
| 162 |
s.join = append(s.join, JoinInfo{typ, table, on}) |
| 163 |
return s |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func (s *SelectQuery) InnerJoin(table string, on Expression) *SelectQuery { |
| 169 |
return s.Join("INNER JOIN", table, on) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func (s *SelectQuery) LeftJoin(table string, on Expression) *SelectQuery { |
| 175 |
return s.Join("LEFT JOIN", table, on) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (s *SelectQuery) RightJoin(table string, on Expression) *SelectQuery { |
| 181 |
return s.Join("RIGHT JOIN", table, on) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
func (s *SelectQuery) OrderBy(cols ...string) *SelectQuery { |
| 187 |
s.orderBy = cols |
| 188 |
return s |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
func (s *SelectQuery) AndOrderBy(cols ...string) *SelectQuery { |
| 194 |
s.orderBy = append(s.orderBy, cols...) |
| 195 |
return s |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func (s *SelectQuery) GroupBy(cols ...string) *SelectQuery { |
| 201 |
s.groupBy = cols |
| 202 |
return s |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
func (s *SelectQuery) AndGroupBy(cols ...string) *SelectQuery { |
| 208 |
s.groupBy = append(s.groupBy, cols...) |
| 209 |
return s |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func (s *SelectQuery) Having(e Expression) *SelectQuery { |
| 214 |
s.having = e |
| 215 |
return s |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
func (s *SelectQuery) AndHaving(e Expression) *SelectQuery { |
| 220 |
s.having = And(s.having, e) |
| 221 |
return s |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (s *SelectQuery) OrHaving(e Expression) *SelectQuery { |
| 226 |
s.having = Or(s.having, e) |
| 227 |
return s |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
func (s *SelectQuery) Union(q *Query) *SelectQuery { |
| 232 |
s.union = append(s.union, UnionInfo{false, q}) |
| 233 |
return s |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
func (s *SelectQuery) UnionAll(q *Query) *SelectQuery { |
| 238 |
s.union = append(s.union, UnionInfo{true, q}) |
| 239 |
return s |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func (s *SelectQuery) Limit(limit int64) *SelectQuery { |
| 245 |
s.limit = limit |
| 246 |
return s |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
func (s *SelectQuery) Offset(offset int64) *SelectQuery { |
| 252 |
s.offset = offset |
| 253 |
return s |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
func (s *SelectQuery) Bind(params Params) *SelectQuery { |
| 258 |
s.params = params |
| 259 |
return s |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
func (s *SelectQuery) AndBind(params Params) *SelectQuery { |
| 264 |
if len(s.params) == 0 { |
| 265 |
s.params = params |
| 266 |
} else { |
| 267 |
for k, v := range params { |
| 268 |
s.params[k] = v |
| 269 |
} |
| 270 |
} |
| 271 |
return s |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
func (s *SelectQuery) Build() *Query { |
| 276 |
params := Params{} |
| 277 |
for k, v := range s.params { |
| 278 |
params[k] = v |
| 279 |
} |
| 280 |
|
| 281 |
qb := s.builder.QueryBuilder() |
| 282 |
|
| 283 |
clauses := []string{ |
| 284 |
s.preFragment, |
| 285 |
qb.BuildSelect(s.selects, s.distinct, s.selectOption), |
| 286 |
qb.BuildFrom(s.from), |
| 287 |
qb.BuildJoin(s.join, params), |
| 288 |
qb.BuildWhere(s.where, params), |
| 289 |
qb.BuildGroupBy(s.groupBy), |
| 290 |
qb.BuildHaving(s.having, params), |
| 291 |
} |
| 292 |
|
| 293 |
sql := "" |
| 294 |
for _, clause := range clauses { |
| 295 |
if clause != "" { |
| 296 |
if sql == "" { |
| 297 |
sql = clause |
| 298 |
} else { |
| 299 |
sql += " " + clause |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
sql = qb.BuildOrderByAndLimit(sql, s.orderBy, s.limit, s.offset) |
| 305 |
|
| 306 |
if s.postFragment != "" { |
| 307 |
sql += " " + s.postFragment |
| 308 |
} |
| 309 |
|
| 310 |
sql = qb.CombineUnion(sql, qb.BuildUnion(s.union, params)) |
| 311 |
|
| 312 |
query := s.builder.NewQuery(sql).Bind(params).WithContext(s.ctx) |
| 313 |
|
| 314 |
if s.buildHook != nil { |
| 315 |
s.buildHook(query) |
| 316 |
} |
| 317 |
|
| 318 |
return query |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
func (s *SelectQuery) One(a interface{}) error { |
| 329 |
if len(s.from) == 0 { |
| 330 |
if tableName := s.TableMapper(a); tableName != "" { |
| 331 |
s.from = []string{tableName} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
return s.Build().One(a) |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
func (s *SelectQuery) Model(pk, model interface{}) error { |
| 345 |
t := reflect.TypeOf(model) |
| 346 |
if t.Kind() == reflect.Ptr { |
| 347 |
t = t.Elem() |
| 348 |
} |
| 349 |
if t.Kind() != reflect.Struct { |
| 350 |
return VarTypeError("must be a pointer to a struct") |
| 351 |
} |
| 352 |
|
| 353 |
si := getStructInfo(t, s.FieldMapper) |
| 354 |
if len(si.pkNames) == 1 { |
| 355 |
return s.AndWhere(HashExp{si.nameMap[si.pkNames[0]].dbName: pk}).One(model) |
| 356 |
} |
| 357 |
if len(si.pkNames) == 0 { |
| 358 |
return MissingPKError |
| 359 |
} |
| 360 |
|
| 361 |
return CompositePKError |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
func (s *SelectQuery) All(slice interface{}) error { |
| 372 |
if len(s.from) == 0 { |
| 373 |
if tableName := s.TableMapper(slice); tableName != "" { |
| 374 |
s.from = []string{tableName} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return s.Build().All(slice) |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
func (s *SelectQuery) Rows() (*Rows, error) { |
| 384 |
return s.Build().Rows() |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
func (s *SelectQuery) Row(a ...interface{}) error { |
| 390 |
return s.Build().Row(a...) |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
func (s *SelectQuery) Column(a interface{}) error { |
| 397 |
return s.Build().Column(a) |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
type QueryInfo struct { |
| 402 |
PreFragment string |
| 403 |
PostFragment string |
| 404 |
Builder Builder |
| 405 |
Selects []string |
| 406 |
Distinct bool |
| 407 |
SelectOption string |
| 408 |
From []string |
| 409 |
Where Expression |
| 410 |
Join []JoinInfo |
| 411 |
OrderBy []string |
| 412 |
GroupBy []string |
| 413 |
Having Expression |
| 414 |
Union []UnionInfo |
| 415 |
Limit int64 |
| 416 |
Offset int64 |
| 417 |
Params Params |
| 418 |
Context context.Context |
| 419 |
BuildHook BuildHookFunc |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
func (s *SelectQuery) Info() *QueryInfo { |
| 425 |
return &QueryInfo{ |
| 426 |
Builder: s.builder, |
| 427 |
PreFragment: s.preFragment, |
| 428 |
PostFragment: s.postFragment, |
| 429 |
Selects: s.selects, |
| 430 |
Distinct: s.distinct, |
| 431 |
SelectOption: s.selectOption, |
| 432 |
From: s.from, |
| 433 |
Where: s.where, |
| 434 |
Join: s.join, |
| 435 |
OrderBy: s.orderBy, |
| 436 |
GroupBy: s.groupBy, |
| 437 |
Having: s.having, |
| 438 |
Union: s.union, |
| 439 |
Limit: s.limit, |
| 440 |
Offset: s.offset, |
| 441 |
Params: s.params, |
| 442 |
Context: s.ctx, |
| 443 |
BuildHook: s.buildHook, |
| 444 |
} |
| 445 |
} |