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

Commit

Permalink
Merge pull request #598 from kuba--/fix-594/show-create-table
Browse files Browse the repository at this point in the history
Support SQLAlchemy syntax for show create table
  • Loading branch information
ajnavarro authored Jan 28, 2019
2 parents f33e6ea + 9ae39c2 commit e6d07a7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
14 changes: 14 additions & 0 deletions SUPPORTED_CLIENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ These are the clients we actively test against to check are compatible with go-m
- Python
- [pymysql](#pymysql)
- [mysql-connector](#python-mysql-connector)
- [sqlalchemy](#python-sqlalchemy)
- Ruby
- [ruby-mysql](#ruby-mysql)
- [PHP](#php)
Expand Down Expand Up @@ -65,6 +66,19 @@ finally:
connection.close()
```

### Python sqlalchemy

```python
import pandas as pd
import sqlalchemy

engine = sqlalchemy.create_engine('mysql+pymysql://user:pass@127.0.0.1:3306/dbname')
with engine.connect() as conn:
repo_df = pd.read_sql_table("mytable", con=conn)
for table_name in repo_df.to_dict():
print(table_name)
```

### ruby-mysql

```ruby
Expand Down
7 changes: 7 additions & 0 deletions _integration/python-sqlalchemy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies:
python -m pip install -r requirements.txt

test: dependencies
python -m unittest discover

.PHONY: dependencies test
2 changes: 2 additions & 0 deletions _integration/python-sqlalchemy/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pandas
sqlalchemy
24 changes: 24 additions & 0 deletions _integration/python-sqlalchemy/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
import pandas as pd
import sqlalchemy


class TestMySQL(unittest.TestCase):

def test_connect(self):
engine = sqlalchemy.create_engine('mysql+pymysql://user:pass@127.0.0.1:3306/test')
with engine.connect() as conn:
expected = {
"name": {0: 'John Doe', 1: 'John Doe', 2: 'Jane Doe', 3: 'Evil Bob'},
"email": {0: 'john@doe.com', 1: 'johnalt@doe.com', 2: 'jane@doe.com', 3: 'evilbob@gmail.com'},
"phone_numbers": {0: '["555-555-555"]', 1: '[]', 2: '[]', 3: '["555-666-555","666-666-666"]'},
"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')},
}

repo_df = pd.read_sql_table("mytable", con=conn)

self.assertEqual(expected, repo_df.to_dict())


if __name__ == '__main__':
unittest.main()
4 changes: 2 additions & 2 deletions sql/plan/show_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func produceCreateStatement(table sql.Table) string {

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

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

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

return composedCreateTableStatement
}
Expand Down
6 changes: 3 additions & 3 deletions sql/plan/show_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func TestShowCreateTable(t *testing.T) {

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

require.Equal(expected, row)
Expand Down
Binary file added test-server
Binary file not shown.

0 comments on commit e6d07a7

Please sign in to comment.