diff options
author | Gord Thompson <gord@gordthompson.com> | 2019-12-19 19:58:52 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-24 14:05:19 -0400 |
commit | 8e3a05ab987dcb783385e555aa607248df1469ca (patch) | |
tree | 8b34847dcfd0e63cf56ed5e530254da19cb875ef /lib/sqlalchemy/sql/compiler.py | |
parent | e6b6ec78e6d6f96537eaf542f469a7e88134e9fc (diff) | |
download | sqlalchemy-8e3a05ab987dcb783385e555aa607248df1469ca.tar.gz |
Implement SQL VALUES in core.
Added a core :class:`Values` object that enables a VALUES construct
to be used in the FROM clause of an SQL statement for databases that
support it (mainly PostgreSQL and SQL Server).
Fixes: #4868
Closes: #5030
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5030
Pull-request-sha: 84684038a8efa93b460318e0db53f6c644554588
Change-Id: Ib8109b63bc1a9dc04ab987c5322ca3375f7e824d
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 1f183b5c1..bf389d21d 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2281,6 +2281,46 @@ class SQLCompiler(Compiled): return text + def visit_values(self, element, asfrom=False, from_linter=None, **kw): + v = "VALUES %s" % ", ".join( + self.process(elem, literal_binds=element.literal_binds) + for elem in element._data + ) + + if isinstance(element.name, elements._truncated_label): + name = self._truncated_identifier("values", element.name) + else: + name = element.name + + if element._is_lateral: + lateral = "LATERAL " + else: + lateral = "" + + if asfrom: + if from_linter: + from_linter.froms[element] = ( + name if name is not None else "(unnamed VALUES element)" + ) + + if name: + v = "%s(%s)%s (%s)" % ( + lateral, + v, + self.get_render_as_alias_suffix(self.preparer.quote(name)), + ( + ", ".join( + c._compiler_dispatch( + self, include_table=False, **kw + ) + for c in element.columns + ) + ), + ) + else: + v = "%s(%s)" % (lateral, v) + return v + def get_render_as_alias_suffix(self, alias_name_text): return " AS " + alias_name_text |