U a&2@sddlZddlmZddlmZddlmZddlmZ ddlm Z d Z e j e j Ze jd ed d d Ze jd ed d d Ze jded d d Ze jded d d Ze jded d d Ze jded d d ZGdddejejejZGddde jZGddde jZGddde jZGddde jZGddde jZGddde jZ Gd d!d!e jZ!Gd"d#d#e jZ"e#d$ej$Z%e#d%ej$Z&d&d'Z'd(d)Z(d*d+Z)dS),N)ARRAY)types)util) functions) operators)HSTOREhstorez->T) precedenceZnatural_self_precedentZeager_grouping?z?&z?|z@>z<@c@sXeZdZdZdZdZeZd ddZ Gdddej j ej j Z e Z dd Zd d ZdS) r atRepresent the PostgreSQL HSTORE type. The :class:`.HSTORE` type stores dictionaries containing strings, e.g.:: data_table = Table('data_table', metadata, Column('id', Integer, primary_key=True), Column('data', HSTORE) ) with engine.connect() as conn: conn.execute( data_table.insert(), data = {"key1": "value1", "key2": "value2"} ) :class:`.HSTORE` provides for a wide range of operations, including: * Index operations:: data_table.c.data['some key'] == 'some value' * Containment operations:: data_table.c.data.has_key('some key') data_table.c.data.has_all(['one', 'two', 'three']) * Concatenation:: data_table.c.data + {"k1": "v1"} For a full list of special methods see :class:`.HSTORE.comparator_factory`. For usage with the SQLAlchemy ORM, it may be desirable to combine the usage of :class:`.HSTORE` with :class:`.MutableDict` dictionary now part of the :mod:`sqlalchemy.ext.mutable` extension. This extension will allow "in-place" changes to the dictionary, e.g. addition of new keys or replacement/removal of existing keys to/from the current dictionary, to produce events which will be detected by the unit of work:: from sqlalchemy.ext.mutable import MutableDict class MyClass(Base): __tablename__ = 'data_table' id = Column(Integer, primary_key=True) data = Column(MutableDict.as_mutable(HSTORE)) my_object = session.query(MyClass).one() # in-place mutation, requires Mutable extension # in order for the ORM to detect my_object.data['some_key'] = 'some value' session.commit() When the :mod:`sqlalchemy.ext.mutable` extension is not used, the ORM will not be alerted to any changes to the contents of an existing dictionary, unless that dictionary value is re-assigned to the HSTORE-attribute itself, thus generating a change event. .. seealso:: :class:`.hstore` - render the PostgreSQL ``hstore()`` function. FNcCs|dk r||_dS)zConstruct a new :class:`.HSTORE`. :param text_type: the type that should be used for indexed values. Defaults to :class:`_types.Text`. .. versionadded:: 1.1.0 N) text_type)selfr rfC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\dialects\postgresql\hstore.py__init__s zHSTORE.__init__c@sxeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZdS)zHSTORE.Comparatorz2Define comparison operations for :class:`.HSTORE`.cCs|jt|tjdS)zvBoolean expression. Test for presence of a key. Note that the key may be a SQLA expression. Z result_type)operateHAS_KEYsqltypesBooleanrotherrrrhas_keyszHSTORE.Comparator.has_keycCs|jt|tjdS)z;Boolean expression. Test for presence of all keys in jsonbr)rHAS_ALLrrrrrrhas_allszHSTORE.Comparator.has_allcCs|jt|tjdS)z:Boolean expression. Test for presence of any key in jsonbr)rHAS_ANYrrrrrrhas_anyszHSTORE.Comparator.has_anycKs|jt|tjdS)zBoolean expression. Test if keys (or array) are a superset of/contained the keys of the argument jsonb expression. r)rCONTAINSrr)rrkwargsrrrcontainsszHSTORE.Comparator.containscCs|jt|tjdS)z|Boolean expression. Test if keys are a proper subset of the keys of the argument jsonb expression. r)r CONTAINED_BYrrrrrr contained_bys zHSTORE.Comparator.contained_bycCst||jjfSN)GETITEMtyper )rindexrrr_setup_getitemsz HSTORE.Comparator._setup_getitemcCs t|j|S)zBoolean expression. Test for presence of a non-NULL value for the key. Note that the key may be a SQLA expression. )_HStoreDefinedFunctionexprrkeyrrrdefinedszHSTORE.Comparator.definedcCst|trt|}t|j|S)zHStore expression. Returns the contents of this hstore with the given key deleted. Note that the key may be a SQLA expression. ) isinstancedict_serialize_hstore_HStoreDeleteFunctionr)r*rrrdeletes zHSTORE.Comparator.deletecCs t|j|S)zdHStore expression. Returns a subset of an hstore defined by array of keys. )_HStoreSliceFunctionr))rarrayrrrsliceszHSTORE.Comparator.slicecCs t|jS)z.Text array expression. Returns array of keys.)_HStoreKeysFunctionr)rrrrkeysszHSTORE.Comparator.keyscCs t|jS)z0Text array expression. Returns array of values.)_HStoreValsFunctionr)r6rrrvalsszHSTORE.Comparator.valscCs t|jS)z^Text array expression. Returns array of alternating keys and values. )_HStoreArrayFunctionr)r6rrrr3szHSTORE.Comparator.arraycCs t|jS)z.processcSst|trt|S|SdSr#)r-r.r/rCrrrrGs rZpy2krF)rdialectrGrrErbind_processors zHSTORE.bind_processorcs&tjr|jfdd}ndd}|S)Ncs|dk rt|S|SdSr#) _parse_hstoredecoderCrErrrGsz(HSTORE.result_processor..processcSs|dk rt|S|SdSr#)rKrCrrrrGsrH)rrIZcoltyperGrrErresult_processors zHSTORE.result_processor)N)r=r>r?r@Z__visit_name__ZhashablerTextr r IndexablerA ConcatenableZcomparator_factoryrJrMrrrrr @sF Jr c@seZdZdZeZdZdS)r aGConstruct an hstore value within a SQL expression using the PostgreSQL ``hstore()`` function. The :class:`.hstore` function accepts one or two arguments as described in the PostgreSQL documentation. E.g.:: from sqlalchemy.dialects.postgresql import array, hstore select(hstore('key1', 'value1')) select( hstore( array(['key1', 'key2', 'key3']), array(['value1', 'value2', 'value3']) ) ) .. seealso:: :class:`.HSTORE` - the PostgreSQL ``HSTORE`` datatype. N)r=r>r?r@r r%namerrrrr sr c@seZdZejZdZdS)r(r,N)r=r>r?rrr%rQrrrrr(*sr(c@seZdZeZdZdS)r0r1Nr=r>r?r r%rQrrrrr0/sr0c@seZdZeZdZdS)r2r4NrRrrrrr24sr2c@seZdZeejZdZdS)r5ZakeysNr=r>r?rrrNr%rQrrrrr59s r5c@seZdZeejZdZdS)r8ZavalsNrSrrrrr8>s r8c@seZdZeejZdZdS)r:Zhstore_to_arrayNrSrrrrr:Cs r:c@seZdZeejZdZdS)r;Zhstore_to_matrixNrSrrrrr;Hs r;z ( "(?P (\\ . | [^"])* )" # Quoted key ) [ ]* => [ ]* # Pair operator, optional adjoining whitespace ( (?P NULL ) # NULL value | "(?P (\\ . | [^"])* )" # Quoted value ) z [ ]* , [ ]* cCsd}t|}|t||ddt||}|t||t||d|}t||krhd|dd}t||kr|ddd}d|||fS)zformat an unmarshalling error.rrz[...]Nz5After %r, could not parse residual at position %d: %r)lenmaxmin) hstore_strposctxZhslenZ parsed_tailZresidualrrr _parse_errorks    r\cCsi}d}t|}|dk r|ddddd}|drDd}n|d dddd}|||<||7}t||d}|dk r||7}t||d}q|t|krtt|||S) aParse an hstore from its literal string representation. Attempts to approximate PG's hstore input parsing rules as closely as possible. Although currently this is not strictly necessary, since the current implementation of hstore's output syntax is stricter than what it accepts as input, the documentation makes no guarantees that will always be the case. rNr+\""\\\Z value_nullrD) HSTORE_PAIR_REmatchgroupreplaceendHSTORE_DELIMITER_RErV ValueErrorr\)rYresultrZZ pair_matchr+rDZ delim_matchrrrrKs0       rKcs$dddfdd|DS)zxSerialize a dictionary into an hstore literal. Keys and values must both be strings (except None for values). cSsL|dkr|dkrdSt|tjr8d|ddddStd||fdS) NrDNULLz"%s"r`r_r^r]z"%r in %s position is not a string.)r-r string_typesrdrg)spositionrrrescs  z_serialize_hstore..escz, c3s*|]"\}}d|d|dfVqdS)z%s=>%sr+rDNr).0kvrmrr sz$_serialize_hstore..)joinitems)valrrqrr/s r/)*rer3rrrrZsqlrZsqlfuncr__all__Z _PRECEDENCEZjson_getitem_opZidx_precedenceZ custom_opr$rrrrr!rOrPZ TypeEnginer ZGenericFunctionr r(r0r2r5r8r:r;compileVERBOSErarfr\rKr/rrrrs      M  *