Skip to content

CLN: remove deprecated sqlite cursor #42507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _wrap_result(
return frame


def execute(sql, con, cur=None, params=None):
def execute(sql, con, params=None):
"""
Execute the given SQL query using the provided connection object.

Expand All @@ -194,18 +194,14 @@ def execute(sql, con, cur=None, params=None):
Using SQLAlchemy makes it possible to use any DB supported by the
library.
If a DBAPI2 object, only sqlite3 is supported.
cur : deprecated, cursor is obtained from connection, default: None
params : list or tuple, optional, default: None
List of parameters to pass to execute method.

Returns
-------
Results Iterable
"""
if cur is None:
pandas_sql = pandasSQL_builder(con)
else:
pandas_sql = pandasSQL_builder(cur, is_cursor=True)
pandas_sql = pandasSQL_builder(con)
args = _convert_params(sql, params)
return pandas_sql.execute(*args)

Expand Down Expand Up @@ -774,22 +770,18 @@ def _engine_builder(con):
return con


def pandasSQL_builder(
con, schema: str | None = None, meta=None, is_cursor: bool = False
):
def pandasSQL_builder(con, schema: str | None = None, meta=None):
"""
Convenience function to return the correct PandasSQL subclass based on the
provided parameters.
"""
# When support for DBAPI connections is removed,
# is_cursor should not be necessary.
con = _engine_builder(con)
if _is_sqlalchemy_connectable(con):
return SQLDatabase(con, schema=schema, meta=meta)
elif isinstance(con, str):
raise ImportError("Using URI string without sqlalchemy installed.")
else:
return SQLiteDatabase(con, is_cursor=is_cursor)
return SQLiteDatabase(con)


class SQLTable(PandasObject):
Expand Down Expand Up @@ -2031,8 +2023,7 @@ class SQLiteDatabase(PandasSQL):

"""

def __init__(self, con, is_cursor: bool = False):
self.is_cursor = is_cursor
def __init__(self, con):
self.con = con

@contextmanager
Expand All @@ -2048,10 +2039,7 @@ def run_transaction(self):
cur.close()

def execute(self, *args, **kwargs):
if self.is_cursor:
cur = self.con
else:
cur = self.con.cursor()
cur = self.con.cursor()
try:
cur.execute(*args, **kwargs)
return cur
Expand Down
14 changes: 6 additions & 8 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2615,9 +2615,9 @@ def format_query(sql, *args):
return sql % tuple(processed_args)


def tquery(query, con=None, cur=None):
def tquery(query, con=None):
"""Replace removed sql.tquery function"""
res = sql.execute(query, con=con, cur=cur).fetchall()
res = sql.execute(query, con=con).fetchall()
if res is None:
return None
else:
Expand Down Expand Up @@ -2649,12 +2649,10 @@ def test_write_row_by_row(self):
cur = self.conn.cursor()
cur.execute(create_sql)

cur = self.conn.cursor()

ins = "INSERT INTO test VALUES (%s, %s, %s, %s)"
for idx, row in frame.iterrows():
for _, row in frame.iterrows():
fmt_sql = format_query(ins, *row)
tquery(fmt_sql, cur=cur)
tquery(fmt_sql, con=self.conn)

self.conn.commit()

Expand Down Expand Up @@ -2912,9 +2910,9 @@ def test_write_row_by_row(self):
cur.execute(drop_sql)
cur.execute(create_sql)
ins = "INSERT INTO test VALUES (%s, %s, %s, %s)"
for idx, row in frame.iterrows():
for _, row in frame.iterrows():
fmt_sql = format_query(ins, *row)
tquery(fmt_sql, cur=cur)
tquery(fmt_sql, con=self.conn)

self.conn.commit()

Expand Down