Skip to content

Commit 3d9bbc6

Browse files
authored
Merge branch 'master' into fix-time
2 parents fe1fc3d + 8aa9aa6 commit 3d9bbc6

File tree

2 files changed

+113
-48
lines changed

2 files changed

+113
-48
lines changed

README.md

+2-48
Original file line numberDiff line numberDiff line change
@@ -190,54 +190,8 @@ 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). You can run this testable example with
194+
`go test -v ./client -run Example`.
241195

242196
Tested MySQL versions for the client include:
243197
- 5.5.x

client/example_client_test.go

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

0 commit comments

Comments
 (0)