...
1 package goqu_test
2
3 import (
4 "context"
5 "database/sql"
6 "fmt"
7 "time"
8
9 "github.com/doug-martin/goqu/v9"
10 )
11
12 func ExampleDatabase_Begin() {
13 db := getDB()
14
15 tx, err := db.Begin()
16 if err != nil {
17 fmt.Println("Error starting transaction", err.Error())
18 }
19
20
21 update := tx.Update("goqu_user").
22 Set(goqu.Record{"last_name": "Ucon"}).
23 Where(goqu.Ex{"last_name": "Yukon"}).
24 Returning("id").
25 Executor()
26
27 var ids []int64
28 if err := update.ScanVals(&ids); err != nil {
29 if rErr := tx.Rollback(); rErr != nil {
30 fmt.Println("An error occurred while issuing ROLLBACK\n\t", rErr.Error())
31 } else {
32 fmt.Println("An error occurred while updating users ROLLBACK transaction\n\t", err.Error())
33 }
34 return
35 }
36 if err := tx.Commit(); err != nil {
37 fmt.Println("An error occurred while issuing COMMIT\n\t", err.Error())
38 } else {
39 fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
40 }
41
42
43 }
44
45 func ExampleDatabase_BeginTx() {
46 db := getDB()
47
48 ctx := context.Background()
49 tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
50 if err != nil {
51 fmt.Println("Error starting transaction", err.Error())
52 }
53
54
55 update := tx.Update("goqu_user").
56 Set(goqu.Record{"last_name": "Ucon"}).
57 Where(goqu.Ex{"last_name": "Yukon"}).
58 Returning("id").
59 Executor()
60
61 var ids []int64
62 if err := update.ScanVals(&ids); err != nil {
63 if rErr := tx.Rollback(); rErr != nil {
64 fmt.Println("An error occurred while issuing ROLLBACK\n\t", rErr.Error())
65 } else {
66 fmt.Println("An error occurred while updating users ROLLBACK transaction\n\t", err.Error())
67 }
68 return
69 }
70 if err := tx.Commit(); err != nil {
71 fmt.Println("An error occurred while issuing COMMIT\n\t", err.Error())
72 } else {
73 fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
74 }
75
76
77 }
78
79 func ExampleDatabase_WithTx() {
80 db := getDB()
81 var ids []int64
82 if err := db.WithTx(func(tx *goqu.TxDatabase) error {
83
84 update := tx.Update("goqu_user").
85 Where(goqu.Ex{"last_name": "Yukon"}).
86 Returning("id").
87 Set(goqu.Record{"last_name": "Ucon"}).
88 Executor()
89
90 return update.ScanVals(&ids)
91 }); err != nil {
92 fmt.Println("An error occurred in transaction\n\t", err.Error())
93 } else {
94 fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
95 }
96
97
98 }
99
100 func ExampleDatabase_Dialect() {
101 db := getDB()
102
103 fmt.Println(db.Dialect())
104
105
106
107 }
108
109 func ExampleDatabase_Exec() {
110 db := getDB()
111
112 _, err := db.Exec(`DROP TABLE "user_role"; DROP TABLE "goqu_user"`)
113 if err != nil {
114 fmt.Println("Error occurred while dropping tables", err.Error())
115 }
116 fmt.Println("Dropped tables user_role and goqu_user")
117
118
119 }
120
121 func ExampleDatabase_ExecContext() {
122 db := getDB()
123 d := time.Now().Add(50 * time.Millisecond)
124 ctx, cancel := context.WithDeadline(context.Background(), d)
125 defer cancel()
126 _, err := db.ExecContext(ctx, `DROP TABLE "user_role"; DROP TABLE "goqu_user"`)
127 if err != nil {
128 fmt.Println("Error occurred while dropping tables", err.Error())
129 }
130 fmt.Println("Dropped tables user_role and goqu_user")
131
132
133 }
134
135 func ExampleDatabase_From() {
136 db := getDB()
137 var names []string
138
139 if err := db.From("goqu_user").Select("first_name").ScanVals(&names); err != nil {
140 fmt.Println(err.Error())
141 } else {
142 fmt.Println("Fetched Users names:", names)
143 }
144
145
146 }
147
View as plain text