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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
U
¸ý°dŽJã@sÐdZddlmZddlZddlmZddlmZddlmZ    ddlm
Z
dd    l m Z dd
l mZdd lmZdd lmZdd lmZddlmZer ddlmZGdd„dƒZGdd„deƒZGdd„dƒZdS)z1Public API functions and helpers for declarative.é)Ú annotationsN)ÚCallable)Ú TYPE_CHECKINGé)Úexc)Ú relationships)Ú_mapper_or_none)Ú    _resolver)Ú_DeferredMapperConfig)Úpolymorphic_union©ÚTable)Ú OrderedDict)ÚMetaDatac@s(eZdZdZedd„ƒZedd„ƒZdS)Ú ConcreteBasea_    A helper class for 'concrete' declarative mappings.
 
    :class:`.ConcreteBase` will use the :func:`.polymorphic_union`
    function automatically, against all tables mapped as a subclass
    to this class.   The function is called via the
    ``__declare_last__()`` function, which is essentially
    a hook for the :meth:`.after_configured` event.
 
    :class:`.ConcreteBase` produces a mapped
    table for the class itself.  Compare to :class:`.AbstractConcreteBase`,
    which does not.
 
    Example::
 
        from sqlalchemy.ext.declarative import ConcreteBase
 
        class Employee(ConcreteBase, Base):
            __tablename__ = 'employee'
            employee_id = Column(Integer, primary_key=True)
            name = Column(String(50))
            __mapper_args__ = {
                            'polymorphic_identity':'employee',
                            'concrete':True}
 
        class Manager(Employee):
            __tablename__ = 'manager'
            employee_id = Column(Integer, primary_key=True)
            name = Column(String(50))
            manager_data = Column(String(40))
            __mapper_args__ = {
                            'polymorphic_identity':'manager',
                            'concrete':True}
 
 
    The name of the discriminator column used by :func:`.polymorphic_union`
    defaults to the name ``type``.  To suit the use case of a mapping where an
    actual column in a mapped table is already named ``type``, the
    discriminator name can be configured by setting the
    ``_concrete_discriminator_name`` attribute::
 
        class Employee(ConcreteBase, Base):
            _concrete_discriminator_name = '_concrete_discriminator'
 
    .. versionadded:: 1.3.19 Added the ``_concrete_discriminator_name``
       attribute to :class:`_declarative.ConcreteBase` so that the
       virtual discriminator column name can be customized.
 
    .. versionchanged:: 1.4.2 The ``_concrete_discriminator_name`` attribute
       need only be placed on the basemost class to take correct effect for
       all subclasses.   An explicit error message is now raised if the
       mapped column names conflict with the discriminator name, whereas
       in the 1.3.x series there would be some warnings and then a non-useful
       query would be generated.
 
    .. seealso::
 
        :class:`.AbstractConcreteBase`
 
        :ref:`concrete_inheritance`
 
 
    cCsttdd„|Dƒƒ|dƒS)Ncss|]}|j|jfVqdS©N)Zpolymorphic_identityÚ local_table)Ú.0Úmp©rú\d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/ext/declarative/extensions.pyÚ    <genexpr>asz9ConcreteBase._create_polymorphic_union.<locals>.<genexpr>Úpjoin)r r)ÚclsÚmappersÚdiscriminator_namerrrÚ_create_polymorphic_union^sÿûz&ConcreteBase._create_polymorphic_unioncCsX|j}|jrdSt|ddƒpd}t|jƒ}| ||¡}| d|f¡| |j|¡dS)NÚ_concrete_discriminator_nameÚtypeÚ*)    Ú
__mapper__Úwith_polymorphicÚgetattrÚlistZself_and_descendantsrZ_set_with_polymorphicZ_set_polymorphic_onÚc)rÚmrrrrrrÚ__declare_first__hsÿ
 zConcreteBase.__declare_first__N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__Ú classmethodrr&rrrrrs
?
    rc@s8eZdZdZdZedd„ƒZedd„ƒZedd„ƒZd    S)
