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

Update examples #610

Merged
merged 2 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ addons:
apt:
packages:
- libonig-dev
- libmysqlclient-dev

matrix:
fast_finish: true
Expand Down Expand Up @@ -99,4 +100,13 @@ jobs:
install:
- go get ./...
script:
- make TEST=dotnet integration
- make TEST=dotnet integration

- language: c
compiler: clang
before_install:
- eval "$(gimme 1.11)"
install:
- go get ./...
script:
- make TEST=c integration
103 changes: 80 additions & 23 deletions SUPPORTED_CLIENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ These are the clients we actively test against to check are compatible with go-m
- [mariadb-java-client](#mariadb-java-client)
- Go
- [go-mysql-driver/mysql](#go-mysql-driver-mysql)
- C
- [mysql-connector-c](#mysql-connector-c)
- Grafana
- Tableau Desktop

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

connection = pymysql.connect(host='127.0.0.1',
user='user',
password='pass',
db='db',
user='root',
password='',
db='mydb',
cursorclass=pymysql.cursors.DictCursor)

try:
with connection.cursor() as cursor:
sql = "SELECT foo FROM bar"
sql = "SELECT * FROM mytable LIMIT 1"
cursor.execute(sql)
rows = cursor.fetchall()

Expand All @@ -50,14 +52,14 @@ finally:
import mysql.connector

connection = mysql.connector.connect(host='127.0.0.1',
user='user',
passwd='pass',
user='root',
passwd='',
port=3306,
database='dbname')
database='mydb')

try:
cursor = connection.cursor()
sql = "SELECT foo FROM bar"
sql = "SELECT * FROM mytable LIMIT 1"
cursor.execute(sql)
rows = cursor.fetchall()

Expand All @@ -72,7 +74,7 @@ finally:
import pandas as pd
import sqlalchemy

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

conn = Mysql::new("127.0.0.1", "user", "pass", "dbname")
resp = conn.query "SELECT foo FROM bar"
conn = Mysql::new("127.0.0.1", "root", "", "mydb")
resp = conn.query "SELECT * FROM mytable LIMIT 1"

# use resp

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

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

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

// use result
Expand All @@ -116,13 +118,13 @@ import mysql from 'mysql';
const connection = mysql.createConnection({
host: '127.0.0.1',
port: 3306,
user: 'user',
password: 'pass',
database: 'dbname'
user: 'root',
password: '',
database: 'mydb'
});
connection.connect();

const query = 'SELECT foo FROM bar';
const query = 'SELECT * FROM mytable LIMIT 1';
connection.query(query, function (error, results, _) {
if (error) throw error;

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

using (var conn = new MySqlConnection(connectionString))
{
await conn.OpenAsync();

var sql = "SELECT foo FROM bar";
var sql = "SELECT * FROM mytable LIMIT 1";

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

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

try (Connection connection = DriverManager.getConnection(dbUrl)) {
try (PreparedStatement stmt = connection.prepareStatement(query)) {
Expand Down Expand Up @@ -202,16 +204,71 @@ import (
)

func main() {
db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/test")
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/mydb")
if err != nil {
// handle error
}

rows, err := db.Query("SELECT foo FROM bar")
rows, err := db.Query("SELECT * FROM mytable LIMIT 1")
if err != nil {
// handle error
}

// use rows
}
```

### #mysql-connector-c

```c
#include <my_global.h>
#include <mysql.h>

void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char **argv)
{
MYSQL *con = NULL;
MYSQL_RES *result = NULL;
int num_fields = 0;
MYSQL_ROW row;

printf("MySQL client version: %s\n", mysql_get_client_info());

con = mysql_init(NULL);
if (con == NULL) {
finish_with_error(con);
}

if (mysql_real_connect(con, "127.0.0.1", "root", "", "mydb", 3306, NULL, 0) == NULL) {
finish_with_error(con);
}

if (mysql_query(con, "SELECT name, email, phone_numbers FROM mytable")) {
finish_with_error(con);
}

result = mysql_store_result(con);
if (result == NULL) {
finish_with_error(con);
}

num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
for(int i = 0; i < num_fields; i++) {
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}

mysql_free_result(result);
mysql_close(con);

return 0;
}
```
4 changes: 2 additions & 2 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
config := server.Config{
Protocol: "tcp",
Address: "localhost:3306",
Auth: auth.NewNativeSingle("user", "pass", auth.AllPermissions),
Auth: auth.NewNativeSingle("root", "", auth.AllPermissions),
}

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

func createTestDatabase() *mem.Database {
const (
dbName = "test"
dbName = "mydb"
tableName = "mytable"
)

Expand Down
21 changes: 21 additions & 0 deletions _integration/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Darwin: brew install mysql-connector-c
# Linux: apt-get install libmysqlclient-dev
#
CFLAGS=-Wall `mysql_config --cflags --libs`
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
CFLAGS += mysqlclient
endif

%.c:
@echo CFLAGS: $(CFLAGS)
$(CC) *.c $(CFLAGS)

test: %.c
./a.out

clean:
@rm -f *.o a.out

.PHONY: test clean
68 changes: 68 additions & 0 deletions _integration/c/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <my_global.h>
#include <mysql.h>

#include <string.h>
#include <assert.h>

#define TEST(s1, s2) do { printf("'%s' =?= '%s'\n", s1, s2); assert(0 == strcmp(s1, s2)); } while(0)

static void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char **argv)
{
MYSQL *con = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row;

int n = 0;
const int expected_num_records = 4;
const char *expected_name[expected_num_records] = {
"John Doe\0",
"John Doe\0",
"Jane Doe\0",
"Evil Bob\0"
};
const char *expected_email[expected_num_records] = {
"john@doe.com\0",
"johnalt@doe.com\0",
"jane@doe.com\0",
"evilbob@gmail.com\0"
};

printf("MySQL client version: %s\n", mysql_get_client_info());

con = mysql_init(NULL);
if (con == NULL) {
finish_with_error(con);
}

if (mysql_real_connect(con, "127.0.0.1", "root", "", "mydb", 3306, NULL, 0) == NULL) {
finish_with_error(con);
}

if (mysql_query(con, "SELECT name, email FROM mytable")) {
finish_with_error(con);
}

result = mysql_store_result(con);
if (result == NULL) {
finish_with_error(con);
}

while ((row = mysql_fetch_row(result))) {
TEST(expected_name[n], row[0]);
TEST(expected_email[n], row[1]);
++n;
}
assert(expected_num_records == n);

mysql_free_result(result);
mysql_close(con);

return 0;
}
4 changes: 2 additions & 2 deletions _integration/dotnet/MySQLTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class MySQLTest
[TestMethod]
public async Task TestCanConnect()
{
var connectionString = "server=127.0.0.1;user id=user;password=pass;port=3306;database=db;";
var connectionString = "server=127.0.0.1;user id=root;password=;port=3306;database=mydb;";
var expected = new string[][]{
new string[]{"Evil Bob", "evilbob@gmail.com"},
new string[]{"Jane Doe", "jane@doe.com"},
new string[]{"John Doe", "john@doe.com"},
new string[]{"John Doe", "johnalt@doe.com"},
};

using (var conn = new MySqlConnection(connectionString))
{
await conn.OpenAsync();
Expand Down
2 changes: 1 addition & 1 deletion _integration/go/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
_ "github.com/go-sql-driver/mysql"
)

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

func TestMySQL(t *testing.T) {
db, err := sql.Open("mysql", connectionString)
Expand Down
8 changes: 4 additions & 4 deletions _integration/javascript/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ test.cb('can connect to go-mysql-server', t => {
const connection = mysql.createConnection({
host: '127.0.0.1',
port: 3306,
user: 'user',
password: 'pass',
database: 'db'
user: 'root',
password: '',
database: 'mydb'
});

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

connection.end();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MySQLTest {

@Test
void test() {
String dbUrl = "jdbc:mariadb://127.0.0.1:3306/db?user=user&password=pass";
String dbUrl = "jdbc:mariadb://127.0.0.1:3306/mydb?user=root&password=";
String query = "SELECT name, email FROM mytable ORDER BY name, email";
List<Result> expected = new ArrayList<>();
expected.add(new Result("Evil Bob", "evilbob@gmail.com"));
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy