zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
U
¸ý°d¨1ã@sJddlmZddlmZddlmZddlmZddlmZdZ    ej
ej Z ej de d    d    d
Zej d e d    d    d
Zej d e d    d    d
Zej d e d    d    d
Zej de d    d    d
Zej de d    d    d
Zej de d    d    d
Zej de d    d    d
Zej de d    d    d
Zej de d    d    d
ZGdd„dejjƒZGdd„deƒZGdd„dejƒZGdd„deƒZdS)é)ÚARRAY)Úarrayé)Útypes)Úcast)Ú    operators)ÚJSONÚJSONBz->>T)Ú
precedenceZnatural_self_precedentZeager_groupingz#>>ú?z?&z?|z@>z<@z#-z@?z@@c@s$eZdZdd„Zdd„Zdd„ZdS)Ú JSONPathTypecs‡fdd„}|S)Ncs<t|tƒr|S|r(dd tt|ƒ¡}nd}ˆr8ˆ|ƒ}|S)Nz{%s}z, z{})Ú
isinstanceÚstrÚjoinÚmap)Úvalue©Ú
super_proc©úZd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/dialects/postgresql/json.pyÚprocess_s
z(JSONPathType._processor.<locals>.processr)ÚselfÚdialectrrrrrÚ
_processor^s zJSONPathType._processorcCs| || |¡¡S©N)rZstring_bind_processor©rrrrrÚbind_processorpszJSONPathType.bind_processorcCs| || |¡¡Sr)rZstring_literal_processorrrrrÚliteral_processorsszJSONPathType.literal_processorN)Ú__name__Ú
__module__Ú __qualname__rrrrrrrr ]sr c@seZdZdZdZdS)ÚJSONPATHadJSON Path Type.
 
    This is usually required to cast literal values to json path when using
    json search like function, such as ``jsonb_path_query_array`` or
    ``jsonb_path_exists``::
 
        stmt = sa.select(
            sa.func.jsonb_path_query_array(
                table.c.jsonb_col, cast("$.address.id", JSONPATH)
            )
        )
 
    N)rrr Ú__doc__Ú__visit_name__rrrrr!wsr!csBeZdZdZe ¡Zd‡fdd„    ZGdd„dejj    ƒZ    e    Z
‡Z S)    ra
Represent the PostgreSQL JSON type.
 
    :class:`_postgresql.JSON` is used automatically whenever the base
    :class:`_types.JSON` datatype is used against a PostgreSQL backend,
    however base :class:`_types.JSON` datatype does not provide Python
    accessors for PostgreSQL-specific comparison methods such as
    :meth:`_postgresql.JSON.Comparator.astext`; additionally, to use
    PostgreSQL ``JSONB``, the :class:`_postgresql.JSONB` datatype should
    be used explicitly.
 
    .. seealso::
 
        :class:`_types.JSON` - main documentation for the generic
        cross-platform JSON datatype.
 
    The operators provided by the PostgreSQL version of :class:`_types.JSON`
    include:
 
    * Index operations (the ``->`` operator)::
 
        data_table.c.data['some key']
 
        data_table.c.data[5]
 
 
    * Index operations returning text (the ``->>`` operator)::
 
        data_table.c.data['some key'].astext == 'some value'
 
      Note that equivalent functionality is available via the
      :attr:`.JSON.Comparator.as_string` accessor.
 
    * Index operations with CAST
      (equivalent to ``CAST(col ->> ['some key'] AS <type>)``)::
 
        data_table.c.data['some key'].astext.cast(Integer) == 5
 
      Note that equivalent functionality is available via the
      :attr:`.JSON.Comparator.as_integer` and similar accessors.
 
    * Path index operations (the ``#>`` operator)::
 
        data_table.c.data[('key_1', 'key_2', 5, ..., 'key_n')]
 
    * Path index operations returning text (the ``#>>`` operator)::
 
        data_table.c.data[('key_1', 'key_2', 5, ..., 'key_n')].astext == 'some value'
 
    Index operations return an expression object whose type defaults to
    :class:`_types.JSON` by default,
    so that further JSON-oriented instructions
    may be called upon the result type.
 
    Custom serializers and deserializers are specified at the dialect level,
    that is using :func:`_sa.create_engine`.  The reason for this is that when
    using psycopg2, the DBAPI only allows serializers at the per-cursor
    or per-connection level.   E.g.::
 
        engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test",
                                json_serializer=my_serialize_fn,
                                json_deserializer=my_deserialize_fn
                        )
 
    When using the psycopg2 dialect, the json_deserializer is registered
    against the database using ``psycopg2.extras.register_default_json``.
 
    .. seealso::
 
        :class:`_types.JSON` - Core level JSON type
 
        :class:`_postgresql.JSONB`
 
    FNcs tƒj|d|dk    r||_dS)a|Construct a :class:`_types.JSON` type.
 
        :param none_as_null: if True, persist the value ``None`` as a
         SQL NULL value, not the JSON encoding of ``null``.   Note that
         when this flag is False, the :func:`.null` construct can still
         be used to persist a NULL value::
 
             from sqlalchemy import null
             conn.execute(table.insert(), data=null())
 
         .. seealso::
 
              :attr:`_types.JSON.NULL`
 
        :param astext_type: the type to use for the
         :attr:`.JSON.Comparator.astext`
         accessor on indexed attributes.  Defaults to :class:`_types.Text`.
 
        )Ú none_as_nullN)ÚsuperÚ__init__Ú astext_type)rr$r'©Ú    __class__rrr&Ösz JSON.__init__c@seZdZdZedd„ƒZdS)zJSON.Comparatorú6Define comparison operations for :class:`_types.JSON`.cCsPt|jjjtjjƒr0|jjjt    |jj|jj
