Files
pgm/pgm_table.go

97 lines
1.7 KiB
Go

package pgm
import (
"strconv"
)
// Table in database
type Table struct {
tsQuery *string
tsQueryAs *string
Name string
DerivedTable Query
PK []string
FieldCount uint16
debug bool
}
// Debug when set true will print generated query string in stdout
func (t *Table) Debug() Clause {
t.debug = true
return t
}
func (t *Table) Field(f string) Field {
return Field(t.Name + "." + f)
}
// Insert table statement
func (t *Table) Insert() InsertClause {
qb := &insertQry{
debug: t.debug,
table: t.Name,
fields: make([]string, 0, t.FieldCount),
vals: make([]string, 0, t.FieldCount),
args: make([]any, 0, t.FieldCount),
}
return qb
}
func (t *Table) WithTsQuery(q, as string) *Table {
t.tsQuery = &q
t.tsQueryAs = &as
return t
}
// Select table statement
func (t *Table) Select(field ...Field) SelectClause {
qb := &selectQry{
debug: t.debug,
fields: field,
}
if t.DerivedTable != nil {
tName, args := t.DerivedTable.Build(true)
qb.table = "(" + tName + ") AS " + t.Name
qb.args = args
} else {
qb.table = t.Name
}
if t.tsQuery != nil {
var as string
if t.tsQueryAs != nil && *t.tsQueryAs != "" {
as = *t.tsQueryAs
} else {
// add default as field
as = "query"
}
qb.args = append(qb.args, as)
qb.table += ", TO_TSQUERY('english', $" + strconv.Itoa(len(qb.args)) + ") " + as
}
return qb
}
// Update table statement
func (t *Table) Update() UpdateClause {
qb := &updateQry{
debug: t.debug,
table: t.Name,
cols: make([]string, 0, t.FieldCount),
args: make([]any, 0, t.FieldCount),
}
return qb
}
// Detlete table statement
func (t *Table) Delete() DeleteCluase {
qb := &deleteQry{
debug: t.debug,
table: t.Name,
}
return qb
}