o
    NDi:                     @   s   d Z ddlZeeZddlmZ ddlmZ ddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlZddlmZmZmZmZmZmZmZmZmZ dd ZG d	d
 d
eZG dd deZdS )z
These functions are used to convert Python objects into CQL strings.
When non-prepared statements are executed, these encoder functions are
called on each query parameter.
    N)hexlify)Decimal)UUID)	OrderedDict
OrderedMapOrderedMapSerializedKey	sortedsetTimeDatePoint
LineStringPolygonc                 C   s&   t | trdt| dd S t| S )N'%s''z'')
isinstancestrreplace)term r   J/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/encoder.py	cql_quote%   s   
r   c                   @   s   e Zd ZdS )ValueSequenceN)__name__
__module____qualname__r   r   r   r   r   ,   s    r   c                   @   s   e Zd ZdZdZ	 dd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZeZ	 dd Zdd  Zd!d" Zd*d$d%Zd&d' Zd(d) ZdS )+Encoderz
    A container for mapping python types to CQL string literals when working
    with non-prepared statements.  The type :attr:`~.Encoder.mapping` can be
    directly customized by users.
    Nc                 C   s   i t | jt| jt| jt| jt| j	t
| j	tj| jtj| jtj| jt| jt| jt| jt| jt| jt| jt| jt| jt| jt| jt| jt j!| jt"| j#t$| j%t&| j%t'| j%i| _(| j()t*| jt+| jt,d | j-t.j/| j0t.j1| j0i d S N)2floatcql_encode_floatr   cql_encode_decimal	bytearraycql_encode_bytesr   cql_encode_strintcql_encode_objectr   datetimecql_encode_datetimedatecql_encode_datetimecql_encode_timer
   cql_encode_date_extr	   dictcql_encode_map_collectionr   r   r   listcql_encode_list_collectiontuplesetcql_encode_set_collectionr   	frozensettypesGeneratorTyper   cql_encode_sequencer   cql_encode_str_quotedr   r   mappingupdate
memoryviewbytestypecql_encode_none	ipaddressIPv4Addresscql_encode_ipaddressIPv6Addressselfr   r   r   __init__<   sf   	


zEncoder.__init__c                 C   s   dS )z>
        Converts :const:`None` to the string 'NULL'.
        NULLr   rC   valr   r   r   r=   a   s   zEncoder.cql_encode_nonec                 C   s   t |dS )za
        Converts :class:`unicode` objects to UTF-8 encoded strings with quote escaping.
        utf-8)r   encoderF   r   r   r   cql_encode_unicodeg   s   zEncoder.cql_encode_unicodec                 C      t |S )z9
        Escapes quotes in :class:`str` objects.
        )r   rF   r   r   r   r"   m   s   zEncoder.cql_encode_strc                 C      d| S )Nr   r   rF   r   r   r   r7   s   s   zEncoder.cql_encode_str_quotedc                 C   s   dt | dS )Ns   0xrH   )r   decoderF   r   r   r   r!   v   s   zEncoder.cql_encode_bytesc                 C   rK   )z
        Default encoder for all objects that do not have a specific encoder function
        registered. This function simply calls :meth:`str()` on the object.
        )r   rF   r   r   r   r$   y      zEncoder.cql_encode_objectc                 C   s0   t |r|dkrdS dS t |rdS t|S )z@
        Encode floats using repr to preserve precision
        r   Infinityz	-InfinityNaN)mathisinfisnanreprrF   r   r   r   r      s
   

zEncoder.cql_encode_floatc                 C   s.   t | }tt|d t|ddd  S )z
        Converts a :class:`datetime.datetime` object to a (string) integer timestamp
        with millisecond precision.
        g     @@microsecondr   )calendartimegmutctimetupler   r#   getattr)rC   rG   	timestampr   r   r   r&      s    zEncoder.cql_encode_datetimec                 C   s   d| d S )zj
        Converts a :class:`datetime.date` object to a string with format
        ``YYYY-MM-DD``.
        r   z%Y-%m-%d)strftimerF   r   r   r   r(         zEncoder.cql_encode_datec                 C   rL   )zx
        Converts a :class:`cassandra.util.Time` object to a string with format
        ``HH:MM:SS.mmmuuunnn``.
        r   r   rF   r   r   r   r*      rN   zEncoder.cql_encode_timec                 C   s   t |jd S )zM
        Encodes a :class:`cassandra.util.Date` object as an integer
        l        )r   days_from_epochrF   r   r   r   r+      r\   zEncoder.cql_encode_date_extc                       dd  fdd|D  S )z
        Converts a sequence to a string of the form ``(item1, item2, ...)``.  This
        is suitable for ``IN`` value lists.
        z(%s), c                 3   (    | ]} j t| j|V  qd S r   r8   getr<   r$   .0vrB   r   r   	<genexpr>   s     z.Encoder.cql_encode_sequence.<locals>.<genexpr>joinrF   r   rB   r   r6      s   
zEncoder.cql_encode_sequencec                    s    dd  fdd| D  S )z
        Converts a dict into a string of the form ``{key1: val1, key2: val2, ...}``.
        This is suitable for ``map`` type columns.
        {%s}r_   c                 3   sH    | ]\}}d  j t| j| j t| j|f V  qdS )z%s: %sNra   )rd   kre   rB   r   r   rf      s    z4Encoder.cql_encode_map_collection.<locals>.<genexpr>)rh   itemsrF   r   rB   r   r-      s   
z!Encoder.cql_encode_map_collectionc                    r^   )z
        Converts a sequence to a string of the form ``[item1, item2, ...]``.  This
        is suitable for ``list`` type columns.
        z[%s]r_   c                 3   r`   r   ra   rc   rB   r   r   rf         & z5Encoder.cql_encode_list_collection.<locals>.<genexpr>rg   rF   r   rB   r   r/         z"Encoder.cql_encode_list_collectionc                    r^   )z
        Converts a sequence to a string of the form ``{item1, item2, ...}``.  This
        is suitable for ``set`` type columns.
        ri   r_   c                 3   r`   r   ra   rc   rB   r   r   rf      rl   z4Encoder.cql_encode_set_collection.<locals>.<genexpr>rg   rF   r   rB   r   r2      rm   z!Encoder.cql_encode_set_collectionFc                 C   s4   | j t|| j|}|rt|ts|dS |S )z
        Converts any type into a CQL string, defaulting to ``cql_encode_object``
        if :attr:`~Encoder.mapping` does not contain an entry for the type.
        rH   )r8   rb   r<   r$   r   r   rM   )rC   rG   as_text_typeencodedr   r   r   cql_encode_all_types   s   
zEncoder.cql_encode_all_typesc                 C   s
   d|j  S )z
        Converts an ipaddress (IPV4Address, IPV6Address) to a CQL string. This
        is suitable for ``inet`` type columns.
        r   )
compressedrF   r   r   r   r@      s   
zEncoder.cql_encode_ipaddressc                 C   s   |  t|S r   )r   r   rF   r   r   r   r      s   zEncoder.cql_encode_decimal)F)r   r   r   __doc__r8   rD   r=   rJ   r"   r7   r!   r$   r   r&   r(   r*   r+   r6   cql_encode_tupler-   r/   r2   rp   r@   r   r   r   r   r   r   0   s2    %


r   ) rr   logging	getLoggerr   logbinasciir   decimalr   rV   r%   rQ   sysr4   uuidr   r>   cassandra.utilr   r   r   r   r	   r
   r   r   r   r   r.   r   objectr   r   r   r   r   <module>   s    
,