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 1 commit
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
Prev Previous commit
Add example for C
Signed-off-by: kuba-- <kuba@sourced.tech>
  • Loading branch information
kuba-- committed Jan 31, 2019
commit 2044b452a73620361292860112a7edd53b456bb2
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
93 changes: 75 additions & 18 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 @@ -30,12 +32,12 @@ import pymysql.cursors
connection = pymysql.connect(host='127.0.0.1',
user='root',
password='',
db='gitbase',
db='mydb',
cursorclass=pymysql.cursors.DictCursor)

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

Expand All @@ -53,11 +55,11 @@ connection = mysql.connector.connect(host='127.0.0.1',
user='root',
passwd='',
port=3306,
database='gitbase')
database='mydb')

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

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

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

conn = Mysql::new("127.0.0.1", "root", "", "gitbase")
resp = conn.query "SELECT * FROM commit_files LIMIT 1"
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=gitbase", "root", "");
$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 * FROM commit_files LIMIT 1');
$stmt = $conn->query('SELECT * FROM mytable LIMIT 1');
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

// use result
Expand All @@ -118,11 +120,11 @@ const connection = mysql.createConnection({
port: 3306,
user: 'root',
password: '',
database: 'gitbase'
database: 'mydb'
});
connection.connect();

const query = 'SELECT * FROM commit_files LIMIT 1';
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=root;password=;port=3306;database=gitbase;";
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 * FROM commit_files LIMIT 1";
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/gitbase?user=root&password=";
String query = "SELECT * FROM commit_files LIMIT 1";
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", "root:@tcp(127.0.0.1:3306)/gitbase")
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/mydb")
if err != nil {
// handle error
}

rows, err := db.Query("SELECT * FROM commit_files LIMIT 1")
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;
}
```
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {

func createTestDatabase() *mem.Database {
const (
dbName = "gitbase"
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;
}
2 changes: 1 addition & 1 deletion _integration/dotnet/MySQLTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MySQLTest
[TestMethod]
public async Task TestCanConnect()
{
var connectionString = "server=127.0.0.1;user id=root;password=;port=3306;database=gitbase;";
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"},
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 = "root:@tcp(127.0.0.1:3306)/gitbase"
const connectionString = "root:@tcp(127.0.0.1:3306)/mydb"

func TestMySQL(t *testing.T) {
db, err := sql.Open("mysql", connectionString)
Expand Down
2 changes: 1 addition & 1 deletion _integration/javascript/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test.cb('can connect to go-mysql-server', t => {
port: 3306,
user: 'root',
password: '',
database: 'gitbase'
database: 'mydb'
});

connection.connect();
Expand Down
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/gitbase?user=root&password=";
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
2 changes: 1 addition & 1 deletion _integration/php/tests/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class MySQLTest extends TestCase
{
public function testConnection(): void {
try {
$conn = new PDO("mysql:host=127.0.0.1:3306;dbname=gitbase", "root", "");
$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 name, email FROM mytable ORDER BY name, email');
Expand Down
2 changes: 1 addition & 1 deletion _integration/python-sqlalchemy/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class TestMySQL(unittest.TestCase):

def test_connect(self):
engine = sqlalchemy.create_engine('mysql+pymysql://root:@127.0.0.1:3306/gitbase')
engine = sqlalchemy.create_engine('mysql+pymysql://root:@127.0.0.1:3306/mydb')
with engine.connect() as conn:
expected = {
"name": {0: 'John Doe', 1: 'John Doe', 2: 'Jane Doe', 3: 'Evil Bob'},
Expand Down
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