Files

86 lines
1.3 KiB
Go

package sqimple
import (
"database/sql"
"strings"
)
// https://www.sqlite.org/lang_delete.html
type DeleteStatement struct {
execFunc func(Query) (sql.Result, error)
table SelectTable
where []Where
args []any
}
func (s *DeleteStatement) Args() []any {
return s.args
}
func (s *DeleteStatement) Exec() (sql.Result, error) {
if s.execFunc != nil {
return s.execFunc(s)
}
return nil, nil
}
func (s *DeleteStatement) ExecFunc(f func(Query) (sql.Result, error)) *DeleteStatement {
s.execFunc = f
return s
}
func (s *DeleteStatement) IsValid() bool {
if !s.table.IsValid() {
return false
}
for _, w := range s.where {
if !w.IsValid() {
return false
}
}
return false
}
func (s *DeleteStatement) String() string {
strs := []string{"delete", s.table.String()}
if len(s.where) > 0 {
strs = append(strs, "where")
for _, w := range s.where {
strs = append(strs, w.String())
}
}
return strings.Join(strs, " ")
}
func (s *DeleteStatement) Where(def string, args ...any) *DeleteStatement {
s.where = append(s.where, Where(def))
if len(args) > 0 {
if s.args == nil {
s.args = []any{}
}
s.args = append(s.args, args...)
}
return s
}
func Delete(from string) *DeleteStatement {
s := &DeleteStatement{
table: From(from),
where: []Where{},
}
return s
}