U aH1@sddlZddlmZddlmZddlmZddlmZddlmZddlm Z ej fd d Z ej fd d Z Gd ddej ejZejdddZejdddZejdddZGdddejZdS)N)types)util) coercions) expression) operators)rolescCs |||S)zA synonym for the :meth:`.ARRAY.Comparator.any` method. This method is legacy and is here for backwards-compatibility. .. seealso:: :func:`_expression.any_` )anyotherZarrexproperatorr eC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\dialects\postgresql\array.pyAnys rcCs |||S)zA synonym for the :meth:`.ARRAY.Comparator.all` method. This method is legacy and is here for backwards-compatibility. .. seealso:: :func:`_expression.all_` )allr r r rAll s rcsHeZdZdZdZdZfddZeddZd d d Z dd d Z Z S)arrayaA PostgreSQL ARRAY literal. This is used to produce ARRAY literals in SQL expressions, e.g.:: from sqlalchemy.dialects.postgresql import array from sqlalchemy.dialects import postgresql from sqlalchemy import select, func stmt = select(array([1,2]) + array([3,4,5])) print(stmt.compile(dialect=postgresql.dialect())) Produces the SQL:: SELECT ARRAY[%(param_1)s, %(param_2)s] || ARRAY[%(param_3)s, %(param_4)s, %(param_5)s]) AS anon_1 An instance of :class:`.array` will always have the datatype :class:`_types.ARRAY`. The "inner" type of the array is inferred from the values present, unless the ``type_`` keyword argument is passed:: array(['foo', 'bar'], type_=CHAR) Multidimensional arrays are produced by nesting :class:`.array` constructs. The dimensionality of the final :class:`_types.ARRAY` type is calculated by recursively adding the dimensions of the inner :class:`_types.ARRAY` type:: stmt = select( array([ array([1, 2]), array([3, 4]), array([column('q'), column('x')]) ]) ) print(stmt.compile(dialect=postgresql.dialect())) Produces:: SELECT ARRAY[ARRAY[%(param_1)s, %(param_2)s], ARRAY[%(param_3)s, %(param_4)s], ARRAY[q, x]] AS anon_1 .. versionadded:: 1.3.6 added support for multidimensional array literals .. seealso:: :class:`_postgresql.ARRAY` Z postgresqlc sdd|D}tt|j||dd|D|_|d|jrF|jdntj}t|tr~t|j |j dk rr|j dndd|_ n t||_ dS) NcSsg|]}ttj|qSr )rexpectrZExpressionElementRole).0cr r r fsz"array.__init__..cSsg|] }|jqSr )type)rargr r rrlstype_r) dimensions) superr__init__Z _type_tuplepopsqltypesZNULLTYPE isinstanceARRAY item_typerr)selfZclauseskwZ main_type __class__r rres"   zarray.__init__cCs|fSNr r$r r r_select_iterable|szarray._select_iterableFNcsD|stjkr&tjd|jddStfdd|DSdS)NT)Z_compared_to_operatorrZ_compared_to_typeuniquecsg|]}j|ddqS)T)_assume_scalarr) _bind_param)ror r$rr rrsz%array._bind_param..)rgetitemrZ BindParameterrr)r$r objr,rr r/rr-s zarray._bind_paramcCs&|tjtjtjfkrt|S|SdSr()rZany_opZall_opr0rZGrouping)r$Zagainstr r r self_groups zarray.self_group)FN)N) __name__ __module__ __qualname____doc__Z__visit_name__Zstringify_dialectrpropertyr*r-r2 __classcell__r r r&rr.s1   rz@>) precedencez<@z&&c@seZdZdZGdddejjZeZdddZe dd Z e d d Z d d Z ddZ ejddZddZddZddZdS)r"aPostgreSQL ARRAY type. .. versionchanged:: 1.1 The :class:`_postgresql.ARRAY` type is now a subclass of the core :class:`_types.ARRAY` type. The :class:`_postgresql.ARRAY` type is constructed in the same way as the core :class:`_types.ARRAY` type; a member type is required, and a number of dimensions is recommended if the type is to be used for more than one dimension:: from sqlalchemy.dialects import postgresql mytable = Table("mytable", metadata, Column("data", postgresql.ARRAY(Integer, dimensions=2)) ) The :class:`_postgresql.ARRAY` type provides all operations defined on the core :class:`_types.ARRAY` type, including support for "dimensions", indexed access, and simple matching such as :meth:`.types.ARRAY.Comparator.any` and :meth:`.types.ARRAY.Comparator.all`. :class:`_postgresql.ARRAY` class also provides PostgreSQL-specific methods for containment operations, including :meth:`.postgresql.ARRAY.Comparator.contains` :meth:`.postgresql.ARRAY.Comparator.contained_by`, and :meth:`.postgresql.ARRAY.Comparator.overlap`, e.g.:: mytable.c.data.contains([1, 2]) The :class:`_postgresql.ARRAY` type may not be supported on all PostgreSQL DBAPIs; it is currently known to work on psycopg2 only. Additionally, the :class:`_postgresql.ARRAY` type does not work directly in conjunction with the :class:`.ENUM` type. For a workaround, see the special type at :ref:`postgresql_array_of_enum`. .. seealso:: :class:`_types.ARRAY` - base array type :class:`_postgresql.array` - produces a literal array value. c@s(eZdZdZddZddZddZdS) zARRAY.Comparatora*Define comparison operations for :class:`_types.ARRAY`. Note that these operations are in addition to those provided by the base :class:`.types.ARRAY.Comparator` class, including :meth:`.types.ARRAY.Comparator.any` and :meth:`.types.ARRAY.Comparator.all`. cKs|jt|tjdS)zBoolean expression. Test if elements are a superset of the elements of the argument array expression. Z result_type)operateCONTAINSr Boolean)r$r kwargsr r rcontainsszARRAY.Comparator.containscCs|jt|tjdS)zBoolean expression. Test if elements are a proper subset of the elements of the argument array expression. r;)r< CONTAINED_BYr r>r$r r r r contained_bys zARRAY.Comparator.contained_bycCs|jt|tjdS)zuBoolean expression. Test if array has elements in common with an argument array expression. r;)r<OVERLAPr r>rBr r roverlapszARRAY.Comparator.overlapN)r3r4r5r6r@rCrEr r r r Comparators rFFNcCs>t|trtdt|tr"|}||_||_||_||_dS)aPConstruct an ARRAY. E.g.:: Column('myarray', ARRAY(Integer)) Arguments are: :param item_type: The data type of items of this array. Note that dimensionality is irrelevant here, so multi-dimensional arrays like ``INTEGER[][]``, are constructed as ``ARRAY(Integer)``, not as ``ARRAY(ARRAY(Integer))`` or such. :param as_tuple=False: Specify whether return results should be converted to tuples from lists. DBAPIs such as psycopg2 return lists by default. When tuples are returned, the results are hashable. :param dimensions: if non-None, the ARRAY will assume a fixed number of dimensions. This will cause the DDL emitted for this ARRAY to include the exact number of bracket clauses ``[]``, and will also optimize the performance of the type overall. Note that PG arrays are always implicitly "non-dimensioned", meaning they can store any number of dimensions no matter how they were declared. :param zero_indexes=False: when True, index values will be converted between Python zero-based and PostgreSQL one-based indexes, e.g. a value of one will be added to all index values before passing to the database. .. versionadded:: 0.9.5 zUDo not nest ARRAY types; ARRAY(basetype) handles multi-dimensional arrays of basetypeN)r!r" ValueErrorrr#as_tupler zero_indexes)r$r#rHrrIr r rrs&  zARRAY.__init__cCs|jSr()rHr)r r rhashable%szARRAY.hashablecCstSr()listr)r r r python_type)szARRAY.python_typecCs||kSr(r )r$xyr r rcompare_values-szARRAY.compare_valuescszdkrt|}dks6dkrZ|r6t|dttfsZrPfdd|DS|Snfdd|DSdS)Nrrc3s|]}|VqdSr(r rrM)itemprocr r ?sz$ARRAY._proc_array..c3s.|]&}|dk rdndVqdS)Nr) _proc_arrayrP collectiondimrQr$r rrRCs)rKr!tuple)r$ZarrrQrVrUr rTrrS0s   zARRAY._proc_arraycCst|jtjo|jjSr()r!r#r EnumZ native_enumr)r r r_against_native_enumMszARRAY._against_native_enumcCs|Sr(r )r$Z bindvaluer r rbind_expressionTszARRAY.bind_expressioncs$j||fdd}|S)Ncs"|dkr |S|jtSdSr()rSrrKvalue item_procr$r rprocess\sz%ARRAY.bind_processor..process)r# dialect_implbind_processor)r$dialectr_r r]rraWs  zARRAY.bind_processorcsFj|||fdd}jrB|ddfdd}|S)Ncs,|dkr |S|jjr"tntSdSr()rSrrHrWrKr[r]r rr_ks z'ARRAY.result_processor..processcSs$td|d}|r |dSgS)Nz^{(.*)}$r,)rematchgroupsplit)r\innerr r rhandle_raw_stringysz1ARRAY.result_processor..handle_raw_stringcs(|dkr |St|tjr"|n|Sr()r!r string_typesr[)risuper_rpr rr_}s )r#r`result_processorrY)r$rbZcoltyper_r )rir^r$rkrrlfs   zARRAY.result_processor)FNF)r3r4r5r6r r"rFZcomparator_factoryrr7rJrLrOrSrZmemoized_propertyrYrZrarlr r r rr"s$- 2   r")rdrr rZsqlrrrreqrrZ ClauseListZ ColumnElementrZ custom_opr=rArDr"r r r rs      n