zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
U
¸ý°dƒ1ã@s–ddlZddlmZddlmZddlmZddlm    Z    dZ
e    j e    j Z e    jd    e d
d
d Ze    jd e d
d
d Ze    jd e d
d
d Ze    jde d
d
d Ze    jde d
d
d Ze    jde d
d
d ZGdd„dejejejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd „d ejƒZ Gd!d"„d"ejƒZ!e "d#ej#¡Z$e "d$ej#¡Z%d%d&„Z&d'd(„Z'd)d*„Z(dS)+éNé)ÚARRAYé)Útypes)Ú    functions)Ú    operators)ÚHSTOREÚhstorez->T)Ú
precedenceZnatural_self_precedentZeager_groupingú?z?&z?|z@>z<@c@sXeZdZdZdZdZe ¡Zd dd„Z    Gdd„dej
j ej j ƒZ e Z dd    „Zd
d „ZdS) ra%    Represent the PostgreSQL HSTORE type.
 
    The :class:`.HSTORE` type stores dictionaries containing strings, e.g.::
 
        data_table = Table('data_table', metadata,
            Column('id', Integer, primary_key=True),
            Column('data', HSTORE)
        )
 
        with engine.connect() as conn:
            conn.execute(
                data_table.insert(),
                data = {"key1": "value1", "key2": "value2"}
            )
 
    :class:`.HSTORE` provides for a wide range of operations, including:
 
    * Index operations::
 
        data_table.c.data['some key'] == 'some value'
 
    * Containment operations::
 
        data_table.c.data.has_key('some key')
 
        data_table.c.data.has_all(['one', 'two', 'three'])
 
    * Concatenation::
 
        data_table.c.data + {"k1": "v1"}
 
    For a full list of special methods see
    :class:`.HSTORE.comparator_factory`.
 
    .. container:: topic
 
        **Detecting Changes in HSTORE columns when using the ORM**
 
        For usage with the SQLAlchemy ORM, it may be desirable to combine the
        usage of :class:`.HSTORE` with :class:`.MutableDict` dictionary now
        part of the :mod:`sqlalchemy.ext.mutable` extension. This extension
        will allow "in-place" changes to the dictionary, e.g. addition of new
        keys or replacement/removal of existing keys to/from the current
        dictionary, to produce events which will be detected by the unit of
        work::
 
            from sqlalchemy.ext.mutable import MutableDict
 
            class MyClass(Base):
                __tablename__ = 'data_table'
 
                id = Column(Integer, primary_key=True)
                data = Column(MutableDict.as_mutable(HSTORE))
 
            my_object = session.query(MyClass).one()
 
            # in-place mutation, requires Mutable extension
            # in order for the ORM to detect
            my_object.data['some_key'] = 'some value'
 
            session.commit()
 
        When the :mod:`sqlalchemy.ext.mutable` extension is not used, the ORM
        will not be alerted to any changes to the contents of an existing
        dictionary, unless that dictionary value is re-assigned to the
        HSTORE-attribute itself, thus generating a change event.
 
    .. seealso::
 
        :class:`.hstore` - render the PostgreSQL ``hstore()`` function.
 
 
    FNcCs|dk    r||_dS)z¢Construct a new :class:`.HSTORE`.
 
        :param text_type: the type that should be used for indexed values.
         Defaults to :class:`_types.Text`.
 
        N)Ú    text_type)Úselfr ©rú\d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/dialects/postgresql/hstore.pyÚ__init__szHSTORE.__init__c@sxeZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd d „Z    dd„Z
