package sqimple import ( "context" "database/sql" "strings" ) // https://www.sqlite.org/lang_delete.html type DeleteStatement struct { Ctx context.Context DB *sql.DB table SelectTable where []Where args []any } func (s *DeleteStatement) Args() []any { return s.args } func (s *DeleteStatement) Exec() (sql.Result, error) { if s.DB != nil { if s.Ctx != nil { return s.DB.ExecContext(s.Ctx, s.String(), s.Args()...) } return s.DB.Exec(s.String(), s.Args()...) } return nil, ErrNoDatabase } 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 }