summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/ext.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-04-29 23:26:36 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-05-18 17:46:10 -0400
commitf07e050c9ce4afdeb9c0c136dbcc547f7e5ac7b8 (patch)
tree1b3cd7409ae2eddef635960126551d74f469acc1 /lib/sqlalchemy/dialects/postgresql/ext.py
parent614dfb5f5b5a2427d5d6ce0bc5f34bf0581bf698 (diff)
downloadsqlalchemy-f07e050c9ce4afdeb9c0c136dbcc547f7e5ac7b8.tar.gz
Implement new ClauseElement role and coercion system
A major refactoring of all the functions handle all detection of Core argument types as well as perform coercions into a new class hierarchy based on "roles", each of which identify a syntactical location within a SQL statement. In contrast to the ClauseElement hierarchy that identifies "what" each object is syntactically, the SQLRole hierarchy identifies the "where does it go" of each object syntactically. From this we define a consistent type checking and coercion system that establishes well defined behviors. This is a breakout of the patch that is reorganizing select() constructs to no longer be in the FromClause hierarchy. Also includes a rename of as_scalar() into scalar_subquery(); deprecates automatic coercion to scalar_subquery(). Partially-fixes: #4617 Change-Id: I26f1e78898693c6b99ef7ea2f4e7dfd0e8e1a1bd
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/ext.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/ext.py34
1 files changed, 26 insertions, 8 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/ext.py b/lib/sqlalchemy/dialects/postgresql/ext.py
index 426028239..f9cbc945a 100644
--- a/lib/sqlalchemy/dialects/postgresql/ext.py
+++ b/lib/sqlalchemy/dialects/postgresql/ext.py
@@ -6,9 +6,12 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php
from .array import ARRAY
+from ... import util
+from ...sql import coercions
from ...sql import elements
from ...sql import expression
from ...sql import functions
+from ...sql import roles
from ...sql.schema import ColumnCollectionConstraint
@@ -50,16 +53,18 @@ class aggregate_order_by(expression.ColumnElement):
__visit_name__ = "aggregate_order_by"
def __init__(self, target, *order_by):
- self.target = elements._literal_as_binds(target)
+ self.target = coercions.expect(roles.ExpressionElementRole, target)
_lob = len(order_by)
if _lob == 0:
raise TypeError("at least one ORDER BY element is required")
elif _lob == 1:
- self.order_by = elements._literal_as_binds(order_by[0])
+ self.order_by = coercions.expect(
+ roles.ExpressionElementRole, order_by[0]
+ )
else:
self.order_by = elements.ClauseList(
- *order_by, _literal_as_text=elements._literal_as_binds
+ *order_by, _literal_as_text_role=roles.ExpressionElementRole
)
def self_group(self, against=None):
@@ -166,7 +171,10 @@ class ExcludeConstraint(ColumnCollectionConstraint):
expressions, operators = zip(*elements)
for (expr, column, strname, add_element), operator in zip(
- self._extract_col_expression_collection(expressions), operators
+ coercions.expect_col_expression_collection(
+ roles.DDLConstraintColumnRole, expressions
+ ),
+ operators,
):
if add_element is not None:
columns.append(add_element)
@@ -177,8 +185,6 @@ class ExcludeConstraint(ColumnCollectionConstraint):
# backwards compat
self.operators[name] = operator
- expr = expression._literal_as_column(expr)
-
render_exprs.append((expr, name, operator))
self._render_exprs = render_exprs
@@ -193,9 +199,21 @@ class ExcludeConstraint(ColumnCollectionConstraint):
self.using = kw.get("using", "gist")
where = kw.get("where")
if where is not None:
- self.where = expression._literal_as_text(
- where, allow_coercion_to_text=True
+ self.where = coercions.expect(roles.StatementOptionRole, where)
+
+ def _set_parent(self, table):
+ super(ExcludeConstraint, self)._set_parent(table)
+
+ self._render_exprs = [
+ (
+ expr if isinstance(expr, elements.ClauseElement) else colexpr,
+ name,
+ operator,
+ )
+ for (expr, name, operator), colexpr in util.zip_longest(
+ self._render_exprs, self.columns
)
+ ]
def copy(self, **kw):
elements = [(col, self.operators[col]) for col in self.columns.keys()]