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 all 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: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ For example:
Other enhancements
^^^^^^^^^^^^^^^^^^
- :class:`Index` with object dtype supports division and multiplication (:issue:`34160`)
-
- :meth:`get_schema` will now allow a schema kwarg that will add a schema into the create table statement (:issue:`28486`)
-

.. _whatsnew_120.api_breaking.python:
Expand Down
41 changes: 33 additions & 8 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import date, datetime, time
from functools import partial
import re
from typing import Iterator, Optional, Union, overload
from typing import Iterator, List, Optional, Union, overload
import warnings

import numpy as np
Expand Down Expand Up @@ -1455,9 +1455,22 @@ 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: DataFrame,
table_name: str,
keys: Optional[List[str]] = None,
dtype : Optional[dict] = None,
schema: Optional[str] = None
):
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 @@ -1588,9 +1601,13 @@ def _create_table_setup(self):
create_tbl_stmts.append(
f"CONSTRAINT {self.name}_pk PRIMARY KEY ({cnames_br})"
)

if self.schema:
schema_name = self.schema + "."
else:
schema_name = ""
create_stmts = [
"CREATE TABLE "
+ schema_name
+ escape(self.name)
+ " (\n"
+ ",\n ".join(create_tbl_stmts)
Expand Down Expand Up @@ -1845,14 +1862,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 @@ -1873,4 +1896,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
)
7 changes: 7 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,13 @@ def test_get_schema(self):
create_sql = sql.get_schema(self.test_frame1, "test", con=self.conn)
assert "CREATE" in create_sql

# GH28486
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