-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
107 lines (87 loc) · 2.5 KB
/
example_test.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package twowaysql_test
import (
"context"
"fmt"
"log"
"github.com/future-architect/go-twowaysql"
)
var (
tw *twowaysql.Twowaysql
ctx context.Context
)
func ExampleTwowaysql_Exec() {
type Info struct {
Name string `twowaysql:"name"`
EmpNo int `twowaysql:"EmpNo"`
MaxEmpNo int `twowaysql:"maxEmpNo"`
DeptNo int `twowaysql:"deptNo"`
Email string `twowaysql:"email"`
GenderList []string `twowaysql:"gender_list"`
IntList []int `twowaysql:"int_list"`
}
var params = Info{
MaxEmpNo: 3,
DeptNo: 12,
}
result, err := tw.Exec(ctx, `UPDATE persons SET dept_no = /*deptNo*/1 WHERE employee_no = /*EmpNo*/1`, ¶ms)
if err != nil {
log.Fatal(err)
}
rows, err := result.RowsAffected()
if err != nil {
log.Fatal(err)
}
if rows != 1 {
log.Fatalf("expected to affect 1 row. affected %d", rows)
}
}
func ExampleTwowaysql_Select() {
type Person struct {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string `db:"email"`
}
type Info struct {
Name string `twowaysql:"name"`
EmpNo int `twowaysql:"EmpNo"`
MaxEmpNo int `twowaysql:"maxEmpNo"`
DeptNo int `twowaysql:"deptNo"`
Email string `twowaysql:"email"`
GenderList []string `twowaysql:"gender_list"`
IntList []int `twowaysql:"int_list"`
}
var params = Info{
MaxEmpNo: 3,
DeptNo: 12,
}
var people []Person
err := tw.Select(ctx, &people, `SELECT first_name, last_name, email FROM persons WHERE employee_no < /*maxEmpNo*/1000 /* IF deptNo */ AND dept_no < /*deptNo*/1 /* END */`, ¶ms)
if err != nil {
log.Fatal(err)
}
}
func ExampleEval() {
type Info struct {
Name string `twowaysql:"name"`
EmpNo int `twowaysql:"EmpNo"`
MaxEmpNo int `twowaysql:"maxEmpNo"`
DeptNo int `twowaysql:"deptNo"`
Email string `twowaysql:"email"`
GenderList []string `twowaysql:"gender_list"`
IntList []int `twowaysql:"int_list"`
}
var params = Info{
Name: "Jeff",
MaxEmpNo: 3,
DeptNo: 12,
GenderList: []string{"M", "F"},
IntList: []int{1, 2, 3},
}
var before = `SELECT * FROM person WHERE employee_no = /*maxEmpNo*/1000 AND /* IF int_list !== null */ person.gender in /*int_list*/(3,5,7) /* END */`
after, afterParams, _ := twowaysql.Eval(before, ¶ms)
fmt.Println(after)
fmt.Println(afterParams)
// Output:
// SELECT * FROM person WHERE employee_no = ?/*maxEmpNo*/ AND person.gender in (?, ?, ?)/*int_list*/
// [3 1 2 3]
}