Skip to content

visit_create_table method added to dialect #3

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 1 commit into from
Jul 19, 2023
Merged
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
54 changes: 54 additions & 0 deletions src/databricks/sqlalchemy/dialect/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from sqlalchemy import util, exc
from sqlalchemy.sql import compiler, sqltypes, ColumnElement


Expand Down Expand Up @@ -64,3 +65,56 @@ def _format_table_from_column(self, column_object, use_schema=False):
name = schema_table_column.split(".")[0] + '.' + name

return name

def visit_create_table(self, create, **kw):
table = create.element
preparer = self.preparer

text = "\nCREATE "
if table._prefixes:
text += " ".join(table._prefixes) + " "

# Default to 'IF NOT EXISTS'
text += "TABLE IF NOT EXISTS "

text += preparer.format_table(table) + " "

create_table_suffix = self.create_table_suffix(table)
if create_table_suffix:
text += create_table_suffix + " "

text += "("

separator = "\n"

# if only one primary key, specify it along with the column
first_pk = False
for create_column in create.columns:
column = create_column.element
try:
processed = self.process(
create_column, first_pk=column.primary_key and not first_pk
)
if processed is not None:
text += separator
separator = ", \n"
text += "\t" + processed
if column.primary_key:
first_pk = True
except exc.CompileError as ce:
util.raise_(
exc.CompileError(
util.u(f"(in table '{table.description}', column '{column.name}'): {ce.args[0]}")
),
from_=ce,
)

const = self.create_table_constraints(
table,
_include_foreign_key_constraints=create.include_foreign_key_constraints, # noqa
)
if const:
text += separator + "\t" + const

text += f"\n){self.post_create_table(table)}\n\n"
return text