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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
U
¸ý°d8Dã@sºddlmZddlmZddlmZddlmZddlmZddlmZddlm    Z    dd    lm
Z
dd
l m Z dd l m Z dd l mZdd l mZddl mZddlmZddlmZddlmZerÐddlmZGdd„dejƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deejejƒZ Gdd „d eƒZ!Gd!d"„d"eƒZ"Gd#d$„d$eej#ƒZ$Gd%d&„d&e    j%ƒZ&Gd'd(„d(e    j%ƒZ'Gd)d*„d*e    j%ƒZ(Gd+d,„d,e    j%ƒZ)d-S).é)Ú annotations)ÚAny)ÚOptional)ÚType)Ú TYPE_CHECKING)ÚUnioné©Úschema)Úutil)Ú    coercions)Úelements)Úroles)Úsqltypes)Útype_api)Ú_NoArg)ÚInvokeCreateDDLBase)ÚInvokeDropDDLBase)Ú_TypeEngineArgumentc@sreZdZUdZdZded<ded<ded<dd    d
„Zdd d „Zd d„Zddd„Z    ddd„Z
ddd„Z ddd„Z dS)Ú    NamedTypezBase for named types.TzType[NamedTypeGenerator]Ú DDLGeneratorzType[NamedTypeDropper]Ú
DDLDropperÚboolÚ create_typecKs|j|j||ddS)alEmit ``CREATE`` DDL for this type.
 
        :param bind: a connectable :class:`_engine.Engine`,
         :class:`_engine.Connection`, or similar object to emit
         SQL.
        :param checkfirst: if ``True``, a query against
         the PG catalog will be first performed to see
         if the type does not exist already before
         creating.
 
        ©Ú
checkfirstN)Ú_run_ddl_visitorr©ÚselfÚbindrÚkw©r!úad:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/dialects/postgresql/named_types.pyÚcreate's zNamedType.createcKs|j|j||ddS)aZEmit ``DROP`` DDL for this type.
 
        :param bind: a connectable :class:`_engine.Engine`,
         :class:`_engine.Connection`, or similar object to emit
         SQL.
        :param checkfirst: if ``True``, a query against
         the PG catalog will be first performed to see
         if the type actually exists before dropping.
 
        rN)rrrr!r!r"Údrop5s zNamedType.dropcCsz|js
dSd|krr|d}d|j›}||jkr<|j|}ntƒ}|j|<|j|jf|k}| |j|jf¡|SdSdS)aLook in the 'ddl runner' for 'memos', then
        note our name in that collection.
 
        This to ensure a particular named type is operated
        upon only once within any kind of create/drop
        sequence without relying upon "checkfirst".
 
        TZ _ddl_runnerZpg_FN)rÚ__visit_name__ÚmemoÚsetr
ÚnameÚadd)rrr Z
ddl_runnerZ    type_nameÚexistingZpresentr!r!r"Ú_check_for_name_in_memosBs     
 z"NamedType._check_for_name_in_memosFcKs4|s|js0| dd¡s0| ||¡s0|j||ddS©NZ_is_metadata_operationF©rr)ÚmetadataÚgetr+r#©rÚtargetrrr r!r!r"Ú_on_table_createZsÿý
