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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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, "")
|
|
|
|
return &updatePrebuilder{name, "", "", "", 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, "")
|
2017-11-13 00:31:46 +00:00
|
|
|
return &deletePrebuilder{name, "", "", build.adapter}
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
type deletePrebuilder struct {
|
|
|
|
name string
|
|
|
|
table string
|
|
|
|
where string
|
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (delete *deletePrebuilder) Table(table string) *deletePrebuilder {
|
|
|
|
delete.table = table
|
|
|
|
return delete
|
|
|
|
}
|
|
|
|
|
|
|
|
func (delete *deletePrebuilder) Where(where string) *deletePrebuilder {
|
2018-03-31 05:25:27 +00:00
|
|
|
if delete.where != "" {
|
|
|
|
delete.where += " AND "
|
|
|
|
}
|
|
|
|
delete.where += where
|
2017-11-12 05:25:04 +00:00
|
|
|
return delete
|
|
|
|
}
|
|
|
|
|
|
|
|
func (delete *deletePrebuilder) Text() (string, error) {
|
|
|
|
return delete.build.SimpleDelete(delete.name, delete.table, delete.where)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (delete *deletePrebuilder) Parse() {
|
|
|
|
delete.build.SimpleDelete(delete.name, delete.table, delete.where)
|
|
|
|
}
|
|
|
|
|
|
|
|
type updatePrebuilder struct {
|
2018-12-27 05:42:41 +00:00
|
|
|
name string
|
|
|
|
table string
|
|
|
|
set string
|
|
|
|
where string
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (update *updatePrebuilder) Table(table string) *updatePrebuilder {
|
|
|
|
update.table = table
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
|
|
|
func (update *updatePrebuilder) Set(set string) *updatePrebuilder {
|
|
|
|
update.set = set
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
|
|
|
func (update *updatePrebuilder) Where(where string) *updatePrebuilder {
|
2018-03-31 05:25:27 +00:00
|
|
|
if update.where != "" {
|
|
|
|
update.where += " AND "
|
|
|
|
}
|
|
|
|
update.where += where
|
2017-11-12 05:25:04 +00:00
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
func (update *updatePrebuilder) WhereQ(sel *selectPrebuilder) *updatePrebuilder {
|
|
|
|
update.whereSubQuery = sel
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (update *updatePrebuilder) Text() (string, error) {
|
2018-12-27 05:42:41 +00:00
|
|
|
return update.build.SimpleUpdate(update)
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (update *updatePrebuilder) Parse() {
|
2018-12-27 05:42:41 +00:00
|
|
|
update.build.SimpleUpdate(update)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (selectItem *selectPrebuilder) Table(table string) *selectPrebuilder {
|
|
|
|
selectItem.table = table
|
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (selectItem *selectPrebuilder) Columns(columns string) *selectPrebuilder {
|
|
|
|
selectItem.columns = columns
|
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (selectItem *selectPrebuilder) Where(where string) *selectPrebuilder {
|
2018-03-31 05:25:27 +00:00
|
|
|
if selectItem.where != "" {
|
|
|
|
selectItem.where += " AND "
|
|
|
|
}
|
|
|
|
selectItem.where += where
|
2017-11-12 05:25:04 +00:00
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:17:43 +00:00
|
|
|
func (selectItem *selectPrebuilder) InQ(subBuilder *selectPrebuilder) *selectPrebuilder {
|
|
|
|
selectItem.inChain = subBuilder
|
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (selectItem *selectPrebuilder) Orderby(orderby string) *selectPrebuilder {
|
|
|
|
selectItem.orderby = orderby
|
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (selectItem *selectPrebuilder) Limit(limit string) *selectPrebuilder {
|
|
|
|
selectItem.limit = limit
|
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: We probably want to avoid the double allocation of two builders somehow
|
2018-04-22 12:33:56 +00:00
|
|
|
func (selectItem *selectPrebuilder) FromAcc(accBuilder *AccSelectBuilder) *selectPrebuilder {
|
2018-01-03 07:46:18 +00:00
|
|
|
selectItem.table = accBuilder.table
|
|
|
|
selectItem.columns = accBuilder.columns
|
|
|
|
selectItem.where = accBuilder.where
|
|
|
|
selectItem.orderby = accBuilder.orderby
|
|
|
|
selectItem.limit = accBuilder.limit
|
2018-01-21 11:17:43 +00:00
|
|
|
|
|
|
|
selectItem.dateCutoff = accBuilder.dateCutoff
|
|
|
|
if accBuilder.inChain != nil {
|
2018-12-27 05:42:41 +00:00
|
|
|
selectItem.inChain = &selectPrebuilder{"", accBuilder.inChain.table, accBuilder.inChain.columns, accBuilder.inChain.where, accBuilder.inChain.orderby, accBuilder.inChain.limit, accBuilder.inChain.dateCutoff, nil, "", selectItem.build}
|
2018-01-21 11:17:43 +00:00
|
|
|
selectItem.inColumn = accBuilder.inColumn
|
|
|
|
}
|
2018-01-03 07:46:18 +00:00
|
|
|
return selectItem
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add support for dateCutoff
|
2017-11-12 05:25:04 +00:00
|
|
|
func (selectItem *selectPrebuilder) Text() (string, error) {
|
|
|
|
return selectItem.build.SimpleSelect(selectItem.name, selectItem.table, selectItem.columns, selectItem.where, selectItem.orderby, selectItem.limit)
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: Add support for dateCutoff
|
2017-11-12 05:25:04 +00:00
|
|
|
func (selectItem *selectPrebuilder) Parse() {
|
|
|
|
selectItem.build.SimpleSelect(selectItem.name, selectItem.table, selectItem.columns, selectItem.where, selectItem.orderby, selectItem.limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
type insertPrebuilder struct {
|
|
|
|
name string
|
|
|
|
table string
|
|
|
|
columns string
|
|
|
|
fields string
|
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (insert *insertPrebuilder) Table(table string) *insertPrebuilder {
|
|
|
|
insert.table = table
|
|
|
|
return insert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (insert *insertPrebuilder) Columns(columns string) *insertPrebuilder {
|
|
|
|
insert.columns = columns
|
|
|
|
return insert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (insert *insertPrebuilder) Fields(fields string) *insertPrebuilder {
|
|
|
|
insert.fields = fields
|
|
|
|
return insert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (insert *insertPrebuilder) Text() (string, error) {
|
|
|
|
return insert.build.SimpleInsert(insert.name, insert.table, insert.columns, insert.fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (insert *insertPrebuilder) Parse() {
|
|
|
|
insert.build.SimpleInsert(insert.name, insert.table, insert.columns, insert.fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
type countPrebuilder struct {
|
|
|
|
name string
|
|
|
|
table string
|
|
|
|
where string
|
|
|
|
limit string
|
|
|
|
|
|
|
|
build Adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (count *countPrebuilder) Table(table string) *countPrebuilder {
|
|
|
|
count.table = table
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (count *countPrebuilder) Where(where string) *countPrebuilder {
|
2018-03-31 05:25:27 +00:00
|
|
|
if count.where != "" {
|
|
|
|
count.where += " AND "
|
|
|
|
}
|
|
|
|
count.where += where
|
2017-11-12 05:25:04 +00:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (count *countPrebuilder) Limit(limit string) *countPrebuilder {
|
|
|
|
count.limit = limit
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (count *countPrebuilder) Text() (string, error) {
|
|
|
|
return count.build.SimpleCount(count.name, count.table, count.where, count.limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (count *countPrebuilder) Parse() {
|
|
|
|
count.build.SimpleCount(count.name, count.table, count.where, count.limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func optString(nlist []string, defaultStr string) string {
|
|
|
|
if len(nlist) == 0 {
|
|
|
|
return defaultStr
|
|
|
|
}
|
|
|
|
return nlist[0]
|
|
|
|
}
|