From f24f15a4f62f1ebccaf118327b9238980a25dc75 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Tue, 7 Jul 2026 22:34:02 +0100 Subject: [PATCH] add ctx/db to all query types --- delete.go | 17 +++++++++++++++++ error.go | 7 +++++++ insert.go | 17 +++++++++++++++++ select.go | 29 +++++++++++++++++++++++++++++ update.go | 17 +++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 error.go diff --git a/delete.go b/delete.go index 048e356..b339591 100644 --- a/delete.go +++ b/delete.go @@ -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 diff --git a/error.go b/error.go new file mode 100644 index 0000000..51ebbb3 --- /dev/null +++ b/error.go @@ -0,0 +1,7 @@ +package sqimple + +import "errors" + +var ( + ErrNoDatabase = errors.New("no database") +) diff --git a/insert.go b/insert.go index b2f4eee..768e6b8 100644 --- a/insert.go +++ b/insert.go @@ -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 diff --git a/select.go b/select.go index 6fcc0f6..e1c1b0e 100644 --- a/select.go +++ b/select.go @@ -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"} diff --git a/update.go b/update.go index 072ffd1..afa3d64 100644 --- a/update.go +++ b/update.go @@ -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