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

Commit e6d07a7

Browse files
authored
Merge pull request #598 from kuba--/fix-594/show-create-table
Support SQLAlchemy syntax for show create table
2 parents f33e6ea + 9ae39c2 commit e6d07a7

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

SUPPORTED_CLIENTS.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ These are the clients we actively test against to check are compatible with go-m
55
- Python
66
- [pymysql](#pymysql)
77
- [mysql-connector](#python-mysql-connector)
8+
- [sqlalchemy](#python-sqlalchemy)
89
- Ruby
910
- [ruby-mysql](#ruby-mysql)
1011
- [PHP](#php)
@@ -65,6 +66,19 @@ finally:
6566
connection.close()
6667
```
6768

69+
### Python sqlalchemy
70+
71+
```python
72+
import pandas as pd
73+
import sqlalchemy
74+
75+
engine = sqlalchemy.create_engine('mysql+pymysql://user:[email protected]:3306/dbname')
76+
with engine.connect() as conn:
77+
repo_df = pd.read_sql_table("mytable", con=conn)
78+
for table_name in repo_df.to_dict():
79+
print(table_name)
80+
```
81+
6882
### ruby-mysql
6983

7084
```ruby
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dependencies:
2+
python -m pip install -r requirements.txt
3+
4+
test: dependencies
5+
python -m unittest discover
6+
7+
.PHONY: dependencies test
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pandas
2+
sqlalchemy
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
import pandas as pd
3+
import sqlalchemy
4+
5+
6+
class TestMySQL(unittest.TestCase):
7+
8+
def test_connect(self):
9+
engine = sqlalchemy.create_engine('mysql+pymysql://user:[email protected]:3306/test')
10+
with engine.connect() as conn:
11+
expected = {
12+
"name": {0: 'John Doe', 1: 'John Doe', 2: 'Jane Doe', 3: 'Evil Bob'},
13+
14+
"phone_numbers": {0: '["555-555-555"]', 1: '[]', 2: '[]', 3: '["555-666-555","666-666-666"]'},
15+
"created_at": {0: pd.Timestamp('2019-01-28 15:35:51'), 1: pd.Timestamp('2019-01-28 15:35:51'), 2: pd.Timestamp('2019-01-28 15:35:51'), 3: pd.Timestamp('2019-01-28 15:35:51')},
16+
}
17+
18+
repo_df = pd.read_sql_table("mytable", con=conn)
19+
20+
self.assertEqual(expected, repo_df.to_dict())
21+
22+
23+
if __name__ == '__main__':
24+
unittest.main()

sql/plan/show_create_table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func produceCreateStatement(table sql.Table) string {
8989

9090
// Statement creation parts for each column
9191
for indx, col := range schema {
92-
createStmtPart := fmt.Sprintf("`%s` %s", col.Name, col.Type.Type())
92+
createStmtPart := fmt.Sprintf(" `%s` %s", col.Name, col.Type.Type())
9393

9494
if !col.Nullable {
9595
createStmtPart = fmt.Sprintf("%s NOT NULL", createStmtPart)
@@ -111,7 +111,7 @@ func produceCreateStatement(table sql.Table) string {
111111

112112
prettyColCreateStmts := strings.Join(colCreateStatements, ",\n")
113113
composedCreateTableStatement :=
114-
fmt.Sprintf("CREATE TABLE `%s` (%s) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", table.Name(), prettyColCreateStmts)
114+
fmt.Sprintf("CREATE TABLE `%s` (\n%s\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", table.Name(), prettyColCreateStmts)
115115

116116
return composedCreateTableStatement
117117
}

sql/plan/show_create_table_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func TestShowCreateTable(t *testing.T) {
3737

3838
expected := sql.NewRow(
3939
table.Name(),
40-
"CREATE TABLE `test-table` (`baz` TEXT NOT NULL,\n"+
41-
"`zab` INT32 DEFAULT 0,\n"+
42-
"`bza` INT64 DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
40+
"CREATE TABLE `test-table` (\n `baz` TEXT NOT NULL,\n"+
41+
" `zab` INT32 DEFAULT 0,\n"+
42+
" `bza` INT64 DEFAULT 0\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
4343
)
4444

4545
require.Equal(expected, row)

test-server

15.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)