summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/postgres.py
diff options
context:
space:
mode:
Diffstat (limited to 'sqlglot/dialects/postgres.py')
-rw-r--r--sqlglot/dialects/postgres.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py
index c709665..7612330 100644
--- a/sqlglot/dialects/postgres.py
+++ b/sqlglot/dialects/postgres.py
@@ -58,17 +58,17 @@ def _date_diff_sql(self, expression):
age = f"AGE({end}, {start})"
if unit == "WEEK":
- extract = f"EXTRACT(year FROM {age}) * 48 + EXTRACT(month FROM {age}) * 4 + EXTRACT(day FROM {age}) / 7"
+ unit = f"EXTRACT(year FROM {age}) * 48 + EXTRACT(month FROM {age}) * 4 + EXTRACT(day FROM {age}) / 7"
elif unit == "MONTH":
- extract = f"EXTRACT(year FROM {age}) * 12 + EXTRACT(month FROM {age})"
+ unit = f"EXTRACT(year FROM {age}) * 12 + EXTRACT(month FROM {age})"
elif unit == "QUARTER":
- extract = f"EXTRACT(year FROM {age}) * 4 + EXTRACT(month FROM {age}) / 3"
+ unit = f"EXTRACT(year FROM {age}) * 4 + EXTRACT(month FROM {age}) / 3"
elif unit == "YEAR":
- extract = f"EXTRACT(year FROM {age})"
+ unit = f"EXTRACT(year FROM {age})"
else:
- self.unsupported(f"Unsupported DATEDIFF unit {unit}")
+ unit = age
- return f"CAST({extract} AS BIGINT)"
+ return f"CAST({unit} AS BIGINT)"
def _substring_sql(self, expression):
@@ -206,6 +206,8 @@ class Postgres(Dialect):
}
class Tokenizer(tokens.Tokenizer):
+ QUOTES = ["'", "$$"]
+
BIT_STRINGS = [("b'", "'"), ("B'", "'")]
HEX_STRINGS = [("x'", "'"), ("X'", "'")]
BYTE_STRINGS = [("e'", "'"), ("E'", "'")]
@@ -236,7 +238,7 @@ class Postgres(Dialect):
"UUID": TokenType.UUID,
"CSTRING": TokenType.PSEUDO_TYPE,
}
- QUOTES = ["'", "$$"]
+
SINGLE_TOKENS = {
**tokens.Tokenizer.SINGLE_TOKENS,
"$": TokenType.PARAMETER,
@@ -265,6 +267,7 @@ class Postgres(Dialect):
class Generator(generator.Generator):
LOCKING_READS_SUPPORTED = True
+ PARAMETER_TOKEN = "$"
TYPE_MAPPING = {
**generator.Generator.TYPE_MAPPING, # type: ignore