Skip to content

Commit e85087f

Browse files
author
Thomas Grainger
committed
support both sqlalchemy engines and connections Fixes pandas-dev#7877
1 parent eafd22d commit e85087f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

pandas/io/sql.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def compile_big_int_sqlite(type_, compiler, **kw):
6262

6363
if _SQLALCHEMY_INSTALLED:
6464
import sqlalchemy
65-
return isinstance(con, sqlalchemy.engine.Engine)
65+
return isinstance(con, sqlalchemy.engine.Connectable)
6666
else:
6767
return False
6868

@@ -637,7 +637,7 @@ def exists(self):
637637

638638
def sql_schema(self):
639639
from sqlalchemy.schema import CreateTable
640-
return str(CreateTable(self.table).compile(self.pd_sql.engine))
640+
return str(CreateTable(self.table).compile(self.pd_sql.connection))
641641

642642
def _execute_create(self):
643643
# Inserting table into database, add to MetaData object
@@ -1006,20 +1006,31 @@ class SQLDatabase(PandasSQL):
10061006
10071007
"""
10081008

1009-
def __init__(self, engine, schema=None, meta=None):
1010-
self.engine = engine
1009+
def __init__(self, connection, schema=None, meta=None):
1010+
import sqlalchemy.engine
1011+
if isinstance(connection, sqlalchemy.engine.Engine):
1012+
self.connection = connection.connect()
1013+
else:
1014+
self.connection = connection
10111015
if not meta:
10121016
from sqlalchemy.schema import MetaData
1013-
meta = MetaData(self.engine, schema=schema)
1017+
meta = MetaData(self.connection, schema=schema)
10141018

10151019
self.meta = meta
10161020

1021+
@contextmanager
10171022
def run_transaction(self):
1018-
return self.engine.begin()
1023+
trans = self.connection.begin()
1024+
try:
1025+
yield self.connection
1026+
trans.commit()
1027+
except:
1028+
trans.rollback()
1029+
raise
10191030

10201031
def execute(self, *args, **kwargs):
10211032
"""Simple passthrough to SQLAlchemy engine"""
1022-
return self.engine.execute(*args, **kwargs)
1033+
return self.connection.execute(*args, **kwargs)
10231034

10241035
def read_table(self, table_name, index_col=None, coerce_float=True,
10251036
parse_dates=None, columns=None, schema=None,
@@ -1187,7 +1198,7 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
11871198
table.create()
11881199
table.insert(chunksize)
11891200
# check for potentially case sensitivity issues (GH7815)
1190-
if name not in self.engine.table_names(schema=schema or self.meta.schema):
1201+
if name not in self.connection.engine.table_names(schema=schema or self.meta.schema, connection=self.connection):
11911202
warnings.warn("The provided table name '{0}' is not found exactly "
11921203
"as such in the database after writing the table, "
11931204
"possibly due to case sensitivity issues. Consider "
@@ -1198,7 +1209,7 @@ def tables(self):
11981209
return self.meta.tables
11991210

12001211
def has_table(self, name, schema=None):
1201-
return self.engine.has_table(name, schema or self.meta.schema)
1212+
return self.connection.engine.has_table(name, schema or self.meta.schema)
12021213

12031214
def get_table(self, table_name, schema=None):
12041215
schema = schema or self.meta.schema
@@ -1217,7 +1228,7 @@ def get_table(self, table_name, schema=None):
12171228

12181229
def drop_table(self, table_name, schema=None):
12191230
schema = schema or self.meta.schema
1220-
if self.engine.has_table(table_name, schema):
1231+
if self.connection.engine.has_table(table_name, schema):
12211232
self.meta.reflect(only=[table_name], schema=schema)
12221233
self.get_table(table_name, schema).drop()
12231234
self.meta.clear()

0 commit comments

Comments
 (0)