Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom SQLAlchemy dialects #532

Merged
merged 4 commits into from
Jan 30, 2016
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ The following individuals have contributed code to csvkit:
* Kev++
* edwardros
* Martin Burch
* Pedro Silva
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Fixes:
* in2csv again supports nested JSON objects (fixes regression).
* csvgrep can match multiline values.
* csvgrep supports --no-header-row.
* csvsql supports custom SQLAlchemy dialects.
* csvstack supports stacking a single file.
* FilteringCSVReader's any_match argument works correctly.
* A file of two empty rows won't raise an error.
Expand Down
19 changes: 4 additions & 15 deletions csvkit/sql.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
#!/usr/bin/env python

import datetime
from pkg_resources import iter_entry_points

import six

from sqlalchemy import Column, MetaData, Table, create_engine
from sqlalchemy import Column, MetaData, Table, create_engine, dialects
from sqlalchemy import BigInteger, Boolean, Date, DateTime, Float, Integer, String, Time
from sqlalchemy.schema import CreateTable

NoneType = type(None)

DIALECTS = {
'access': 'access.base',
'firebird': 'firebird.kinterbasdb',
'informix': 'informix.informixdb',
'maxdb': 'maxdb.sapdb',
'mssql': 'mssql.pyodbc',
'mysql': 'mysql.mysqlconnector',
'oracle': 'oracle.cx_oracle',
'postgresql': 'postgresql.psycopg2',
'sqlite': 'sqlite.pysqlite',
'sybase': 'sybase.pyodbc'
}
DIALECTS = dialects.__all__ + tuple(e.name for e in iter_entry_points('sqlalchemy.dialects'))

NULL_COLUMN_MAX_LENGTH = 32
SQL_INTEGER_MAX = 2147483647
Expand Down Expand Up @@ -97,8 +87,7 @@ def make_create_table_statement(sql_table, dialect=None):
Generates a CREATE TABLE statement for a sqlalchemy table.
"""
if dialect:
module = __import__('sqlalchemy.dialects.%s' % DIALECTS[dialect], fromlist=['dialect'])
sql_dialect = module.dialect()
sql_dialect = dialects.registry.load(dialect)()
else:
sql_dialect = None

Expand Down
5 changes: 5 additions & 0 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ def test_make_create_table_statement_with_schema(self):
\tempty_column VARCHAR(32)
);""")

def test_make_create_table_statement_with_dialects(self):
for dialect in sql.DIALECTS:
sql_table = sql.make_table(self.csv_table, 'csvsql', db_schema='test_schema')
statement = sql.make_create_table_statement(sql_table, dialect)

def make_insert_statement(self):
sql_table = sql.make_table(self.csv_table, 'csvsql')
statement = sql.make_insert_statement(sql_table, self.csv_table._prepare_rows_for_serialization()[0])
Expand Down