Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"fmt"
"go/scanner"
"go/token"
"log/slog"
"reflect"
"strings"
"text/template"

"database/sql/driver"

"github.com/jonbodner/proteus/logger"
"github.com/jonbodner/proteus/mapper"
"github.com/jonbodner/stackerr"
)
Expand Down Expand Up @@ -248,7 +248,7 @@ func validIdentifier(ctx context.Context, curVar string) (string, error) {
loop:
for {
pos, tok, lit := s.Scan()
logger.Log(ctx, logger.DEBUG, fmt.Sprintf("%s\t%s\t%q\n", fset.Position(pos), tok, lit))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintf("%s\t%s\t%q\n", fset.Position(pos), tok, lit))
switch tok {
case token.EOF:
if first || lastPeriod {
Expand Down
11 changes: 5 additions & 6 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package proteus

import (
"context"
"github.com/google/go-cmp/cmp"
"reflect"
"testing"

"github.com/jonbodner/proteus/logger"
"github.com/google/go-cmp/cmp"
)

func TestFixNameForTemplate(t *testing.T) {
Expand Down Expand Up @@ -123,9 +122,9 @@ func Test_convertToPositionalParameters(t *testing.T) {
}{
// TODO: Add test cases.
}
c := logger.WithLevel(context.Background(), logger.DEBUG)
ctx := context.Background()
for _, tt := range tests {
got, got1, err := buildFixedQueryAndParamOrder(c, tt.args.query, tt.args.paramMap, tt.args.funcType, tt.args.pa)
got, got1, err := buildFixedQueryAndParamOrder(ctx, tt.args.query, tt.args.paramMap, tt.args.funcType, tt.args.pa)
if (err != nil) != tt.wantErr {
t.Errorf("%q. buildFixedQueryAndParamOrder() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
Expand Down Expand Up @@ -206,9 +205,9 @@ func Test_validIdentifier(t *testing.T) {
}{
// TODO: Add test cases.
}
c := logger.WithLevel(context.Background(), logger.DEBUG)
ctx := context.Background()
for _, tt := range tests {
got, err := validIdentifier(c, tt.args.curVar)
got, err := validIdentifier(ctx, tt.args.curVar)
if (err != nil) != tt.wantErr {
t.Errorf("%q. validIdentifier() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
Expand Down
30 changes: 13 additions & 17 deletions cmd/null/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"os"

"github.com/jonbodner/proteus"
"github.com/jonbodner/proteus/logger"
_ "github.com/lib/pq"
)

Expand Down Expand Up @@ -35,11 +35,7 @@ type Product2Dao struct {
type closer func(error) error

func main() {
proteus.SetLogLevel(logger.DEBUG)
logger.Config(logger.LoggerFunc(func(vals ...interface{}) error {
fmt.Printf("%s: (%s) - %s\n", vals[1], vals[3], vals[5])
return nil
}))
slog.SetLogLoggerLevel(slog.LevelDebug)
var product2DaoPostgres Product2Dao
err := proteus.Build(&product2DaoPostgres, proteus.Postgres)
if err != nil {
Expand All @@ -49,7 +45,7 @@ func main() {
}

func run(productDao Product2Dao) {
ctx := logger.WithLevel(context.Background(), logger.DEBUG)
ctx := context.Background()
tx, closer := setupDBPostgres(ctx)
var err error
defer func() {
Expand All @@ -63,16 +59,16 @@ func run(productDao Product2Dao) {

populate(ctx, tx, productDao)

logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDao.FindByID(ctx, tx, 10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDao.FindByID(ctx, tx, 10)))
cost := sql.NullFloat64{
Float64: 56.23,
Valid: true,
}
p := Product2{10, "Thingie", cost}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDao.Update(ctx, tx, p)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDao.FindByID(ctx, tx, 10)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDao.FindByNameAndCost(ctx, tx, "fred", 54.10)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDao.FindByNameAndCost(ctx, tx, "Thingie", 56.23)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDao.Update(ctx, tx, p)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDao.FindByID(ctx, tx, 10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDao.FindByNameAndCost(ctx, tx, "fred", 54.10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDao.FindByNameAndCost(ctx, tx, "Thingie", 56.23)))
}

func defineSchema(ctx context.Context, tx proteus.ContextWrapper) error {
Expand All @@ -82,7 +78,7 @@ func defineSchema(ctx context.Context, tx proteus.ContextWrapper) error {
`
_, err := tx.ExecContext(ctx, sqlStmt)
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintf("%q: %s\n", err, sqlStmt))
slog.Log(ctx, slog.LevelError, fmt.Sprintf("%q: %s\n", err, sqlStmt))
return err
}
return nil
Expand All @@ -92,13 +88,13 @@ func setupDBPostgres(ctx context.Context) (proteus.ContextWrapper, closer) {
db, err := sql.Open("postgres", "postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")

if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
os.Exit(1)
}

tx, err := db.Begin()
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
os.Exit(1)
}

Expand All @@ -118,9 +114,9 @@ func populate(ctx context.Context, w proteus.ContextWrapper, productDao Product2
}
rowCount, err := productDao.Insert(ctx, w, i, fmt.Sprintf("person%d", i), cost)
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
os.Exit(1)
}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(rowCount))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(rowCount))
}
}
48 changes: 22 additions & 26 deletions cmd/sample-ctx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"

"github.com/jonbodner/dbtimer"
"github.com/jonbodner/proteus"
"github.com/jonbodner/proteus/logger"
_ "github.com/lib/pq"
)

Expand Down Expand Up @@ -46,10 +46,6 @@ func main() {
dbtimer.SetTimerLoggerFunc(func(ti dbtimer.TimerInfo) {
fmt.Printf("%s %s %v %v %d\n", ti.Method, ti.Query, ti.Args, ti.Err, ti.End.Sub(ti.Start).Nanoseconds()/1000)
})
logger.Config(logger.LoggerFunc(func(vals ...interface{}) error {
fmt.Printf("%s: (%s) - %+v\n", vals[1], vals[3], vals[5])
return nil
}))

ctx := context.Background()
var productDAO ProductDAO
Expand All @@ -62,7 +58,7 @@ func main() {
}

func run(setupDb setupDb, productDAO ProductDAO) {
ctx := logger.WithLevel(context.Background(), logger.DEBUG)
ctx := context.Background()
db := setupDb(ctx, productDAO)
defer db.Close()
tx, err := db.Begin()
Expand All @@ -71,52 +67,52 @@ func run(setupDb setupDb, productDAO ProductDAO) {
}
defer tx.Commit()

logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByID(ctx, tx, 10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByID(ctx, tx, 10)))
cost := new(float64)
*cost = 56.23
p := Product{10, "Thingie", cost}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.Update(ctx, tx, p)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByID(ctx, tx, 10)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByNameAndCost(ctx, tx, "fred", 54.10)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByNameAndCost(ctx, tx, "Thingie", 56.23)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.Update(ctx, tx, p)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByID(ctx, tx, 10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByNameAndCost(ctx, tx, "fred", 54.10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByNameAndCost(ctx, tx, "Thingie", 56.23)))

//using a map of [string]interface{} works too!
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDMap(ctx, tx, 10))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByNameAndCostMap(ctx, tx, "Thingie", 56.23))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDMap(ctx, tx, 10))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByNameAndCostMap(ctx, tx, "Thingie", 56.23))))

logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByID(ctx, tx, 11))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByID(ctx, tx, 11))))
m := map[string]interface{}{
"Id": 11,
"Name": "bobbo",
"Cost": 12.94,
}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.UpdateMap(ctx, tx, m))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByID(ctx, tx, 11))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.UpdateMap(ctx, tx, m))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByID(ctx, tx, 11))))

//searching using a slice
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSlice(ctx, tx, []int{1, 3, 5}))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSliceAndName(ctx, tx, []int{1, 3, 5}, "person1"))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSliceNameAndCost(ctx, tx, []int{1, 3, 5}, "person3", nil))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSliceCostAndNameSlice(ctx, tx, []int{1, 3, 5}, []string{"person3", "person5"}, nil))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSlice(ctx, tx, []int{1, 3, 5}))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSliceAndName(ctx, tx, []int{1, 3, 5}, "person1"))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSliceNameAndCost(ctx, tx, []int{1, 3, 5}, "person3", nil))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSliceCostAndNameSlice(ctx, tx, []int{1, 3, 5}, []string{"person3", "person5"}, nil))))

//using positional parameters instead of names
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByNameAndCostUnlabeled(ctx, tx, "Thingie", 56.23))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByNameAndCostUnlabeled(ctx, tx, "Thingie", 56.23))))
}

func setupDbPostgres(ctx context.Context, productDAO ProductDAO) *sql.DB {
//db, err := sql.Open("postgres", "postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")
db, err := sql.Open("timer", "postgres postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")

if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
}
sqlStmt := `
drop table if exists product;
create table product (id integer not null primary key, name text, cost real);
`
_, err = db.Exec(sqlStmt)
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintf("%q: %s\n", err, sqlStmt))
slog.Log(ctx, slog.LevelError, fmt.Sprintf("%q: %s\n", err, sqlStmt))
return nil
}
populate(ctx, db, productDAO)
Expand All @@ -126,7 +122,7 @@ func setupDbPostgres(ctx context.Context, productDAO ProductDAO) *sql.DB {
func populate(ctx context.Context, db *sql.DB, productDao ProductDAO) {
tx, err := db.Begin()
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
}
defer tx.Commit()

Expand All @@ -138,8 +134,8 @@ func populate(ctx context.Context, db *sql.DB, productDao ProductDAO) {
}
rowCount, err := productDao.Insert(ctx, tx, i, fmt.Sprintf("person%d", i), cost)
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(rowCount))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(rowCount))
}
}
48 changes: 22 additions & 26 deletions cmd/sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"

"github.com/jonbodner/dbtimer"
"github.com/jonbodner/proteus"
"github.com/jonbodner/proteus/logger"
_ "github.com/lib/pq"
)

Expand Down Expand Up @@ -46,10 +46,6 @@ func main() {
dbtimer.SetTimerLoggerFunc(func(ti dbtimer.TimerInfo) {
fmt.Printf("%s %s %v %v %d\n", ti.Method, ti.Query, ti.Args, ti.Err, ti.End.Sub(ti.Start).Nanoseconds()/1000)
})
logger.Config(logger.LoggerFunc(func(vals ...interface{}) error {
fmt.Printf("%s: (%s) - %+v\n", vals[1], vals[3], vals[5])
return nil
}))

var productDaoPostgres ProductDAO
err := proteus.Build(&productDaoPostgres, proteus.Postgres)
Expand All @@ -61,7 +57,7 @@ func main() {
}

func run(setupDb setupDb, productDAO ProductDAO) {
ctx := logger.WithLevel(context.Background(), logger.DEBUG)
ctx := context.Background()
db := setupDb(ctx, productDAO)
defer db.Close()
tx, err := db.Begin()
Expand All @@ -70,52 +66,52 @@ func run(setupDb setupDb, productDAO ProductDAO) {
}
defer tx.Commit()

logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByID(tx, 10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByID(tx, 10)))
cost := new(float64)
*cost = 56.23
p := Product{10, "Thingie", cost}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.Update(tx, p)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByID(tx, 10)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByNameAndCost(tx, "fred", 54.10)))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(productDAO.FindByNameAndCost(tx, "Thingie", 56.23)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.Update(tx, p)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByID(tx, 10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByNameAndCost(tx, "fred", 54.10)))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(productDAO.FindByNameAndCost(tx, "Thingie", 56.23)))

//using a map of [string]interface{} works too!
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDMap(tx, 10))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByNameAndCostMap(tx, "Thingie", 56.23))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDMap(tx, 10))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByNameAndCostMap(tx, "Thingie", 56.23))))

logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByID(tx, 11))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByID(tx, 11))))
m := map[string]interface{}{
"Id": 11,
"Name": "bobbo",
"Cost": 12.94,
}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.UpdateMap(tx, m))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByID(tx, 11))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.UpdateMap(tx, m))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByID(tx, 11))))

//searching using a slice
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSlice(tx, []int{1, 3, 5}))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSliceAndName(tx, []int{1, 3, 5}, "person1"))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSliceNameAndCost(tx, []int{1, 3, 5}, "person3", nil))))
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByIDSliceCostAndNameSlice(tx, []int{1, 3, 5}, []string{"person3", "person5"}, nil))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSlice(tx, []int{1, 3, 5}))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSliceAndName(tx, []int{1, 3, 5}, "person1"))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSliceNameAndCost(tx, []int{1, 3, 5}, "person3", nil))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByIDSliceCostAndNameSlice(tx, []int{1, 3, 5}, []string{"person3", "person5"}, nil))))

//using positional parameters instead of names
logger.Log(ctx, logger.DEBUG, fmt.Sprintln((productDAO.FindByNameAndCostUnlabeled(tx, "Thingie", 56.23))))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln((productDAO.FindByNameAndCostUnlabeled(tx, "Thingie", 56.23))))
}

func setupDbPostgres(ctx context.Context, productDAO ProductDAO) *sql.DB {
//db, err := sql.Open("postgres", "postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")
db, err := sql.Open("timer", "postgres postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")

if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
}
sqlStmt := `
drop table if exists product;
create table product (id integer not null primary key, name text, cost real);
`
_, err = db.Exec(sqlStmt)
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintf("%q: %s\n", err, sqlStmt))
slog.Log(ctx, slog.LevelError, fmt.Sprintf("%q: %s\n", err, sqlStmt))
return nil
}
populate(ctx, db, productDAO)
Expand All @@ -125,7 +121,7 @@ func setupDbPostgres(ctx context.Context, productDAO ProductDAO) *sql.DB {
func populate(ctx context.Context, db *sql.DB, productDao ProductDAO) {
tx, err := db.Begin()
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
}
defer tx.Commit()

Expand All @@ -137,8 +133,8 @@ func populate(ctx context.Context, db *sql.DB, productDao ProductDAO) {
}
rowCount, err := productDao.Insert(tx, i, fmt.Sprintf("person%d", i), cost)
if err != nil {
logger.Log(ctx, logger.FATAL, fmt.Sprintln(err))
slog.Log(ctx, slog.LevelError, fmt.Sprintln(err))
}
logger.Log(ctx, logger.DEBUG, fmt.Sprintln(rowCount))
slog.Log(ctx, slog.LevelDebug, fmt.Sprintln(rowCount))
}
}
Loading
Loading