U aL@sdZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z d d lmZd dlmZd dlmZd dlmZdZdZdZedZedZGdddeZGdddeZddZddZddZddZdd Zd!d"Z d#d$Z!d%d&Z"Gd'd(d(ej#j$Z%Gd)d*d*ej#j$Z&e'd+d,d-Z(Gd.d/d/eZ)Gd0d1d1eZ*Gd2d3d3eZ+Gd4d5d5eZ,Gd6d7d7e+Z-Gd8d9d9e.Z/Gd:d;d;e0e/Z1Gdd?d?e eZ3Gd@dAdAej4e+Z5GdBdCdCe6Z7GdDdEdEeZ8GdFdGdGe Z9GdHdIdIeZ:GdJdKdKe:Z;GdLdMdMejZ?dVdPdQZ@dRdSZAdTdUZBdS)Wz4Foundational utilities common to many sql modules. N)roles)visitors) HasCacheKey)HasCopyInternals)MemoizedHasCacheKey) ClauseVisitor)ExtendedInternalTraversal)InternalTraversal)exc)util) HasMemoized) hybridmethodPARSE_AUTOCOMMITNO_ARGc@s0eZdZdZddZddZddZdd Zd S) Immutablez@mark a ClauseElement as 'immutable' when expressions are cloned.cOs tddSNz(Immutable objects do not support copyingNotImplementedErrorselfZ optionaldictkwargsrTC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\sql\base.py unique_params*szImmutable.unique_paramscOs tddSrrrrrrparams-szImmutable.paramscKs|SNrrkwrrr_clone0szImmutable._clonecKsdSrrrrrr_copy_internals3szImmutable._copy_internalsN)__name__ __module__ __qualname____doc__rrr r!rrrrr's rc@s*eZdZdZddZeddZeZdS)SingletonConstantz.Represent SQL constants like NULL, TRUE, FALSEcOs|jSr) _singleton)clsargrrrr__new__:szSingletonConstant.__new__cCst|}|||_dSr)objectr*__init__r')r(objrrr_create_singleton=s z#SingletonConstant._create_singletonN) r"r#r$r%r* classmethodr. frozenset proxy_setrrrrr&7s  r&cGstjdd|DS)NcSsg|] }|jqSr) _from_objects).0elementrrr Msz!_from_objects.. itertoolschain from_iterableelementsrrrr2Ks r2cCstjdd|DS)zXexpand tables into individual columns in the given list of column expressions. cSsg|] }|jqSr)Z_select_iterabler3crrrr5Wsz%_select_iterables..r6r:rrr_select_iterablesQs r>cCs tjdd}||}||_|S)znon-caching _generative() decorator. This is basically the legacy decorator that copies the object and runs a method on the new copy. c_s,|}||f||}|dks(td|S)zMark a method as generative.Nz,generative methods must have no return value) _generateAssertionError)fnrargsrxrrr _generativecsz _generative.._generative)r decoratorZnon_generative)rArDZ decoratedrrrrD[s  rDcsB|di|difdd|Dtjfdd}|S)Nmsgsdefaultscs$g|]}|t||dfqSr)operator attrgetterget)r3name)rGrrr5vsz&_exclusive_against..csND]8\}}}|||k r|d|j|jf}t|q||f||S)Nz9Method %s() has already been invoked on this %s construct)rJr" __class__r InvalidRequestError)rArrBrrKgetterZdefault_msg)gettersrFrrcheck{s   z!_exclusive_against..check)popr rE)namesrrQr)rGrPrFr_exclusive_againstqs    rTcKs |jf|Sr)r )r4rrrrr sr cCstjdd|DS)z\expand the given set of ClauseElements to be the set of all 'cloned' predecessors. cSsg|] }|jqSr) _cloned_setr3rCrrrr5sz"_expand_cloned..)r7r8r:rrr_expand_clonedsrWcs,tt|t|tfdd|DS)zreturn the intersection of sets a and b, counting any overlap between 'cloned' predecessors. The returned set is in terms of the entities present within 'a'. c3s|]}|jr|VqdSr intersectionrUr3elemZ all_overlaprr s z'_cloned_intersection..setrWrYabrr\r_cloned_intersections rccs,tt|t|tfdd|DS)Nc3s|]}|js|VqdSrrXrZr\rrr]s z%_cloned_difference..r^r`rr\r_cloned_differences rdc@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)_DialectArgViewzaA dictionary view of dialect-level arguments in the form _. cCs ||_dSr)r-)rr-rrrr,sz_DialectArgView.__init__c CsVz|dd\}}Wn4tk rH}ztjt||dW5d}~XYn X||fSdS)N_rZreplace_context)split ValueErrorr raise_KeyError)rkeydialect value_keyerrrrr_keys $z_DialectArgView._keyc Csb||\}}z|jj|}Wn6tjk rT}ztjt||dW5d}~XYn X||SdSNrg)rpr-dialect_optionsr NoSuchModuleErrorr rjrk)rrlrmrnoptrorrr __getitem__s $z_DialectArgView.__getitem__c Cs^z||\}}Wn6tk rH}ztjtd|dW5d}~XYnX||jj||<dS)Nz0Keys must be of the form _rg)rprkr rjr ArgumentErrorr-rr)rrlvaluermrnrorrr __setitem__sz_DialectArgView.__setitem__cCs ||\}}|jj||=dSr)rpr-rr)rrlrmrnrrr __delitem__sz_DialectArgView.__delitem__cCstdd|jjDS)Ncss|]}t|jVqdSr)len _non_defaults)r3rBrrrr]sz*_DialectArgView.__len__..)sumr-rrvaluesrrrr__len__s z_DialectArgView.__len__csfddjjDS)Nc3s.|]&}jj|jD]}d||fVqqdS)z%s_%sN)r-rrr{)r3 dialect_nameZ value_namer~rrr]s z+_DialectArgView.__iter__..)r-rrr~rr~r__iter__s z_DialectArgView.__iter__N) r"r#r$r%r,rprurxryrrrrrrres  rec@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{ _defaultsr~rrrr,sz_DialectArgDict.__init__cCstt|j|jSr)rzr_r{unionrr~rrrrsz_DialectArgDict.__len__cCstt|j|jSr)iterr_r{rrr~rrrrsz_DialectArgDict.__iter__cCs"||jkr|j|S|j|SdSrrrrlrrrrus  z_DialectArgDict.__getitem__cCs||j|<dSrr{rrlrwrrrrxsz_DialectArgDict.__setitem__cCs |j|=dSrrrrrrrysz_DialectArgDict.__delitem__N) r"r#r$r%r,rrrurxryrrrrrsrzsqlalchemy.dialectscCs(tjjj|}|jdkrdSt|jSr)r Z preloadedZdialectsregistryloadZconstruct_argumentsdict)rZ dialect_clsrrr_kw_reg_for_dialects rc@sjeZdZdZdejfgZeddZe j ddZ e ddZ e eZd d Ze j d d Zd dZdS) 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` rrcCs@tj|}|dkr td|||kr0i||<||||<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)r _kw_registryr rv)r(rZ 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 )rer~rrrdialect_kwargsQszDialectKWArgs.dialect_kwargscCs|jS)z4A synonym for :attr:`.DialectKWArgs.dialect_kwargs`.)rr~rrrrkszDialectKWArgs.kwargscCsXtj|}t}|dkr*|jddin*t|jjD]}||kr6|j||q6|S)N*)rrrrupdatereversedrL__mro__)rrrdr(rrr_kw_reg_for_dialect_clsrs 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 PopulateDictZportable_instancemethodrr~rrrrr~s zDialectKWArgs.dialect_optionsc Cs|sdS|D]}td|}|s,td||dd\}}z|j|}WnVtjk rtd||ft |j|<}|j ddi|||j |<Yq Xd|kr||krt d|||jfq ||||<q dS)Nz ^(.+?)_(.+)$zGAdditional arguments should be named _, got '%s'rr zHCan't validate argument %r; can't locate any SQLAlchemy dialect named %rrz9Argument %r is not accepted by dialect %r on behalf of %r)rematch TypeErrorgrouprrr rsr warnrrrr{rvrL)rrkmrZarg_namerrrrr_validate_dialect_kwargss@  z&DialectKWArgs._validate_dialect_kwargsN)r"r#r$r%r Zdp_dialect_optionsZ"_dialect_kwargs_traverse_internalsr/rr memoized_propertyrpropertyrrrrrrrrrrrrrs  1     rc@sPeZdZdZdZiZeddZddZeddZ ed d Z ed d Z d S) CompileStateaProduces additional object state necessary for a statement to be compiled. the :class:`.CompileState` class is at the base of classes that assemble state for a particular statement object that is then used by the compiler. This process is essentially an extension of the process that the SQLCompiler.visit_XYZ() method takes, however there is an emphasis on converting raw user intent into more organized structures rather than producing string output. The top-level :class:`.CompileState` for the statement being executed is also accessible when the execution context works with invoking the statement and collecting results. The production of :class:`.CompileState` is specific to the compiler, such as within the :meth:`.SQLCompiler.visit_insert`, :meth:`.SQLCompiler.visit_select` etc. methods. These methods are also responsible for associating the :class:`.CompileState` with the :class:`.SQLCompiler` itself, if the statement is the "toplevel" statement, i.e. the outermost SQL statement that's actually being executed. There can be other :class:`.CompileState` objects that are not the toplevel, such as when a SELECT subquery or CTE-nested INSERT/UPDATE/DELETE is generated. .. versionadded:: 1.4  statementcKs||jrB|jdd}|j||jfd}|dkrR|jd|jf}n|jd|jf}||krh|||f|S|j||f|SdSNZcompile_state_pluginr)_propagate_attrsrJplugins_effective_plugin_targetcreate_for_statement)r(rcompilerr plugin_nameklassrrrrs&z!CompileState.create_for_statementcKs ||_dSrr)rrrrrrrr,szCompileState.__init__cCs<|jdd}z|j||jfWStk r6YdSXdSr)rrJrrrkr(rrrrrget_plugin_classszCompileState.get_plugin_classcCs.z|j||jfWStk r(YdSXdSr)rrrkrrrr_get_plugin_class_for_plugins z)CompileState._get_plugin_class_for_plugincsfdd}|S)Ncs|jf<|Sr)r)Zcls_to_decorater(r visit_namerrdecoratesz)CompileState.plugin_for..decorater)r(rrrrrr plugin_forszCompileState.plugin_forN) r"r#r$r% __slots__rr/rr,rrrrrrrrs   rc@seZdZdZddZdS) GenerativezUProvide a method-chaining pattern in conjunction with the @_generative decorator.csF|j|j}||}r6fdd|jD|_n |j|_|S)Ncsi|]\}}|kr||qSrr)r3rvskiprr sz(Generative._generate..)_memoized_keysrLr*__dict__itemscopy)rr(srrrr?s    zGenerative._generateNr"r#r$r%r?rrrrrsrc@seZdZdZddZdS)InPlaceGenerativezkProvide a method-chaining pattern in conjunction with the @_generative decorator that mutates in place.cCs"|j}|D]}|j|dq |Sr)rrrR)rrrrrrr?,szInPlaceGenerative._generateNrrrrrr(src@s"eZdZdZdZeZej Z dS)HasCompileStatez=A class that has a :class:`.CompileState` associated with it.N) r"r#r$r%Z_compile_state_pluginr immutabledict _attributesrrZ_compile_state_factoryrrrrr3src@s eZdZdZddZddZdS) _MetaOptionsz metaclass for the Options class.cCs,ttdd|D|_t||||dS)Ncss$|]}|ds|dkr|VqdS)__)Z_cache_key_traversalN) startswith)r3rrrrr]Bs z(_MetaOptions.__init__..)tuplesorted _cache_attrstyper,)r( classnamebasesZdict_rrrr,@s z_MetaOptions.__init__cCsB|}t||jr2td|t||jf|j||SNzBdictionary contains attributes not covered by Options class %s: %r)r_ differencerrrrrotherZo1rrr__add__Ks z_MetaOptions.__add__N)r"r#r$r%r,rrrrrr=s rc@seZdZdZddZddZddZdd Zed d Z e d d Z e ddZ e Ze jddZ eddZeddZdS)Optionsz,A cacheable option dictionary with defaults.cKs|j|dSr)rrrrrrr,\szOptions.__init__cCsX|j|j}|j|jt||jrHtd|t||jf|j||Sr)rLr*rrr_rrrrrrrr_s zOptions.__add__cCs8t|j|jD]"\}}t||t||krdSqdSNFT)r zip_longestrgetattr)rrrarbrrr__eq__mszOptions.__eq__cs&djjdfddjDfS)N%s(%s), c3s*|]"}|jkrd|j|fVqdS)z%s=%rNr)r3rr~rrr]zs z#Options.__repr__..)rLr"joinrr~rr~r__repr__us zOptions.__repr__cCs t||Sr) issubclass)r(rrrr isinstanceszOptions.isinstancecCs||t|||iSr)r)rrKrwrrradd_to_elementszOptions.add_to_elementcCs|jSrrr~rrr _state_dictszOptions._state_dictcCs|jSr)_state_dict_const)r(rrrrscCsR|}||jk rJ|jrJt|j|jrJtd||t|j|jf||S)Nz]other element %r is not empty, is not of type %s, and contains attributes not covered here %r)rrLrr_rr)r(rrrrr safe_merges  zOptions.safe_mergec Cs|t||}|||}|ri}|D]6}d|} ||krN|||| <q,||kr,|||| <q,||} t||| i}| |fS||fSdS)a{process Options argument in terms of execution options. e.g.:: ( load_options, execution_options, ) = QueryContext.default_load_options.from_execution_options( "_sa_orm_load_options", { "populate_existing", "autoflush", "yield_per" }, execution_options, statement._execution_options, ) get back the Options and refresh "_sa_orm_load_options" in the exec options dict w/ the Options as well rfN)rYr_rrJr rZ merge_with) r(rlattrsZ exec_optionsZstatement_exec_optionsZcheck_argnamesZexisting_optionsresultargnamelocal new_optionsrrrfrom_execution_optionss&  zOptions.from_execution_optionsN)r"r#r$r%r,rrrr/rrrrr rr classlevelrrrrrrrYs$      rc@s2eZdZeddZejddZeddZdS)CacheableOptionscCst|||Sr)r_gen_cache_key)ranon_map bindparamsrrrrszCacheableOptions._gen_cache_keycCs|dfS)Nrr)r(rrrrrrscCs t|Sr)rZ_generate_cache_key_for_objectr~rrr_generate_cache_keysz$CacheableOptions._generate_cache_keyN)r"r#r$rrrrrrrrrs   rc@seZdZejZdZddZdS)ExecutableOptionZexecutable_optioncKs|j|j}t|j|_|S)z/Create a shallow copy of this ExecutableOption.)rLr*rr)rrr=rrrr s zExecutableOption._cloneN)r"r#r$r EMPTY_DICT _annotations__visit_name__r rrrrrsrc@seZdZdZdZeZdZdZ dZ de j fde jfde jfgZdZdZdZdZdZdZed d Zed d Zed dZeddZeddZeddZddZej dddddZ!ej dddddZ"eej d d!dd"d#d$Z#dS)% ExecutablezMark a :class:`_expression.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`. TNr _with_options_with_context_optionsrFcCs|jSr)rr~rrrrsz#Executable._effective_plugin_targetcGs |jtdd|D7_dS)aKApply options to this statement. In the general sense, options are any kind of Python object that can be interpreted by the SQL compiler for the statement. These options can be consumed by specific dialects or specific kinds of compilers. The most commonly known kind of option are the ORM level options that apply "eager load" and other loading behaviors to an ORM query. However, options can theoretically be used for many other purposes. For background on specific kinds of options for specific kinds of statements, refer to the documentation for those option objects. .. versionchanged:: 1.4 - added :meth:`.Generative.options` to Core statement objects towards the goal of allowing unified Core / ORM querying capabilities. .. seealso:: :ref:`deferred_options` - refers to options specific to the usage of ORM queries :ref:`relationship_loader_options` - refers to options specific to the usage of ORM queries css|]}ttj|VqdSr) coercionsexpectrZHasCacheKeyRole)r3rtrrrr]>sz%Executable.options..N)rrroptionsrrrr szExecutable.optionscCs ||_dS)z|Assign the compile options to a new value. :param compile_options: appropriate CacheableOptions structure NZ_compile_options)rZcompile_optionsrrr_set_compile_optionsBszExecutable._set_compile_optionscCs|j|7_dS)z*update the _compile_options with new keys.Nrrrrr_update_compile_optionsLsz"Executable._update_compile_optionscCs|j||ff7_dS)aMAdd a context option to this statement. These are callable functions that will be given the CompileState object upon compilation. A second argument cache_args is required, which will be combined with the ``__code__`` identity of the function itself in order to produce a cache key. N)r)rZ callable_Z cache_argsrrr_add_context_optionRs zExecutable._add_context_optioncKs6d|krtdd|kr$td|j||_dS)aSet non-SQL options for the statement which take effect during execution. Execution options can be set on a per-statement or per :class:`_engine.Connection` basis. Additionally, the :class:`_engine.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:`_engine.Connection.execution_options` for a full list of possible options. .. seealso:: :meth:`_engine.Connection.execution_options` :meth:`_query.Query.execution_options` :meth:`.Executable.get_execution_options` Zisolation_levelz'isolation_level' execution option may only be specified on Connection.execution_options(), or per-engine using the isolation_level argument to create_engine().Zcompiled_cachezm'compiled_cache' execution option may only be specified on Connection.execution_options(), not per statement.N)r rv_execution_optionsrrrrrexecution_options`s!zExecutable.execution_optionscCs|jS)zGet the non-SQL options which will take effect during execution. .. versionadded:: 1.3 .. seealso:: :meth:`.Executable.execution_options` )rr~rrrget_execution_optionss z Executable.get_execution_optionsz:meth:`.Executable.execute`zAll statement execution in SQLAlchemy 2.0 is performed by the :meth:`_engine.Connection.execute` method of :class:`_engine.Connection`, or in the ORM by the :meth:`.Session.execute` method of :class:`.Session`.) alternativecOsH|j}|dkr4t|ddp |jj}d|}t|||||tS)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.) bindrrLr"r UnboundExecutionErrorZ_execute_clauseelementr r)r multiparamsrelabelrOrrrexecutes  zExecutable.executez:meth:`.Executable.scalar`zScalar execution in SQLAlchemy 2.0 is performed by the :meth:`_engine.Connection.scalar` method of :class:`_engine.Connection`, or in the ORM by the :meth:`.Session.scalar` method of :class:`.Session`.cOs|j||S)znCompile and execute this :class:`.Executable`, returning the result's scalar representation. )r scalar)rrrrrrr s zExecutable.scalarz:attr:`.Executable.bind`z5Bound metadata is being removed as of SQLAlchemy 2.0.)rZenable_warningscCsB|jdk r|jSt|D]$}||kr&q|j}|dk r|SqdS)aKReturns the :class:`_engine.Engine` or :class:`_engine.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)_bindr2r)rfZenginerrrrs   zExecutable.bind)$r"r#r$r%Zsupports_executionr rrr rrr Zdp_executable_optionsr Zdp_with_context_optionsZdp_propagate_attrsZ_executable_traverse_internalsZ is_selectZ is_updateZ is_insertZis_textZ is_deleteZis_dmlrrrDrrrrrrZ deprecated_20r r rrrrrrs`   !    .   rc@seZdZdZddZdS)prefix_anon_mapafA map that creates new keys for missing key access. Considers keys of the form " " to produce new symbols "_", where "index" is an incrementing integer corresponding to . Inlines the approach taken by :class:`sqlalchemy.util.PopulateDict` which is otherwise usually used for this type of operation. cCsD|dd\}}||d}|d||<|dt|}|||<|S)N rrf)rhrJstr)rrlidentZderivedZanonymous_counterrwrrr __missing__s   zprefix_anon_map.__missing__N)r"r#r$r%rrrrrrs rc@s eZdZdZddZddZdS)SchemaEventTargetzBase class for elements that are the targets of :class:`.DDLEvents` events. This includes :class:`.SchemaItem` as well as :class:`.SchemaType`. cKsdS)z0Associate with this SchemaEvent's parent object.Nrrparentrrrr _set_parentszSchemaEventTarget._set_parentcKs.|j|||j|f||j||dSr)dispatchZbefore_parent_attachrZafter_parent_attachrrrr_set_parent_with_dispatchsz+SchemaEventTarget._set_parent_with_dispatchN)r"r#r$r%rrrrrrrsrc@seZdZdZddiZdS) SchemaVisitorz/Define the visiting for ``SchemaItem`` objects.Zschema_visitorTN)r"r#r$r%Z__traverse_options__rrrrrsrc@s eZdZdZdZd?ddZddZedd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZd@d d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/ZdZd0d1ZdAd2d3Zd4d5Zd6d7Z d8d9Z!d:d;Z"dBd=d>Z#dS)CColumnCollectionaCollection of :class:`_expression.ColumnElement` instances, typically for :class:`_sql.FromClause` objects. The :class:`_sql.ColumnCollection` object is most commonly available as the :attr:`_schema.Table.c` or :attr:`_schema.Table.columns` collection on the :class:`_schema.Table` object, introduced at :ref:`metadata_tables_and_columns`. The :class:`_expression.ColumnCollection` has both mapping- and sequence- like behaviors. A :class:`_expression.ColumnCollection` usually stores :class:`_schema.Column` objects, which are then accessible both via mapping style access as well as attribute access style. To access :class:`_schema.Column` objects using ordinary attribute-style access, specify the name like any other object attribute, such as below a column named ``employee_name`` is accessed:: >>> employee_table.c.employee_name To access columns that have names with special characters or spaces, index-style access is used, such as below which illustrates a column named ``employee ' payment`` is accessed:: >>> employee_table.c["employee ' payment"] As the :class:`_sql.ColumnCollection` object provides a Python dictionary interface, common dictionary method names like :meth:`_sql.ColumnCollection.keys`, :meth:`_sql.ColumnCollection.values`, and :meth:`_sql.ColumnCollection.items` are available, which means that database columns that are keyed under these names also need to use indexed access:: >>> employee_table.c["values"] The name for which a :class:`_schema.Column` would be present is normally that of the :paramref:`_schema.Column.key` parameter. In some contexts, such as a :class:`_sql.Select` object that uses a label style set using the :meth:`_sql.Select.set_label_style` method, a column of a certain key may instead be represented under a particular label name such as ``tablename_columnname``:: >>> from sqlalchemy import select, column, table >>> from sqlalchemy import LABEL_STYLE_TABLENAME_PLUS_COL >>> t = table("t", column("c")) >>> stmt = select(t).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) >>> subq = stmt.subquery() >>> subq.c.t_c :class:`.ColumnCollection` also indexes the columns in order and allows them to be accessible by their integer position:: >>> cc[0] Column('x', Integer(), table=None) >>> cc[1] Column('y', Integer(), table=None) .. versionadded:: 1.4 :class:`_expression.ColumnCollection` allows integer-based index access to the collection. Iterating the collection yields the column expressions in order:: >>> list(cc) [Column('x', Integer(), table=None), Column('y', Integer(), table=None)] The base :class:`_expression.ColumnCollection` object can store duplicates, which can mean either two columns with the same key, in which case the column returned by key access is **arbitrary**:: >>> x1, x2 = Column('x', Integer), Column('x', Integer) >>> cc = ColumnCollection(columns=[(x1.name, x1), (x2.name, x2)]) >>> list(cc) [Column('x', Integer(), table=None), Column('x', Integer(), table=None)] >>> cc['x'] is x1 False >>> cc['x'] is x2 True Or it can also mean the same column multiple times. These cases are supported as :class:`_expression.ColumnCollection` is used to represent the columns in a SELECT statement which may include duplicates. A special subclass :class:`.DedupeColumnCollection` exists which instead maintains SQLAlchemy's older behavior of not allowing duplicates; this collection is used for schema level objects like :class:`_schema.Table` and :class:`.PrimaryKeyConstraint` where this deduping is helpful. The :class:`.DedupeColumnCollection` class also has additional mutation methods as the schema constructs have more use cases that require removal and replacement of columns. .. versionchanged:: 1.4 :class:`_expression.ColumnCollection` now stores duplicate column keys as well as the same column in multiple positions. The :class:`.DedupeColumnCollection` class is added to maintain the former behavior in those cases where deduplication as well as additional replace/remove operations are needed. ) _collection_index_colsetNcCs>t|dtt|dit|dg|r:||dS)Nrrr)r+ __setattr__r__initial_populate)rcolumnsrrrr,zs zColumnCollection.__init__cCs||dSr_populate_separate_keysriter_rrrrsz"ColumnCollection._initial_populatecCsdd|jDS)NcSsg|] \}}|qSrrr3rcolrrrr5sz1ColumnCollection._all_columns..rr~rrr _all_columnsszColumnCollection._all_columnscCsdd|jDS)zQReturn a sequence of string key names for all columns in this collection.cSsg|] \}}|qSrrr%rrrr5sz)ColumnCollection.keys..r'r~rrrkeysszColumnCollection.keyscCsdd|jDS)zReturn a sequence of :class:`_sql.ColumnClause` or :class:`_schema.Column` objects for all columns in this collection.cSsg|] \}}|qSrrr%rrrr5sz+ColumnCollection.values..r'r~rrrr}szColumnCollection.valuescCs t|jS)zReturn a sequence of (key, column) tuples for all columns in this collection each consisting of a string key name and a :class:`_sql.ColumnClause` or :class:`_schema.Column` object. )listrr~rrrrszColumnCollection.itemscCs t|jSr)boolrr~rrr__bool__szColumnCollection.__bool__cCs t|jSr)rzrr~rrrrszColumnCollection.__len__cCstdd|jDS)NcSsg|] \}}|qSrrr%rrrr5sz-ColumnCollection.__iter__..)rrr~rrrrszColumnCollection.__iter__c CsVz |j|WStk rP}z&t|tjr>tjt||dnW5d}~XYnXdSrq)rrkrr int_typesrj IndexErrorrrlrorrrrus   zColumnCollection.__getitem__c CsFz |j|WStk r@}ztjt||dW5d}~XYnXdSrq)rrkr rjAttributeErrorr/rrr __getattr__s zColumnCollection.__getattr__cCs,||jkr$t|tjs tddSdSdS)Nz'__contains__ requires a string argumentFT)rrr string_typesr rvrrrr __contains__s  zColumnCollection.__contains__cCs(t||D]\}}||k r dSq dS)zdCompare this :class:`_expression.ColumnCollection` to another based on the names of the keysFTN)r r)rrlrrrrcompareszColumnCollection.comparecCs ||Sr)r6rrrrrrszColumnCollection.__eq__cCs||jkr|j|S|SdS)zGet a :class:`_sql.ColumnClause` or :class:`_schema.Column` object based on a string key name from this :class:`_expression.ColumnCollection`.N)r)rrlrrrrrJs  zColumnCollection.getcCs d|jjddd|DfS)Nrrcss|]}t|VqdSr)rr<rrrr]sz+ColumnCollection.__str__..)rLr"rr~rrr__str__szColumnCollection.__str__cCs tdSrrrrrrrxszColumnCollection.__setitem__cCs tdSrrrrrrryszColumnCollection.__delitem__cCs tdSrr)rrlr-rrrrszColumnCollection.__setattr__cCs tdS)zQDictionary clear() is not implemented for :class:`_sql.ColumnCollection`.Nrr~rrrclearszColumnCollection.clearcCs tdS)zRDictionary remove() is not implemented for :class:`_sql.ColumnCollection`.NrrcolumnrrrremoveszColumnCollection.removecCs tdS)zRDictionary update() is not implemented for :class:`_sql.ColumnCollection`.Nrr#rrrrszColumnCollection.updatecCsjt|}||jdd<|jdd|jD|jddt|jD|jddt|jDdS)*populate from an iterator of (key, column)Ncss|]\}}|VqdSrrr3rr=rrrr]sz;ColumnCollection._populate_separate_keys..css|]\}\}}||fVqdSrrr3idxrr=rrrr]s cSsi|]\}}||qSrrr%rrrrsz.)r*rrrr enumerater)rr$colsrrrr"s z(ColumnCollection._populate_separate_keyscCsV|dkr|j}t|j}|j||f|j|||j|<||jkrR||j|<dS)aAdd a column to this :class:`_sql.ColumnCollection`. .. note:: This method is **not normally used by user-facing code**, as the :class:`_sql.ColumnCollection` is usually part of an existing object such as a :class:`_schema.Table`. To add a :class:`_schema.Column` to an existing :class:`_schema.Table` object, use the :meth:`_schema.Table.append_column` method. N)rlrzrappendraddr)rr;rlr4rrrrDs     zColumnCollection.addcCs|j|jdS)NrrrEr~rrr __getstate__szColumnCollection.__getstate__cCsBt|d|dt|d|dt|ddd|jDdS)NrrrcSsh|] \}}|qSrrr%rrr sz0ColumnCollection.__setstate__..)r+rr)rstaterrr __setstate__szColumnCollection.__setstate__cCs,||jkr$t|tjr tddSdSdS)z3Checks if a column object exists in this collectionzZcontains_column cannot be used with string arguments. Use ``col_name in table.c`` instead.FTN)rrr r2r rvrr&rrrcontains_column s  z ColumnCollection.contains_columncCst|S)zJReturn an "immutable" form of this :class:`_sql.ColumnCollection`.)ImmutableColumnCollectionr~rrr as_immutable,szColumnCollection.as_immutableFc sdd}|jkrSd\}}j}dd|jD}|D]}tt|j} || } | r8|rf|| |r8|dkrz|| }}q8t| t|kr|| }}q8| |kr8tt j fdd| D} tt j fdd| D} | | kr8|| }}q8|S) aGiven a :class:`_expression.ColumnElement`, return the exported :class:`_expression.ColumnElement` object from this :class:`_expression.ColumnCollection` which corresponds to that original :class:`_expression.ColumnElement` via a common ancestor column. :param column: the target :class:`_expression.ColumnElement` to be matched. :param require_embedded: only return corresponding columns for the given :class:`_expression.ColumnElement`, if the given :class:`_expression.ColumnElement` is actually present within a sub-element of this :class:`_expression.Selectable`. Normally the column will match if it merely shares a common ancestor with one of the exported columns of this :class:`_expression.Selectable`. .. seealso:: :meth:`_expression.Selectable.corresponding_column` - invokes this method against the collection returned by :attr:`_expression.Selectable.exported_columns`. .. versionchanged:: 1.4 the implementation for ``corresponding_column`` was moved onto the :class:`_expression.ColumnCollection` itself. cSs.||D]}tt|g|s dSq dSr)rr_rWrY)expanded_proxy_set target_settrrrembeddedRs z7ColumnCollection.corresponding_column..embedded)NNcSsg|] \}}|qSrrr>rrrr5_sz9ColumnCollection.corresponding_column..Ncs$g|]}|r|jddqSZweightrshares_lineagerrJr3Zscr;rrr5s cs$g|]}|r|jddqSrRrSrUrVrrr5s ) rr1rr_rWrYrzr reducerHrDZ_uncached_proxy_set) rr;Zrequire_embeddedrQr&Z intersectrOrBr=rNiZ col_distanceZ c_distancerrVrcorresponding_column2sD         z%ColumnCollection.corresponding_column)N)N)N)F)$r"r#r$r%rr,rrr(r)r}rr,rrrur1r3r6rrJr8rxryrr9r<r__hash__r"rDrFrIrKrMrYrrrrr sBl         rc@s:eZdZdZd ddZddZddZd d Zd d ZdS)DedupeColumnCollectionauA :class:`_expression.ColumnCollection` that maintains deduplicating behavior. This is useful by schema level objects such as :class:`_schema.Table` and :class:`.PrimaryKeyConstraint`. The collection includes more sophisticated mutator methods as well to suit schema objects which require mutable column collections. .. versionadded:: 1.4 NcCs|dk r|j|krtd|j}|dkr4td||jkrn|j|}||krTdS||tj|dn:t|j }|j ||f|j |||j|<||j|<dS)NKDedupeColumnCollection requires columns be under the same key as their .keyz-Can't add unnamed column to column collectionr1) rlr rvrreplacer rresetrzrrCrrD)rr;rlexistingr4rrrrDs(      zDedupeColumnCollection.addcCst|}g}|D]r\}}|j|kr,td|j|jkrP|j|jkrP||q|j|jkrh||q||j|<|j||fq|j dd|jD|j ddt |jD|D]}| |qdS)r=r\css|]\}}|VqdSrrr>rrrr]szADedupeColumnCollection._populate_separate_keys..css|]\}\}}||fVqdSrrr?rrrr]s N) r*rlr rvrKrrCrrrrAr])rr$rBZ replace_colrr&rrrr"s&       z.DedupeColumnCollection._populate_separate_keyscCs|dd|DdS)Ncss|]}|j|fVqdSr)rl)r3r&rrrr]sz0DedupeColumnCollection.extend..r!r#rrrextendszDedupeColumnCollection.extendcsx|jkrtd|jj=|jfdd|jD|jdd<|jddt|jD|jt|j=dS)Nz8Can't remove column %r; column is not in this collectioncs g|]\}}|k r||fqSrrr>rVrrr5sz1DedupeColumnCollection.remove..cSsi|]\}\}}||qSrrr3r@rr&rrrrs z1DedupeColumnCollection.remove..) rrirrlr<rrrArzr:rrVrr<s    zDedupeColumnCollection.removecCst}|j|jkr@|j|jkr@|j|j}|j|jkr@|||j|jkr^||j|jg}d}|jD]8\}}||kr|sd}||j|fql|||fql|r|j||s||j|f|j|||jdd<|j |j ddt |jD|j |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. FTNcSsi|]\}\}}||qSrrrarrrr s z2DedupeColumnCollection.replace..) r_rKrrlrDrrCrdifference_updater9rrA)rr;Z remove_colrZnew_colsZreplacedrr&rrrr]s4       zDedupeColumnCollection.replace)N) r"r#r$r%rDr"r`r<r]rrrrr[s  "r[c@s8eZdZdZddZddZddZejj Z Z Z dS) rL_parentcCsBt|d|t|d|jt|d|jt|d|jdS)Nrdrrr)r+rrrr)rZ collectionrrrr,(sz"ImmutableColumnCollection.__init__cCs d|jiSNrdrcr~rrrrF.sz&ImmutableColumnCollection.__getstate__cCs|d}||dSre)r,)rrHrrrrrI1sz&ImmutableColumnCollection.__setstate__N) r"r#r$rr,rFrIr ImmutableContainerZ _immutablerDr`r<rrrrrL%s rLc@s4eZdZddZddZddZddZd d Zd S) ColumnSetcCs||kSrrrJrrrrK9szColumnSet.contains_columncCs|D]}||qdSr)rD)rrBr&rrrr`<szColumnSet.extendcCst|t|Sr)r*r7rrrr@szColumnSet.__add__cCs:g}|D]&}|D]}||r|||kqqtj|Sr)rTrCr;and_)rrr4r=rrrrrCs  zColumnSet.__eq__cCsttdd|DS)Ncss|] }|VqdSrrrVrrrr]Lsz%ColumnSet.__hash__..)hashrr~rrrrZKszColumnSet.__hash__N)r"r#r$rKr`rrrZrrrrrg8s rgcCshtd|j}|sd|jj}t|dt|dd}|rBd||f}nd|}|dkrZd|}t||S)Nz|The ``bind`` argument for schema methods that invoke SQL against an engine or connection will be required in SQLAlchemy 2.0.fullnamerKz %s object %rz %s objectzm%s is not bound to an Engine or Connection. Execution can not proceed without a database to execute against.)r Zwarn_deprecated_20rrLr"rr r)Z schemaitemrOrrKr itemrrr_bind_or_errorOs*  rlcCsLz|jWStk rFt|D]}t|dr"|jYSq"YnXdS)zReturn the nearest .entity_namespace for the given entity. If not immediately available, does an iterate to find a sub-element that has one, if any. entity_namespaceN)rmr0rZiteratehasattr)entityr[rrr_entity_namespaceis rpc CsXzt|}t||WStk rR}z tjtd||f|dW5d}~XYnXdS)zReturn an entry from an entity_namespace. Raises :class:`_exc.InvalidRequestError` rather than attribute error on not found. z.Entity namespace for "%s" has no property "%s"rgN)rprr0r rjr rM)rorlnsrorrr_entity_namespace_keyzs  rr)N)Cr%r7rHrrrZ traversalsrrrrr r r r rrrr;Ztype_apisymbolrrr+rr&r2r>rDrTr rWrcrdcollections_abcMutableMappingrerZpreload_modulerrrrrrrrwith_metaclassrrrZ StatementRolerrrrrrr[rfrLZordered_column_setrgrlrprrrrrrsz                <  0Z    c