U
    9aT%                     @   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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dZG dd deZG dd deZG dd deZdS )   )ext   )util)	coercions)roles)schema)_exclusive_against)_generative)ColumnCollection)Insert)ClauseElement)alias)public_factory)r   insertc                   @   sV   e Zd ZdZdZejdd ZedddidZ	e
e	dd	d
Ze
e	dddZdS )r   a  PostgreSQL-specific implementation of INSERT.

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

    The :class:`_postgresql.Insert` object is created using the
    :func:`sqlalchemy.dialects.postgresql.insert` function.

    .. versionadded:: 1.1

    
postgresqlc                 C   s   t | jddjS )ak  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.

        .. tip::  The :attr:`_postgresql.Insert.excluded` attribute is an
            instance of :class:`_expression.ColumnCollection`, which provides
            an interface the same as that of the :attr:`_schema.Table.c`
            collection described at :ref:`metadata_tables_and_columns`.
            With this collection, ordinary names are accessible like attributes
            (e.g. ``stmt.excluded.some_column``), but special names and
            dictionary method names should be accessed using indexed access,
            such as ``stmt.excluded["column name"]`` or
            ``stmt.excluded["values"]``.   See the docstring for
            :class:`_expression.ColumnCollection` for further examples.

        .. seealso::

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

        excluded)name)r   tablecolumns)self r   F/tmp/pip-unpacked-wheel-7pet0iyd/sqlalchemy/dialects/postgresql/dml.pyr   '   s    zInsert.excluded_post_values_clausezCThis Insert construct already has an ON CONFLICT clause established)ZmsgsNc                 C   s   t |||||| _dS )as  
        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:`_schema.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\_:
         A dictionary or other mapping object
         where the keys are either names of columns in the target table,
         or :class:`_schema.Column` objects or other ORM-mapped columns
         matching that of the target table, and expressions or literals
         as values, specifying the ``SET`` actions to take.

         .. versionadded:: 1.4 The
            :paramref:`_postgresql.Insert.on_conflict_do_update.set_`
            parameter supports :class:`_schema.Column` objects from the target
            :class:`_schema.Table` as keys.

         .. warning:: This dictionary does **not** take into account
            Python-specified default UPDATE values or generation functions,
            e.g. those specified using :paramref:`_schema.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`

        N)OnConflictDoUpdater   r   
constraintindex_elementsindex_whereset_wherer   r   r   on_conflict_do_updateJ   s    ?    zInsert.on_conflict_do_updatec                 C   s   t |||| _d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:`_schema.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`

        N)OnConflictDoNothingr   r   r   r   r   r   r   r   on_conflict_do_nothing   s
      zInsert.on_conflict_do_nothing)NNNNN)NNN)__name__
__module____qualname____doc__stringify_dialectr   Zmemoized_propertyr   r   Z_on_conflict_exclusiver	   r    r#   r   r   r   r   r      s0   
      A     r   z.dialects.postgresql.insertz.dialects.postgresql.Insertc                   @   s   e Zd ZdZdddZdS )OnConflictClauser   Nc                 C   s   |d k	r8t |tjs8t |tjtjtjfr8t|dp6|}|d k	r|d k	rPt	dt |tjrp|| _
d | _d | _nTt |tjr|j}|jd d}n0t |tjr|j}|j}n|j}|jd d}|d k	rd | _
|| _|| _n|d krd  | _
 | _| _d S )Nr   z8'constraint' and 'index_elements' are mutually exclusiver   r   )
isinstancer   string_typesr   ZIndexZ
Constraintr   ZExcludeConstraintgetattr
ValueErrorconstraint_targetinferred_target_elementsZinferred_target_whereclauseZexpressionsZdialect_optionsgetr   r   r"   r   r   r   __init__   sJ    

zOnConflictClause.__init__)NNN)r$   r%   r&   r(   r1   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                    s   t t| j|||d | jd kr2| jd kr2tdt|trJ|sftdnt|tr^t|}n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 dictionary must not be emptyzqset parameter must be a non-empty dictionary or a ColumnCollection such as the `.c.` collection of a Table objectc                 S   s"   g | ]\}}t tj||fqS r   )r   expectr   ZDMLColumnRole).0keyvaluer   r   r   
<listcomp>  s   z/OnConflictDoUpdate.__init__.<locals>.<listcomp>)superr   r1   r/   r.   r-   r*   dictr
   itemsZupdate_values_to_setZupdate_whereclauser   	__class__r   r   r1      s0    




zOnConflictDoUpdate.__init__)NNNNN)r$   r%   r&   r2   r1   __classcell__r   r   r;   r   r      s        r   N) r   r   Zsqlr   r   r   Zsql.baser   r	   r
   Zsql.dmlr   ZStandardInsertZsql.elementsr   Zsql.expressionr   Zutil.langhelpersr   __all__r   r)   r!   r   r   r   r   r   <module>   s,      .