-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
215 lines (177 loc) · 4.95 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cli_test
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"strings"
"time"
_ "github.com/asifjalil/cli"
)
func Example() {
ExampleOpen()
ExampleLoad()
// Skip ExampleProc because the output is not always the same.
ExampleProc()
//Output:
//1.1
//Hello
//World
}
func ExampleOpen() {
var val float64
connStr := getConStr() // trailing semi-colon is required
qry := "SELECT double(1.1) FROM sysibm.sysdummy1"
log.Println(strings.Repeat("#", 30))
log.Println("Shows how to connect, query, and disconnect from a DB2 database using the \"cli\" driver.")
log.Printf("sql.Open(\"cli\",\"%s\")\n", connStr)
db, err := sql.Open("cli", connStr)
checkError(err)
defer db.Close()
log.Println("Connected...")
// run a dummy query
log.Printf("db.QueryRow(\"%s\").Scan(&val)\n", qry)
err = db.QueryRow(qry).Scan(&val)
checkError(err)
log.Println(val)
fmt.Println(val)
log.Println("Disconnecting...")
log.Printf("db.Close()")
}
func ExampleCurrentSchema() {
connStr := getConStr()
connStr += " CurrentSchema = SYSIBM;"
qry := "Select 100 from systables fetch first 1 row only"
log.Println(strings.Repeat("#", 30))
log.Println("Shows how to set current schema in the connection string")
log.Printf("sql.Open(\"cli\",\"%s\")\n", connStr)
db, err := sql.Open("cli", connStr)
checkError(err)
defer db.Close()
var val int
err = db.QueryRow(qry).Scan(&val)
checkError(err)
log.Printf("Expecting 100, got %v\n", val)
fmt.Println(val)
// Output: 100
}
func ExampleProc() {
connStr := getConStr()
var (
snapTime time.Time
dbsize int64
dbcapacity int64
)
procStmt := "call sysproc.get_dbsize_info(?, ?, ?, 0)"
db, err := sql.Open("cli", connStr)
checkError(err)
defer db.Close()
log.Println(strings.Repeat("#", 30))
log.Println("Shows how to use a stored procedure with OUTPUT parameters")
log.Printf("Running %q\n", procStmt)
_, err = db.ExecContext(context.Background(), procStmt,
sql.Out{Dest: &snapTime},
sql.Out{Dest: &dbsize},
sql.Out{Dest: &dbcapacity})
checkError(err)
log.Printf("snapshot time: %v, dbsize: %d, dbcapacity: %d\n", snapTime, dbsize, dbcapacity)
log.Println("success ...")
}
func ExampleLoad() {
tabname := "loadtable"
createStmt := fmt.Sprintf("CREATE TABLE %s (Col1 VARCHAR(30))", tabname)
dropStmt := fmt.Sprintf("DROP TABLE %s", tabname)
connStr := getConStr()
tmpflName := "_TEST/test.del"
log.Println(strings.Repeat("#", 30))
log.Println("Shows how to load a table using SYSPROC.ADMIN_CMD and the \"cli\" driver.")
if home := os.Getenv("DATABASE_HOMEDIR"); home == "" {
log.Println("DATABASE_HOMEDIR is not set; skipping ExampleLoad")
return
} else {
tmpflName = home + "/" + tmpflName
}
db, err := sql.Open("cli", connStr)
checkError(err)
defer db.Close()
// setup
log.Println("Table to load: ", createStmt)
_, err = db.Exec(createStmt)
checkError(err)
// load
var (
rows_read, rows_skipped,
rows_loaded, rows_rejected, rows_deleted,
rows_committed, rows_partitioned, num_agentinfo_entries sql.NullInt64
msg_retrieval, msg_removal sql.NullString
)
admin_cmd := fmt.Sprintf("CALL SYSPROC.ADMIN_CMD('LOAD FROM %s"+
" OF DEL REPLACE INTO %s NONRECOVERABLE')", tmpflName, tabname)
log.Println("load command: ", admin_cmd)
rows, err := db.Query(admin_cmd)
checkError(err)
// only get the first result set
rows.Next()
err = rows.Scan(&rows_read, &rows_skipped, &rows_loaded,
&rows_rejected, &rows_deleted, &rows_committed,
&rows_partitioned, &num_agentinfo_entries,
&msg_retrieval, &msg_removal)
checkError(err)
log.Println("Rows Read : ", rows_read.Int64)
log.Println("Rows Skipped : ", rows_skipped.Int64)
log.Println("Rows Loaded : ", rows_loaded.Int64)
log.Println("Rows Rejected : ", rows_rejected.Int64)
log.Println("Rows Deleted : ", rows_deleted.Int64)
log.Println("Rows Committed: ", rows_committed.Int64)
log.Println("Msg Retrieval : ", msg_retrieval.String)
log.Println("Msg Removal : ", msg_removal.String)
err = rows.Close()
checkError(err)
// check the data
var col1 string
selectStmt := fmt.Sprintf("SELECT Col1 FROM %s", tabname)
log.Println("select: ", selectStmt)
rows, err = db.Query(selectStmt)
checkError(err)
for rows.Next() {
err = rows.Scan(&col1)
checkError(err)
log.Println(col1)
fmt.Println(col1)
}
checkError(rows.Err())
err = rows.Close()
checkError(err)
// cleanup
log.Println("Cleanup: ", dropStmt)
_, err = db.Exec(dropStmt)
checkError(err)
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func getConStr() string {
config := struct {
database string
uid string
pwd string
}{
database: "sample",
uid: "",
pwd: "",
}
if name := os.Getenv("DATABASE_NAME"); name != "" {
config.database = name
}
if user := os.Getenv("DATABASE_USER"); user != "" {
config.uid = user
}
if pass := os.Getenv("DATABASE_PASSWORD"); pass != "" {
config.pwd = pass
}
return fmt.Sprintf("DATABASE = %s; UID = %s; PWD = %s;",
config.database, config.uid, config.pwd)
}