remove ctx, db stuff and use callbacks instead

This commit is contained in:
2026-07-08 01:34:52 +01:00
parent 6e8fdbba1e
commit 86468a85c9
4 changed files with 57 additions and 45 deletions
+30 -15
View File
@@ -11,8 +11,11 @@ import (
// https://www.sqlite.org/lang_select.html
type SelectQuery struct {
Ctx context.Context
DB *sql.DB
ctx context.Context
db *sql.DB
queryFunc func(Query) (*sql.Rows, error)
queryRowFunc func(Query) *sql.Row
distinct bool
@@ -48,6 +51,16 @@ func (q *SelectQuery) Columns(defs ...string) *SelectQuery {
return q
}
func (q *SelectQuery) Ctx(ctx context.Context) *SelectQuery {
q.ctx = ctx
return q
}
func (q *SelectQuery) DB(db *sql.DB) *SelectQuery {
q.db = db
return q
}
func (q *SelectQuery) Distinct(distinct bool) *SelectQuery {
q.distinct = distinct
@@ -114,15 +127,16 @@ func (q *SelectQuery) OrderBy(defs ...string) *SelectQuery {
}
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()...)
if q.queryFunc != nil {
return q.queryFunc(q)
}
return nil, ErrNoDatabase
return nil, nil
}
func (q *SelectQuery) QueryFunc(f func(Query) (*sql.Rows, error)) *SelectQuery {
q.queryFunc = f
return q
}
func (q *SelectQuery) QueryRecord() (record.Record, error) {
@@ -144,17 +158,18 @@ func (q *SelectQuery) QueryRecords() ([]record.Record, error) {
}
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()...)
if q.queryRowFunc != nil {
return q.queryRowFunc(q)
}
return nil
}
func (q *SelectQuery) QueryRowFunc(f func(Query) *sql.Row) *SelectQuery {
q.queryRowFunc = f
return q
}
func (q *SelectQuery) String() string {
strs := []string{"select"}