@@ -29,6 +29,37 @@ class DatabaseError(IOError):
2929#------------------------------------------------------------------------------
3030# Helper functions
3131
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+
3263def _convert_params (sql , params ):
3364 """convert sql and params args to DBAPI2.0 compliant format"""
3465 args = [sql ]
@@ -76,17 +107,6 @@ def _parse_date_columns(data_frame, parse_dates):
76107 return data_frame
77108
78109
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-
90110def execute (sql , con , cur = None , params = None ):
91111 """
92112 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,
271291 read_sql_query : Read SQL query into a DataFrame.
272292 read_sql
273293
274-
275294 """
295+ if not _is_sqlalchemy_engine (con ):
296+ raise NotImplementedError ("read_sql_table only supported for "
297+ "SQLAlchemy engines." )
276298 import sqlalchemy
277299 from sqlalchemy .schema import MetaData
278300 meta = MetaData (con )
0 commit comments