ü
úzNamedType._on_table_createcKs0|js,| dd¡s,| ||¡s,|j||ddSr,)r.r/r+r$r0r!r!r"Ú_on_table_dropdsÿ
þ
ýzNamedType._on_table_dropcKs| ||¡s|j||ddS©Nr-)r+r#r0r!r!r"Ú_on_metadata_createls zNamedType._on_metadata_createcKs| ||¡s|j||ddSr4)r+r$r0r!r!r"Ú_on_metadata_dropps zNamedType._on_metadata_dropN)T)T)F)F)F)F) Ú__name__Ú
__module__Ú __qualname__Ú__doc__Z __abstract__Ú__annotations__r#r$r+r2r3r5r6r!r!r!r"rs
 
 
 
 
 
 
rcs&eZdZd‡fdd„    Zdd„Z‡ZS)ÚNamedTypeGeneratorFc stƒj|f|Ž||_dS©N©ÚsuperÚ__init__r©rÚdialectÚ
connectionrÚkwargs©Ú    __class__r!r"r@vszNamedTypeGenerator.__init__cCs0|js
dS|j |¡}|jjj|j|j|d S©NTr    ©rrCZschema_for_objectrBZhas_typer(©rÚtype_Zeffective_schemar!r!r"Ú_can_create_typezs ÿz#NamedTypeGenerator._can_create_type)F)r7r8r9r@rKÚ __classcell__r!r!rEr"r<usr<cs&eZdZd‡fdd„    Zdd„Z‡ZS)ÚNamedTypeDropperFc stƒj|f|Ž||_dSr=r>rArEr!r"r@…szNamedTypeDropper.__init__cCs.|js
dS|j |¡}|jjj|j|j|dSrGrHrIr!r!r"Ú_can_drop_type‰s ÿzNamedTypeDropper._can_drop_type)F)r7r8r9r@rNrLr!r!rEr"rM„srMc@seZdZdd„ZdS)Ú EnumGeneratorc    Cs8| |¡sdS| |¡|j t|ƒ¡W5QRXdSr=)rKÚwith_ddl_eventsrCÚexecuteÚCreateEnumType©rÚenumr!r!r"Ú
visit_enum”s
 zEnumGenerator.visit_enumN©r7r8r9rUr!r!r!r"rO“srOc@seZdZdd„ZdS)Ú EnumDropperc    Cs8| |¡sdS| |¡|j t|ƒ¡W5QRXdSr=)rNrPrCrQÚ DropEnumTyperSr!r!r"rUs
 zEnumDropper.visit_enumNrVr!r!r!r"rWœsrWcs†eZdZdZdZeZeZe    j
