
    Vi9.                         S SK r S SK7  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S r   SS	 jrSS
 jr	SS jr
SS jr " S S\5      rSS jr " S S\5      rg)    N)*)Alias)CompoundSelectQuery)Metadata)	callable_)__deprecated__c                 :    U (       a  [        U 5      $ [        5       $ N)set)ss    f/home/james-whalen/.local/share/pipx/venvs/semgrep/lib/python3.13/site-packages/playhouse/shortcuts.py<lambda>r      s    s1v--    c
                 L  ^^ Uc  SOUnUS:X  a  Sn[        T5      m[        U5      nUU4S jn
Ub~  TR                  S5        UR                   H]  n[        U[        5      (       a  TR                  U5        M+  [        U[
        5      (       d  MB  UR                  UR                  5        M_     0 n[        T5      m[        U5      nTU-  m[        U 5      nU	(       a  U R                  R                  R                  5        H  u  pU
" U5      (       a  M  TR                  XR                  R                  R                  UR                     45        UR                  R                  R                   H  nTR                  U5        M     / n[!        X5       H#  nUR#                  [%        UUUTTUS-
  S95        M%     UX'   M     U R                  R&                   H  nU
" U5      (       a  M  U R(                  R+                  UR,                  5      n[        U[.        5      (       aB  U(       a;  Ub6  [!        U UR,                  5      n[%        UUUTTU[1        U45      -  US-
  S	9nOSnUUUR,                  '   M     U(       a6  U H0  n[!        U U5      n[3        U5      (       a  U" 5       UU'   M+  UUU'   M2     U(       a  U(       a  U R                  R4                  R                  5        H  u  nnUR                  S
:X  a  M  [!        UUR                  5      nUT;   d  UT;   a  M<  T(       a  UT;  a  UT;  a  MQ  / nTR                  U5        [!        U UR                  5      nU H#  nUR#                  [%        UUUTTUS-
  S95        M%     UUUR                  '   M     U$ )a   
Convert a model instance (and any related objects) to a dictionary.

:param bool recurse: Whether foreign-keys should be recursed.
:param bool backrefs: Whether lists of related objects should be recursed.
:param only: A list (or set) of field instances indicating which fields
    should be included.
:param exclude: A list (or set) of field instances that should be
    excluded from the dictionary.
:param list extra_attrs: Names of model instance attributes or methods
    that should be included.
:param SelectQuery fields_from_query: Query that was source of model. Take
    fields explicitly selected by the query and serialize them.
:param int max_depth: Maximum depth to recurse, value <= 0 means no max.
:param bool manytomany: Process many-to-many fields.
Nr   Fc                 8   > U T;   =(       d    T=(       a    U T;  $ r
    )nexcludeonlys    r   r   model_to_dict.<locals>.<lambda>'   s    Q'\Ht/G$Hr   __sentinel__   )recursebackrefsr   r   	max_depth)r   r   r   r   seenr   +)
_clone_setadd
_returning
isinstanceFieldr   _aliastype_meta
manytomanyitemsupdate	rel_modelbackrefthrough_modelrefsgetattrappendmodel_to_dictsorted_fields__data__getnameForeignKeyFieldr   r   r   )modelr   r   r   r   r   extra_attrsfields_from_queryr   r'   should_skipitemdatamodel_classr4   m2mfkfaccumrel_objfield
field_data	attr_nameattrforeign_keyr*   
descriptorrelated_querys      ``                      r   r0   r0      s?   &  'YIA~dD[)KHK$ %00D$&&D%((,	 1 D!GdDtOGu+K//557ID4  NNC!4!4!?!?!LMN((..33C  4 E"5/]#%#'!m- . 0 DJ# 8& **u^^''

3
e_--'%!%4*#%#UH-'!m-
 "
%UZZ' +* $I5),D"&&Y"&Y % G&+kk&:&:&@&@&B"K""c)8 k.A.ABJW$w(>4/k6MEKK$#E;+>+>?M(]#%#'!m- . ) ).D$$%+ 'C. Kr   c           
         U R                   n[        UR                   Vs/ s H  oDR                  U4PM     sn5      nUR	                  5        GHk  u  pgXcR
                  ;   a  UR
                  U   nSn	O>Xe;   a  XV   nSn	O2U(       a  [        XU5        MH  [        SU< S[        U 5      < S35      e[        U[        5      n
U	(       dY  U
(       aR  [        U[        5      (       a=   U R                  UR                     n[        U UR                  [        XU5      5        M  U	(       ay  [        U[         ["        45      (       a^  U Vs/ s H  n[%        UR&                  X5      PM     nnU H  n[        XR                  U 5        M     [        XR                  U5        GMU  [        XR                  U5        GMn     U $ s  snf ! [         a    UR                  5       n Nf = fs  snf )NFTzUnrecognized attribute "z" for model class .)r&   dictr   r+   r(   combinedsetattrAttributeErrorr%   r"   r5   __rel__r4   KeyErrorr*   update_model_from_dictlisttupledict_to_modelr6   )instancer;   ignore_unknownmetafkr   keyvaluerA   
is_backrefis_foreign_keyrel_instancerow_data	instancess                 r   rP   rP      s   >>D>"jj"%>?Hjjl
--MM#&EJ_MEJH5) 03T(^"E F F $E?;nE41H1H1'//

; 

&|NKM JutUm<< !&' %H ekk8D %  ' !*jj(; !*HmmY7Hjj%0C #F OK ?*  1$01's   G 'G	 G%G"!G"c                 $    [        U " 5       X5      $ r
   )rP   )r<   r;   rU   s      r   rS   rS      s    !+-FFr   c                 b   U R                   R                  R                  5        H9  u  p4UR                  U;   d  X1;   a  M  [	        U5      (       a  U" 5       OUnXQU'   M;     [        UR                  5       6 u  pg[        US9R                  U5      nU R                  X5      R                  5       $ )a  
Helper for generating conditional INSERT queries.

For example, prevent INSERTing a new tweet if the user has tweeted within
the last hour::

    INSERT INTO "tweet" ("user_id", "content", "timestamp")
    SELECT 234, 'some content', now()
    WHERE NOT EXISTS (
        SELECT 1 FROM "tweet"
        WHERE user_id = 234 AND timestamp > now() - interval '1 hour')

Using this helper:

    cond = ~fn.EXISTS(Tweet.select().where(
        Tweet.user == user_obj,
        Tweet.timestamp > one_hour_ago))

    iq = insert_where(Tweet, {
        Tweet.user: user_obj,
        Tweet.content: 'some content'}, where=cond)

    res = iq.execute()
)columns)
r&   defaultsr(   r4   r   zipSelectwhereinsert_fromas_rowcount)	clsr;   re   rA   defaultrY   fieldsvaluessqs	            r   insert_whererm      s    2 )),,224::&w//	WU 5 $**,'NF			%	%e	,B??2&2244r   c                   p   ^  \ rS rSrSr\S4\S4\S4\S4\S44rU 4S jrSU 4S	 jjrU 4S
 jr	S r
SrU =r$ )ReconnectMixin   a  
Mixin class that attempts to automatically reconnect to the database under
certain error conditions.

For example, MySQL servers will typically close connections that are idle
for 28800 seconds ("wait_timeout" setting). If your application makes use
of long-lived connections, you may find your connections are closed after
a period of no activity. This mixin will attempt to reconnect automatically
when these errors occur.

This mixin class probably should not be used with Postgres (unless you
REALLY know what you are doing) and definitely has no business being used
with Sqlite. If you wish to use with Postgres, you will need to adapt the
`reconnect_errors` attribute to something appropriate for Postgres.
2006201320144031zMySQL Connection not available.c                    > [         [        U ]
  " U0 UD6  0 U l        U R                   HM  u  p4U R                  R                  U/ 5        U R                  U   R                  UR                  5       5        MO     g r
   )superro   __init___reconnect_errorsreconnect_errors
setdefaultr/   lower)selfargskwargs	exc_classerr_fragment	__class__s        r   rw   ReconnectMixin.__init__   sl    nd,d=f= "$'+'<'<#I""--i<""9-44\5G5G5IJ (=r   c                 \   > Ub  [        S5        U R                  [        [        U ]  X5      $ )Nz,"commit" has been deprecated and is a no-op.)r   
_reconnectrv   ro   execute_sql)r|   sqlparamscommitr   s       r   r   ReconnectMixin.execute_sql   s/    IJu^TF", 	,r   c                 H   > U R                   " [        [        U ]  /UQ70 UD6$ r
   )r   rv   ro   beginr|   r}   r~   r   s      r   r   ReconnectMixin.begin  s-    u^T@ 0 $0(.0 	0r   c                     U" U0 UD6$ ! [          a  nU R                  5       (       a  Ue[        U5      nXPR                  ;  a  Ue[	        U5      R                  5       nU R                  U    H
  nXv;   d  M
    O   UeU R                  5       (       d   U R                  5         U R                  5         U" U0 UD6s S nA$ S nAff = fr
   )		Exceptionin_transactionr%   rx   strr{   	is_closedcloseconnect)r|   funcr}   r~   excr   exc_reprr   s           r   r   ReconnectMixin._reconnect  s    	)((( 	) ""$$	S	I 6 66	3x~~'H $ 6 6y A+ !B 	>>##

(((+	)s"   
 
CA!C9AC;CC)rx   )NN)__name__
__module____qualname____firstlineno____doc__OperationalErrorry   rw   r   r   r   __static_attributes____classcell__r   s   @r   ro   ro      sW    " 
6"	6"	6"	6" 
<= K,0) )r   ro   c                   ^ ^^ 0 mT /nU(       a  UR                  5       n[        U[        5      (       a)  UR                  UR                  UR
                  45        MU  UR                  nUR                  R                  nUTU'   UR                  R                  [        U5      R                  T5      5        U(       a  M  UUU 4S jnU" 5       $ )Nc               3      >#    TR                  5       R                  5        H#  n U R                  T5      nTU   nU" S0 U D6v   M%     g 7f)Nr   )dictsiteratorpop)row
identifierr6   rX   mappingquerys      r   wrapped_iterator2resolve_multimodel_query.<locals>.wrapped_iterator/  sB     ;;=))+CJJ'E,#, ,s   AA
)r   r"   r   extendlhsrhsr6   r&   
table_namer!   r/   Valuealias)r   rX   r?   currr<   r4   r   r   s   ``     @r   resolve_multimodel_queryr   !  s    GGE
yy{d/00LL$((DHH-.jj  ++#uT{0056 % r   c                   J   ^  \ rS rSrSrU 4S jrS rS r\" \\5      r	Sr
U =r$ )ThreadSafeDatabaseMetadatai8  z
Metadata class to allow swapping database at run-time in a multi-threaded
application. To use:

class Base(Model):
    class Meta:
        model_metadata_class = ThreadSafeDatabaseMetadata
c                 p   > S U l         [        R                  " 5       U l        [        [
        U ]  " U0 UD6  g r
   )	_database	threadinglocal_localrv   r   rw   r   s      r   rw   #ThreadSafeDatabaseMetadata.__init__A  s-    oo'($8$I&Ir   c                 D    [        U R                  SU R                  5      $ )Ndatabase)r.   r   r   )r|   s    r   _get_db"ThreadSafeDatabaseMetadata._get_dbG  s    t{{J??r   c                 J    U R                   c  Xl         XR                  l        g r
   )r   r   r   )r|   dbs     r   _set_db"ThreadSafeDatabaseMetadata._set_dbI  s    >>!N!r   )r   r   )r   r   r   r   r   rw   r   r   propertyr   r   r   r   s   @r   r   r   8  s'    J@" )Hr   r   )	TFNNNNNNF)Fr
   )_model_identifier)r   peeweer   r   r   r   r   r   r0   rP   rS   rm   objectro   r   r   r   r   r   <module>r      sf       &   ! .
 =A7;EJrj'TG5DK)V K)\.* *r   