Files
sqimple/table.go
T
2026-07-07 17:25:02 +01:00

84 lines
1.6 KiB
Go

package simql
import (
"fmt"
"regexp"
"strings"
)
var selectTableRegexp = regexp.MustCompile("^(from|left join|inner join|right join) ([^ ]+)( (as) ([^ ]+))?( (on) ([^ ]+) (=|!=|>|>=|<|<=) ([^ ]+)( (and) ([^ ]+) (=|!=|>|>=|<|<=) ([^ ]+))*)?$")
type SelectTable string
func (t SelectTable) IsValid() bool {
return selectTableRegexp.MatchString(string(t))
}
func (t SelectTable) Parse() (selectType, table, as string, on *OnClause, ok bool) {
if selectTableRegexp.MatchString(string(t)) {
ok = true
result := selectTableRegexp.FindAllStringSubmatch(string(t), -1)
for i, r := range result {
// for j, c := range r {
// fmt.Println(i, j, c)
// }
if i == 0 {
selectType, table = r[1], r[2]
if r[4] == "as" {
as = r[5]
}
if r[7] == "on" {
on = On(r[8], r[9], r[10])
}
}
if r[12] == "and" {
on.And(r[13], r[14], r[15])
}
}
}
return
}
func (t SelectTable) String() string {
return string(t)
}
func From(def string) SelectTable {
if strings.Index(def, "from ") == 0 {
return SelectTable(def)
}
return SelectTable(fmt.Sprintf("from %s", def))
}
func InnerJoin(def string) SelectTable {
if strings.Index(def, "inner join ") == 0 {
return SelectTable(def)
}
return SelectTable(fmt.Sprintf("inner join %s", def))
}
func LeftJoin(def string) SelectTable {
if strings.Index(def, "left join ") == 0 {
return SelectTable(def)
}
return SelectTable(fmt.Sprintf("left join %s", def))
}
func RightJoin(def string) SelectTable {
if strings.Index(def, "right join ") == 0 {
return SelectTable(def)
}
return SelectTable(fmt.Sprintf("right join %s", def))
}