add ctx/db to all query types

This commit is contained in:
2026-07-07 22:34:02 +01:00
parent f2ebd5b53f
commit f24f15a4f6
5 changed files with 87 additions and 0 deletions
+17
View File
@@ -1,11 +1,16 @@
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
@@ -16,6 +21,18 @@ 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
+7
View File
@@ -0,0 +1,7 @@
package sqimple
import "errors"
var (
ErrNoDatabase = errors.New("no database")
)
+17
View File
@@ -1,6 +1,8 @@
package sqimple
import (
"context"
"database/sql"
"fmt"
"slices"
"strings"
@@ -8,6 +10,9 @@ import (
// https://www.sqlite.org/lang_delete.html
type InsertStatement struct {
Ctx context.Context
DB *sql.DB
table InsertTable
or string
@@ -32,6 +37,18 @@ func (s *InsertStatement) Args() []any {
return args
}
func (s *InsertStatement) 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 *InsertStatement) IsValid() bool {
if !s.table.IsValid() {
return false
+29
View File
@@ -1,12 +1,17 @@
package sqimple
import (
"context"
"database/sql"
"fmt"
"strings"
)
// https://www.sqlite.org/lang_select.html
type SelectQuery struct {
Ctx context.Context
DB *sql.DB
distinct bool
columns []Column
@@ -106,6 +111,30 @@ func (q *SelectQuery) OrderBy(defs ...string) *SelectQuery {
return q
}
func (q *SelectQuery) Query() (*sql.Rows, error) {
if q.DB != nil {
if q.Ctx != nil {
return q.DB.QueryContext(q.Ctx, q.String(), q.Args()...)
}
return q.DB.Query(q.String(), q.Args()...)
}
return nil, ErrNoDatabase
}
func (q *SelectQuery) QueryRow() *sql.Row {
if q.DB != nil {
if q.Ctx != nil {
return q.DB.QueryRowContext(q.Ctx, q.String(), q.Args()...)
}
return q.DB.QueryRow(q.String(), q.Args()...)
}
return nil
}
func (q *SelectQuery) String() string {
strs := []string{"select"}
+17
View File
@@ -1,12 +1,17 @@
package sqimple
import (
"context"
"database/sql"
"slices"
"strings"
)
// https://www.sqlite.org/lang_update.html
type UpdateStatement struct {
Ctx context.Context
DB *sql.DB
table UpdateTable
or string
@@ -31,6 +36,18 @@ func (s *UpdateStatement) Args() []any {
return args
}
func (s *UpdateStatement) 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 *UpdateStatement) IsValid() bool {
if !s.table.IsValid() {
return false