diff --git a/delete.go b/delete.go index b339591..592f487 100644 --- a/delete.go +++ b/delete.go @@ -1,15 +1,13 @@ package sqimple import ( - "context" "database/sql" "strings" ) // https://www.sqlite.org/lang_delete.html type DeleteStatement struct { - Ctx context.Context - DB *sql.DB + execFunc func(Query) (sql.Result, error) table SelectTable @@ -22,15 +20,16 @@ func (s *DeleteStatement) Args() []any { } 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()...) + if s.execFunc != nil { + return s.execFunc(s) } - return nil, ErrNoDatabase + return nil, nil +} + +func (s *DeleteStatement) ExecFunc(f func(Query) (sql.Result, error)) *DeleteStatement { + s.execFunc = f + return s } func (s *DeleteStatement) IsValid() bool { diff --git a/insert.go b/insert.go index 768e6b8..a9c3683 100644 --- a/insert.go +++ b/insert.go @@ -1,7 +1,6 @@ package sqimple import ( - "context" "database/sql" "fmt" "slices" @@ -10,8 +9,7 @@ import ( // https://www.sqlite.org/lang_delete.html type InsertStatement struct { - Ctx context.Context - DB *sql.DB + execFunc func(Query) (sql.Result, error) table InsertTable or string @@ -38,15 +36,16 @@ func (s *InsertStatement) Args() []any { } 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()...) + if s.execFunc != nil { + return s.execFunc(s) } - return nil, ErrNoDatabase + return nil, nil +} + +func (s *InsertStatement) ExecFunc(f func(Query) (sql.Result, error)) *InsertStatement { + s.execFunc = f + return s } func (s *InsertStatement) IsValid() bool { diff --git a/select.go b/select.go index ff3b3a6..daaa682 100644 --- a/select.go +++ b/select.go @@ -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"} diff --git a/update.go b/update.go index afa3d64..00073ef 100644 --- a/update.go +++ b/update.go @@ -1,7 +1,6 @@ package sqimple import ( - "context" "database/sql" "slices" "strings" @@ -9,8 +8,7 @@ import ( // https://www.sqlite.org/lang_update.html type UpdateStatement struct { - Ctx context.Context - DB *sql.DB + execFunc func(Query) (sql.Result, error) table UpdateTable or string @@ -37,15 +35,16 @@ func (s *UpdateStatement) Args() []any { } 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()...) + if s.execFunc != nil { + return s.execFunc(s) } - return nil, ErrNoDatabase + return nil, nil +} + +func (s *UpdateStatement) ExecFunc(f func(Query) (sql.Result, error)) *UpdateStatement { + s.execFunc = f + return s } func (s *UpdateStatement) IsValid() bool {