dd„Z dd„Z dd„Z dd„Zdd„Zdd„ZdS)zHSTORE.Comparatorz2Define comparison operations for :class:`.HSTORE`.cCs|jt|tjdS)zvBoolean expression.  Test for presence of a key.  Note that the
            key may be a SQLA expression.
            ©Z result_type)ÚoperateÚHAS_KEYÚsqltypesÚBoolean©r ÚotherrrrÚhas_keyŸszHSTORE.Comparator.has_keycCs|jt|tjdS)z;Boolean expression.  Test for presence of all keys in jsonbr)rÚHAS_ALLrrrrrrÚhas_all¥szHSTORE.Comparator.has_allcCs|jt|tjdS)z:Boolean expression.  Test for presence of any key in jsonbr)rÚHAS_ANYrrrrrrÚhas_any©szHSTORE.Comparator.has_anycKs|jt|tjdS)zòBoolean expression.  Test if keys (or array) are a superset
            of/contained the keys of the argument jsonb expression.
 
            kwargs may be ignored by this operator but are required for API
            conformance.
            r)rÚCONTAINSrr)r rÚkwargsrrrÚcontains­szHSTORE.Comparator.containscCs|jt|tjdS)z|Boolean expression.  Test if keys are a proper subset of the
            keys of the argument jsonb expression.
            r)rÚ CONTAINED_BYrrrrrrÚ contained_by¶s
ÿzHSTORE.Comparator.contained_bycCst||jjfS©N)ÚGETITEMÚtyper )r ÚindexrrrÚ_setup_getitem¾sz HSTORE.Comparator._setup_getitemcCs t|j|ƒS)zBoolean expression.  Test for presence of a non-NULL value for
            the key.  Note that the key may be a SQLA expression.
            )Ú_HStoreDefinedFunctionÚexpr©r ÚkeyrrrÚdefinedÁszHSTORE.Comparator.definedcCst|tƒrt|ƒ}t|j|ƒS)z™HStore expression.  Returns the contents of this hstore with the
            given key deleted.  Note that the key may be a SQLA expression.
            )Ú
isinstanceÚdictÚ_serialize_hstoreÚ_HStoreDeleteFunctionr(r)rrrÚdeleteÇs
zHSTORE.Comparator.deletecCs t|j|ƒS)zdHStore expression.  Returns a subset of an hstore defined by
            array of keys.
            )Ú_HStoreSliceFunctionr()r ÚarrayrrrÚsliceÏszHSTORE.Comparator.slicecCs
t|jƒS)z.Text array expression.  Returns array of keys.)Ú_HStoreKeysFunctionr(©r rrrÚkeysÕszHSTORE.Comparator.keyscCs
t|jƒS)z0Text array expression.  Returns array of values.)Ú_HStoreValsFunctionr(r5rrrÚvalsÙszHSTORE.Comparator.valscCs
t|jƒS)z^Text array expression.  Returns array of alternating keys and
            values.
            )Ú_HStoreArrayFunctionr(r5rrrr2ÝszHSTORE.Comparator.arraycCs
t|jƒS)z<Text array expression.  Returns array of [key, value] pairs.)Ú_HStoreMatrixFunctionr(r5rrrÚmatrixãszHSTORE.Comparator.matrixN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrrrr!r&r+r0r3r6r8r2r;rrrrÚ
Comparatoršs    r@cCs dd„}|S)NcSst|tƒrt|ƒS|SdSr")r,r-r.©ÚvaluerrrÚprocessês
z&HSTORE.bind_processor.<locals>.processr)r ÚdialectrCrrrÚbind_processorészHSTORE.bind_processorcCs dd„}|S)NcSs|dk    rt|ƒS|SdSr")Ú _parse_hstorerArrrrCósz(HSTORE.result_processor.<locals>.processr)r rDZcoltyperCrrrÚresult_processoròszHSTORE.result_processor)N)r<r=r>r?Z__visit_name__ZhashablerÚTextr rÚ    Indexabler@Ú ConcatenableZcomparator_factoryrErGrrrrrAsJ
 
 
ÿM    rc@seZdZdZeZdZdZdS)r    aGConstruct an hstore value within a SQL expression using the
    PostgreSQL ``hstore()`` function.
 
    The :class:`.hstore` function accepts one or two arguments as described
    in the PostgreSQL documentation.
 
    E.g.::
 
        from sqlalchemy.dialects.postgresql import array, hstore
 
        select(hstore('key1', 'value1'))
 
        select(
            hstore(
                array(['key1', 'key2', 'key3']),
                array(['value1', 'value2', 'value3'])
            )
        )
 
    .. seealso::
 
        :class:`.HSTORE` - the PostgreSQL ``HSTORE`` datatype.
 
    TN)r<r=r>r?rr$ÚnameÚ inherit_cacherrrrr    üsr    c@seZdZejZdZdZdS)r'r+TN)r<r=r>rrr$rKrLrrrrr'sr'c@seZdZeZdZdZdS)r/r0TN©r<r=r>rr$rKrLrrrrr/!sr/c@seZdZeZdZdZdS)r1r3TNrMrrrrr1'sr1c@seZdZeejƒZdZdZdS)r4ZakeysTN©    r<r=r>rrrHr$rKrLrrrrr4-s
