@@ -76,6 +76,17 @@ def _parse_date_columns(data_frame, parse_dates):
76
76
return data_frame
77
77
78
78
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
+
79
90
def execute (sql , con , cur = None , params = None ):
80
91
"""
81
92
Execute the given SQL query using the provided connection object.
@@ -262,7 +273,15 @@ def read_sql_table(table_name, con, index_col=None, coerce_float=True,
262
273
263
274
264
275
"""
265
- pandas_sql = PandasSQLAlchemy (con )
276
+ import sqlalchemy
277
+ from sqlalchemy .schema import MetaData
278
+ meta = MetaData (con )
279
+ try :
280
+ meta .reflect (only = [table_name ])
281
+ except sqlalchemy .exc .InvalidRequestError :
282
+ raise ValueError ("Table %s not found" % table_name )
283
+
284
+ pandas_sql = PandasSQLAlchemy (con , meta = meta )
266
285
table = pandas_sql .read_table (
267
286
table_name , index_col = index_col , coerce_float = coerce_float ,
268
287
parse_dates = parse_dates , columns = columns )
@@ -380,6 +399,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
380
399
coerce_float = coerce_float , parse_dates = parse_dates )
381
400
382
401
if pandas_sql .has_table (sql ):
402
+ pandas_sql .meta .reflect (only = [sql ])
383
403
return pandas_sql .read_table (
384
404
sql , index_col = index_col , coerce_float = coerce_float ,
385
405
parse_dates = parse_dates , columns = columns )
@@ -471,17 +491,9 @@ def pandasSQL_builder(con, flavor=None, meta=None, is_cursor=False):
471
491
"""
472
492
# When support for DBAPI connections is removed,
473
493
# is_cursor should not be necessary.
474
- try :
475
- import sqlalchemy
476
-
477
- if isinstance (con , sqlalchemy .engine .Engine ):
478
- return PandasSQLAlchemy (con , meta = meta )
479
- else :
480
- if flavor == 'mysql' :
481
- warnings .warn (_MYSQL_WARNING , FutureWarning )
482
- return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
483
-
484
- except ImportError :
494
+ if _is_sqlalchemy_engine (con ):
495
+ return PandasSQLAlchemy (con , meta = meta )
496
+ else :
485
497
if flavor == 'mysql' :
486
498
warnings .warn (_MYSQL_WARNING , FutureWarning )
487
499
return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
@@ -768,7 +780,6 @@ def __init__(self, engine, meta=None):
768
780
if not meta :
769
781
from sqlalchemy .schema import MetaData
770
782
meta = MetaData (self .engine )
771
- meta .reflect (self .engine )
772
783
773
784
self .meta = meta
774
785
@@ -813,19 +824,16 @@ def tables(self):
813
824
return self .meta .tables
814
825
815
826
def has_table (self , name ):
816
- if self .meta .tables .get (name ) is not None :
817
- return True
818
- else :
819
- return False
827
+ return self .engine .has_table (name )
820
828
821
829
def get_table (self , table_name ):
822
830
return self .meta .tables .get (table_name )
823
831
824
832
def drop_table (self , table_name ):
825
833
if self .engine .has_table (table_name ):
834
+ self .meta .reflect (only = [table_name ])
826
835
self .get_table (table_name ).drop ()
827
836
self .meta .clear ()
828
- self .meta .reflect ()
829
837
830
838
def _create_sql_schema (self , frame , table_name ):
831
839
table = PandasSQLTable (table_name , self , frame = frame )
0 commit comments