initial commit
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
package sqaffold
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"code.aneur.in/go/record"
|
||||
"code.aneur.in/go/version"
|
||||
)
|
||||
|
||||
type Scaffold struct {
|
||||
ctx context.Context
|
||||
conn *sql.DB
|
||||
|
||||
apps map[string]version.Migration
|
||||
}
|
||||
|
||||
func (s *Scaffold) Exec(query string, args ...any) (sql.Result, error) {
|
||||
return s.conn.ExecContext(s.ctx, query, args...)
|
||||
}
|
||||
|
||||
func (s *Scaffold) Query(query string, args ...any) ([]record.Record, error) {
|
||||
rows, err := s.conn.QueryContext(s.ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, Err(err)
|
||||
}
|
||||
|
||||
return ScanRecords(rows)
|
||||
}
|
||||
|
||||
func (s *Scaffold) QueryOne(query string, args ...any) (record.Record, error) {
|
||||
rows, err := s.conn.QueryContext(s.ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, Err(err)
|
||||
}
|
||||
|
||||
return ScanOneRecord(rows)
|
||||
}
|
||||
|
||||
func (s *Scaffold) WithApp(name string, migration version.Migration) *Scaffold {
|
||||
s.apps[name] = migration
|
||||
return s
|
||||
}
|
||||
|
||||
func NewScaffold(ctx context.Context, conn *sql.DB) *Scaffold {
|
||||
s := &Scaffold{
|
||||
ctx: ctx,
|
||||
conn: conn,
|
||||
|
||||
apps: map[string]version.Migration{},
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user