text search query

This commit is contained in:
2025-11-08 14:29:54 +05:30
parent a795c0e8d6
commit a2b984c342
4 changed files with 38 additions and 31 deletions

View File

@@ -1,13 +1,8 @@
package pgm
import (
"strconv"
)
// Table in database
type Table struct {
tsQuery *string
tsQueryAs *string
textSearch *textSearchCTE
Name string
DerivedTable Query
@@ -16,6 +11,13 @@ type Table struct {
debug bool
}
// text search Common Table Expression
type textSearchCTE struct {
name string
value string
alias string
}
// Debug when set true will print generated query string in stdout
func (t *Table) Debug() Clause {
t.debug = true
@@ -38,17 +40,17 @@ func (t *Table) Insert() InsertClause {
return qb
}
func (t *Table) WithTsQuery(q, as string) *Table {
t.tsQuery = &q
t.tsQueryAs = &as
func (t *Table) WithTextSearch(name, alias, textToSearch string) *Table {
t.textSearch = &textSearchCTE{name: name, value: textToSearch, alias: alias}
return t
}
// Select table statement
func (t *Table) Select(field ...Field) SelectClause {
qb := &selectQry{
debug: t.debug,
fields: field,
debug: t.debug,
fields: field,
textSearch: t.textSearch,
}
if t.DerivedTable != nil {
@@ -59,18 +61,6 @@ func (t *Table) Select(field ...Field) SelectClause {
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
}