o znhT@s0dZddlZddlZddlmZddlmZddlmZedZ ed Z Gd d d e Z d d Z ejddZGdddejjZGdddejjZGddde ZGddde ZGdddeZGddde ZGdddeZGdddejZGd d!d!ejeZGd"d#d#ejZd&d$d%ZdS)'z4Foundational utilities common to many sql modules. N) ClauseVisitor)exc)utilPARSE_AUTOCOMMITNO_ARGc@s(eZdZdZddZddZddZdS) Immutablez@mark a ClauseElement as 'immutable' when expressions are cloned.cOtdNz(Immutable objects do not support copyingNotImplementedErrorself optionaldictkwargsr>/usr/local/lib/python3.10/dist-packages/sqlalchemy/sql/base.py unique_paramszImmutable.unique_paramscOr r r rrrrparamsrzImmutable.paramscCs|SNrrrrr_clone"szImmutable._cloneN)__name__ __module__ __qualname____doc__rrrrrrrr s  r cGstjdd|DS)NcSsg|]}|jqSr) _from_objects).0elementrrr 'sz!_from_objects..) itertoolschain)elementsrrrr&srcOs.|d}||g|ddRi||S)zMark a method as generative.rrN) _generate)fnargskwrrrr _generative*s r)c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)_DialectArgViewzaA dictionary view of dialect-level arguments in the form _. cCs ||_dSr)obj)rr+rrr__init__9s z_DialectArgView.__init__cCs2z |dd\}}W||fStyt|w)N_r)split ValueErrorKeyErrorrkeydialect value_keyrrr_key<s  z_DialectArgView._keycCs>||\}}z |jj|}W||Stjyt|wr)r5r+dialect_optionsrNoSuchModuleErrorr0)rr2r3r4optrrr __getitem__Dsz_DialectArgView.__getitem__cCs@z ||\}}Wn tytdw||jj||<dS)Nz0Keys must be of the form _)r5r0r ArgumentErrorr+r6)rr2valuer3r4rrr __setitem__Ns z_DialectArgView.__setitem__cCs ||\}}|jj||=dSr)r5r+r6r1rrr __delitem__Xz_DialectArgView.__delitem__cCstdd|jjDS)Ncss|]}t|jVqdSr)len _non_defaults)rr'rrr ]s  z*_DialectArgView.__len__..)sumr+r6valuesrrrr__len__\s z_DialectArgView.__len__csfddjjDS)Nc3s6|]}jj|jD] }td||fVq qdS)z%s_%sN)r+r6r@r safe_kwarg)r dialect_name value_namerrrrAcs z+_DialectArgView.__iter__..)r+r6rrrr__iter__bs z_DialectArgView.__iter__N) rrrrr,r5r9r<r=rDrHrrrrr*3s   r*c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)_DialectArgDictzA dictionary view of dialect-level arguments for a specific dialect. Maintains a separate collection of user-specified arguments and dialect-specified default arguments. cCsi|_i|_dSrr@ _defaultsrrrrr,us z_DialectArgDict.__init__cCtt|j|jSr)r?setr@unionrKrrrrrDyz_DialectArgDict.__len__cCrLr)iterrMr@rNrKrrrrrH|rOz_DialectArgDict.__iter__cCs||jvr |j|S|j|SrrJrr2rrrr9s   z_DialectArgDict.__getitem__cCs||j|<dSrr@)rr2r;rrrr<z_DialectArgDict.__setitem__cCs |j|=dSrrRrQrrrr=s z_DialectArgDict.__delitem__N) rrrrr,rDrHr9r<r=rrrrrIls rIc@speZdZdZeddZejddZe ddZ e dd d Z e e Zd d Zejd dZddZdS) DialectKWArgsa9Establish the ability for a class to have dialect-specific arguments with defaults and constructor validation. The :class:`.DialectKWArgs` interacts with the :attr:`.DefaultDialect.construct_arguments` present on a dialect. .. seealso:: :attr:`.DefaultDialect.construct_arguments` cCs@tj|}|durtd|||vri||<||||<dS)aAdd a new kind of dialect-specific keyword argument for this class. E.g.:: Index.argument_for("mydialect", "length", None) some_index = Index('a', 'b', mydialect_length=5) The :meth:`.DialectKWArgs.argument_for` method is a per-argument way adding extra arguments to the :attr:`.DefaultDialect.construct_arguments` dictionary. This dictionary provides a list of argument names accepted by various schema-level constructs on behalf of a dialect. New dialects should typically specify this dictionary all at once as a data member of the dialect class. The use case for ad-hoc addition of argument names is typically for end-user code that is also using a custom compilation scheme which consumes the additional arguments. :param dialect_name: name of a dialect. The dialect must be locatable, else a :class:`.NoSuchModuleError` is raised. The dialect must also include an existing :attr:`.DefaultDialect.construct_arguments` collection, indicating that it participates in the keyword-argument validation and default system, else :class:`.ArgumentError` is raised. If the dialect does not include this collection, then any keyword argument can be specified on behalf of this dialect already. All dialects packaged within SQLAlchemy include this collection, however for third party dialects, support may vary. :param argument_name: name of the parameter. :param default: default value of the parameter. .. versionadded:: 0.9.4 NzRDialect '%s' does have keyword-argument validation and defaults enabled configured)rT _kw_registryrr:)clsrF argument_namedefaultconstruct_arg_dictionaryrrr argument_fors (zDialectKWArgs.argument_forcCst|S)a9A collection of keyword arguments specified as dialect-specific options to this construct. The arguments are present here in their original ``_`` format. Only arguments that were actually passed are included; unlike the :attr:`.DialectKWArgs.dialect_options` collection, which contains all options known by this dialect including defaults. The collection is also writable; keys are accepted of the form ``_`` where the value will be assembled into the list of options. .. versionadded:: 0.9.2 .. versionchanged:: 0.9.4 The :attr:`.DialectKWArgs.dialect_kwargs` collection is now writable. .. seealso:: :attr:`.DialectKWArgs.dialect_options` - nested dictionary form )r*rrrrdialect_kwargsszDialectKWArgs.dialect_kwargscC|jS)z4A synonym for :attr:`.DialectKWArgs.dialect_kwargs`.)r[rrrrrszDialectKWArgs.kwargszsqlalchemy.dialectscCs$|j|}|jdur dSt|jSr)registryloadconstruct_argumentsdict)dialectsrF dialect_clsrrr_kw_reg_for_dialects   z!DialectKWArgs._kw_reg_for_dialectcCsZtj|}t}|dur|jddi|St|jjD]}||vr*|j||q|S)N*)rTrUrIrKupdatereversed __class____mro__)rrFrYdrVrrr_kw_reg_for_dialect_clss z%DialectKWArgs._kw_reg_for_dialect_clscCstt|jS)aA collection of keyword arguments specified as dialect-specific options to this construct. This is a two-level nested registry, keyed to ```` and ````. For example, the ``postgresql_where`` argument would be locatable as:: arg = my_object.dialect_options['postgresql']['where'] .. versionadded:: 0.9.2 .. seealso:: :attr:`.DialectKWArgs.dialect_kwargs` - flat dictionary form )r PopulateDictportable_instancemethodrjrrrrr6s zDialectKWArgs.dialect_optionsc Cs|sdS|D]c}td|}|std||dd\}}z|j|}Wn*tjyOtd||ft |j|<}|j ddi|||j |<Yqwd|vrc||vrct d|||jf||||<qdS)Nz ^(.+?)_(.+)$zGAdditional arguments should be named _, got '%s'rrzHCan't validate argument %r; can't locate any SQLAlchemy dialect named %rrdz9Argument %r is not accepted by dialect %r on behalf of %r)rematch TypeErrorgroupr6rr7rwarnrIrKrer@r:rg)rrkmrFarg_namerYrirrr_validate_dialect_kwargss@   z&DialectKWArgs._validate_dialect_kwargsN)rrrr classmethodrZrmemoized_propertyr[propertyr dependenciesrcrkrUrjr6rurrrrrTs 1      rTc@seZdZdZddZdS) GenerativezRAllow a ClauseElement to generate itself via the @_generative decorator. cCs|j|j}|j|_|Sr)rg__new____dict__copy)rsrrrr%Ds zGenerative._generateN)rrrrr%rrrrrz>s rzc@sPeZdZdZdZeZdZe ddZ ddZ dd Z d d Z ed d ZdS) ExecutablezMark a ClauseElement as supporting execution. :class:`.Executable` is a superclass for all "statement" types of objects, including :func:`select`, :func:`delete`, :func:`update`, :func:`insert`, :func:`text`. TNcKs6d|vr tdd|vrtd|j||_dS)ac Set non-SQL options for the statement which take effect during execution. Execution options can be set on a per-statement or per :class:`.Connection` basis. Additionally, the :class:`.Engine` and ORM :class:`~.orm.query.Query` objects provide access to execution options which they in turn configure upon connections. The :meth:`execution_options` method is generative. A new instance of this statement is returned that contains the options:: statement = select([table.c.x, table.c.y]) statement = statement.execution_options(autocommit=True) Note that only a subset of possible execution options can be applied to a statement - these include "autocommit" and "stream_results", but not "isolation_level" or "compiled_cache". See :meth:`.Connection.execution_options` for a full list of possible options. .. seealso:: :meth:`.Connection.execution_options` :meth:`.Query.execution_options` :meth:`.Executable.get_execution_options` isolation_levelz'isolation_level' execution option may only be specified on Connection.execution_options(), or per-engine using the isolation_level argument to create_engine().compiled_cachezm'compiled_cache' execution option may only be specified on Connection.execution_options(), not per statement.N)rr:_execution_optionsrN)rr(rrrexecution_optionsWs zExecutable.execution_optionscCr\)z Get the non-SQL options which will take effect during execution. .. versionadded:: 1.3 .. seealso:: :meth:`.Executable.execution_options` )rrrrrget_execution_optionss z Executable.get_execution_optionscOs>|j}|durt|d|jj}d|}t|||||S)z.Compile and execute this :class:`.Executable`.N descriptionzThis %s is not directly bound to a Connection or Engine. Use the .execute() method of a Connection or Engine to execute this construct.)bindgetattrrgrrUnboundExecutionError_execute_clauseelement)r multiparamsrelabelmsgrrrexecutes zExecutable.executecOs|j|i|S)znCompile and execute this :class:`.Executable`, returning the result's scalar representation. )rscalar)rrrrrrrszExecutable.scalarcCsB|jdur|jSt|D]}||urq |j}|dur|Sq dS)a5Returns the :class:`.Engine` or :class:`.Connection` to which this :class:`.Executable` is bound, or None if none found. This is a traversal which checks locally, then checks among the "from" clauses of associated objects until a bound engine or connection is found. N)_bindrr)rfenginerrrrs  zExecutable.bind)rrrrsupports_executionr immutabledictrrr)rrrrrxrrrrrrJs -  rc@s eZdZdZddZddZdS)SchemaEventTargetzBase class for elements that are the targets of :class:`.DDLEvents` events. This includes :class:`.SchemaItem` as well as :class:`.SchemaType`. cCsdS)z0Associate with this SchemaEvent's parent object.Nrrparentrrr _set_parentszSchemaEventTarget._set_parentcCs*|j|||||j||dSr)dispatchbefore_parent_attachrafter_parent_attachrrrr_set_parent_with_dispatchs z+SchemaEventTarget._set_parent_with_dispatchN)rrrrrrrrrrrs rc@seZdZdZddiZdS) SchemaVisitorz/Define the visiting for ``SchemaItem`` objects.schema_visitorTN)rrrr__traverse_options__rrrrrs rcseZdZdZdZfddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZdZedddZddZdd Zd!d"Zd#d$Zd%d&ZZS)'ColumnCollectionzAn ordered dictionary that stores a list of ColumnElement instances. Overrides the ``__eq__()`` method to produce SQL clauses between sets of correlated columns. _all_columnscs4tt|t|dg|D]}||qdSNr)superrr,object __setattr__add)rcolumnscrgrrr,s  zColumnCollection.__init__cCstdd|DS)NcSsg|]}t|qSr)strrrrrrr!sz,ColumnCollection.__str__..)reprrrrr__str__szColumnCollection.__str__csdj|vrjjkr|j}|j|jkr||j|j=j|jvr+|jj|jj<durGfdd|jD|jdd<dS|jdS)aadd the given column to this collection, removing unaliased versions of this column as well as existing columns with the same key. e.g.:: t = Table('sometable', metadata, Column('col1', Integer)) t.columns.replace(Column('col1', Integer, key='columnone')) will remove the original 'col1' from the collection, and add the new column under the name 'columnname'. Used by schema.Column to override columns during table reflection. Ncsg|] }|ur n|qSrrrcolumn remove_colrrr!sz,ColumnCollection.replace..)namer2_datarappend)rrotherrrrreplaces       zColumnCollection.replacecCs|jstd|||j<dS)zAdd a column to this collection. The key attribute of the column will be used as the hash key for this dictionary. z-Can't add unnamed column to column collectionN)r2rr:rrrrrrs zColumnCollection.addcCtrr rQrrrr=zColumnCollection.__delitem__cCrrr )rr2r+rrrrrzColumnCollection.__setattr__cCsj||vr(||}||urdS||s!td|t|dd|ftj|d|j|||j|<dS)NzqColumn %r on table %r being replaced by %r, which has the same key. Consider use_labels for select() statements.table proxy_set) shares_lineagerrqrrwresetrrr)rr2r;existingrrrr<s  zColumnCollection.__setitem__cCrrr rrrrclear7rzColumnCollection.clearcs,|jj=fdd|jD|jdd<dS)Ncsg|]}|ur|qSrrrrrrr!<sz+ColumnCollection.remove..)rr2rrrrrremove:s  zColumnCollection.removecFt|}t|j|jfdd|D|jdd|DdS)Nc3s |] \}}|vr|VqdSrrrrr all_col_setrrrACsz*ColumnCollection.update..css|] \}}||fVqdSrrrrrrrAFlistrMrextendrreriter_colsrrrre@s  zColumnCollection.updatecr)Nc3s|] }|vr|VqdSrrrrrrrAKrz*ColumnCollection.extend..css|]}|j|fVqdSr)r2rrrrrALsrrrrrrHs zColumnCollection.extendNsqlalchemy.sql.elementscCsDg}t|d|D]}|jD]}||r|||kq q|j|Sr)rrrrand_rr$rlrlocalrrr__eq__Ps   zColumnCollection.__eq__cCs$t|tjs tdtj||S)Nz'__contains__ requires a string argument) isinstancer string_typesrr:OrderedProperties __contains__rrrrrrYs  zColumnCollection.__contains__cCs|j|jdS)Nrrrrrrr __getstate__^rSzColumnCollection.__getstate__cCs(t|d|dt|d|ddS)Nrr)rr)rstaterrr __setstate__aszColumnCollection.__setstate__cCs|t|jvSr)rMrrcolrrrcontains_columnerSz ColumnCollection.contains_columncCst|j|jSr)ImmutableColumnCollectionrrrrrr as_immutablehrSzColumnCollection.as_immutable)rrrr __slots__r,rrrr=rr<rrrer__hash__rryrrrrrr __classcell__rrrrrs, "  rc@s eZdZddZejjZZdS)rcCs tj||t|d|dSr)rImmutablePropertiesr,rr)rdata all_columnsrrrr,mr>z"ImmutableColumnCollection.__init__N) rrrr,rr _immutablerrrrrrrlsrc@s>eZdZddZddZddZeddd Zd d Z d S) ColumnSetcCs||vSrrrrrrrurzColumnSet.contains_columncCs|D]}||qdSr)r)rrrrrrrxs zColumnSet.extendcCst|t|Sr)rrrrr__add__|szColumnSet.__add__rcCs:g}|D]}|D]}||r|||kqq|j|Sr)rrrrrrrrs  zColumnSet.__eq__cCsttdd|DS)Ncss|]}|VqdSrr)rxrrrrAsz%ColumnSet.__hash__..)hashtuplerrrrrrOzColumnSet.__hash__N) rrrrrrrryrrrrrrrts  rcCs^|j}|s-|jj}t|dt|dd}|rd||f}nd|}|dur(d|}t||S)Nfullnamerz %s object %rz %s objectzm%s is not bound to an Engine or Connection. Execution can not proceed without a database to execute against.)rrgrrrr) schemaitemrrrritemrrr_bind_or_errors  rr) rr"rmvisitorsrrrsymbolrrrr r decoratorr)collections_abcMutableMappingr*rIrTrzrrrrrrrordered_column_setrrrrrrs2       9 3 q