From e437783dea014f06d1439110a6f74bf6304340e9 Mon Sep 17 00:00:00 2001 From: Adam Greenhall Date: Thu, 1 Aug 2013 14:56:47 -0700 Subject: [PATCH 1/3] add postgres support for pandas.io.sql.get_schema --- pandas/io/sql.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 11b139b620175..f39492d8ef032 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -259,28 +259,34 @@ def table_exists(name, con, flavor): def get_sqltype(pytype, flavor): sqltype = {'mysql': 'VARCHAR (63)', - 'sqlite': 'TEXT'} + 'sqlite': 'TEXT', + 'postgres': 'text'} if issubclass(pytype, np.floating): sqltype['mysql'] = 'FLOAT' sqltype['sqlite'] = 'REAL' + sqltype['postgres'] = 'real' if issubclass(pytype, np.integer): #TODO: Refine integer size. sqltype['mysql'] = 'BIGINT' sqltype['sqlite'] = 'INTEGER' + sqltype['postgres'] = 'integer' if issubclass(pytype, np.datetime64) or pytype is datetime: # Caution: np.datetime64 is also a subclass of np.number. sqltype['mysql'] = 'DATETIME' sqltype['sqlite'] = 'TIMESTAMP' + sqltype['postgres'] = 'timestamp' if pytype is datetime.date: sqltype['mysql'] = 'DATE' sqltype['sqlite'] = 'TIMESTAMP' + sqltype['postgres'] = 'date' if issubclass(pytype, np.bool_): sqltype['sqlite'] = 'INTEGER' + sqltype['postgres'] = 'boolean' return sqltype[flavor] @@ -292,8 +298,12 @@ def get_schema(frame, name, flavor, keys=None): column_types = zip(safe_columns, map(lookup_type, frame.dtypes)) if flavor == 'sqlite': columns = ',\n '.join('[%s] %s' % x for x in column_types) - else: + elif flavor == 'mysql': columns = ',\n '.join('`%s` %s' % x for x in column_types) + elif flavor == 'postgres': + columns = ',\n '.join('%s %s' % x for x in column_types) + else: + raise ValueError("Don't have a template for that database flavor.") keystr = '' if keys is not None: From 46423987b9acd5ad23bc97540bfcff9d0e65455f Mon Sep 17 00:00:00 2001 From: Adam Greenhall Date: Thu, 1 Aug 2013 14:56:47 -0700 Subject: [PATCH 2/3] add postgres support for pandas.io.sql.get_schema --- pandas/io/sql.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index b65c35e6b352a..c88cf0a7b605d 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -267,28 +267,34 @@ def table_exists(name, con, flavor): def get_sqltype(pytype, flavor): sqltype = {'mysql': 'VARCHAR (63)', - 'sqlite': 'TEXT'} + 'sqlite': 'TEXT', + 'postgres': 'text'} if issubclass(pytype, np.floating): sqltype['mysql'] = 'FLOAT' sqltype['sqlite'] = 'REAL' + sqltype['postgres'] = 'real' if issubclass(pytype, np.integer): #TODO: Refine integer size. sqltype['mysql'] = 'BIGINT' sqltype['sqlite'] = 'INTEGER' + sqltype['postgres'] = 'integer' if issubclass(pytype, np.datetime64) or pytype is datetime: # Caution: np.datetime64 is also a subclass of np.number. sqltype['mysql'] = 'DATETIME' sqltype['sqlite'] = 'TIMESTAMP' + sqltype['postgres'] = 'timestamp' if pytype is datetime.date: sqltype['mysql'] = 'DATE' sqltype['sqlite'] = 'TIMESTAMP' + sqltype['postgres'] = 'date' if issubclass(pytype, np.bool_): sqltype['sqlite'] = 'INTEGER' + sqltype['postgres'] = 'boolean' return sqltype[flavor] @@ -301,8 +307,12 @@ def get_schema(frame, name, flavor, keys=None): column_types = lzip(safe_columns, map(lookup_type, frame.dtypes)) if flavor == 'sqlite': columns = ',\n '.join('[%s] %s' % x for x in column_types) - else: + elif flavor == 'mysql': columns = ',\n '.join('`%s` %s' % x for x in column_types) + elif flavor == 'postgres': + columns = ',\n '.join('%s %s' % x for x in column_types) + else: + raise ValueError("Don't have a template for that database flavor.") keystr = '' if keys is not None: From dcda0bc83c0e424114befa9f5776ccdaef53fef0 Mon Sep 17 00:00:00 2001 From: Adam Greenhall Date: Thu, 1 Aug 2013 14:56:47 -0700 Subject: [PATCH 3/3] add postgres support for pandas.io.sql.get_schema --- pandas/io/sql.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index b65c35e6b352a..c88cf0a7b605d 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -267,28 +267,34 @@ def table_exists(name, con, flavor): def get_sqltype(pytype, flavor): sqltype = {'mysql': 'VARCHAR (63)', - 'sqlite': 'TEXT'} + 'sqlite': 'TEXT', + 'postgres': 'text'} if issubclass(pytype, np.floating): sqltype['mysql'] = 'FLOAT' sqltype['sqlite'] = 'REAL' + sqltype['postgres'] = 'real' if issubclass(pytype, np.integer): #TODO: Refine integer size. sqltype['mysql'] = 'BIGINT' sqltype['sqlite'] = 'INTEGER' + sqltype['postgres'] = 'integer' if issubclass(pytype, np.datetime64) or pytype is datetime: # Caution: np.datetime64 is also a subclass of np.number. sqltype['mysql'] = 'DATETIME' sqltype['sqlite'] = 'TIMESTAMP' + sqltype['postgres'] = 'timestamp' if pytype is datetime.date: sqltype['mysql'] = 'DATE' sqltype['sqlite'] = 'TIMESTAMP' + sqltype['postgres'] = 'date' if issubclass(pytype, np.bool_): sqltype['sqlite'] = 'INTEGER' + sqltype['postgres'] = 'boolean' return sqltype[flavor] @@ -301,8 +307,12 @@ def get_schema(frame, name, flavor, keys=None): column_types = lzip(safe_columns, map(lookup_type, frame.dtypes)) if flavor == 'sqlite': columns = ',\n '.join('[%s] %s' % x for x in column_types) - else: + elif flavor == 'mysql': columns = ',\n '.join('`%s` %s' % x for x in column_types) + elif flavor == 'postgres': + columns = ',\n '.join('%s %s' % x for x in column_types) + else: + raise ValueError("Don't have a template for that database flavor.") keystr = '' if keys is not None: