B *]E@sdZddlZddlmZmZddlmZmZmZddlm Z m Z m Z ddl m Z GdddeZd0d d ZGd d d eZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZeeeeeeddddZeeeeddd dZeeeed!d"d#dZeed$ed%d&d'dZeed(ed)d*d+dZeed,ed-d.d/dZedS)1z1Implementation of the Range type and adaptation N)ProgrammingErrorInterfaceError) ISQLQuoteadaptregister_adapter)new_typenew_array_type register_type) string_typesc@seZdZdZdZd2ddZdd Zd d Zed d Z eddZ eddZ eddZ eddZ eddZeddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1ZdS)3RangeaPython representation for a PostgreSQL |range|_ type. :param lower: lower bound for the range. `!None` means unbound :param upper: upper bound for the range. `!None` means unbound :param bounds: one of the literal strings ``()``, ``[)``, ``(]``, ``[]``, representing whether the lower or upper bounds are included :param empty: if `!True`, the range is empty )_lower_upper_boundsN[)FcCsB|s,|dkrtd|||_||_||_nd|_|_|_dS)N)z[)z(]z()z[]zbound flags not valid: %r) ValueErrorr r r)selflowerupperboundsemptyrB/opt/alt/python37/lib64/python3.7/site-packages/psycopg2/_range.py__init__/s zRange.__init__cCs4|jdkrd|jjSd|jj|j|j|jfSdS)Nz%s(empty=True)z%s(%r, %r, %r))r __class____name__r r )rrrr__repr__:s  zRange.__repr__cCs>|jdkrdS|jdt|jdt|j|jdg}d|S)Nrrz, )rstrr r join)ritemsrrr__str__As  z Range.__str__cCs|jS)z:The lower bound of the range. `!None` if empty or unbound.)r )rrrrrNsz Range.lowercCs|jS)z:The upper bound of the range. `!None` if empty or unbound.)r )rrrrrSsz Range.uppercCs |jdkS)z`!True` if the range is empty.N)r)rrrrisemptyXsz Range.isemptycCs|jdkrdS|jdkS)z0`!True` if the range doesn't have a lower bound.NF)rr )rrrr lower_inf]s zRange.lower_infcCs|jdkrdS|jdkS)z1`!True` if the range doesn't have an upper bound.NF)rr )rrrr upper_infds zRange.upper_infcCs&|jdks|jdkrdS|jddkS)z4`!True` if the lower bound is included in the range.NFr[)rr )rrrr lower_inckszRange.lower_inccCs&|jdks|jdkrdS|jddkS)z4`!True` if the upper bound is included in the range.NFr])rr )rrrr upper_incrszRange.upper_inccCs~|jdkrdS|jdk rD|jddkr6||jkrDdSn||jkrDdS|jdk rz|jddkrl||jkrzdSn||jkrzdSdS)NFrr%rr'T)rr r )rxrrr __contains__ys       zRange.__contains__cCs |jdk S)N)r)rrrr__bool__szRange.__bool__cCst||S)N)typer+)rrrr __nonzero__szRange.__nonzero__cCs2t|tsdS|j|jko0|j|jko0|j|jkS)NF) isinstancer r r r)rotherrrr__eq__s    z Range.__eq__cCs || S)N)r0)rr/rrr__ne__sz Range.__ne__cCst|j|j|jfS)N)hashr r r)rrrr__hash__szRange.__hash__cCs^t|tstSxJdD]B}t||}t||}||kr6q|dkrBdS|dkrNdS||kSqWdS)N)r r rTF)r.r NotImplementedgetattr)rr/attrZ self_valueZ other_valuerrr__lt__s     z Range.__lt__cCs||kr dS||SdS)NT)r7)rr/rrr__le__sz Range.__le__cCst|tr||StSdS)N)r.r r7r4)rr/rrr__gt__s  z Range.__gt__cCs||kr dS||SdS)NT)r9)rr/rrr__ge__sz Range.__ge__csfddjDS)Ncs"i|]}t|rt||qSr)hasattrr5).0slot)rrr sz&Range.__getstate__..) __slots__)rr)rr __getstate__s zRange.__getstate__cCs&x |D]\}}t|||q WdS)N)r setattr)rstater=valuerrr __setstate__szRange.__setstate__)NNrF)r __module__ __qualname____doc__r?rrr!propertyrrr"r#r$r&r(r*r+r-r0r1r3r7r8r9r:r@rDrrrrr #s0         r FcCs&t|||}|| r|pd|S)aCreate and register an adapter and the typecasters to convert between a PostgreSQL |range|_ type and a PostgreSQL `Range` subclass. :param pgrange: the name of the PostgreSQL |range| type. Can be schema-qualified :param pyrange: a `Range` strict subclass, or just a name to give to a new class :param conn_or_curs: a connection or cursor used to find the oid of the range and its subtype; the typecaster is registered in a scope limited to this object, unless *globally* is set to `!True` :param globally: if `!False` (default) register the typecaster only on *conn_or_curs*, otherwise register it globally :return: `RangeCaster` instance responsible for the conversion If a string is passed to *pyrange*, a new `Range` subclass is created with such name and will be available as the `~RangeCaster.range` attribute of the returned `RangeCaster` object. The function queries the database on *conn_or_curs* to inspect the *pgrange* type and raises `~psycopg2.ProgrammingError` if the type is not found. If querying the database is not advisable, use directly the `RangeCaster` class and register the adapter and typecasters using the provided functions. N) RangeCaster_from_db _register)pgrangepyrange conn_or_cursZgloballyZcasterrrrregister_rangesrOc@s4eZdZdZdZddZddZddZd d ZdS) RangeAdapterz`ISQLQuote` adapter for `Range` subclasses. This is an abstract class: concrete classes must set a `name` class attribute or override `getquoted()`. NcCs ||_dS)N)adapted)rrQrrrrszRangeAdapter.__init__cCs|jtkr|SdS)N)Z_protor)rprotorrr __conform__s zRangeAdapter.__conform__cCs ||_dS)N)_conn)rconnrrrprepareszRangeAdapter.preparecCs|jdkrtd|j}|jr.d|jdS|jdk rbt|j}t|drX||j | }nd}|j dk rt|j }t|dr||j | }nd}|jdd|d|d|j dd S) NzMRangeAdapter must be subclassed overriding its name or the getquoted() methods 'empty'::utf8rVsNULL(s, s, 's')) nameNotImplementedErrorrQr"encoderrr;rVrT getquotedrr)rrarrrrrr\s&           zRangeAdapter.getquoted) rrErFrGrYrrSrVr\rrrrrPs rPc@sZeZdZdZdddZddZeddZe d ej Z e d Z dd d Z dd dZdS)rIa Helper class to convert between `Range` and PostgreSQL range types. Objects of this class are usually created by `register_range()`. Manual creation could be useful if querying the database is not advisable: in this case the oids must be provided. NcCs`||_||||jjp"|jjj}t|f||j|_|dk rVt |f|d|j|_ nd|_ dS)NZARRAY) subtype_oid_create_rangesadapterrYrrrparse typecasterrarray_typecaster)rrLrMoidr_ array_oidrYrrrr(s zRangeCaster.__init__cCsd|_t|tr*t|tfi|_||j_n2yt|trD|tk rD||_Wntk rZYnX|jdkrntdd|_y6t|trt|t fi|_t|t r|t k r||_Wntk rYnX|jdkrtddS)z0Create Range and RangeAdapter classes if needed.Nz:pgrange must be a string or a RangeAdapter strict subclassz1pyrange must be a type or a Range strict subclass) rar.r r,rPrY issubclass TypeErrorranger )rrLrMrrrr`6s0        zRangeCaster._create_rangescCsddlm}ddlm}||\}}|jjdkr@td|jj|j}d|kr`|dd\} } n|} d} y| d | | fWn$tk r|j s| Yn X| } ||kr|j s| | std || \} } }t ||| | |d S) z|Return a `RangeCaster` instance for the type *pgrange*. Raise `ProgrammingError` if the type is not found. r)STATUS_IN_TRANSACTION)_solve_conn_cursiX`z'range types not available in version %s.rZpubliczselect rngtypid, rngsubtype, (select typarray from pg_type where oid = rngtypid) from pg_range r join pg_type t on t.oid = rngtypid join pg_namespace ns on ns.oid = typnamespace where typname = %s and ns.nspname = %s; zPostgreSQL type '%s' not found)rer_rf)psycopg2.extensionsrjZpsycopg2.extrasrkinfoZserver_versionrZstatussplitZexecuteZ autocommitZrollbackZfetchonerI)rrYrMrNrjrkrUZcursZ conn_statusZschemaZtnameZrecr,ZsubtypeZarrayrrrrJYs8       zRangeCaster._from_dba] ( \(|\[ ) # lower bound flag (?: # lower bound: " ( (?: [^"] | "")* ) " # - a quoted string | ( [^",]+ ) # - or an unquoted string )? # - or empty (not catched) , (?: # upper bound: " ( (?: [^"] | "")* ) " # - a quoted string | ( [^"\)\]]+ ) # - or an unquoted string )? # - or empty (not catched) ( \)|\] ) # upper bound flag z (["\\])\1cCs|dkr dS|dkr |jddS|j|}|dkr@td||d}|dkrr|d}|dk rr|jd|}|d}|dkr|d }|dk r|jd|}|dk r||j|}||j|}|d |d }||||S) NrT)rzfailed to parse range: '%s'z\1r) ri _re_rangematchrgroup _re_undoublesubcastr_)rsZcurmrrrrrrrbs,       zRangeCaster.parsecCs4t|j||jdk r"t|j|t|j|jdS)N)r rcrdrrira)rZscoperrrrKs   zRangeCaster._register)N)N)N)rrErFrGrr` classmethodrJrecompileVERBOSErurxrbrKrrrrrI!s # 8   rIc@seZdZdZdS) NumericRangezA `Range` suitable to pass Python numeric types to a PostgreSQL range. PostgreSQL types :sql:`int4range`, :sql:`int8range`, :sql:`numrange` are casted into `!NumericRange` instances. N)rrErFrGrrrrrsrc@seZdZdZdS) DateRangez#Represents :sql:`daterange` values.N)rrErFrGrrrrrsrc@seZdZdZdS) DateTimeRangez!Represents :sql:`tsrange` values.N)rrErFrGrrrrrsrc@seZdZdZdS)DateTimeTZRangez#Represents :sql:`tstzrange` values.N)rrErFrGrrrrrsrc@seZdZdZddZdS)NumberRangeAdapterz1Adapt a range if the subtype doesn't need quotes.cCsr|j}|jrdS|js,t|jd}nd}|jsLt|jd}nd}d|j d|||j df dS)Ns'empty'asciirz '%s%s,%s%s'rr) rQr"r#rrr\decoder$rrr[)rr]rrrrrr\szNumberRangeAdapter.getquotedN)rrErFrGr\rrrrrsri@iA)rer_rfiViWiBiiCZ daterangeiHi:iIZtsrangeiDiZiEZ tstzrangeiFiiG)F)rGr~Zpsycopg2._psycopgrrrmrrrrrr Zpsycopg2.compatr objectr rOrPrIrrrrrZint4range_casterrKZint8range_casterZnumrange_casterZdaterange_casterZtsrange_casterZtstzrange_casterrrrrsF 0 0(