Files
sqimple/delete.go
T

86 lines
1.3 KiB
Go
Raw Normal View History

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