-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexample2_test.go
More file actions
44 lines (36 loc) · 913 Bytes
/
example2_test.go
File metadata and controls
44 lines (36 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package proteus
import (
"context"
"database/sql"
"fmt"
"log"
)
type CreateProductDao struct {
Insert func(ctx context.Context, e ContextExecutor, id int, name string, cost float64) (int64, error) `proq:"insert into product(id, name, cost) values(:id:, :name:, :cost:)" prop:"id,name,cost"`
}
func Example_create() {
db, err := sql.Open("postgres", "postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")
if err != nil {
log.Fatal(err)
}
var productDao = CreateProductDao{}
err = Build(&productDao, Postgres)
if err != nil {
panic(err)
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer func() { _ = tx.Rollback() }()
ctx := context.Background()
for i := 0; i < 100; i++ {
_, err = productDao.Insert(ctx, tx, i, fmt.Sprintf("person%d", i), 1.1*float64(i))
if err != nil {
log.Fatal(err)
}
}
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}