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 package sqimple
import ( import (
"context"
"database/sql"
"strings" "strings"
) )
// https://www.sqlite.org/lang_delete.html // https://www.sqlite.org/lang_delete.html
type DeleteStatement struct { type DeleteStatement struct {
Ctx context.Context
DB *sql.DB
table SelectTable table SelectTable
where []Where where []Where
@@ -16,6 +21,18 @@ func (s *DeleteStatement) Args() []any {
return s.args 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 { func (s *DeleteStatement) IsValid() bool {
if !s.table.IsValid() { if !s.table.IsValid() {
return false 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 package sqimple
import ( import (
"context"
"database/sql"
"fmt" "fmt"
"slices" "slices"
"strings" "strings"
@@ -8,6 +10,9 @@ import (
// https://www.sqlite.org/lang_delete.html // https://www.sqlite.org/lang_delete.html
type InsertStatement struct { type InsertStatement struct {
Ctx context.Context
DB *sql.DB
table InsertTable table InsertTable
or string or string
@@ -32,6 +37,18 @@ func (s *InsertStatement) Args() []any {
return args 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 { func (s *InsertStatement) IsValid() bool {
if !s.table.IsValid() { if !s.table.IsValid() {
return false return false
+29
View File
@@ -1,12 +1,17 @@
package sqimple package sqimple
import ( import (
"context"
"database/sql"
"fmt" "fmt"
"strings" "strings"
) )
// https://www.sqlite.org/lang_select.html // https://www.sqlite.org/lang_select.html
type SelectQuery struct { type SelectQuery struct {
Ctx context.Context
DB *sql.DB
distinct bool distinct bool
columns []Column columns []Column
@@ -106,6 +111,30 @@ func (q *SelectQuery) OrderBy(defs ...string) *SelectQuery {
return q 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 { func (q *SelectQuery) String() string {
strs := []string{"select"} strs := []string{"select"}
+17
View File
@@ -1,12 +1,17 @@
package sqimple package sqimple
import ( import (
"context"
"database/sql"
"slices" "slices"
"strings" "strings"
) )
// https://www.sqlite.org/lang_update.html // https://www.sqlite.org/lang_update.html
type UpdateStatement struct { type UpdateStatement struct {
Ctx context.Context
DB *sql.DB
table UpdateTable table UpdateTable
or string or string
@@ -31,6 +36,18 @@ func (s *UpdateStatement) Args() []any {
return args 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 { func (s *UpdateStatement) IsValid() bool {
if !s.table.IsValid() { if !s.table.IsValid() {
return false return false