summaryrefslogtreecommitdiffstats
path: root/sqlglot/generator.py
diff options
context:
space:
mode:
Diffstat (limited to 'sqlglot/generator.py')
-rw-r--r--sqlglot/generator.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
index bb7fd71..6decd16 100644
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -47,6 +47,8 @@ class Generator:
The default is on the smaller end because the length only represents a segment and not the true
line length.
Default: 80
+ annotations: Whether or not to show annotations in the SQL.
+ Default: True
"""
TRANSFORMS = {
@@ -116,6 +118,7 @@ class Generator:
"_escaped_quote_end",
"_leading_comma",
"_max_text_width",
+ "_annotations",
)
def __init__(
@@ -141,6 +144,7 @@ class Generator:
max_unsupported=3,
leading_comma=False,
max_text_width=80,
+ annotations=True,
):
import sqlglot
@@ -169,6 +173,7 @@ class Generator:
self._escaped_quote_end = self.escape + self.quote_end
self._leading_comma = leading_comma
self._max_text_width = max_text_width
+ self._annotations = annotations
def generate(self, expression):
"""
@@ -275,7 +280,9 @@ class Generator:
raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
def annotation_sql(self, expression):
- return f"{self.sql(expression, 'expression')} # {expression.name.strip()}"
+ if self._annotations:
+ return f"{self.sql(expression, 'expression')} # {expression.name}"
+ return self.sql(expression, "expression")
def uncache_sql(self, expression):
table = self.sql(expression, "this")
@@ -423,8 +430,11 @@ class Generator:
def delete_sql(self, expression):
this = self.sql(expression, "this")
+ using_sql = (
+ f" USING {self.expressions(expression, 'using', sep=', USING ')}" if expression.args.get("using") else ""
+ )
where_sql = self.sql(expression, "where")
- sql = f"DELETE FROM {this}{where_sql}"
+ sql = f"DELETE FROM {this}{using_sql}{where_sql}"
return self.prepend_ctes(expression, sql)
def drop_sql(self, expression):
@@ -571,7 +581,7 @@ class Generator:
null = f" NULL DEFINED AS {null}" if null else ""
return f"ROW FORMAT DELIMITED{fields}{escaped}{items}{keys}{lines}{null}"
- def table_sql(self, expression):
+ def table_sql(self, expression, sep=" AS "):
table = ".".join(
part
for part in [
@@ -582,13 +592,20 @@ class Generator:
if part
)
+ alias = self.sql(expression, "alias")
+ alias = f"{sep}{alias}" if alias else ""
laterals = self.expressions(expression, key="laterals", sep="")
joins = self.expressions(expression, key="joins", sep="")
pivots = self.expressions(expression, key="pivots", sep="")
- return f"{table}{laterals}{joins}{pivots}"
+
+ if alias and pivots:
+ pivots = f"{pivots}{alias}"
+ alias = ""
+
+ return f"{table}{alias}{laterals}{joins}{pivots}"
def tablesample_sql(self, expression):
- if self.alias_post_tablesample and isinstance(expression.this, exp.Alias):
+ if self.alias_post_tablesample and expression.this.alias:
this = self.sql(expression.this, "this")
alias = f" AS {self.sql(expression.this, 'alias')}"
else:
@@ -1188,7 +1205,7 @@ class Generator:
if isinstance(arg_value, list):
for value in arg_value:
args.append(value)
- elif arg_value:
+ else:
args.append(arg_value)
return f"{self.normalize_func(expression.sql_name())}({self.format_args(*args)})"