ddœdddœ‡fdd„Z ‡fdd    „Z e d
d „ƒZe d d „ƒZd‡fdd„    Zd‡fdd„    Zdd„Z‡ZS)ÚENUMa-
PostgreSQL ENUM type.
 
    This is a subclass of :class:`_types.Enum` which includes
    support for PG's ``CREATE TYPE`` and ``DROP TYPE``.
 
    When the builtin type :class:`_types.Enum` is used and the
    :paramref:`.Enum.native_enum` flag is left at its default of
    True, the PostgreSQL backend will use a :class:`_postgresql.ENUM`
    type as the implementation, so the special create/drop rules
    will be used.
 
    The create/drop behavior of ENUM is necessarily intricate, due to the
    awkward relationship the ENUM type has in relationship to the
    parent table, in that it may be "owned" by just a single table, or
    may be shared among many tables.
 
    When using :class:`_types.Enum` or :class:`_postgresql.ENUM`
    in an "inline" fashion, the ``CREATE TYPE`` and ``DROP TYPE`` is emitted
    corresponding to when the :meth:`_schema.Table.create` and
    :meth:`_schema.Table.drop`
    methods are called::
 
        table = Table('sometable', metadata,
            Column('some_enum', ENUM('a', 'b', 'c', name='myenum'))
        )
 
        table.create(engine)  # will emit CREATE ENUM and CREATE TABLE
        table.drop(engine)  # will emit DROP TABLE and DROP ENUM
 
    To use a common enumerated type between multiple tables, the best
    practice is to declare the :class:`_types.Enum` or
    :class:`_postgresql.ENUM` independently, and associate it with the
    :class:`_schema.MetaData` object itself::
 
        my_enum = ENUM('a', 'b', 'c', name='myenum', metadata=metadata)
 
        t1 = Table('sometable_one', metadata,
            Column('some_enum', myenum)
        )
 
        t2 = Table('sometable_two', metadata,
            Column('some_enum', myenum)
        )
 
    When this pattern is used, care must still be taken at the level
    of individual table creates.  Emitting CREATE TABLE without also
    specifying ``checkfirst=True`` will still cause issues::
 
        t1.create(engine) # will fail: no such type 'myenum'
 
    If we specify ``checkfirst=True``, the individual table-level create
    operation will check for the ``ENUM`` and create if not exists::
 
        # will check if enum exists, and emit CREATE TYPE if not
        t1.create(engine, checkfirst=True)
 
    When using a metadata-level ENUM type, the type will always be created
    and dropped if either the metadata-wide create/drop is called::
 
        metadata.create_all(engine)  # will emit CREATE TYPE
        metadata.drop_all(engine)  # will emit DROP TYPE
 
    The type can also be created and dropped directly::
 
        my_enum.create(engine)
        my_enum.drop(engine)
 
    T)r(rzUnion[str, _NoArg, None]rcsH| dd¡}|dkrt d¡||_|tjk    r6||d<tƒj||ŽdS)aConstruct an :class:`_postgresql.ENUM`.
 
        Arguments are the same as that of
        :class:`_types.Enum`, but also including
        the following parameters.
 
        :param create_type: Defaults to True.
         Indicates that ``CREATE TYPE`` should be
         emitted, after optionally checking for the
         presence of the type, when the parent
         table is being created; and additionally
         that ``DROP TYPE`` is called when the table
         is dropped.    When ``False``, no check
         will be performed and no ``CREATE TYPE``
         or ``DROP TYPE`` is emitted, unless
         :meth:`~.postgresql.ENUM.create`
         or :meth:`~.postgresql.ENUM.drop`
         are called directly.
         Setting to ``False`` is helpful
         when invoking a creation scheme to a SQL file
         without access to the actual database -
         the :meth:`~.postgresql.ENUM.create` and
         :meth:`~.postgresql.ENUM.drop` methods can
         be used to emit SQL to a target bind.
 
        Ú native_enumNFzªthe native_enum flag does not apply to the sqlalchemy.dialects.postgresql.ENUM datatype; this type always refers to ENUM.   Use sqlalchemy.types.Enum for non-native enum.r()Úpopr ÚwarnrrÚNO_ARGr?r@)rr(rÚenumsr rZrEr!r"r@ðs! ÿ
z ENUM.__init__cs(tƒ ||¡}|jtjjkr |S|SdSr=)r?Úcoerce_compared_valueZ_type_affinityrZ
STRINGTYPE)rÚopÚvalueZsuper_coerced_typerEr!r"r_sÿÿzENUM.coerce_compared_valuecCs
|ddS)Nr()r(r!©Úclsr!r!r"Ú __test_init__(szENUM.__test_init__cKs„| d|j¡| d|j¡| d|j¡| d|j¡| d|j¡| dd¡| d|j¡| d    |j¡| d
|¡|f|ŽS) zbProduce a PostgreSQL native :class:`_postgresql.ENUM` from plain
        :class:`.Enum`.
 
        Úvalidate_stringsr(r
Úinherit_schemar.Z_create_eventsFÚvalues_callableZ omit_aliasesZ _adapted_from)Ú
setdefaultrer(r
rfr.rgZ _omit_aliases)rcÚimplr r!r!r"Úadapt_emulated_to_native,s  zENUM.adapt_emulated_to_nativeNcs |jjs dStƒj||ddS)aòEmit ``CREATE TYPE`` for this
        :class:`_postgresql.ENUM`.
 
        If the underlying dialect does not support
        PostgreSQL CREATE TYPE, no action is taken.
 
        :param bind: a connectable :class:`_engine.Engine`,
         :class:`_engine.Connection`, or similar object to emit
         SQL.
        :param checkfirst: if ``True``, a query against
         the PG catalog will be first performed to see
         if the type does not exist already before
         creating.
 
        Nr)rBÚsupports_native_enumr?r#©rrrrEr!r"r#=sz ENUM.createcs |jjs dStƒj||ddS)aÞEmit ``DROP TYPE`` for this
        :class:`_postgresql.ENUM`.
 
        If the underlying dialect does not support
        PostgreSQL DROP TYPE, no action is taken.
 
        :param bind: a connectable :class:`_engine.Engine`,
         :class:`_engine.Connection`, or similar object to emit
         SQL.
        :param checkfirst: if ``True``, a query against
         the PG catalog will be first performed to see
         if the type actually exists before dropping.
 
        Nr)rBrkr?r$rlrEr!r"r$Rsz    ENUM.dropcCsdS)zVdont return dbapi.STRING for ENUM in PostgreSQL, since that's
        a different typeNr!)rZdbapir!r!r"Úget_dbapi_typefszENUM.get_dbapi_type)NT)NT)r7r8r9r:rZrOrrWrrr]r@r_Ú classmethodrdrjr#r$rmrLr!r!rEr"rY¥sEü.
 
 
rYc@seZdZdd„ZdS)ÚDomainGeneratorc    Cs8| |¡sdS| |¡|j t|ƒ¡W5QRXdSr=)rKrPrCrQÚCreateDomainType©rÚdomainr!r!r"Ú visit_DOMAINns
 zDomainGenerator.visit_DOMAINN©r7r8r9rsr!r!r!r"romsroc@seZdZdd„ZdS)Ú DomainDropperc    Cs8| |¡sdS| |¡|j t|ƒ¡W5QRXdSr=)rNrPrCrQÚDropDomainTyperqr!r!r"rsvs
 zDomainDropper.visit_DOMAINNrtr!r!r!r"ruusruc s^eZdZdZeZeZdZdddddddœdddddd    dd
