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

Support SQLAlchemy syntax for show create table #598

Merged
merged 2 commits into from
Jan 28, 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
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:[email protected]: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:[email protected]:3306/test')
with engine.connect() as conn:
expected = {
"name": {0: 'John Doe', 1: 'John Doe', 2: 'Jane Doe', 3: 'Evil Bob'},
"email": {0: '[email protected]', 1: '[email protected]', 2: '[email protected]', 3: '[email protected]'},
"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.