Files
sqimple/select.go
T

188 lines
3.0 KiB
Go
Raw Normal View History

2026-07-07 20:29:58 +01:00
package sqimple
2026-07-07 17:25:02 +01:00
import (
"fmt"
"strings"
)
2026-07-07 19:59:32 +01:00
// https://www.sqlite.org/lang_select.html
2026-07-07 17:25:02 +01:00
type SelectQuery struct {
2026-07-07 19:59:32 +01:00
distinct bool
2026-07-07 17:25:02 +01:00
columns []Column
tables []SelectTable
where []Where
args []any
2026-07-07 19:59:32 +01:00
// group // TODO
// having // TODO
orderBy []OrderBy
2026-07-07 17:25:02 +01:00
limit int
offset int
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) Args() []any {
return q.args
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) Columns(defs ...string) *SelectQuery {
2026-07-07 22:07:50 +01:00
if q.columns == nil {
q.columns = []Column{}
}
2026-07-07 17:25:02 +01:00
for _, def := range defs {
2026-07-07 20:07:15 +01:00
q.columns = append(q.columns, Column(def))
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
return q
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) Distinct(distinct bool) *SelectQuery {
q.distinct = distinct
2026-07-07 19:59:32 +01:00
2026-07-07 20:07:15 +01:00
return q
2026-07-07 19:59:32 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) IsValid() bool {
for _, c := range q.columns {
2026-07-07 17:25:02 +01:00
if !c.IsValid() {
return false
}
}
2026-07-07 20:07:15 +01:00
for _, t := range q.tables {
2026-07-07 17:25:02 +01:00
if !t.IsValid() {
return false
}
}
2026-07-07 20:07:15 +01:00
for _, w := range q.where {
2026-07-07 17:25:02 +01:00
if !w.IsValid() {
return false
}
}
return false
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) LeftJoin(def string) *SelectQuery {
q.tables = append(q.tables, LeftJoin(def))
return q
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) Limit(limit int) *SelectQuery {
q.limit = limit
return q
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) InnerJoin(def string) *SelectQuery {
q.tables = append(q.tables, InnerJoin(def))
return q
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) RightJoin(def string) *SelectQuery {
q.tables = append(q.tables, RightJoin(def))
return q
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) Offset(limit int) *SelectQuery {
q.limit = limit
return q
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) OrderBy(defs ...string) *SelectQuery {
if q.orderBy == nil {
q.orderBy = []OrderBy{}
2026-07-07 19:59:32 +01:00
}
for _, def := range defs {
2026-07-07 20:07:15 +01:00
q.orderBy = append(q.orderBy, OrderBy(def))
2026-07-07 19:59:32 +01:00
}
2026-07-07 20:07:15 +01:00
return q
2026-07-07 19:59:32 +01:00
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) String() string {
2026-07-07 17:25:02 +01:00
strs := []string{"select"}
2026-07-07 20:07:15 +01:00
if q.distinct {
2026-07-07 19:59:32 +01:00
strs = append(strs, "distinct")
}
2026-07-07 20:07:15 +01:00
if len(q.columns) > 0 {
for i, column := range q.columns {
if i < len(q.columns)-1 {
2026-07-07 19:59:32 +01:00
strs = append(strs, fmt.Sprintf("%s,", column))
2026-07-07 17:25:02 +01:00
} else {
strs = append(strs, column.String())
}
}
} else {
strs = append(strs, "*")
}
2026-07-07 20:07:15 +01:00
for _, table := range q.tables {
2026-07-07 17:25:02 +01:00
strs = append(strs, table.String())
}
2026-07-07 20:07:15 +01:00
if len(q.where) > 0 {
2026-07-07 17:25:02 +01:00
strs = append(strs, "where")
2026-07-07 20:07:15 +01:00
for _, w := range q.where {
2026-07-07 17:25:02 +01:00
strs = append(strs, w.String())
}
}
2026-07-07 20:07:15 +01:00
if len(q.orderBy) > 0 {
2026-07-07 19:59:32 +01:00
strs = append(strs, "order by")
2026-07-07 20:07:15 +01:00
for i, o := range q.orderBy {
if i < len(q.orderBy)-1 {
2026-07-07 19:59:32 +01:00
strs = append(strs, fmt.Sprintf("%s,", o))
} else {
strs = append(strs, o.String())
}
}
}
2026-07-07 20:07:15 +01:00
if q.limit > 0 {
if q.offset > 0 {
strs = append(strs, fmt.Sprintf("limit %d offset %d", q.offset, q.limit))
2026-07-07 17:25:02 +01:00
} else {
2026-07-07 20:07:15 +01:00
strs = append(strs, fmt.Sprintf("limit %d", q.limit))
2026-07-07 17:25:02 +01:00
}
}
return strings.Join(strs, " ")
}
2026-07-07 20:07:15 +01:00
func (q *SelectQuery) Where(def string, args ...any) *SelectQuery {
2026-07-07 22:07:50 +01:00
if q.where == nil {
q.where = []Where{}
}
2026-07-07 20:07:15 +01:00
q.where = append(q.where, Where(def))
2026-07-07 17:25:02 +01:00
if len(args) > 0 {
2026-07-07 20:07:15 +01:00
if q.args == nil {
q.args = []any{}
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
q.args = append(q.args, args...)
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
return q
2026-07-07 17:25:02 +01:00
}
func Select(from string) *SelectQuery {
2026-07-07 20:07:15 +01:00
q := &SelectQuery{
2026-07-07 22:07:50 +01:00
tables: []SelectTable{From(from)},
2026-07-07 17:25:02 +01:00
}
2026-07-07 20:07:15 +01:00
return q
2026-07-07 17:25:02 +01:00
}