ÚAbstractConcreteBaseaºA helper class for 'concrete' declarative mappings.
 
    :class:`.AbstractConcreteBase` will use the :func:`.polymorphic_union`
    function automatically, against all tables mapped as a subclass
    to this class.   The function is called via the
    ``__declare_first__()`` function, which is essentially
    a hook for the :meth:`.before_configured` event.
 
    :class:`.AbstractConcreteBase` applies :class:`_orm.Mapper` for its
    immediately inheriting class, as would occur for any other
    declarative mapped class. However, the :class:`_orm.Mapper` is not
    mapped to any particular :class:`.Table` object.  Instead, it's
    mapped directly to the "polymorphic" selectable produced by
    :func:`.polymorphic_union`, and performs no persistence operations on its
    own.  Compare to :class:`.ConcreteBase`, which maps its
    immediately inheriting class to an actual
    :class:`.Table` that stores rows directly.
 
    .. note::
 
        The :class:`.AbstractConcreteBase` delays the mapper creation of the
        base class until all the subclasses have been defined,
        as it needs to create a mapping against a selectable that will include
        all subclass tables.  In order to achieve this, it waits for the
        **mapper configuration event** to occur, at which point it scans
        through all the configured subclasses and sets up a mapping that will
        query against all subclasses at once.
 
        While this event is normally invoked automatically, in the case of
        :class:`.AbstractConcreteBase`, it may be necessary to invoke it
        explicitly after **all** subclass mappings are defined, if the first
        operation is to be a query against this base class. To do so, once all
        the desired classes have been configured, the
        :meth:`_orm.registry.configure` method on the :class:`_orm.registry`
        in use can be invoked, which is available in relation to a particular
        declarative base class::
 
            Base.registry.configure()
 
    Example::
 
        from sqlalchemy.orm import DeclarativeBase
        from sqlalchemy.ext.declarative import AbstractConcreteBase
 
        class Base(DeclarativeBase):
            pass
 
        class Employee(AbstractConcreteBase, Base):
            pass
 
        class Manager(Employee):
            __tablename__ = 'manager'
            employee_id = Column(Integer, primary_key=True)
            name = Column(String(50))
            manager_data = Column(String(40))
 
            __mapper_args__ = {
                'polymorphic_identity':'manager',
                'concrete':True
            }
 
        Base.registry.configure()
 
    The abstract base class is handled by declarative in a special way;
    at class configuration time, it behaves like a declarative mixin
    or an ``__abstract__`` base class.   Once classes are configured
    and mappings are produced, it then gets mapped itself, but
    after all of its descendants.  This is a very unique system of mapping
    not found in any other SQLAlchemy API feature.
 
    Using this approach, we can specify columns and properties
    that will take place on mapped subclasses, in the way that
    we normally do as in :ref:`declarative_mixins`::
 
        from sqlalchemy.ext.declarative import AbstractConcreteBase
 
        class Company(Base):
            __tablename__ = 'company'
            id = Column(Integer, primary_key=True)
 
        class Employee(AbstractConcreteBase, Base):
            strict_attrs = True
 
            employee_id = Column(Integer, primary_key=True)
 
            @declared_attr
            def company_id(cls):
                return Column(ForeignKey('company.id'))
 
            @declared_attr
            def company(cls):
                return relationship("Company")
 
        class Manager(Employee):
            __tablename__ = 'manager'
 
            name = Column(String(50))
            manager_data = Column(String(40))
 
            __mapper_args__ = {
                'polymorphic_identity':'manager',
                'concrete':True
            }
 
        Base.registry.configure()
 
    When we make use of our mappings however, both ``Manager`` and
    ``Employee`` will have an independently usable ``.company`` attribute::
 
        session.execute(
            select(Employee).filter(Employee.company.has(id=5))
        )
 
    :param strict_attrs: when specified on the base class, "strict" attribute
     mode is enabled which attempts to limit ORM mapped attributes on the
     base class to only those that are immediately present, while still
     preserving "polymorphic" loading behavior.
 
     .. versionadded:: 2.0
 
    .. seealso::
 
        :class:`.ConcreteBase`
 
        :ref:`concrete_inheritance`
 
        :ref:`abstract_concrete_base`
 
    TcCs | ¡dSr)Ú_sa_decl_prepare_nocascade©rrrrr&ýsz&AbstractConcreteBase.__declare_first__cs”t|ddƒrdSt |¡}g}t| ¡ƒ}|r`| ¡}| | ¡¡t|ƒ}|dk    r*| |¡q*t|ddƒpnd‰|     |ˆ¡‰t
|j ƒ}dd„|Dƒ‰t|j   ¡ƒD].\}}||kr¢ˆj|j|j |<ˆ |j¡q¢ˆ|_|j dd¡‰|jpît‰‡‡‡‡‡fdd    „}    |    |_| ¡|g}|r| d
¡}
| |
 ¡¡t|
ƒ} | r| jr| jdkr|
jd d…D]$} t| ƒ} | rf|  | ¡qqfqdS) Nr rrcSsh|]
}|j’qSr©Úkey)rr$rrrÚ    <setcomp>!szBAbstractConcreteBase._sa_decl_prepare_nocascade.<locals>.<setcomp>Ú strict_attrsFcsHˆƒ}ˆjˆ|d<d|d<ˆrDtˆjƒˆBˆhB|d<dˆf|d<|S)NZpolymorphic_onTZpolymorphic_abstractZinclude_propertiesrr!)r$ÚsetZ primary_key)Úargs©Zdeclared_col_keysrZm_argsrr2rrÚ mapper_args-sÿþÿ zDAbstractConcreteBase._sa_decl_prepare_nocascade.<locals>.mapper_argsré)r"r
Zconfig_for_clsr#Ú__subclasses__ÚpopÚextendrÚappendrr3Zdeclared_columnsZ
propertiesÚitemsr$r0ÚremoverÚ__dict__ÚgetZmapper_args_fnÚdictÚmapZconcreteZinheritsÚ__mro__Z_set_concrete_base)rÚto_maprÚstackÚklassÚmnZ declared_colsÚkÚvr6ZsclsÚsmZsup_Zsup_smrr5rr-sJ 
  ÿ 
 
 
 
z/AbstractConcreteBase._sa_decl_prepare_nocascadecCstj|dt |¡d‚dS)NzíClass %s is a subclass of AbstractConcreteBase and has a mapping pending until all subclasses are defined. Call the sqlalchemy.orm.configure_mappers() function after all subclasses have been defined to complete the mapping of this class.©Úmsg©Úorm_excZUnmappedClassErrorZ_safe_cls_namer.rrrÚ_sa_raise_deferred_configKs ûþz.AbstractConcreteBase._sa_raise_deferred_configN)    r'r(r)r*Z __no_table__r+r&r-rNrrrrr,xs
 
Ir,c@s@eZdZdZedd„ƒZedddœdd„ƒZd    Zed
d „ƒZd S) ÚDeferredReflectionaN
A helper class for construction of mappings based on
    a deferred reflection step.
 
    Normally, declarative can be used with reflection by
    setting a :class:`_schema.Table` object using autoload_with=engine
    as the ``__table__`` attribute on a declarative class.
    The caveat is that the :class:`_schema.Table` must be fully
    reflected, or at the very least have a primary key column,
    at the point at which a normal declarative mapping is
    constructed, meaning the :class:`_engine.Engine` must be available
    at class declaration time.
 
    The :class:`.DeferredReflection` mixin moves the construction
    of mappers to be at a later point, after a specific
    method is called which first reflects all :class:`_schema.Table`
    objects created so far.   Classes can define it as such::
 
        from sqlalchemy.ext.declarative import declarative_base
        from sqlalchemy.ext.declarative import DeferredReflection
        Base = declarative_base()
 
        class MyClass(DeferredReflection, Base):
            __tablename__ = 'mytable'
 
    Above, ``MyClass`` is not yet mapped.   After a series of
    classes have been defined in the above fashion, all tables
    can be reflected and mappings created using
    :meth:`.prepare`::
 
        engine = create_engine("someengine://...")
        DeferredReflection.prepare(engine)
 
    The :class:`.DeferredReflection` mixin can be applied to individual
    classes, used as the base for the declarative base itself,
    or used in a custom abstract class.   Using an abstract base
    allows that only a subset of classes to be prepared for a
    particular prepare step, which is necessary for applications
    that use more than one engine.  For example, if an application
    has two engines, you might use two bases, and prepare each
    separately, e.g.::
 
        class ReflectedOne(DeferredReflection, Base):
            __abstract__ = True
 
        class ReflectedTwo(DeferredReflection, Base):
            __abstract__ = True
 
        class MyClass(ReflectedOne):
            __tablename__ = 'mytable'
 
        class MyOtherClass(ReflectedOne):
            __tablename__ = 'myothertable'
 
        class YetAnotherClass(ReflectedTwo):
            __tablename__ = 'yetanothertable'
 
        # ... etc.
 
    Above, the class hierarchies for ``ReflectedOne`` and
    ``ReflectedTwo`` can be configured separately::
 
        ReflectedOne.prepare(engine_one)
        ReflectedTwo.prepare(engine_two)
 
    .. seealso::
 
        :ref:`orm_declarative_reflected_deferred_reflection` - in the
        :ref:`orm_declarative_table_config_toplevel` section.
 
    c    Csžt |¡}t t¡}|D],}|jdk    r||jj|jjf |jj    ¡q| 
¡D}|  ¡D] \\}}}|j |||dddqZ|  ¡|D]Ú}| ¡|jj}    |    jj}|    j ¡D]²}
t|
tjƒr®|
jj ¡r®|
jj} t| jtƒr| j} || j| jf | j    ¡q®t| jtƒr®t|
jj|
ƒ\} }|| jdƒ}|||jjf | j¡|j|  |¡f7_|ƒ| _q®qˆ|  ¡D]"\\}}}|j |||dddqlW5QRXdS)zjReflect all :class:`_schema.Table` objects for all current
        :class:`.DeferredReflection` subclassesNTF)ÚonlyÚschemaZextend_existingZautoload_replace)!r
Zclasses_for_baseÚ collectionsÚ defaultdictr3rÚmetadatarQÚaddÚnameÚconnectr<ZreflectÚclearrArr Úclass_Z_propsÚvaluesÚ
isinstancerZRelationshipPropertyZ
_init_argsZ    secondaryZ _is_populatedZargumentr Ústrr    ÚparentZ
_resolversÚ_sa_deferred_table_resolver)rZenginerCZmetadata_to_tableZthingyÚconnrTrQZ table_namesZmapperÚrelZ secondary_argZsecondary_tableÚ_Z resolve_argÚresolverrrrÚprepare sz
 
 
ÿþ û
ÿ
þþÿû ÿ
ÿþÿ ûzDeferredReflection.preparerzCallable[[str], Table])rTÚreturncsdddœ‡fdd„ }|S)Nr\r )r0rdcs
t|ˆƒSrr r/©rTrrÚ_resolveõsz@DeferredReflection._sa_deferred_table_resolver.<locals>._resolver)rrTrfrrerr^ñsz.DeferredReflection._sa_deferred_table_resolverTcCstj|dt |¡d‚dS)Nz†Class %s is a subclass of DeferredReflection.  Mappings are not produced until the .prepare() method is called on the class hierarchy.rJrLr.rrrrNþs ýþz,DeferredReflection._sa_raise_deferred_configN)    r'r(r)r*r+rcr^Z_sa_decl_preparerNrrrrrOXsG
P
rO)r*Ú
__future__rrRÚtypingrrZormrrMrZorm.baserZorm.clsregistryr    Z orm.decl_baser
Zorm.utilr rQr ÚutilrZ
sql.schemarrr,rOrrrrÚ<module>
s$            Za