Skip to content
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

ENH: Added schema kwarg to get_schema method #33278

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ Other enhancements
- :meth:`~pandas.io.gbq.read_gbq` now allows to disable progress bar (:issue:`33360`).
- :meth:`~pandas.io.gbq.read_gbq` now supports the ``max_results`` kwarg from ``pandas-gbq`` (:issue:`34639`).
- :meth:`DataFrame.to_html` and :meth:`DataFrame.to_string`'s ``col_space`` parameter now accepts a list of dict to change only some specific columns' width (:issue:`28917`).
- :meth:`get_schema` will now allow a schema kwarg that will add a schema into the create table statement (:issue:`28486`)
-
gregorylivschitz marked this conversation as resolved.
Show resolved Hide resolved

.. ---------------------------------------------------------------------------

Expand Down
32 changes: 25 additions & 7 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,15 @@ def drop_table(self, table_name, schema=None):
self.get_table(table_name, schema).drop()
self.meta.clear()

def _create_sql_schema(self, frame, table_name, keys=None, dtype=None):
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, schema=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here, could you add type annotations to these arguments? Not all old code has them, but they're encouraged for new developments. Feel free to ping if it's not something you're familiar with

table = SQLTable(
table_name, self, frame=frame, index=False, keys=keys, dtype=dtype
table_name,
self,
frame=frame,
index=False,
keys=keys,
dtype=dtype,
schema=schema,
)
return str(table.sql_schema())

Expand Down Expand Up @@ -1574,9 +1580,13 @@ def _create_table_setup(self):
create_tbl_stmts.append(
f"CONSTRAINT {self.name}_pk PRIMARY KEY ({cnames_br})"
)

if self.schema:
schema_to_add = self.schema + "."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Granted I'm no SQL expert, but perhaps schema_name would be a better variable name?

else:
schema_to_add = ""
create_stmts = [
"CREATE TABLE "
+ schema_to_add
+ escape(self.name)
+ " (\n"
+ ",\n ".join(create_tbl_stmts)
Expand Down Expand Up @@ -1831,14 +1841,20 @@ def drop_table(self, name, schema=None):
drop_sql = f"DROP TABLE {_get_valid_sqlite_name(name)}"
self.execute(drop_sql)

def _create_sql_schema(self, frame, table_name, keys=None, dtype=None):
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, schema=None):
table = SQLiteTable(
table_name, self, frame=frame, index=False, keys=keys, dtype=dtype
table_name,
self,
frame=frame,
index=False,
keys=keys,
dtype=dtype,
schema=schema,
)
return str(table.sql_schema())


def get_schema(frame, name, keys=None, con=None, dtype=None):
def get_schema(frame, name, keys=None, con=None, dtype=None, schema=None):
"""
Get the SQL db table schema for the given frame.

Expand All @@ -1859,4 +1875,6 @@ def get_schema(frame, name, keys=None, con=None, dtype=None):

"""
pandas_sql = pandasSQL_builder(con=con)
return pandas_sql._create_sql_schema(frame, name, keys=keys, dtype=dtype)
return pandas_sql._create_sql_schema(
frame, name, keys=keys, dtype=dtype, schema=schema
)
6 changes: 6 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,12 @@ def test_get_schema(self):
create_sql = sql.get_schema(self.test_frame1, "test", con=self.conn)
assert "CREATE" in create_sql

def test_get_schema_with_schema(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the github issue number here, e.g. # GH8624 (but with the correct number)

create_sql = sql.get_schema(
self.test_frame1, "test", con=self.conn, schema="pypi"
)
assert "CREATE TABLE pypi." in create_sql

def test_get_schema_dtypes(self):
float_frame = DataFrame({"a": [1.1, 1.2], "b": [2.1, 2.2]})
dtype = sqlalchemy.Integer if self.mode == "sqlalchemy" else "INTEGER"
Expand Down