r4c@seZdZeejƒZdZdZdS)r7ZavalsTNrNrrrrr73s
r7c@seZdZeejƒZdZdZdS)r9Zhstore_to_arrayTNrNrrrrr99s
r9c@seZdZeejƒZdZdZdS)r:Zhstore_to_matrixTNrNrrrrr:?s
r:zÝ
(
  "(?P<key> (\\ . | [^"])* )"       # Quoted key
)
[ ]* => [ ]*    # Pair operator, optional adjoining whitespace
(
    (?P<value_null> NULL )          # NULL value
  | "(?P<value> (\\ . | [^"])* )"   # Quoted value
)
z
[ ]* , [ ]*
cCs’d}t|ƒ}|t||ddƒt||ƒ…}|t||ƒt||d|ƒ…}t|ƒ|krhd|dd…}t|ƒ|kr„|dd…d}d|||fS)zformat an unmarshalling error.érrz[...]Néÿÿÿÿz5After %r, could not parse residual at position %d: %r)ÚlenÚmaxÚmin)Ú
hstore_strÚposÚctxZhslenZ parsed_tailZresidualrrrÚ _parse_errorcs    ýrWcCsÊi}d}t |¡}|dk    r¬| d¡ dd¡ dd¡}| d¡rDd}n| d    ¡ dd¡ dd¡}|||<|| ¡7}t ||d…¡}|dk    r˜|| ¡7}t ||d…¡}q|t|ƒkrÆtt||ƒƒ‚|S)
aParse an hstore from its literal string representation.
 
    Attempts to approximate PG's hstore input parsing rules as closely as
    possible. Although currently this is not strictly necessary, since the
    current implementation of hstore's output syntax is stricter than what it
    accepts as input, the documentation makes no guarantees that will always
    be the case.
 
 
 
    rNr*ú\"ú"ú\\ú\Z
value_nullrB)    ÚHSTORE_PAIR_REÚmatchÚgroupÚreplaceÚendÚHSTORE_DELIMITER_RErQÚ
ValueErrorrW)rTÚresultrUZ
pair_matchr*rBZ delim_matchrrrrFxs0 
 
 
ÿþÿ   rFcs$dd„‰d ‡fdd„| ¡Dƒ¡S)zxSerialize a dictionary into an hstore literal.  Keys and values must
    both be strings (except None for values).
 
    cSsJ|dkr|dkrdSt|tƒr6d| dd¡ dd¡Std||fƒ‚dS)    NrBÚNULLz"%s"r[rZrYrXz"%r in %s position is not a string.)r,Ústrr_rb)ÚsÚpositionrrrÚesc¨s
 
ÿz_serialize_hstore.<locals>.escz, c3s*|]"\}}dˆ|dƒˆ|dƒfVqdS)z%s=>%sr*rBNr)Ú.0ÚkÚv©rhrrÚ    <genexpr>²sz$_serialize_hstore.<locals>.<genexpr>)ÚjoinÚitems)Úvalrrlrr.¢s
ÿr.))Úrer2rÚrrZsqlrZsqlfuncrÚ__all__Z _PRECEDENCEZjson_getitem_opZidx_precedenceZ    custom_opr#rrrrr rIrJZ
TypeEnginerZGenericFunctionr    r'r/r1r4r7r9r:ÚcompileÚVERBOSEr\rarWrFr.rrrrÚ<module>
s~     üüüüüü<
õü*