o
    y¬´\š  ã                   @   s²   d dl mZ ddl mZ ddlmZ ddlmZ ddlmZ	 ddl
mZ ddlmZ dd	lmZ d
ZG dd„ de	ƒZeedƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é   )Úexté   )Úutil)Úschema)Ú_generative)ÚInsert)ÚClauseElement)Úalias)Úpublic_factory)r   Úinsertc                   @   sF   e Zd ZdZejdd„ ƒZe					d	dd„ƒZe	d
dd„ƒZ	dS )r   zŽPostgreSQL-specific implementation of INSERT.

    Adds methods for PG-specific syntaxes such as ON CONFLICT.

    .. versionadded:: 1.1

    c                 C   s   t | jdd�jS )a�  Provide the ``excluded`` namespace for an ON CONFLICT statement

        PG's ON CONFLICT clause allows reference to the row that would
        be inserted, known as ``excluded``.  This attribute provides
        all columns in this row to be referenceable.

        .. seealso::

            :ref:`postgresql_insert_on_conflict` - example of how
            to use :attr:`.Insert.excluded`

        Úexcluded)Úname)r	   ÚtableÚcolumns)Úself© r   úT/var/www/html/env/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/dml.pyr      s   zInsert.excludedNc                 C   s   t |||||ƒ| _| S )a  
        Specifies a DO UPDATE SET action for ON CONFLICT clause.

        Either the ``constraint`` or ``index_elements`` argument is
        required, but only one of these can be specified.

        :param constraint:
         The name of a unique or exclusion constraint on the table,
         or the constraint object itself if it has a .name attribute.

        :param index_elements:
         A sequence consisting of string column names, :class:`.Column`
         objects, or other column expression objects that will be used
         to infer a target index.

        :param index_where:
         Additional WHERE criterion that can be used to infer a
         conditional target index.

        :param set\_:
         Required argument. A dictionary or other mapping object
         with column names as keys and expressions or literals as values,
         specifying the ``SET`` actions to take.
         If the target :class:`.Column` specifies a ".key" attribute distinct
         from the column name, that key should be used.

         .. warning:: This dictionary does **not** take into account
            Python-specified default UPDATE values or generation functions,
            e.g. those specified using :paramref:`.Column.onupdate`.
            These values will not be exercised for an ON CONFLICT style of
            UPDATE, unless they are manually specified in the
            :paramref:`.Insert.on_conflict_do_update.set_` dictionary.

        :param where:
         Optional argument. If present, can be a literal SQL
         string or an acceptable expression for a ``WHERE`` clause
         that restricts the rows affected by ``DO UPDATE SET``. Rows
         not meeting the ``WHERE`` condition will not be updated
         (effectively a ``DO NOTHING`` for those rows).

         .. versionadded:: 1.1


        .. seealso::

            :ref:`postgresql_insert_on_conflict`

        )ÚOnConflictDoUpdateÚ_post_values_clause©r   Ú
constraintÚindex_elementsÚindex_whereÚset_Úwherer   r   r   Úon_conflict_do_update-   s   9
ÿzInsert.on_conflict_do_updatec                 C   s   t |||ƒ| _| S )a"  
        Specifies a DO NOTHING action for ON CONFLICT clause.

        The ``constraint`` and ``index_elements`` arguments
        are optional, but only one of these can be specified.

        :param constraint:
         The name of a unique or exclusion constraint on the table,
         or the constraint object itself if it has a .name attribute.

        :param index_elements:
         A sequence consisting of string column names, :class:`.Column`
         objects, or other column expression objects that will be used
         to infer a target index.

        :param index_where:
         Additional WHERE criterion that can be used to infer a
         conditional target index.

         .. versionadded:: 1.1

        .. seealso::

            :ref:`postgresql_insert_on_conflict`

        )ÚOnConflictDoNothingr   ©r   r   r   r   r   r   r   Úon_conflict_do_nothingk   s   ÿzInsert.on_conflict_do_nothing©NNNNN©NNN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úmemoized_propertyr   r   r   r   r   r   r   r   r      s    
ú=ÿr   z.dialects.postgresql.insertc                   @   s   e Zd Zddd„ZdS )ÚOnConflictClauseNc                 C   s  |d urt |tjƒst |tjtjtjfƒrt|dƒp|}|d urb|d ur(t	dƒ‚t |tjƒr8|| _
d | _d | _n*t |tjƒrJ|j}|jd  d¡}nt |tjƒrW|j}|j}n|j}|jd  d¡}|d urqd | _
|| _|| _d S |d u r€d  | _
 | _| _d S d S )Nr   z8'constraint' and 'index_elements' are mutually exclusiveÚ
postgresqlr   )Ú
isinstancer   Ústring_typesr   ÚIndexÚ
Constraintr   ÚExcludeConstraintÚgetattrÚ
ValueErrorÚconstraint_targetÚinferred_target_elementsÚinferred_target_whereclauseÚexpressionsÚdialect_optionsÚgetr   r   r   r   r   r   Ú__init__“   sJ   þÿ
ÿ
ÿ
þýzOnConflictClause.__init__r    )r!   r"   r#   r5   r   r   r   r   r&   ’   s    r&   c                   @   s   e Zd ZdZdS )r   r   N)r!   r"   r#   Ú__visit_name__r   r   r   r   r   ¾   s    r   c                       s,   e Zd ZdZ					d‡ fdd„	Z‡  ZS )r   r   Nc                    sf   t t| ƒj|||d� | jd u r| jd u rtdƒ‚t|tƒr |s$tdƒ‚dd„ | ¡ D ƒ| _	|| _
d S )N)r   r   r   zVEither constraint or index_elements, but not both, must be specified unless DO NOTHINGz,set parameter must be a non-empty dictionaryc                 S   s   g | ]\}}||f‘qS r   r   )Ú.0ÚkeyÚvaluer   r   r   Ú
<listcomp>Þ   s    ÿz/OnConflictDoUpdate.__init__.<locals>.<listcomp>)Úsuperr   r5   r0   r/   r.   r(   ÚdictÚitemsÚupdate_values_to_setÚupdate_whereclauser   ©Ú	__class__r   r   r5   Å   s    
ý

ÿÿ
zOnConflictDoUpdate.__init__r   )r!   r"   r#   r6   r5   Ú__classcell__r   r   r@   r   r   Â   s    úr   N)Ú r   r   Úsqlr   Úsql.baser   Úsql.dmlr   ÚStandardInsertÚsql.elementsr   Úsql.expressionr	   Úutil.langhelpersr
   Ú__all__r   r&   r   r   r   r   r   r   Ú<module>   s   
{,