Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit d3cf828

Browse files
authored
Merge pull request #610 from kuba--/doc-597
Update examples
2 parents 31ad0f9 + 2044b45 commit d3cf828

File tree

14 files changed

+207
-51
lines changed

14 files changed

+207
-51
lines changed

.travis.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ addons:
66
apt:
77
packages:
88
- libonig-dev
9+
- libmysqlclient-dev
910

1011
matrix:
1112
fast_finish: true
@@ -99,4 +100,13 @@ jobs:
99100
install:
100101
- go get ./...
101102
script:
102-
- make TEST=dotnet integration
103+
- make TEST=dotnet integration
104+
105+
- language: c
106+
compiler: clang
107+
before_install:
108+
- eval "$(gimme 1.11)"
109+
install:
110+
- go get ./...
111+
script:
112+
- make TEST=c integration

SUPPORTED_CLIENTS.md

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ These are the clients we actively test against to check are compatible with go-m
1717
- [mariadb-java-client](#mariadb-java-client)
1818
- Go
1919
- [go-mysql-driver/mysql](#go-mysql-driver-mysql)
20+
- C
21+
- [mysql-connector-c](#mysql-connector-c)
2022
- Grafana
2123
- Tableau Desktop
2224

@@ -28,14 +30,14 @@ These are the clients we actively test against to check are compatible with go-m
2830
import pymysql.cursors
2931

3032
connection = pymysql.connect(host='127.0.0.1',
31-
user='user',
32-
password='pass',
33-
db='db',
33+
user='root',
34+
password='',
35+
db='mydb',
3436
cursorclass=pymysql.cursors.DictCursor)
3537

3638
try:
3739
with connection.cursor() as cursor:
38-
sql = "SELECT foo FROM bar"
40+
sql = "SELECT * FROM mytable LIMIT 1"
3941
cursor.execute(sql)
4042
rows = cursor.fetchall()
4143

@@ -50,14 +52,14 @@ finally:
5052
import mysql.connector
5153

5254
connection = mysql.connector.connect(host='127.0.0.1',
53-
user='user',
54-
passwd='pass',
55+
user='root',
56+
passwd='',
5557
port=3306,
56-
database='dbname')
58+
database='mydb')
5759

5860
try:
5961
cursor = connection.cursor()
60-
sql = "SELECT foo FROM bar"
62+
sql = "SELECT * FROM mytable LIMIT 1"
6163
cursor.execute(sql)
6264
rows = cursor.fetchall()
6365

@@ -72,7 +74,7 @@ finally:
7274
import pandas as pd
7375
import sqlalchemy
7476

75-
engine = sqlalchemy.create_engine('mysql+pymysql://user:pass@127.0.0.1:3306/dbname')
77+
engine = sqlalchemy.create_engine('mysql+pymysql://root:@127.0.0.1:3306/mydb')
7678
with engine.connect() as conn:
7779
repo_df = pd.read_sql_table("mytable", con=conn)
7880
for table_name in repo_df.to_dict():
@@ -84,8 +86,8 @@ with engine.connect() as conn:
8486
```ruby
8587
require "mysql"
8688

87-
conn = Mysql::new("127.0.0.1", "user", "pass", "dbname")
88-
resp = conn.query "SELECT foo FROM bar"
89+
conn = Mysql::new("127.0.0.1", "root", "", "mydb")
90+
resp = conn.query "SELECT * FROM mytable LIMIT 1"
8991

9092
# use resp
9193

@@ -96,10 +98,10 @@ conn.close()
9698

9799
```php
98100
try {
99-
$conn = new PDO("mysql:host=127.0.0.1:3306;dbname=dbname", "user", "pass");
101+
$conn = new PDO("mysql:host=127.0.0.1:3306;dbname=mydb", "root", "");
100102
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
101103

102-
$stmt = $conn->query('SELECT foo FROM bar');
104+
$stmt = $conn->query('SELECT * FROM mytable LIMIT 1');
103105
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
104106

105107
// use result
@@ -116,13 +118,13 @@ import mysql from 'mysql';
116118
const connection = mysql.createConnection({
117119
host: '127.0.0.1',
118120
port: 3306,
119-
user: 'user',
120-
password: 'pass',
121-
database: 'dbname'
121+
user: 'root',
122+
password: '',
123+
database: 'mydb'
122124
});
123125
connection.connect();
124126

125-
const query = 'SELECT foo FROM bar';
127+
const query = 'SELECT * FROM mytable LIMIT 1';
126128
connection.query(query, function (error, results, _) {
127129
if (error) throw error;
128130

@@ -144,13 +146,13 @@ namespace something
144146
{
145147
public async Task DoQuery()
146148
{
147-
var connectionString = "server=127.0.0.1;user id=user;password=pass;port=3306;database=dbname;";
149+
var connectionString = "server=127.0.0.1;user id=root;password=;port=3306;database=mydb;";
148150

149151
using (var conn = new MySqlConnection(connectionString))
150152
{
151153
await conn.OpenAsync();
152154

153-
var sql = "SELECT foo FROM bar";
155+
var sql = "SELECT * FROM mytable LIMIT 1";
154156

155157
using (var cmd = new MySqlCommand(sql, conn))
156158
using (var reader = await cmd.ExecuteReaderAsync())
@@ -172,8 +174,8 @@ import java.sql.*;
172174

173175
class Main {
174176
public static void main(String[] args) {
175-
String dbUrl = "jdbc:mariadb://127.0.0.1:3306/dbname?user=user&password=pass";
176-
String query = "SELECT foo FROM bar";
177+
String dbUrl = "jdbc:mariadb://127.0.0.1:3306/mydb?user=root&password=";
178+
String query = "SELECT * FROM mytable LIMIT 1";
177179

178180
try (Connection connection = DriverManager.getConnection(dbUrl)) {
179181
try (PreparedStatement stmt = connection.prepareStatement(query)) {
@@ -202,16 +204,71 @@ import (
202204
)
203205

204206
func main() {
205-
db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/test")
207+
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/mydb")
206208
if err != nil {
207209
// handle error
208210
}
209211

210-
rows, err := db.Query("SELECT foo FROM bar")
212+
rows, err := db.Query("SELECT * FROM mytable LIMIT 1")
211213
if err != nil {
212214
// handle error
213215
}
214216

215217
// use rows
216218
}
219+
```
220+
221+
### #mysql-connector-c
222+
223+
```c
224+
#include <my_global.h>
225+
#include <mysql.h>
226+
227+
void finish_with_error(MYSQL *con)
228+
{
229+
fprintf(stderr, "%s\n", mysql_error(con));
230+
mysql_close(con);
231+
exit(1);
232+
}
233+
234+
int main(int argc, char **argv)
235+
{
236+
MYSQL *con = NULL;
237+
MYSQL_RES *result = NULL;
238+
int num_fields = 0;
239+
MYSQL_ROW row;
240+
241+
printf("MySQL client version: %s\n", mysql_get_client_info());
242+
243+
con = mysql_init(NULL);
244+
if (con == NULL) {
245+
finish_with_error(con);
246+
}
247+
248+
if (mysql_real_connect(con, "127.0.0.1", "root", "", "mydb", 3306, NULL, 0) == NULL) {
249+
finish_with_error(con);
250+
}
251+
252+
if (mysql_query(con, "SELECT name, email, phone_numbers FROM mytable")) {
253+
finish_with_error(con);
254+
}
255+
256+
result = mysql_store_result(con);
257+
if (result == NULL) {
258+
finish_with_error(con);
259+
}
260+
261+
num_fields = mysql_num_fields(result);
262+
while ((row = mysql_fetch_row(result))) {
263+
for(int i = 0; i < num_fields; i++) {
264+
printf("%s ", row[i] ? row[i] : "NULL");
265+
}
266+
printf("\n");
267+
}
268+
269+
mysql_free_result(result);
270+
mysql_close(con);
271+
272+
return 0;
273+
}
217274
```

_example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
config := server.Config{
3232
Protocol: "tcp",
3333
Address: "localhost:3306",
34-
Auth: auth.NewNativeSingle("user", "pass", auth.AllPermissions),
34+
Auth: auth.NewNativeSingle("root", "", auth.AllPermissions),
3535
}
3636

3737
s, err := server.NewDefaultServer(config, engine)
@@ -44,7 +44,7 @@ func main() {
4444

4545
func createTestDatabase() *mem.Database {
4646
const (
47-
dbName = "test"
47+
dbName = "mydb"
4848
tableName = "mytable"
4949
)
5050

_integration/c/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Darwin: brew install mysql-connector-c
3+
# Linux: apt-get install libmysqlclient-dev
4+
#
5+
CFLAGS=-Wall `mysql_config --cflags --libs`
6+
UNAME_S := $(shell uname -s)
7+
ifeq ($(UNAME_S),Darwin)
8+
CFLAGS += mysqlclient
9+
endif
10+
11+
%.c:
12+
@echo CFLAGS: $(CFLAGS)
13+
$(CC) *.c $(CFLAGS)
14+
15+
test: %.c
16+
./a.out
17+
18+
clean:
19+
@rm -f *.o a.out
20+
21+
.PHONY: test clean

_integration/c/test.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <my_global.h>
2+
#include <mysql.h>
3+
4+
#include <string.h>
5+
#include <assert.h>
6+
7+
#define TEST(s1, s2) do { printf("'%s' =?= '%s'\n", s1, s2); assert(0 == strcmp(s1, s2)); } while(0)
8+
9+
static void finish_with_error(MYSQL *con)
10+
{
11+
fprintf(stderr, "%s\n", mysql_error(con));
12+
mysql_close(con);
13+
exit(1);
14+
}
15+
16+
int main(int argc, char **argv)
17+
{
18+
MYSQL *con = NULL;
19+
MYSQL_RES *result = NULL;
20+
MYSQL_ROW row;
21+
22+
int n = 0;
23+
const int expected_num_records = 4;
24+
const char *expected_name[expected_num_records] = {
25+
"John Doe\0",
26+
"John Doe\0",
27+
"Jane Doe\0",
28+
"Evil Bob\0"
29+
};
30+
const char *expected_email[expected_num_records] = {
31+
32+
33+
34+
35+
};
36+
37+
printf("MySQL client version: %s\n", mysql_get_client_info());
38+
39+
con = mysql_init(NULL);
40+
if (con == NULL) {
41+
finish_with_error(con);
42+
}
43+
44+
if (mysql_real_connect(con, "127.0.0.1", "root", "", "mydb", 3306, NULL, 0) == NULL) {
45+
finish_with_error(con);
46+
}
47+
48+
if (mysql_query(con, "SELECT name, email FROM mytable")) {
49+
finish_with_error(con);
50+
}
51+
52+
result = mysql_store_result(con);
53+
if (result == NULL) {
54+
finish_with_error(con);
55+
}
56+
57+
while ((row = mysql_fetch_row(result))) {
58+
TEST(expected_name[n], row[0]);
59+
TEST(expected_email[n], row[1]);
60+
++n;
61+
}
62+
assert(expected_num_records == n);
63+
64+
mysql_free_result(result);
65+
mysql_close(con);
66+
67+
return 0;
68+
}

_integration/dotnet/MySQLTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public class MySQLTest
1010
[TestMethod]
1111
public async Task TestCanConnect()
1212
{
13-
var connectionString = "server=127.0.0.1;user id=user;password=pass;port=3306;database=db;";
13+
var connectionString = "server=127.0.0.1;user id=root;password=;port=3306;database=mydb;";
1414
var expected = new string[][]{
1515
new string[]{"Evil Bob", "[email protected]"},
1616
new string[]{"Jane Doe", "[email protected]"},
1717
new string[]{"John Doe", "[email protected]"},
1818
new string[]{"John Doe", "[email protected]"},
1919
};
20-
20+
2121
using (var conn = new MySqlConnection(connectionString))
2222
{
2323
await conn.OpenAsync();

_integration/go/mysql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
_ "github.com/go-sql-driver/mysql"
99
)
1010

11-
const connectionString = "user:pass@tcp(127.0.0.1:3306)/test"
11+
const connectionString = "root:@tcp(127.0.0.1:3306)/mydb"
1212

1313
func TestMySQL(t *testing.T) {
1414
db, err := sql.Open("mysql", connectionString)

_integration/javascript/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ test.cb('can connect to go-mysql-server', t => {
55
const connection = mysql.createConnection({
66
host: '127.0.0.1',
77
port: 3306,
8-
user: 'user',
9-
password: 'pass',
10-
database: 'db'
8+
user: 'root',
9+
password: '',
10+
database: 'mydb'
1111
});
1212

1313
connection.connect();
@@ -29,4 +29,4 @@ test.cb('can connect to go-mysql-server', t => {
2929
});
3030

3131
connection.end();
32-
});
32+
});

_integration/jdbc-mariadb/src/test/java/tech/sourced/jdbcmariadb/MySQLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MySQLTest {
1414

1515
@Test
1616
void test() {
17-
String dbUrl = "jdbc:mariadb://127.0.0.1:3306/db?user=user&password=pass";
17+
String dbUrl = "jdbc:mariadb://127.0.0.1:3306/mydb?user=root&password=";
1818
String query = "SELECT name, email FROM mytable ORDER BY name, email";
1919
List<Result> expected = new ArrayList<>();
2020
expected.add(new Result("Evil Bob", "[email protected]"));

0 commit comments

Comments
 (0)