Skip to content

Commit 165b604

Browse files
SQL: put old tests back + some fixes to get them running
- removed meta keyword from has_table (was not used) - added check for if_exists - added table_exists back as alias for has_table - added frame_query back as alias for read_sql (and deprecated it) - changed way to delegate read_sql/table without using has_table for backwards compatibility (because this gave an error when uqing a mysql connection but not specifying the flavor) Things that I had to change to the tests to get them running: - changed MySQLdb to pymysql - changed `sqlite3.OperationalError` to `pd.io.sql.DatabaseError` (in `test_tquery` and `test_uquery`) - test_write_row_by_row are skipped for now (they depend on the issue of `execute` being able to handle cursor objects). - in `test_execute` with pymysql converted the row values to python types (with numpy's `tolist()`), as pymysql errored on the numpy types.
1 parent 2b4cd9f commit 165b604

File tree

2 files changed

+639
-12
lines changed

2 files changed

+639
-12
lines changed

pandas/io/sql.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def tquery(sql, con=None, cur=None, retry=True):
125125
If only one column selected, then plain list is returned.
126126
127127
To obtain the same result in the future, you can use the following:
128-
128+
129129
>>> execute(sql, con, params).fetchall()
130130
131131
Parameters
@@ -144,7 +144,7 @@ def tquery(sql, con=None, cur=None, retry=True):
144144
"tquery is depreciated, and will be removed in future versions. "
145145
"You can use ``execute(...).fetchall()`` instead.",
146146
FutureWarning)
147-
147+
148148
cur = execute(sql, con, cur=cur)
149149
result = _safe_fetch(cur)
150150

@@ -178,7 +178,7 @@ def uquery(sql, con=None, cur=None, retry=True, params=None):
178178
returns the number of rows affected. Good for update queries.
179179
180180
To obtain the same result in the future, you can use the following:
181-
181+
182182
>>> execute(sql, con).rowcount
183183
184184
Parameters
@@ -382,18 +382,26 @@ def read_sql(sql, con, index_col=None, flavor='sqlite', coerce_float=True,
382382
"""
383383
pandas_sql = pandasSQL_builder(con, flavor=flavor)
384384

385-
if pandas_sql.has_table(sql):
385+
if 'select' in sql.lower():
386+
try:
387+
if pandas_sql.has_table(sql):
388+
return pandas_sql.read_table(
389+
sql, index_col=index_col, coerce_float=coerce_float,
390+
parse_dates=parse_dates, columns=columns)
391+
except:
392+
pass
393+
394+
return pandas_sql.read_sql(
395+
sql, index_col=index_col, params=params,
396+
coerce_float=coerce_float, parse_dates=parse_dates)
397+
else:
386398
if isinstance(pandas_sql, PandasSQLLegacy):
387399
raise ValueError("Reading a table with read_sql is not supported "
388400
"for a DBAPI2 connection. Use an SQLAlchemy "
389401
"engine or specify an sql query")
390402
return pandas_sql.read_table(
391403
sql, index_col=index_col, coerce_float=coerce_float,
392404
parse_dates=parse_dates, columns=columns)
393-
else:
394-
return pandas_sql.read_sql(
395-
sql, index_col=index_col, params=params, coerce_float=coerce_float,
396-
parse_dates=parse_dates)
397405

398406

399407
def to_sql(frame, name, con, flavor='sqlite', if_exists='fail', index=True,
@@ -425,6 +433,9 @@ def to_sql(frame, name, con, flavor='sqlite', if_exists='fail', index=True,
425433
A sequence should be given if the DataFrame uses MultiIndex.
426434
427435
"""
436+
if if_exists not in ('fail', 'replace', 'append'):
437+
raise ValueError("'{0}' is not valid for if_exists".format(if_exists))
438+
428439
pandas_sql = pandasSQL_builder(con, flavor=flavor)
429440

430441
if isinstance(frame, Series):
@@ -436,7 +447,7 @@ def to_sql(frame, name, con, flavor='sqlite', if_exists='fail', index=True,
436447
index_label=index_label)
437448

438449

439-
def has_table(table_name, con, meta=None, flavor='sqlite'):
450+
def has_table(table_name, con, flavor='sqlite'):
440451
"""
441452
Check if DataBase has named table.
442453
@@ -459,6 +470,8 @@ def has_table(table_name, con, meta=None, flavor='sqlite'):
459470
pandas_sql = pandasSQL_builder(con, flavor=flavor)
460471
return pandas_sql.has_table(table_name)
461472

473+
table_exists = has_table
474+
462475

463476
def pandasSQL_builder(con, flavor=None, meta=None):
464477
"""
@@ -518,6 +531,9 @@ def __init__(self, name, pandas_sql_engine, frame=None, index=True,
518531
self.table = self.pd_sql.get_table(self.name)
519532
if self.table is None:
520533
self.table = self._create_table_statement()
534+
else:
535+
raise ValueError(
536+
"'{0}' is not valid for if_exists".format(if_exists))
521537
else:
522538
self.table = self._create_table_statement()
523539
self.create()
@@ -1133,6 +1149,13 @@ def read_frame(*args, **kwargs):
11331149
return read_sql(*args, **kwargs)
11341150

11351151

1152+
def frame_query(*args, **kwargs):
1153+
"""DEPRECIATED - use read_sql
1154+
"""
1155+
warnings.warn("frame_query is depreciated, use read_sql", FutureWarning)
1156+
return read_sql(*args, **kwargs)
1157+
1158+
11361159
def write_frame(frame, name, con, flavor='sqlite', if_exists='fail', **kwargs):
11371160
"""DEPRECIATED - use to_sql
11381161
@@ -1177,3 +1200,4 @@ def write_frame(frame, name, con, flavor='sqlite', if_exists='fail', **kwargs):
11771200

11781201
# Append wrapped function docstrings
11791202
read_frame.__doc__ += read_sql.__doc__
1203+
frame_query.__doc__ += read_sql.__doc__

0 commit comments

Comments
 (0)