@@ -98,7 +98,10 @@ def execute(sql, con, cur=None, params=None, flavor='sqlite'):
98
98
-------
99
99
Results Iterable
100
100
"""
101
- pandas_sql = pandasSQL_builder (con , flavor = flavor )
101
+ if cur is None :
102
+ pandas_sql = pandasSQL_builder (con , flavor = flavor )
103
+ else :
104
+ pandas_sql = pandasSQL_builder (cur , flavor = flavor , is_cursor = True )
102
105
args = _convert_params (sql , params )
103
106
return pandas_sql .execute (* args )
104
107
@@ -473,11 +476,13 @@ def has_table(table_name, con, flavor='sqlite'):
473
476
table_exists = has_table
474
477
475
478
476
- def pandasSQL_builder (con , flavor = None , meta = None ):
479
+ def pandasSQL_builder (con , flavor = None , meta = None , is_cursor = False ):
477
480
"""
478
481
Convenience function to return the correct PandasSQL subclass based on the
479
482
provided parameters
480
483
"""
484
+ # When support for DBAPI connections is removed,
485
+ # is_cursor should not be necessary.
481
486
try :
482
487
import sqlalchemy
483
488
@@ -491,14 +496,14 @@ def pandasSQL_builder(con, flavor=None, meta=None):
491
496
"PandasSQL must be created with an SQLAlchemy engine "
492
497
"or a DBAPI2 connection and SQL flavor" )
493
498
else :
494
- return PandasSQLLegacy (con , flavor )
499
+ return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
495
500
496
501
except ImportError :
497
502
warnings .warn ("SQLAlchemy not installed, using legacy mode" )
498
503
if flavor is None :
499
504
raise SQLAlchemyRequired
500
505
else :
501
- return PandasSQLLegacy (con , flavor )
506
+ return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
502
507
503
508
504
509
class PandasSQLTable (PandasObject ):
@@ -983,16 +988,20 @@ def _sql_type_name(self, dtype):
983
988
984
989
class PandasSQLLegacy (PandasSQL ):
985
990
986
- def __init__ (self , con , flavor ):
991
+ def __init__ (self , con , flavor , is_cursor = False ):
992
+ self .is_cursor = is_cursor
987
993
self .con = con
988
994
if flavor not in ['sqlite' , 'mysql' ]:
989
995
raise NotImplementedError
990
996
else :
991
997
self .flavor = flavor
992
998
993
999
def execute (self , * args , ** kwargs ):
994
- try :
1000
+ if self .is_cursor :
1001
+ cur = self .con
1002
+ else :
995
1003
cur = self .con .cursor ()
1004
+ try :
996
1005
if kwargs :
997
1006
cur .execute (* args , ** kwargs )
998
1007
else :
0 commit comments