dS|jjjt |jj|jj
dSdS)aOn an indexed expression, use the "astext" (e.g. "->>")
            conversion when rendered in SQL.
 
            E.g.::
 
                select(data_table.c.data['some key'].astext)
 
            .. seealso::
 
                :meth:`_expression.ColumnElement.cast`
 
            ©Z result_typeN) r ÚexprÚrightÚtypeÚsqltypesrr ÚleftÚoperateÚJSONPATH_ASTEXTr'ÚASTEXT)rrrrÚastextñsýÿzJSON.Comparator.astextN)rrr r"Úpropertyr4rrrrÚ
Comparatorîsr6)FN) rrr r"r/ÚTextr'r&rr6Úcomparator_factoryÚ __classcell__rrr(rr‰s
Jrc@s*eZdZdZdZGdd„dejƒZeZdS)r    a Represent the PostgreSQL JSONB type.
 
    The :class:`_postgresql.JSONB` type stores arbitrary JSONB format data,
    e.g.::
 
        data_table = Table('data_table', metadata,
            Column('id', Integer, primary_key=True),
            Column('data', JSONB)
        )
 
        with engine.connect() as conn:
            conn.execute(
                data_table.insert(),
                data = {"key1": "value1", "key2": "value2"}
            )
 
    The :class:`_postgresql.JSONB` type includes all operations provided by
    :class:`_types.JSON`, including the same behaviors for indexing
    operations.
    It also adds additional operators specific to JSONB, including
    :meth:`.JSONB.Comparator.has_key`, :meth:`.JSONB.Comparator.has_all`,
    :meth:`.JSONB.Comparator.has_any`, :meth:`.JSONB.Comparator.contains`,
    :meth:`.JSONB.Comparator.contained_by`,
    :meth:`.JSONB.Comparator.delete_path`,
    :meth:`.JSONB.Comparator.path_exists` and
    :meth:`.JSONB.Comparator.path_match`.
 
    Like the :class:`_types.JSON` type, the :class:`_postgresql.JSONB`
    type does not detect
    in-place changes when used with the ORM, unless the
    :mod:`sqlalchemy.ext.mutable` extension is used.
 
    Custom serializers and deserializers
    are shared with the :class:`_types.JSON` class,
    using the ``json_serializer``
    and ``json_deserializer`` keyword arguments.  These must be specified
    at the dialect level using :func:`_sa.create_engine`.  When using
    psycopg2, the serializers are associated with the jsonb type using
    ``psycopg2.extras.register_default_jsonb`` on a per-connection basis,
    in the same way that ``psycopg2.extras.register_default_json`` is used
    to register these handlers with the json type.
 
    .. seealso::
 
        :class:`_types.JSON`
 
    c@sPeZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd d „Z    dd„Z
dd„Z dS)zJSONB.Comparatorr*cCs|jt|tjdS)zvBoolean expression.  Test for presence of a key.  Note that the
            key may be a SQLA expression.
            r+)r1ÚHAS_KEYr/ÚBoolean©rÚotherrrrÚhas_keyCszJSONB.Comparator.has_keycCs|jt|tjdS)z;Boolean expression.  Test for presence of all keys in jsonbr+)r1ÚHAS_ALLr/r;r<rrrÚhas_allIszJSONB.Comparator.has_allcCs|jt|tjdS)z:Boolean expression.  Test for presence of any key in jsonbr+)r1ÚHAS_ANYr/r;r<rrrÚhas_anyMszJSONB.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+)r1ÚCONTAINSr/r;)rr=ÚkwargsrrrÚcontainsQszJSONB.Comparator.containscCs|jt|tjdS)z|Boolean expression.  Test if keys are a proper subset of the
            keys of the argument jsonb expression.
            r+)r1Ú CONTAINED_BYr/r;r<rrrÚ contained_byZs
ÿzJSONB.Comparator.contained_bycCs2t|tƒst|ƒ}t|ttjƒƒ}|jt|tdS)aJSONB expression. Deletes field or array element specified in
            the argument array.
 
            The input may be a list of strings that will be coerced to an
            ``ARRAY`` or an instance of :meth:`_postgres.array`.
 
            .. versionadded:: 2.0
            r+)    r Ú    _pg_arrayrrr/ZTEXTr1Ú DELETE_PATHr    )rrZ
right_siderrrÚ delete_pathbs    
zJSONB.Comparator.delete_pathcCs|jt|tjdS)z”Boolean expression. Test for presence of item given by the
            argument JSONPath expression.
 
            .. versionadded:: 2.0
            r+)r1Ú PATH_EXISTSr/r;r<rrrÚ path_existsps
ÿzJSONB.Comparator.path_existscCs|jt|tjdS)zãBoolean expression. Test if JSONPath predicate given by the
            argument JSONPath expression matches.
 
            Only the first item of the result is taken into account.
 
            .. versionadded:: 2.0
            r+)r1Ú
PATH_MATCHr/r;r<rrrÚ
path_matchzs
ÿzJSONB.Comparator.path_matchN) rrr r"r>r@rBrErGrJrLrNrrrrr6@s    
r6N)rrr r"r#rr6r8rrrrr     s0Fr    N)rrrHÚrr/ZsqlrrÚ__all__Z _PRECEDENCEZjson_getitem_opZidx_precedenceZ    custom_opr3r2r:r?rArCrFrIrKrMrr r!r    rrrrÚ<module>
sŽ      üüüüüüüüüü