2017-11-12 05:25:04 +00:00
|
|
|
package qgen
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
type dateCutoff struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
Column string
|
|
|
|
Quantity int
|
|
|
|
Unit string
|
|
|
|
Type int
|
2018-01-03 07:46:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 00:31:46 +00:00
|
|
|
type prebuilder struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
adapter Adapter
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 01:58:04 +00:00
|
|
|
func (b *prebuilder) Select(nlist ...string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
name := optString(nlist, "")
|
|
|
|
return &selectPrebuilder{name, "", "", "", "", "", nil, nil, "", b.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 01:58:04 +00:00
|
|
|
func (b *prebuilder) Count(nlist ...string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
name := optString(nlist, "")
|
|
|
|
return &selectPrebuilder{name, "", "COUNT(*)", "", "", "", nil, nil, "", b.adapter}
|
2019-03-12 09:13:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 01:58:04 +00:00
|
|
|
func (b *prebuilder) Insert(nlist ...string) *insertPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
name := optString(nlist, "")
|
|
|
|
return &insertPrebuilder{name, "", "", "", b.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 01:58:04 +00:00
|
|
|
func (b *prebuilder) Update(nlist ...string) *updatePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
name := optString(nlist, "")
|
|
|
|
return &updatePrebuilder{name, "", "", "", nil, nil, b.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 01:58:04 +00:00
|
|
|
func (b *prebuilder) Delete(nlist ...string) *deletePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
name := optString(nlist, "")
|
|
|
|
return &deletePrebuilder{name, "", "", nil, b.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
type deletePrebuilder struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
where string
|
|
|
|
dateCutoff *dateCutoff
|
2017-11-12 05:25:04 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
build Adapter
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *deletePrebuilder) Table(table string) *deletePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = table
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *deletePrebuilder) Where(where string) *deletePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
|
|
|
}
|
|
|
|
b.where += where
|
|
|
|
return b
|
2019-06-05 04:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We probably want to avoid the double allocation of two builders somehow
|
|
|
|
func (b *deletePrebuilder) FromAcc(acc *accDeleteBuilder) *deletePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = acc.table
|
|
|
|
b.where = acc.where
|
|
|
|
b.dateCutoff = acc.dateCutoff
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *deletePrebuilder) Text() (string, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return b.build.SimpleDelete(b.name, b.table, b.where)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *deletePrebuilder) Parse() {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.build.SimpleDelete(b.name, b.table, b.where)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type updatePrebuilder struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
set string
|
|
|
|
where string
|
|
|
|
dateCutoff *dateCutoff // We might want to do this in a slightly less hacky way
|
|
|
|
whereSubQuery *selectPrebuilder
|
2017-11-12 05:25:04 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
build Adapter
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
func qUpdate(table string, set string, where string) *updatePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
return &updatePrebuilder{table: table, set: set, where: where}
|
2018-12-27 05:42:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) Table(table string) *updatePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = table
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) Set(set string) *updatePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.set = set
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) Where(where string) *updatePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
|
|
|
}
|
|
|
|
b.where += where
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) WhereQ(sel *selectPrebuilder) *updatePrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.whereSubQuery = sel
|
|
|
|
return b
|
2018-12-27 05:42:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) Text() (string, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return b.build.SimpleUpdate(b)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) Parse() {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.build.SimpleUpdate(b)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type selectPrebuilder struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
columns string
|
|
|
|
where string
|
|
|
|
orderby string
|
|
|
|
limit string
|
|
|
|
dateCutoff *dateCutoff
|
|
|
|
inChain *selectPrebuilder
|
|
|
|
inColumn string // for inChain
|
2017-11-12 05:25:04 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
build Adapter
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *selectPrebuilder) Table(table string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = table
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *selectPrebuilder) Columns(columns string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.columns = columns
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *selectPrebuilder) Where(where string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
|
|
|
}
|
|
|
|
b.where += where
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 09:13:57 +00:00
|
|
|
func (b *selectPrebuilder) InQ(subBuilder *selectPrebuilder) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.inChain = subBuilder
|
|
|
|
return b
|
2018-01-21 11:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 09:13:57 +00:00
|
|
|
func (b *selectPrebuilder) Orderby(orderby string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.orderby = orderby
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 09:13:57 +00:00
|
|
|
func (b *selectPrebuilder) Limit(limit string) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.limit = limit
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: We probably want to avoid the double allocation of two builders somehow
|
2019-03-12 09:13:57 +00:00
|
|
|
func (b *selectPrebuilder) FromAcc(acc *AccSelectBuilder) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = acc.table
|
|
|
|
if acc.columns != "" {
|
|
|
|
b.columns = acc.columns
|
|
|
|
}
|
|
|
|
b.where = acc.where
|
|
|
|
b.orderby = acc.orderby
|
|
|
|
b.limit = acc.limit
|
|
|
|
|
|
|
|
b.dateCutoff = acc.dateCutoff
|
|
|
|
if acc.inChain != nil {
|
|
|
|
b.inChain = &selectPrebuilder{"", acc.inChain.table, acc.inChain.columns, acc.inChain.where, acc.inChain.orderby, acc.inChain.limit, acc.inChain.dateCutoff, nil, "", b.build}
|
|
|
|
b.inColumn = acc.inColumn
|
|
|
|
}
|
|
|
|
return b
|
2019-03-12 09:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *selectPrebuilder) FromCountAcc(acc *accCountBuilder) *selectPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = acc.table
|
|
|
|
b.where = acc.where
|
|
|
|
b.limit = acc.limit
|
2019-03-12 09:13:57 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
b.dateCutoff = acc.dateCutoff
|
|
|
|
if acc.inChain != nil {
|
|
|
|
b.inChain = &selectPrebuilder{"", acc.inChain.table, acc.inChain.columns, acc.inChain.where, acc.inChain.orderby, acc.inChain.limit, acc.inChain.dateCutoff, nil, "", b.build}
|
|
|
|
b.inColumn = acc.inColumn
|
|
|
|
}
|
|
|
|
return b
|
2018-01-03 07:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add support for dateCutoff
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *selectPrebuilder) Text() (string, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return b.build.SimpleSelect(b.name, b.table, b.columns, b.where, b.orderby, b.limit)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: Add support for dateCutoff
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *selectPrebuilder) Parse() {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.build.SimpleSelect(b.name, b.table, b.columns, b.where, b.orderby, b.limit)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type insertPrebuilder struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
columns string
|
|
|
|
fields string
|
2017-11-12 05:25:04 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
build Adapter
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *insertPrebuilder) Table(table string) *insertPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = table
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *insertPrebuilder) Columns(columns string) *insertPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.columns = columns
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *insertPrebuilder) Fields(fields string) *insertPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.fields = fields
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *insertPrebuilder) Text() (string, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return b.build.SimpleInsert(b.name, b.table, b.columns, b.fields)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *insertPrebuilder) Parse() {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.build.SimpleInsert(b.name, b.table, b.columns, b.fields)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 09:13:57 +00:00
|
|
|
/*type countPrebuilder struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
where string
|
|
|
|
limit string
|
2017-11-12 05:25:04 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
build Adapter
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *countPrebuilder) Table(table string) *countPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.table = table
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func b *countPrebuilder) Where(where string) *countPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
|
|
|
}
|
|
|
|
b.where += where
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *countPrebuilder) Limit(limit string) *countPrebuilder {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.limit = limit
|
|
|
|
return b
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *countPrebuilder) Text() (string, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return b.build.SimpleCount(b.name, b.table, b.where, b.limit)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *countPrebuilder) Parse() {
|
2022-02-21 03:32:53 +00:00
|
|
|
b.build.SimpleCount(b.name, b.table, b.where, b.limit)
|
2019-03-12 09:13:57 +00:00
|
|
|
}*/
|
2017-11-12 05:25:04 +00:00
|
|
|
|
|
|
|
func optString(nlist []string, defaultStr string) string {
|
2022-02-21 03:32:53 +00:00
|
|
|
if len(nlist) == 0 {
|
|
|
|
return defaultStr
|
|
|
|
}
|
|
|
|
return nlist[0]
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|