2017-11-12 05:25:04 +00:00
|
|
|
package qgen
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
type dateCutoff struct {
|
|
|
|
Column string
|
|
|
|
Quantity int
|
|
|
|
Unit string
|
2019-06-05 04:57:10 +00:00
|
|
|
Type int
|
2018-01-03 07:46:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 00:31:46 +00:00
|
|
|
type prebuilder struct {
|
|
|
|
adapter Adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (build *prebuilder) Select(nlist ...string) *selectPrebuilder {
|
2018-12-27 05:42:41 +00:00
|
|
|
name := optString(nlist, "")
|
2018-01-21 11:17:43 +00:00
|
|
|
return &selectPrebuilder{name, "", "", "", "", "", nil, nil, "", build.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 09:13:57 +00:00
|
|
|
func (build *prebuilder) Count(nlist ...string) *selectPrebuilder {
|
|
|
|
name := optString(nlist, "")
|
|
|
|
return &selectPrebuilder{name, "", "COUNT(*)", "", "", "", nil, nil, "", build.adapter}
|
|
|
|
}
|
|
|
|
|
2017-11-13 00:31:46 +00:00
|
|
|
func (build *prebuilder) Insert(nlist ...string) *insertPrebuilder {
|
2018-12-27 05:42:41 +00:00
|
|
|
name := optString(nlist, "")
|
2017-11-13 00:31:46 +00:00
|
|
|
return &insertPrebuilder{name, "", "", "", build.adapter}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (build *prebuilder) Update(nlist ...string) *updatePrebuilder {
|
2018-12-27 05:42:41 +00:00
|
|
|
name := optString(nlist, "")
|
2019-05-08 09:50:10 +00:00
|
|
|
return &updatePrebuilder{name, "", "", "", nil, nil, build.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (build *prebuilder) Delete(nlist ...string) *deletePrebuilder {
|
2018-12-27 05:42:41 +00:00
|
|
|
name := optString(nlist, "")
|
2019-06-05 04:57:10 +00:00
|
|
|
return &deletePrebuilder{name, "", "", nil, build.adapter}
|
2017-11-13 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
type deletePrebuilder struct {
|
2019-06-05 04:57:10 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
where string
|
|
|
|
dateCutoff *dateCutoff
|
2017-11-12 05:25:04 +00:00
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *deletePrebuilder) Table(table string) *deletePrebuilder {
|
|
|
|
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 {
|
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
2019-06-05 04:57:10 +00:00
|
|
|
b.where += where
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We probably want to avoid the double allocation of two builders somehow
|
|
|
|
func (b *deletePrebuilder) FromAcc(acc *accDeleteBuilder) *deletePrebuilder {
|
|
|
|
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) {
|
|
|
|
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() {
|
|
|
|
b.build.SimpleDelete(b.name, b.table, b.where)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type updatePrebuilder struct {
|
2018-12-27 05:42:41 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
set string
|
|
|
|
where string
|
2019-06-05 04:57:10 +00:00
|
|
|
dateCutoff *dateCutoff // We might want to do this in a slightly less hacky way
|
2018-12-27 05:42:41 +00:00
|
|
|
whereSubQuery *selectPrebuilder
|
2017-11-12 05:25:04 +00:00
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
func qUpdate(table string, set string, where string) *updatePrebuilder {
|
|
|
|
return &updatePrebuilder{table: table, set: set, where: where}
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *updatePrebuilder) Table(table string) *updatePrebuilder {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
2019-06-05 04:57:10 +00:00
|
|
|
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 {
|
|
|
|
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) {
|
|
|
|
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() {
|
|
|
|
b.build.SimpleUpdate(b)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type selectPrebuilder struct {
|
2018-01-03 07:46:18 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
columns string
|
|
|
|
where string
|
|
|
|
orderby string
|
|
|
|
limit string
|
|
|
|
dateCutoff *dateCutoff
|
2018-01-21 11:17:43 +00:00
|
|
|
inChain *selectPrebuilder
|
|
|
|
inColumn string // for inChain
|
2017-11-12 05:25:04 +00:00
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *selectPrebuilder) Table(table string) *selectPrebuilder {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
2019-06-05 04:57:10 +00:00
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
b.table = acc.table
|
|
|
|
if acc.columns != "" {
|
|
|
|
b.columns = acc.columns
|
2018-01-21 11:17:43 +00:00
|
|
|
}
|
2019-03-12 09:13:57 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *selectPrebuilder) FromCountAcc(acc *accCountBuilder) *selectPrebuilder {
|
|
|
|
b.table = acc.table
|
|
|
|
b.where = acc.where
|
|
|
|
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
|
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) {
|
|
|
|
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() {
|
|
|
|
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 {
|
|
|
|
name string
|
|
|
|
table string
|
|
|
|
columns string
|
|
|
|
fields string
|
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *insertPrebuilder) Table(table string) *insertPrebuilder {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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) {
|
|
|
|
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() {
|
|
|
|
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 {
|
2017-11-12 05:25:04 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
where string
|
|
|
|
limit string
|
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (b *countPrebuilder) Table(table string) *countPrebuilder {
|
|
|
|
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 {
|
|
|
|
if b.where != "" {
|
|
|
|
b.where += " AND "
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
2019-06-05 04:57:10 +00:00
|
|
|
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 {
|
|
|
|
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) {
|
|
|
|
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() {
|
|
|
|
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 {
|
|
|
|
if len(nlist) == 0 {
|
|
|
|
return defaultStr
|
|
|
|
}
|
|
|
|
return nlist[0]
|
|
|
|
}
|