d d œ    ‡fd d„Z    e
dd„ƒZ ‡Z S)ÚDOMAINaIRepresent the DOMAIN PostgreSQL type.
 
    A domain is essentially a data type with optional constraints
    that restrict the allowed set of values. E.g.::
 
        PositiveInt = DOMAIN(
            "pos_int", Integer, check="VALUE > 0", not_null=True
        )
 
        UsPostalCode = DOMAIN(
            "us_postal_code",
            Text,
            check="VALUE ~ '^\d{5}$' OR VALUE ~ '^\d{5}-\d{4}$'"
        )
 
    See the `PostgreSQL documentation`__ for additional details
 
    __ https://www.postgresql.org/docs/current/sql-createdomain.html
 
    .. versionadded:: 2.0
 
    NT)Ú    collationÚdefaultÚconstraint_nameÚnot_nullÚcheckrÚstrz_TypeEngineArgument[Any]z Optional[str]z)Optional[Union[str, elements.TextClause]]zOptional[bool]rr)    r(Ú    data_typerxryrzr{r|rr c
 s`t |¡|_||_||_||_||_|dk    r:t t    j
|¡}||_ ||_ t ƒjfd|i|    —ŽdS)a†
        Construct a DOMAIN.
 
        :param name: the name of the domain
        :param data_type: The underlying data type of the domain.
          This can include array specifiers.
        :param collation: An optional collation for the domain.
          If no collation is specified, the underlying data type's default
          collation is used. The underlying type must be collatable if
          ``collation`` is specified.
        :param default: The DEFAULT clause specifies a default value for
          columns of the domain data type. The default should be a string
          or a :func:`_expression.text` value.
          If no default value is specified, then the default value is
          the null value.
        :param constraint_name: An optional name for a constraint.
          If not specified, the backend generates a name.
        :param not_null: Values of this domain are prevented from being null.
          By default domain are allowed to be null. If not specified
          no nullability clause will be emitted.
        :param check: CHECK clause specify integrity constraint or test
          which values of the domain must satisfy. A constraint must be
          an expression producing a Boolean result that can use the key
          word VALUE to refer to the value being tested.
          Differently from PostgreSQL, only a single check clause is
          currently allowed in SQLAlchemy.
        :param schema: optional schema name
        :param metadata: optional :class:`_schema.MetaData` object which
         this :class:`_postgresql.DOMAIN` will be directly associated
        :param create_type: Defaults to True.
         Indicates that ``CREATE TYPE`` should be emitted, after optionally
         checking for the presence of the type, when the parent table is
         being created; and additionally that ``DROP TYPE`` is called
         when the table is dropped.
 
        Nr()rZ to_instancer~ryrxrzr{r ÚexpectrZDDLExpressionRoler|rr?r@)
rr(r~rxryrzr{r|rr rEr!r"r@›s1 zDOMAIN.__init__cCs |dtjƒS)Nr()rZIntegerrbr!r!r"rd×szDOMAIN.__test_init__) r7r8r9r:rorrurr%r@rnrdrLr!r!rEr"rw~sö&<rwc@seZdZdZdS)rRZcreate_enum_typeN©r7r8r9r%r!r!r!r"rRÜsrRc@seZdZdZdS)rXZdrop_enum_typeNr€r!r!r!r"rXàsrXc@seZdZdZdZdS)rpz$Represent a CREATE DOMAIN statement.Zcreate_domain_typeN©r7r8r9r:r%r!r!r!r"rpäsrpc@seZdZdZdZdS)rvz"Represent a DROP DOMAIN statement.Zdrop_domain_typeNrr!r!r!r"rvêsrvN)*Ú
__future__rÚtypingrrrrrÚr
r Zsqlr r rrrZsql.baserZsql.ddlrrZ sql._typingrZ
TypeEnginerr<rMrOrWZNativeForEmulatedÚEnumrYroruZ
SchemaTyperwZ_CreateDropBaserRrXrprvr!r!r!r"Ú<module>s>                 V        I    ^