@@ -29,6 +29,37 @@ class DatabaseError(IOError):
29
29
#------------------------------------------------------------------------------
30
30
# Helper functions
31
31
32
+ _SQLALCHEMY_INSTALLED = None
33
+
34
+ def _is_sqlalchemy_engine (con ):
35
+ global _SQLALCHEMY_INSTALLED
36
+ if _SQLALCHEMY_INSTALLED is None :
37
+ try :
38
+ import sqlalchemy
39
+ _SQLALCHEMY_INSTALLED = True
40
+
41
+ from distutils .version import LooseVersion
42
+ ver = LooseVersion (sqlalchemy .__version__ )
43
+ # For sqlalchemy versions < 0.8.2, the BIGINT type is recognized
44
+ # for a sqlite engine, which results in a warning when trying to
45
+ # read/write a DataFrame with int64 values. (GH7433)
46
+ if ver < '0.8.2' :
47
+ from sqlalchemy import BigInteger
48
+ from sqlalchemy .ext .compiler import compiles
49
+
50
+ @compiles (BigInteger , 'sqlite' )
51
+ def compile_big_int_sqlite (type_ , compiler , ** kw ):
52
+ return 'INTEGER'
53
+ except ImportError :
54
+ _SQLALCHEMY_INSTALLED = False
55
+
56
+ if _SQLALCHEMY_INSTALLED :
57
+ import sqlalchemy
58
+ return isinstance (con , sqlalchemy .engine .Engine )
59
+ else :
60
+ return False
61
+
62
+
32
63
def _convert_params (sql , params ):
33
64
"""convert sql and params args to DBAPI2.0 compliant format"""
34
65
args = [sql ]
@@ -76,17 +107,6 @@ def _parse_date_columns(data_frame, parse_dates):
76
107
return data_frame
77
108
78
109
79
- def _is_sqlalchemy_engine (con ):
80
- try :
81
- import sqlalchemy
82
- if isinstance (con , sqlalchemy .engine .Engine ):
83
- return True
84
- else :
85
- return False
86
- except ImportError :
87
- return False
88
-
89
-
90
110
def execute (sql , con , cur = None , params = None ):
91
111
"""
92
112
Execute the given SQL query using the provided connection object.
@@ -271,8 +291,10 @@ def read_sql_table(table_name, con, index_col=None, coerce_float=True,
271
291
read_sql_query : Read SQL query into a DataFrame.
272
292
read_sql
273
293
274
-
275
294
"""
295
+ if not _is_sqlalchemy_engine (con ):
296
+ raise NotImplementedError ("read_sql_table only supported for "
297
+ "SQLAlchemy engines." )
276
298
import sqlalchemy
277
299
from sqlalchemy .schema import MetaData
278
300
meta = MetaData (con )
0 commit comments