o 3a@sbddlmZddlmZmZddlmZmZmZddl m Z ddl m Z dgZ Gddde ZdS) )NotSupportedError) StatementTable) DeferrableFQ)BaseConstraint)QueryExclusionConstraintcs~eZdZdZddddddfdd ZddZd d Zd d Zd dZddZ ddZ fddZ fddZ ddZ ZS)r zdCONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)sN) index_type condition deferrableinclude opclassescs2|r |dvr td|stdtdd|Dstdt|tdtfs,td|r4|r4tdt|tdtfsAtd t|tdttfsOtd |r]|r]|d kr]td t|ttfshtd |rvt |t |krvtd||_ |p|d|_ ||_ ||_ |rt|nd|_||_tj|ddS)N>gistspgistz;Exclusion constraints only support GiST or SP-GiST indexes.zFAt least one expression is required to define an exclusion constraint.css*|]}t|ttfot|dkVqdS)N) isinstancelisttuplelen).0exprr r E/usr/lib/python3/dist-packages/django/contrib/postgres/constraints.py s  z/ExclusionConstraint.__init__..z+The expressions must be a list of 2-tuples.z3ExclusionConstraint.condition must be a Q instance.z7ExclusionConstraint with conditions cannot be deferred.z=ExclusionConstraint.deferrable must be a Deferrable instance.z4ExclusionConstraint.include must be a list or tuple.rz9Covering exclusion constraints only support GiST indexes.z6ExclusionConstraint.opclasses must be a list or tuple.zhExclusionConstraint.expressions and ExclusionConstraint.opclasses must have the same number of elements.GISTr )name)lower ValueErrorallrtyperrrrr expressionsr r rrrsuper__init__)selfrr"r r rrr __class__r rr$ s^ zExclusionConstraint.__init__c sg}t|jD]K\}\}}t|trt|}|j|d}||\}} z|j|} | r1d|| f}Wn ty;Ynw|t fdd| D}| d||fq|S)N)queryz%s %sc3|]}|VqdSN quote_valuerp schema_editorr rrRz:ExclusionConstraint._get_expression_sql..z %s WITH %s) enumerater"rstrrresolve_expressioncompiler IndexErrorrappend) r%compilerr0r(r"idx expressionoperatorsqlparamsopclassr r/r_get_expression_sqlEs"     z'ExclusionConstraint._get_expression_sqlcsF|jdurdS||j}||j\}}|tfdd|DS)Nc3r)r*r+r-r/r rr1z9ExclusionConstraint._get_condition_sql..)r build_whereas_sql connectionr)r%r8r0r(wherer<r=r r/r_get_condition_sqlVs  z&ExclusionConstraint._get_condition_sqlc stdd}|j|jd}||||}||||}fdd|jD}|j||j|j d || ||r=d|nd| |j d S) NF) alias_cols)rBcsg|] }j|jqSr )_meta get_fieldcolumn)r field_namemodelr r bsz6ExclusionConstraint.constraint_sql..z, z WHERE (%s))rr r"rrCr)r get_compilerrBr?rDrtemplate quote_namerr join_index_include_sql_deferrable_constraint_sqlr)r%rKr0r(r8r"r rr rJrconstraint_sql]s    z"ExclusionConstraint.constraint_sqlcCs,||tdt|jj|j|||dS)Nz(ALTER TABLE %(table)s ADD %(constraint)s)table constraint)check_supportedrrrFdb_tablerPrTr%rKr0r r r create_sqlls  zExclusionConstraint.create_sqlcCs||j|||jSr*)_delete_constraint_sqlsql_delete_checkrPrrYr r r remove_sqlts  zExclusionConstraint.remove_sqlcCs |jr |jjjstddSdS)Nz7Covering exclusion constraints requires PostgreSQL 12+.)rrBfeaturessupports_covering_gist_indexesr)r%r0r r rrW{s z#ExclusionConstraint.check_supportedcst\}}}|j|d<|jdur|j|d<|jdkr#|j|d<|jr+|j|d<|jr3|j|d<|jr;|j|d<|||fS)Nr"r rr rrr) r# deconstructr"r r rrrr)r%pathargskwargsr&r rr`s        zExclusionConstraint.deconstructcslt||jr0|j|jko/|j|jko/|j|jko/|j|jko/|j|jko/|j|jko/|j|jkSt |Sr*) rr'rr r"r rrrr#__eq__)r%otherr&r rrds         zExclusionConstraint.__eq__c Csrd|jj|j|j|jdurdnd|j|jdurdnd|j|js#dndt|j|js0dfSdt|jfS)Nz+<%s: index_type=%s, expressions=%s%s%s%s%s>rMz, condition=%sz, deferrable=%sz , include=%sz, opclasses=%s) r' __qualname__r r"r rrreprr)r%r r r__repr__s zExclusionConstraint.__repr__)__name__ __module__rfrOr$r?rDrTrZr]rWr`rdrh __classcell__r r r&rr s8   N) django.dbr!django.db.backends.ddl_referencesrrdjango.db.modelsrrrdjango.db.models.constraintsrdjango.db.models.sqlr __all__r r r r rs