Skip to content

Commit 9648bc5

Browse files
committed
client: Make example testable
1 parent 0f8d049 commit 9648bc5

File tree

2 files changed

+102
-48
lines changed

2 files changed

+102
-48
lines changed

README.md

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -190,54 +190,7 @@ You can see [go-mysql-elasticsearch](https://github.com/go-mysql-org/go-mysql-el
190190

191191
Client package supports a simple MySQL connection driver which you can use it to communicate with MySQL server.
192192

193-
### Example
194-
195-
```go
196-
import (
197-
"github.com/go-mysql-org/go-mysql/client"
198-
)
199-
200-
// Connect MySQL at 127.0.0.1:3306, with user root, an empty password and database test
201-
conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test")
202-
203-
// Or to use SSL/TLS connection if MySQL server supports TLS
204-
//conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.UseSSL(true)})
205-
206-
// Or to set your own client-side certificates for identity verification for security
207-
//tlsConfig := NewClientTLSConfig(caPem, certPem, keyPem, false, "your-server-name")
208-
//conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.SetTLSConfig(tlsConfig)})
209-
210-
conn.Ping()
211-
212-
// Insert
213-
r, _ := conn.Execute(`insert into table (id, name) values (1, "abc")`)
214-
215-
// Get last insert id
216-
println(r.InsertId)
217-
// Or affected rows count
218-
println(r.AffectedRows)
219-
220-
// Select
221-
r, err := conn.Execute(`select id, name from table where id = 1`)
222-
223-
// Close result for reuse memory (it's not necessary but very useful)
224-
defer r.Close()
225-
226-
// Handle resultset
227-
v, _ := r.GetInt(0, 0)
228-
v, _ = r.GetIntByName(0, "id")
229-
230-
// Direct access to fields
231-
for _, row := range r.Values {
232-
for _, val := range row {
233-
_ = val.Value() // interface{}
234-
// or
235-
if val.Type == mysql.FieldValueTypeFloat {
236-
_ = val.AsFloat64() // float64
237-
}
238-
}
239-
}
240-
```
193+
For an example see [`example_client_test.go`](client/example_client_test.go)
241194

242195
Tested MySQL versions for the client include:
243196
- 5.5.x

client/example_client_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package client_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-mysql-org/go-mysql/client"
7+
"github.com/go-mysql-org/go-mysql/mysql"
8+
)
9+
10+
func Example() {
11+
// Connect MySQL at 127.0.0.1:3306, with user root, an empty password and database test
12+
conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test")
13+
14+
// Or to use SSL/TLS connection if MySQL server supports TLS
15+
//conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.UseSSL(true)})
16+
17+
// Or to set your own client-side certificates for identity verification for security
18+
//tlsConfig := NewClientTLSConfig(caPem, certPem, keyPem, false, "your-server-name")
19+
//conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.SetTLSConfig(tlsConfig)})
20+
21+
conn.Ping()
22+
23+
// (re)create the t1 table
24+
r, err := conn.Execute(`DROP TABLE IF EXISTS t1`)
25+
if err != nil {
26+
panic(err)
27+
}
28+
r.Close()
29+
r, err = conn.Execute(`CREATE TABLE t1 (id int PRIMARY KEY, name varchar(255))`)
30+
if err != nil {
31+
panic(err)
32+
}
33+
r.Close()
34+
35+
// Insert
36+
r, err = conn.Execute(`INSERT INTO t1(id, name) VALUES(1, "abc"),(2, "def")`)
37+
if err != nil {
38+
panic(err)
39+
}
40+
defer r.Close()
41+
42+
// Get last insert id and number of affected rows
43+
fmt.Printf("InsertId: %d, AffectedRows: %d\n", r.InsertId, r.AffectedRows)
44+
45+
// Select
46+
r, err = conn.Execute(`SELECT id, name FROM t1`)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
// Handle resultset
52+
v, err := r.GetInt(0, 0)
53+
if err != nil {
54+
panic(err)
55+
}
56+
fmt.Printf("Value of Row 0, Column 0: %d\n", v)
57+
58+
v, err = r.GetIntByName(0, "id")
59+
if err != nil {
60+
panic(err)
61+
}
62+
fmt.Printf("Value of Row 0, Column 'id': %d\n", v)
63+
64+
// Direct access to fields
65+
for rownum, row := range r.Values {
66+
fmt.Printf("Row number %d\n", rownum)
67+
for colnum, val := range row {
68+
fmt.Printf("\tColumn number %d\n", colnum)
69+
70+
ival := val.Value() // interface{}
71+
fmt.Printf("\t\tvalue (type: %d): %#v\n", val.Type, ival)
72+
73+
if val.Type == mysql.FieldValueTypeSigned {
74+
fval := val.AsInt64()
75+
fmt.Printf("\t\tint64 value: %d\n", fval)
76+
}
77+
if val.Type == mysql.FieldValueTypeString {
78+
fval := val.AsString()
79+
fmt.Printf("\t\tstring value: %s\n", fval)
80+
}
81+
}
82+
}
83+
// Output:
84+
// InsertId: 0, AffectedRows: 2
85+
// Value of Row 0, Column 0: 1
86+
// Value of Row 0, Column 'id': 1
87+
// Row number 0
88+
// Column number 0
89+
// value (type: 2): 1
90+
// int64 value: 1
91+
// Column number 1
92+
// value (type: 4): []byte{0x61, 0x62, 0x63}
93+
// string value: abc
94+
// Row number 1
95+
// Column number 0
96+
// value (type: 2): 2
97+
// int64 value: 2
98+
// Column number 1
99+
// value (type: 4): []byte{0x64, 0x65, 0x66}
100+
// string value: def
101+
}

0 commit comments

Comments
 (0)