-
Notifications
You must be signed in to change notification settings - Fork 213
/
database_example_test.go
146 lines (126 loc) · 3.73 KB
/
database_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package goqu_test
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/doug-martin/goqu/v9"
)
func ExampleDatabase_Begin() {
db := getDB()
tx, err := db.Begin()
if err != nil {
fmt.Println("Error starting transaction", err.Error())
}
// use tx.From to get a dataset that will execute within this transaction
update := tx.Update("goqu_user").
Set(goqu.Record{"last_name": "Ucon"}).
Where(goqu.Ex{"last_name": "Yukon"}).
Returning("id").
Executor()
var ids []int64
if err := update.ScanVals(&ids); err != nil {
if rErr := tx.Rollback(); rErr != nil {
fmt.Println("An error occurred while issuing ROLLBACK\n\t", rErr.Error())
} else {
fmt.Println("An error occurred while updating users ROLLBACK transaction\n\t", err.Error())
}
return
}
if err := tx.Commit(); err != nil {
fmt.Println("An error occurred while issuing COMMIT\n\t", err.Error())
} else {
fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
}
// Output:
// Updated users in transaction [ids:=[1 2 3]]
}
func ExampleDatabase_BeginTx() {
db := getDB()
ctx := context.Background()
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
if err != nil {
fmt.Println("Error starting transaction", err.Error())
}
// use tx.From to get a dataset that will execute within this transaction
update := tx.Update("goqu_user").
Set(goqu.Record{"last_name": "Ucon"}).
Where(goqu.Ex{"last_name": "Yukon"}).
Returning("id").
Executor()
var ids []int64
if err := update.ScanVals(&ids); err != nil {
if rErr := tx.Rollback(); rErr != nil {
fmt.Println("An error occurred while issuing ROLLBACK\n\t", rErr.Error())
} else {
fmt.Println("An error occurred while updating users ROLLBACK transaction\n\t", err.Error())
}
return
}
if err := tx.Commit(); err != nil {
fmt.Println("An error occurred while issuing COMMIT\n\t", err.Error())
} else {
fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
}
// Output:
// Updated users in transaction [ids:=[1 2 3]]
}
func ExampleDatabase_WithTx() {
db := getDB()
var ids []int64
if err := db.WithTx(func(tx *goqu.TxDatabase) error {
// use tx.From to get a dataset that will execute within this transaction
update := tx.Update("goqu_user").
Where(goqu.Ex{"last_name": "Yukon"}).
Returning("id").
Set(goqu.Record{"last_name": "Ucon"}).
Executor()
return update.ScanVals(&ids)
}); err != nil {
fmt.Println("An error occurred in transaction\n\t", err.Error())
} else {
fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
}
// Output:
// Updated users in transaction [ids:=[1 2 3]]
}
func ExampleDatabase_Dialect() {
db := getDB()
fmt.Println(db.Dialect())
// Output:
// postgres
}
func ExampleDatabase_Exec() {
db := getDB()
_, err := db.Exec(`DROP TABLE "user_role"; DROP TABLE "goqu_user"`)
if err != nil {
fmt.Println("Error occurred while dropping tables", err.Error())
}
fmt.Println("Dropped tables user_role and goqu_user")
// Output:
// Dropped tables user_role and goqu_user
}
func ExampleDatabase_ExecContext() {
db := getDB()
d := time.Now().Add(50 * time.Millisecond)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
_, err := db.ExecContext(ctx, `DROP TABLE "user_role"; DROP TABLE "goqu_user"`)
if err != nil {
fmt.Println("Error occurred while dropping tables", err.Error())
}
fmt.Println("Dropped tables user_role and goqu_user")
// Output:
// Dropped tables user_role and goqu_user
}
func ExampleDatabase_From() {
db := getDB()
var names []string
if err := db.From("goqu_user").Select("first_name").ScanVals(&names); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Fetched Users names:", names)
}
// Output:
// Fetched Users names: [Bob Sally Vinita John]
}