我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sqlalchemy.type_coerce()。
def _set_table(self, schema, column, table): if self.native_enum: SchemaType._set_table(self, column, table) if not self.create_constraint: return variant_mapping = self._variant_mapping_for_set_table(column) e = schema.CheckConstraint( type_coerce(column, self).in_(self.enums), name=_defer_name(self.name), _create_rule=util.portable_instancemethod( self._should_create_constraint, {"variant_mapping": variant_mapping}), _type_bound=True ) assert e.table is table
def _set_table(self, schema, column, table): if not self.create_constraint: return variant_mapping = self._variant_mapping_for_set_table(column) e = schema.CheckConstraint( type_coerce(column, self).in_([0, 1]), name=_defer_name(self.name), _create_rule=util.portable_instancemethod( self._should_create_constraint, {"variant_mapping": variant_mapping}), _type_bound=True ) assert e.table is table
def __getitem__(self, index): super_ = super(ARRAY_D.Comparator, self).__getitem__(index) if not isinstance(index, slice) and self.type.dimensions > 1: super_ = type_coerce( super_, ARRAY_D( self.type.item_type, dimensions=self.type.dimensions - 1, zero_indexes=self.type.zero_indexes) ) return super_
def _set_table(self, schema, column, table): if self.native_enum: SchemaType._set_table(self, column, table) if not self.create_constraint: return e = schema.CheckConstraint( type_coerce(column, self).in_(self.enums), name=_defer_name(self.name), _create_rule=util.portable_instancemethod( self._should_create_constraint), _type_bound=True ) assert e.table is table
def _set_table(self, schema, column, table): if not self.create_constraint: return e = schema.CheckConstraint( type_coerce(column, self).in_([0, 1]), name=_defer_name(self.name), _create_rule=util.portable_instancemethod( self._should_create_constraint), _type_bound=True ) assert e.table is table
def type_coerce(expression, type_): """Associate a SQL expression with a particular type, without rendering ``CAST``. E.g.:: from sqlalchemy import type_coerce stmt = select([type_coerce(log_table.date_string, StringDateTime())]) The above construct will produce SQL that is usually otherwise unaffected by the :func:`.type_coerce` call:: SELECT date_string FROM log However, when result rows are fetched, the ``StringDateTime`` type will be applied to result rows on behalf of the ``date_string`` column. A type that features bound-value handling will also have that behavior take effect when literal values or :func:`.bindparam` constructs are passed to :func:`.type_coerce` as targets. For example, if a type implements the :meth:`.TypeEngine.bind_expression` method or :meth:`.TypeEngine.bind_processor` method or equivalent, these functions will take effect at statement compilation/execution time when a literal value is passed, as in:: # bound-value handling of MyStringType will be applied to the # literal value "some string" stmt = select([type_coerce("some string", MyStringType)]) :func:`.type_coerce` is similar to the :func:`.cast` function, except that it does not render the ``CAST`` expression in the resulting statement. :param expression: A SQL expression, such as a :class:`.ColumnElement` expression or a Python string which will be coerced into a bound literal value. :param type_: A :class:`.TypeEngine` class or instance indicating the type to which the expression is coerced. .. seealso:: :func:`.cast` """ type_ = type_api.to_instance(type_) if hasattr(expression, '__clause_element__'): return type_coerce(expression.__clause_element__(), type_) elif isinstance(expression, BindParameter): bp = expression._clone() bp.type = type_ return bp elif not isinstance(expression, Visitable): if expression is None: return Null() else: return literal(expression, type_=type_) else: return Label(None, expression, type_=type_)
def __init__(self, expression, type_): """Produce a ``CAST`` expression. :func:`.cast` returns an instance of :class:`.Cast`. E.g.:: from sqlalchemy import cast, Numeric stmt = select([ cast(product_table.c.unit_price, Numeric(10, 4)) ]) The above statement will produce SQL resembling:: SELECT CAST(unit_price AS NUMERIC(10, 4)) FROM product The :func:`.cast` function performs two distinct functions when used. The first is that it renders the ``CAST`` expression within the resulting SQL string. The second is that it associates the given type (e.g. :class:`.TypeEngine` class or instance) with the column expression on the Python side, which means the expression will take on the expression operator behavior associated with that type, as well as the bound-value handling and result-row-handling behavior of the type. .. versionchanged:: 0.9.0 :func:`.cast` now applies the given type to the expression such that it takes effect on the bound-value, e.g. the Python-to-database direction, in addition to the result handling, e.g. database-to-Python, direction. An alternative to :func:`.cast` is the :func:`.type_coerce` function. This function performs the second task of associating an expression with a specific type, but does not render the ``CAST`` expression in SQL. :param expression: A SQL expression, such as a :class:`.ColumnElement` expression or a Python string which will be coerced into a bound literal value. :param type_: A :class:`.TypeEngine` class or instance indicating the type to which the ``CAST`` should apply. .. seealso:: :func:`.type_coerce` - Python-side type coercion without emitting CAST. """ self.type = type_api.to_instance(type_) self.clause = _literal_as_binds(expression, type_=self.type) self.typeclause = TypeClause(self.type)