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
U
¸ý°d7ã@sddlmZddlZddlmZddlmZddlmZddlmZ    ddlm
Z
dd    l m Z dd
l m Z dd lmZed ed Ze jfdd„Ze jfdd„ZGdd„de jeƒZe jddddZe jddddZe jddddZGdd„de    jƒZdd„ZdS)é)Ú annotationsN)ÚAny)ÚOptional)ÚTypeVaré)Útypes)Úutil)Ú
expression)Ú    operators)Ú_TypeEngineArgumentÚ_T)ÚboundcCs | ||¡S)zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.any` method.
    See that method for details.
 
    )Úany©ÚotherZarrexprÚoperator©rú[d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/dialects/postgresql/array.pyrsrcCs | ||¡S)zjA synonym for the ARRAY-level :meth:`.ARRAY.Comparator.all` method.
    See that method for details.
 
    )ÚallrrrrÚAll$srcsLeZdZdZdZdZdZ‡fdd„Zedd„ƒZ    dd
d „Z
dd d „Z ‡Z S)Úarraya×A PostgreSQL ARRAY literal.
 
    This is used to produce ARRAY literals in SQL expressions, e.g.::
 
        from sqlalchemy.dialects.postgresql import array
        from sqlalchemy.dialects import postgresql
        from sqlalchemy import select, func
 
        stmt = select(array([1,2]) + array([3,4,5]))
 
        print(stmt.compile(dialect=postgresql.dialect()))
 
    Produces the SQL::
 
        SELECT ARRAY[%(param_1)s, %(param_2)s] ||
            ARRAY[%(param_3)s, %(param_4)s, %(param_5)s]) AS anon_1
 
    An instance of :class:`.array` will always have the datatype
    :class:`_types.ARRAY`.  The "inner" type of the array is inferred from
    the values present, unless the ``type_`` keyword argument is passed::
 
        array(['foo', 'bar'], type_=CHAR)
 
    Multidimensional arrays are produced by nesting :class:`.array` constructs.
    The dimensionality of the final :class:`_types.ARRAY`
    type is calculated by
    recursively adding the dimensions of the inner :class:`_types.ARRAY`
    type::
 
        stmt = select(
            array([
                array([1, 2]), array([3, 4]), array([column('q'), column('x')])
            ])
        )
        print(stmt.compile(dialect=postgresql.dialect()))
 
    Produces::
 
        SELECT ARRAY[ARRAY[%(param_1)s, %(param_2)s],
        ARRAY[%(param_3)s, %(param_4)s], ARRAY[q, x]] AS anon_1
 
    .. versionadded:: 1.3.6 added support for multidimensional array literals
 
    .. seealso::
 
        :class:`_postgresql.ARRAY`
 
    Z
postgresqlTc s”| dd¡}tƒjtjf|ž|Ždd„|jDƒ|_|dk    r@|n|jrP|jdntj}t    |t
ƒr†t
|j |j dk    rz|j dndd|_ n
t
|ƒ|_ dS)NÚtype_cSsg|]
}|j‘qSr)Útype)Ú.0ÚargrrrÚ
<listcomp>jsz"array.__init__.<locals>.<listcomp>réé)Ú
dimensions)ÚpopÚsuperÚ__init__r
Zcomma_opÚclausesZ _type_tupleÚsqltypesZNULLTYPEÚ
isinstanceÚARRAYÚ    item_typerr)Úselfr"ÚkwZtype_argZ    main_type©Ú    __class__rrr!es" ÿÿ û
ÿ ü
zarray.__init__cCs|fS©Nr©r'rrrÚ_select_iterable~szarray._select_iterableFNcsD|sˆtjkr&tjd|ˆˆˆjddSt‡‡‡fdd„|DƒƒSdS)NT)Z_compared_to_operatorrZ_compared_to_typeÚuniquecsg|]}ˆjˆ|dˆd‘qS)T)Ú_assume_scalarr)Ú _bind_param)rÚo©rr'rrrrsýÿz%array._bind_param.<locals>.<listcomp>)r
Úgetitemr    Z BindParameterrr)r'rÚobjr/rrr2rr0‚sú
üÿzarray._bind_paramcCs&|tjtjtjfkrt |¡S|SdSr+)r
Zany_opZall_opr3r    ZGrouping)r'ZagainstrrrÚ
self_group—s
zarray.self_group)FN)N) Ú__name__Ú
__module__Ú __qualname__Ú__doc__Z__visit_name__Zstringify_dialectZ inherit_cacher!Úpropertyr-r0r5Ú __classcell__rrr)rr-s1 
 
rz@>éT)Ú
precedenceZ is_comparisonz<@z&&c@s„eZdZdZGdd„dejjƒZeZdddddd    œd
d „Ze    d d „ƒZ
e    dd„ƒZ dd„Z e jdd„ƒZdd„Zdd„Zdd„ZdS)r%aF
PostgreSQL ARRAY type.
 
    The :class:`_postgresql.ARRAY` type is constructed in the same way
    as the core :class:`_types.ARRAY` type; a member type is required, and a
    number of dimensions is recommended if the type is to be used for more
    than one dimension::
 
        from sqlalchemy.dialects import postgresql
 
        mytable = Table("mytable", metadata,
                Column("data", postgresql.ARRAY(Integer, dimensions=2))
            )
 
    The :class:`_postgresql.ARRAY` type provides all operations defined on the
    core :class:`_types.ARRAY` type, including support for "dimensions",
    indexed access, and simple matching such as
    :meth:`.types.ARRAY.Comparator.any` and
    :meth:`.types.ARRAY.Comparator.all`.  :class:`_postgresql.ARRAY`
    class also
    provides PostgreSQL-specific methods for containment operations, including
    :meth:`.postgresql.ARRAY.Comparator.contains`
    :meth:`.postgresql.ARRAY.Comparator.contained_by`, and
    :meth:`.postgresql.ARRAY.Comparator.overlap`, e.g.::
 
        mytable.c.data.contains([1, 2])
 
    The :class:`_postgresql.ARRAY` type may not be supported on all
    PostgreSQL DBAPIs; it is currently known to work on psycopg2 only.
 
    Additionally, the :class:`_postgresql.ARRAY`
    type does not work directly in
    conjunction with the :class:`.ENUM` type.  For a workaround, see the
    special type at :ref:`postgresql_array_of_enum`.
 
    .. container:: topic
 
        **Detecting Changes in ARRAY columns when using the ORM**
 
        The :class:`_postgresql.ARRAY` type, when used with the SQLAlchemy ORM,
        does not detect in-place mutations to the array. In order to detect
        these, the :mod:`sqlalchemy.ext.mutable` extension must be used, using
        the :class:`.MutableList` class::
 
            from sqlalchemy.dialects.postgresql import ARRAY
            from sqlalchemy.ext.mutable import MutableList
 
            class SomeOrmClass(Base):
                # ...
 
                data = Column(MutableList.as_mutable(ARRAY(Integer)))
 
        This extension will allow "in-place" changes such to the array
        such as ``.append()`` to produce events which will be detected by the
        unit of work.  Note that changes to elements **inside** the array,
        including subarrays that are mutated in place, are **not** detected.
 
        Alternatively, assigning a new array value to an ORM element that
        replaces the old one will always trigger a change event.
 
    .. seealso::
 
        :class:`_types.ARRAY` - base array type
 
        :class:`_postgresql.array` - produces a literal array value.
 
    c@s(eZdZdZdd„Zdd„Zdd„ZdS)    zARRAY.Comparatora*Define comparison operations for :class:`_types.ARRAY`.
 
        Note that these operations are in addition to those provided
        by the base :class:`.types.ARRAY.Comparator` class, including
        :meth:`.types.ARRAY.Comparator.any` and
        :meth:`.types.ARRAY.Comparator.all`.
 
        cKs|jt|tjdS)zåBoolean expression.  Test if elements are a superset of the
            elements of the argument array expression.
 
            kwargs may be ignored by this operator but are required for API
            conformance.
            ©Z result_type)ÚoperateÚCONTAINSr#ÚBoolean)r'rÚkwargsrrrÚcontainsôszARRAY.Comparator.containscCs|jt|tjdS)z„Boolean expression.  Test if elements are a proper subset of the
            elements of the argument array expression.
            r>)r?Ú CONTAINED_BYr#rA©r'rrrrÚ contained_byýs
ÿzARRAY.Comparator.contained_bycCs|jt|tjdS)zuBoolean expression.  Test if array has elements in common with
            an argument array expression.
            r>)r?ÚOVERLAPr#rArErrrÚoverlapszARRAY.Comparator.overlapN)r6r7r8r9rCrFrHrrrrÚ
Comparatorés        rIFNz_TypeEngineArgument[Any]Úboolz Optional[int])r&Úas_tuplerÚ zero_indexescCs>t|tƒrtdƒ‚t|tƒr"|ƒ}||_||_||_||_dS)a-Construct an ARRAY.
 
        E.g.::
 
          Column('myarray', ARRAY(Integer))
 
        Arguments are:
 
        :param item_type: The data type of items of this array. Note that
          dimensionality is irrelevant here, so multi-dimensional arrays like
          ``INTEGER[][]``, are constructed as ``ARRAY(Integer)``, not as
          ``ARRAY(ARRAY(Integer))`` or such.
 
        :param as_tuple=False: Specify whether return results
          should be converted to tuples from lists. DBAPIs such
          as psycopg2 return lists by default. When tuples are
          returned, the results are hashable.
 
        :param dimensions: if non-None, the ARRAY will assume a fixed
         number of dimensions.  This will cause the DDL emitted for this
         ARRAY to include the exact number of bracket clauses ``[]``,
         and will also optimize the performance of the type overall.
         Note that PG arrays are always implicitly "non-dimensioned",
         meaning they can store any number of dimensions no matter how
         they were declared.
 
        :param zero_indexes=False: when True, index values will be converted
         between Python zero-based and PostgreSQL one-based indexes, e.g.
         a value of one will be added to all index values before passing
         to the database.
 
        zUDo not nest ARRAY types; ARRAY(basetype) handles multi-dimensional arrays of basetypeN)r$r%Ú
ValueErrorrr&rKrrL)r'r&rKrrLrrrr! s'
ÿ
zARRAY.__init__cCs|jSr+)rKr,rrrÚhashable@szARRAY.hashablecCstSr+)Úlistr,rrrÚ python_typeDszARRAY.python_typecCs||kSr+r)r'ÚxÚyrrrÚcompare_valuesHszARRAY.compare_valuescCst|jtjƒo|jjSr+)r$r&r#ÚEnumZ native_enumr,rrrÚ_against_native_enumKsþzARRAY._against_native_enumcs:ˆj |¡ |¡‰ˆdkrdSdd„‰‡‡‡fdd„}|S)NcSsdd |¡›dS)NzARRAY[z, ú])Újoin)ÚelementsrrrÚto_strYsz'ARRAY.literal_processor.<locals>.to_strcsˆ |ˆˆjˆ¡}|Sr+)Ú_apply_item_processorr©ÚvalueÚinner©Ú    item_procr'rYrrÚprocess\sÿz(ARRAY.literal_processor.<locals>.process)r&Ú dialect_implÚliteral_processor©r'Údialectr`rr^rrbRs ÿzARRAY.literal_processorcs$ˆj |¡ |¡‰‡‡fdd„}|S)Ncs"|dkr |Sˆ |ˆˆjt¡SdSr+)rZrrO©r\©r_r'rrr`isÿz%ARRAY.bind_processor.<locals>.process)r&raÚbind_processorrcrrfrrgds
 ÿzARRAY.bind_processorcsTˆj |¡ ||¡‰‡‡fdd„}ˆjrP|‰t d¡‰‡fdd„‰‡‡fdd„}|S)Ncs,|dkr |Sˆ |ˆˆjˆjr"tnt¡SdSr+)rZrrKÚtuplerOrerfrrr`xs üz'ARRAY.result_processor.<locals>.processz^{(.*)}$csˆ |¡ d¡}t|ƒS)Nr)ÚmatchÚgroupÚ_split_enum_valuesr[)ÚpatternrrÚhandle_raw_string‡sz1ARRAY.result_processor.<locals>.handle_raw_stringcs&|dkr |Sˆt|tƒr ˆ|ƒn|ƒSr+)r$Ústrre)rmÚsuper_rprrr`‹sÿ
ý)r&raÚresult_processorrUÚreÚcompile)r'rdZcoltyper`r)rmr_rlr'rorrpss ÿ 
  zARRAY.result_processor)FNF)r6r7r8r9r#r%rIZcomparator_factoryr!r:rNrPrSrZmemoized_propertyrUrbrgrprrrrr%¥s"C"û3
 
 
r%cCsŽd|kr|r| d¡SgS| dd¡}| dd¡}g}t d|¡}d}|D]>}|dkr^| }qJ|rv| | dd¡¡qJ| t d    |¡¡qJ|S)
Nú"ú,z\"z _$ESC_QUOTE$_z\\ú\z(")Fz ([^\s,]+),?)ÚsplitÚreplacerqÚappendÚextendÚfindall)Z array_stringÚtextÚresultZ    on_quotesZ    in_quotesÚtokrrrrkšs   rk)Ú
__future__rrqÚtypingrrrÚrr#rZsqlr    r
Z sql._typingr r ÚeqrZExpressionClauseListrZ    custom_opr@rDrGr%rkrrrrÚ<module>
s&                  qv