o
    h                     @  sd   d Z ddlmZ ddlZddlmZmZmZmZm	Z	 ddl
mZ G dd dejZG d	d
 d
ZdS )zImplementation of the DbDoc.    )annotationsN)AnyDictKeysViewOptionalUnion   )ProgrammingErrorc                   @  s   e Zd ZdZd	ddZdS )
ExprJSONEncoderzgA :class:`json.JSONEncoder` subclass, which enables encoding of
    :class:`mysqlx.ExprParser` objects.oobjectreturnstrc                 C  s   t |dr| S tj| |S )Nexpr)hasattrjsonJSONEncoderdefault)selfr    r   G/var/www/html/scripts/venv/lib/python3.10/site-packages/mysqlx/dbdoc.pyr   ,   s   
zExprJSONEncoder.defaultN)r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r
   (   s    r
   c                   @  sl   e Zd ZdZd!ddZd"d	d
Zd"ddZd#ddZd$ddZd%ddZ	d&d'ddZ
d(ddZd"dd ZdS ))DbDoczRepresents a generic document in JSON format.

    Args:
        value (object): The value can be a JSON string or a dict.

    Raises:
        ValueError: If ``value`` type is not a basestring or dict.
    valueUnion[str, Dict[str, Any]]r   Nonec                 C  s@   t |tr
|| _d S t |trt|| _d S tdt| )NzUnable to handle type: )
isinstancedict__dict__r   r   loads
ValueErrortype)r   r   r   r   r   __init__=   s
   


zDbDoc.__init__r   c                 C  s   |   S N)as_strr   r   r   r   __str__E   s   zDbDoc.__str__c                 C  s
   t | jS r&   )reprr!   r(   r   r   r   __repr__H      
zDbDoc.__repr__indexr   c                 C  s   |dkrt d|| j|< d S )N_idzCannot modify _id)r	   r!   )r   r-   r   r   r   r   __setitem__K   s   zDbDoc.__setitem__c                 C  s
   | j | S r&   r!   )r   r-   r   r   r   __getitem__P   r,   zDbDoc.__getitem__itemboolc                 C  s
   || j v S r&   r0   )r   r2   r   r   r   __contains__S   r,   zDbDoc.__contains__Ndoc_idOptional[str]c                 C  s4   | j  }|r||d< t|S d|v r|d= t|S )ae  Returns a new copy of a :class:`mysqlx.DbDoc` object containing the
        `doc_id` provided. If `doc_id` is not provided, it will be removed from
        new :class:`mysqlx.DbDoc` object.

        Args:
            doc_id (Optional[str]): Document ID

        Returns:
            mysqlx.DbDoc: A new instance of DbDoc containing the _id provided
        r.   )r!   copyr   )r   r5   new_dictr   r   r   r7   V   s   
z
DbDoc.copyKeysView[str]c                 C  s
   | j  S )zJReturns the keys.

        Returns:
            `list`: The keys.
        )r!   keysr(   r   r   r   r:   h   s   
z
DbDoc.keysc                 C  s   t j| jtdS )zSerialize :class:`mysqlx.DbDoc` to a JSON formatted ``str``.

        Returns:
            str: A JSON formatted ``str`` representation of the document.

        .. versionadded:: 8.0.16
        )cls)r   dumpsr!   r
   r(   r   r   r   r'   p   s   zDbDoc.as_str)r   r   r   r   )r   r   )r-   r   r   r   r   r   )r-   r   r   r   )r2   r   r   r3   r&   )r5   r6   r   r   )r   r9   )r   r   r   r   r%   r)   r+   r/   r1   r4   r7   r:   r'   r   r   r   r   r   3   s    
	





r   )r   
__future__r   r   typingr   r   r   r   r   errorsr	   r   r
   r   r   r   r   r   <module>   s   