Files
aitsc/.venv/Lib/site-packages/sqlalchemy/ext/__pycache__/indexable.cpython-312.pyc

265 lines
12 KiB
Plaintext
Raw Normal View History

2025-02-23 09:07:52 +08:00
<EFBFBD>
2025-08-29 00:34:40 +08:00
<00><19>h<EFBFBD>,<00><00>H<00>dZddlmZddlmZddlmZdgZGd<06>de<04>Zy)a7Define attributes on ORM-mapped classes that have "index" attributes for
2025-02-23 09:07:52 +08:00
columns with :class:`_types.Indexable` types.
"index" means the attribute is associated with an element of an
:class:`_types.Indexable` column with the predefined index to access it.
The :class:`_types.Indexable` types include types such as
:class:`_types.ARRAY`, :class:`_types.JSON` and
:class:`_postgresql.HSTORE`.
The :mod:`~sqlalchemy.ext.indexable` extension provides
:class:`_schema.Column`-like interface for any element of an
:class:`_types.Indexable` typed column. In simple cases, it can be
treated as a :class:`_schema.Column` - mapped attribute.
Synopsis
========
Given ``Person`` as a model with a primary key and JSON data field.
While this field may have any number of elements encoded within it,
we would like to refer to the element called ``name`` individually
as a dedicated attribute which behaves like a standalone column::
from sqlalchemy import Column, JSON, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.indexable import index_property
Base = declarative_base()
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
data = Column(JSON)
name = index_property("data", "name")
Above, the ``name`` attribute now behaves like a mapped column. We
can compose a new ``Person`` and set the value of ``name``::
>>> person = Person(name="Alchemist")
The value is now accessible::
>>> person.name
'Alchemist'
Behind the scenes, the JSON field was initialized to a new blank dictionary
and the field was set::
>>> person.data
{'name': 'Alchemist'}
The field is mutable in place::
>>> person.name = "Renamed"
>>> person.name
'Renamed'
>>> person.data
{'name': 'Renamed'}
When using :class:`.index_property`, the change that we make to the indexable
structure is also automatically tracked as history; we no longer need
to use :class:`~.mutable.MutableDict` in order to track this change
for the unit of work.
Deletions work normally as well::
>>> del person.name
>>> person.data
{}
Above, deletion of ``person.name`` deletes the value from the dictionary,
but not the dictionary itself.
A missing key will produce ``AttributeError``::
>>> person = Person()
>>> person.name
AttributeError: 'name'
Unless you set a default value::
>>> class Person(Base):
... __tablename__ = "person"
...
... id = Column(Integer, primary_key=True)
... data = Column(JSON)
...
... name = index_property("data", "name", default=None) # See default
>>> person = Person()
>>> print(person.name)
None
The attributes are also accessible at the class level.
Below, we illustrate ``Person.name`` used to generate
an indexed SQL criteria::
>>> from sqlalchemy.orm import Session
>>> session = Session()
>>> query = session.query(Person).filter(Person.name == "Alchemist")
The above query is equivalent to::
>>> query = session.query(Person).filter(Person.data["name"] == "Alchemist")
Multiple :class:`.index_property` objects can be chained to produce
multiple levels of indexing::
from sqlalchemy import Column, JSON, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.indexable import index_property
Base = declarative_base()
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
data = Column(JSON)
birthday = index_property("data", "birthday")
year = index_property("birthday", "year")
month = index_property("birthday", "month")
day = index_property("birthday", "day")
Above, a query such as::
q = session.query(Person).filter(Person.year == "1980")
On a PostgreSQL backend, the above query will render as:
.. sourcecode:: sql
SELECT person.id, person.data
FROM person
WHERE person.data -> %(data_1)s -> %(param_1)s = %(param_2)s
Default Values
==============
:class:`.index_property` includes special behaviors for when the indexed
data structure does not exist, and a set operation is called:
* For an :class:`.index_property` that is given an integer index value,
the default data structure will be a Python list of ``None`` values,
at least as long as the index value; the value is then set at its
place in the list. This means for an index value of zero, the list
will be initialized to ``[None]`` before setting the given value,
and for an index value of five, the list will be initialized to
``[None, None, None, None, None]`` before setting the fifth element
to the given value. Note that an existing list is **not** extended
in place to receive a value.
* for an :class:`.index_property` that is given any other kind of index
value (e.g. strings usually), a Python dictionary is used as the
default data structure.
* The default data structure can be set to any Python callable using the
:paramref:`.index_property.datatype` parameter, overriding the previous
rules.
Subclassing
===========
:class:`.index_property` can be subclassed, in particular for the common
use case of providing coercion of values or SQL expressions as they are
accessed. Below is a common recipe for use with a PostgreSQL JSON type,
where we want to also include automatic casting plus ``astext()``::
class pg_json_property(index_property):
def __init__(self, attr_name, index, cast_type):
super(pg_json_property, self).__init__(attr_name, index)
self.cast_type = cast_type
def expr(self, model):
expr = super(pg_json_property, self).expr(model)
return expr.astext.cast(self.cast_type)
The above subclass can be used with the PostgreSQL-specific
version of :class:`_postgresql.JSON`::
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql import JSON
Base = declarative_base()
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
data = Column(JSON)
age = pg_json_property("data", "age", Integer)
The ``age`` attribute at the instance level works as before; however
when rendering SQL, PostgreSQL's ``->>`` operator will be used
for indexed access, instead of the usual index operator of ``->``::
>>> query = session.query(Person).filter(Person.age < 20)
The above query will render:
2025-08-29 00:34:40 +08:00
2025-02-23 09:07:52 +08:00
.. sourcecode:: sql
SELECT person.id, person.data
FROM person
WHERE CAST(person.data ->> %(data_1)s AS INTEGER) < %(param_1)s
<EFBFBD>)<01>inspect)<01>hybrid_property)<01> flag_modified<65>index_propertyc<00>Z<00><00>eZdZdZe<04>Zedddf<04>fd<04> Zd
d<05>Zd<06>Zd<07>Z d<08>Z
d <09>Z <0B>xZ S) rz<>A property generator. The generated property describes an object
attribute that corresponds to an :class:`_types.Indexable`
column.
.. seealso::
:mod:`sqlalchemy.ext.indexable`
NTc<00><><00><01><02>|r;t<00>|<00>|j|j|j|j
<00>n&t<00>|<00>|jdd|j
<00>||_<00>|_||_t<00>t<00>}|xr|}|<04>||_ ||_ y|r<12>fd<02>|_ ||_ yt|_ ||_ y)a}Create a new :class:`.index_property`.
:param attr_name:
An attribute name of an `Indexable` typed column, or other
attribute that returns an indexable structure.
:param index:
The index to be used for getting and setting this value. This
should be the Python-side index value for integers.
:param default:
A value which will be returned instead of `AttributeError`
when there is not a value at given index.
:param datatype: default datatype to use when the field is empty.
By default, this is derived from the type of index used; a
Python list for an integer index, or a Python dictionary for
any other style of index. For a list, the list will be
initialized to a list of None values that is at least
``index`` elements long.
:param mutable: if False, writes and deletes to the attribute will
be disallowed.
:param onebased: assume the SQL representation of this value is
one-based; that is, the first index in SQL is 1, not zero.
2025-08-29 00:34:40 +08:00
Nc<00>F<00><01>t<00>dz<00>D<00>cgc]}d<00><02>c}Scc}w<00>N<>)<01>range)<02>x<>indexs <20><>ED:\pythonpj\aitsc\.venv\Lib\site-packages\sqlalchemy/ext/indexable.py<70><lambda>z)index_property.__init__.<locals>.<lambda>&s"<00><><00>u<EFBFBD>U<EFBFBD>Q<EFBFBD>Y<EFBFBD>7G<37>(H<>7G<37>!<21><14>7G<37>(H<><48>(Hs<00> )<0E>super<65>__init__<5F>fget<65>fset<65>fdel<65>expr<70> attr_namer<00>default<6C>
2025-02-23 09:07:52 +08:00
isinstance<EFBFBD>int<6E>datatype<70>dict<63>onebased) <09>selfrrrr<00>mutabler<00>
2025-08-29 00:34:40 +08:00
is_numeric<EFBFBD> __class__s ` <20>rrzindex_property.__init__<5F>s<><00><><00>@ <13> <11>G<EFBFBD> <1C>T<EFBFBD>Y<EFBFBD>Y<EFBFBD><04> <09> <09>4<EFBFBD>9<EFBFBD>9<EFBFBD>d<EFBFBD>i<EFBFBD>i<EFBFBD> H<> <11>G<EFBFBD> <1C>T<EFBFBD>Y<EFBFBD>Y<EFBFBD><04>d<EFBFBD>D<EFBFBD>I<EFBFBD>I<EFBFBD> ><3E>"<22><04><0E><1A><04>
2025-02-23 09:07:52 +08:00
<EFBFBD><1E><04> <0C><1F><05>s<EFBFBD>+<2B>
2025-08-29 00:34:40 +08:00
<EFBFBD><1D>*<2A>(<28><08> <13> <1F>$<24>D<EFBFBD>M<EFBFBD> !<21><04> <0A> <1A> H<><04> <0A>!<21><04> <0A>!%<25><04> <0A> <20><04> <0A>c<00>x<00>|j|jk(rt|j<00>|<01>|jS<00>N)r<00>_NO_DEFAULT_ARGUMENT<4E>AttributeErrorr)r<00>errs r<00> _fget_defaultzindex_property._fget_default+s/<00><00> <0F><<3C><<3C>4<EFBFBD>4<>4<> 4<> <20><14><1E><1E>0<>c<EFBFBD> 9<><17><<3C><<3C> r"c<00><><00>|j}t||<02>}|<03>|j<00>S ||j}|S#tt
2025-02-23 09:07:52 +08:00
f$r}|j|<05>cYd}~Sd}~wwxYwr$)r<00>getattrr(r<00>KeyError<6F>
2025-08-29 00:34:40 +08:00
IndexError)r<00>instancer<00> column_value<75>valuer's rrzindex_property.fget1sn<00><00><18>N<EFBFBD>N<EFBFBD> <09><1E>x<EFBFBD><19>3<> <0C> <17> <1F><17>%<25>%<25>'<27> '<27> <19> <20><14><1A><1A>,<2C>E<EFBFBD><19>L<EFBFBD><4C><19>*<2A>%<25> +<2B><17>%<25>%<25>c<EFBFBD>*<2A> *<2A><> +<2B>s<00>=<00>A'<03> A"<03>A'<03>"A'c<00><00>|j}t||d<00>}|<04>|j<00>}t|||<04>|||j<t|||<04>|t |<01>j jvr t||<03>yyr$) rr*r<00>setattrrr<00>mapper<65>attrsr)rr-r/rr.s rrzindex_property.fset=sx<00><00><18>N<EFBFBD>N<EFBFBD> <09><1E>x<EFBFBD><19>D<EFBFBD>9<> <0C> <17> <1F><1F>=<3D>=<3D>?<3F>L<EFBFBD> <13>H<EFBFBD>i<EFBFBD><1C> 6<>#(<28> <0C>T<EFBFBD>Z<EFBFBD>Z<EFBFBD> <20><0F><08>)<29>\<5C>2<> <14><07><08>)<29>0<>0<>6<>6<> 6<> <19>(<28>I<EFBFBD> .<2E> 7r"c<00><><00>|j}t||<02>}|<03>t|j<00><00> ||j=t |||<03>t ||<02>y#t $r}t|j<00>|<04>d}~wwxYwr$)rr*r&rr1rr+)rr-rr.r's rrzindex_property.fdelHsx<00><00><18>N<EFBFBD>N<EFBFBD> <09><1E>x<EFBFBD><19>3<> <0C> <17> <1F> <20><14><1E><1E>0<> 0<> /<2F><1C>T<EFBFBD>Z<EFBFBD>Z<EFBFBD>(<28> <14>H<EFBFBD>i<EFBFBD><1C> 6<> <19>(<28>I<EFBFBD> .<2E><> <18> :<3A> <20><14><1E><1E>0<>c<EFBFBD> 9<><39> :<3A>s<00> A<00> A<<03>!A7<03>7A<c<00>r<00>t||j<00>}|j}|jr|dz }||Sr
)r*rrr)r<00>model<65>columnrs rrzindex_property.exprUs5<00><00><18><15><04><0E><0E>/<2F><06><14>
2025-02-23 09:07:52 +08:00
<EFBFBD>
<EFBFBD><05> <0F>=<3D>=<3D> <11>Q<EFBFBD>J<EFBFBD>E<EFBFBD><15>e<EFBFBD>}<7D>r"r$) <0A>__name__<5F>
2025-08-29 00:34:40 +08:00
__module__<EFBFBD> __qualname__<5F>__doc__<5F>objectr%rr(rrrr<00> __classcell__)r!s@rrr<00>s?<00><><00><08>"<22>8<EFBFBD><18> %<25><15><14><15>1!<21>f <20>
2025-02-23 09:07:52 +08:00
<19> /<2F> /<2F>r"N) r;<00>r<00>
2025-08-29 00:34:40 +08:00
ext.hybridr<00>orm.attributesr<00>__all__r<00>r"r<00><module>rCs/<00><01>Y<04>t<17>(<28>*<2A> <1C>
2025-02-23 09:07:52 +08:00
<1C><07>o<1D>_<EFBFBD>or"