selectQuery returns a SQL query for the given query
(query *protocol.KontrolQuery)
| 332 | |
| 333 | // selectQuery returns a SQL query for the given query |
| 334 | func selectQuery(query *protocol.KontrolQuery) (string, []interface{}, error) { |
| 335 | psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar) |
| 336 | |
| 337 | kites := psql.Select("*").From("kite.kite") |
| 338 | fields := query.Fields() |
| 339 | andQuery := sq.And{} |
| 340 | |
| 341 | // we stop for the first empty value |
| 342 | for _, key := range keyOrder { |
| 343 | v := fields[key] |
| 344 | if v == "" { |
| 345 | continue |
| 346 | } |
| 347 | |
| 348 | // we are using "kitename" as the columname |
| 349 | if key == "name" { |
| 350 | key = "kitename" |
| 351 | } |
| 352 | |
| 353 | andQuery = append(andQuery, sq.Eq{key: v}) |
| 354 | } |
| 355 | |
| 356 | if len(andQuery) == 0 { |
| 357 | return "", nil, ErrQueryFieldsEmpty |
| 358 | } |
| 359 | |
| 360 | return kites.Where(andQuery).ToSql() |
| 361 | } |
| 362 | |
| 363 | // inseryKiteQuery inserts the given kite, url and key to the kite.kite table |
| 364 | func insertKiteQuery(kiteProt *protocol.Kite, url, keyId string) (string, []interface{}, error) { |