
    	^c&                         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 Z G d dej                  Z	 G d dej
        j        j                  ZdS )	    N   )indexapic                     |                      dd          } |                      dd          } |                     d          r| d d         dz   } | S )N   _s   \_   %s   \%   *)replaceendswith)patterns    @/usr/lib/python3/dist-packages/hgext/infinitepush/sqlindexapi.py_convertbookmarkpatternr      sU    oodF++GoodF++G &#2#,%N    c                   p     e Zd ZdZ	 	 d fd	Zd Zd Zd Zd Zd	 Z	d
 Z
d Zd Zd Zd Zd Zd Z xZS )sqlindexapiz<
    Sql backend for infinitepush index. See schema.sql
    ,  x   c                 n   t          t          |                                            || _        |||||d| _        d | _        d | _        |st          j        }t          j
        |           t          j                    | _        | j                            |           d| _        |	| _        |
| _        d S )N)s   hosts   ports   databases   users   password)filenameF)superr   __init__reponamesqlargssqlconn	sqlcursorosdevnullloggingbasicConfig	getLoggerlogsetLevel
_connected_waittimeout_locktimeout)selfr   hostportdatabaseuserpasswordlogfileloglevelwaittimeoutlocktimeout	__class__s              r   r   zsqlindexapi.__init__    s     	k4  ))+++ !!
 
  	!jGW----$&&(###''r   c                    | j         rt          j        d          | j        rt          j        d          d}	 	 t	          j        j        di | j        | _         | j                             t                     d| j         _
        n@# t          j        j        j        $ r# |dz  }|dk    r t          j        d           Y nw xY w| j         j                            d	| j        z            }| j                                         | _        | j                            d
|z             | j                            d| j        z             d| _        d S )Ns   SQL connection already opens*   SQL cursor already open without connection   TFr   r   g?s   %ss   SET wait_timeout=%ss   SET innodb_lock_wait_timeout=%s )r   r   indexexceptionr   mysql	connectorconnectr   set_converter_classCustomConverter
autocommiterrorsErrortimesleep	converterescaper$   cursorexecuter%   r#   )r&   retryr.   s      r   
sqlconnectzsqlindexapi.sqlconnectA   sh   < 	J)*HIII> 	)=   	  $6FFFF 00AAA*/'?)/       
A::
3 	 & l,33ED<M4MNN,,..5CDDD.1BB	
 	
 	
 s   AB 9CCc                    t          j                    5  t          j        d           | j                                         | j                                         ddd           n# 1 swxY w Y   d| _        d| _        dS )z(Cleans up the metadata store connection.s   ignoreN)warningscatch_warningssimplefilterr   closer   r&   s    r   rI   zsqlindexapi.closee   s    $&& 	! 	!!),,,N  """L   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! s   AA''A+.A+c                 <    | j         s|                                  | S N)r#   rD   rJ   s    r   	__enter__zsqlindexapi.__enter__n   s!     	OOr   c                 r    || j                                          d S | j                                          d S rL   )r   commitrollback)r&   exc_typeexc_valexc_tbs       r   __exit__zsqlindexapi.__exit__s   s<    L!!!!!L!!#####r   c                    | j         s|                                  | j                            d| j        |fz             | j                            d|| j        f           |D ]i}| j                            d|                                || j        f           |                                }|	                                }|
                    d|	                                          }t          |                                d                   }t          |
                    d|                    }| j                            d|                                |                                |                                                                |                                                                ||||| j        f	           kd S )	Ns   ADD BUNDLE %r %rs5   INSERT INTO bundles(bundle, reponame) VALUES (%s, %s)paramsss   INSERT INTO nodestobundle(node, bundle, reponame) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE bundle=VALUES(bundle)s	   committerr   s   committer_dates   INSERT IGNORE INTO nodesmetadata(node, message, p1, p2, author, committer, author_date, committer_date, reponame) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s))r#   rD   r!   infor   r   rB   hexextrar*   getintdatedescriptionp1p2)	r&   bundleidnodesctxctxrZ   author_namecommitter_nameauthor_datecommitter_dates	            r   	addbundlezsqlindexapi.addbundley   s    	OO)T]H,EEFFFDdm, 	 	
 	
 	
  	 	CN"") 		8T];	 #    IIKKE((**K"YY|SXXZZ@@Nchhjjm,,K +<k!J!JKKNN""8
 GGIIOO%%FFHHLLNNFFHHLLNN""M
 #    	 	r   c                     | j         s|                                  | j                            d| j        ||fz             | j                            d||| j        f           dS )zRTakes a bookmark name and hash, and records mapping in the metadata
        store.s&   ADD BOOKMARKS %r bookmark: %r node: %rss   INSERT INTO bookmarkstonode(bookmark, node, reponame) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE node=VALUES(node)rV   Nr#   rD   r!   rX   r   r   rB   )r&   bookmarknodes      r   addbookmarkzsqlindexapi.addbookmark   s      	OO5}h-.	
 	
 	
 	MdDM2 	 	
 	
 	
 	
 	
r   c                 D   | j         s|                                  g }g }|                                D ]7\  }}|                    d           |                    ||| j        f           8d                    |          }| j                            d|z  |           d S )Ns   (%s, %s, %s)   ,si   INSERT INTO bookmarkstonode(bookmark, node, reponame) VALUES %s ON DUPLICATE KEY UPDATE node=VALUES(node)rV   )	r#   rD   itemsappendextendr   joinr   rB   )r&   	bookmarksargsvaluesrk   rl   s         r   addmanybookmarkszsqlindexapi.addmanybookmarks   s     	OO'oo// 	; 	;NHdKK(((MM8T4=9::::yyCEIJ 	 	
 	
 	
 	
 	
r   c                     | j         s|                                  | j                            d|z             |D ]4}t	          |          }| j                            d|| j        f           5dS )zAccepts list of bookmark patterns and deletes them.
        If `commit` is set then bookmark will actually be deleted. Otherwise
        deletion will be delayed until the end of transaction.
        s   DELETE BOOKMARKS: %ssF   DELETE from bookmarkstonode WHERE bookmark LIKE (%s) and reponame = %srV   N)r#   rD   r!   rX   r   r   rB   r   )r&   patternsr   s      r   deletebookmarkszsqlindexapi.deletebookmarks   s    
  	OO-8999 	 	G-g66GN""%/ #    	 	r   c                    | j         s|                                  | j                            d| j        |fz             | j                            d|| j        f           | j                                        }t          |          dk    st          |d                   dk    r| j                            d           dS |d         d         }| j                            d|z             |S )	zAReturns the bundleid for the bundle that contains the given node.s   GET BUNDLE %r %rsB   SELECT bundle from nodestobundle WHERE node = %s AND reponame = %srV   r   r   s   No matching nodeNs   Found bundle %r	r#   rD   r!   rX   r   r   rB   fetchalllen)r&   rl   resultbundles       r   	getbundlezsqlindexapi.getbundle   s     	OO)T]D,AABBB1$-( 	 	
 	
 	

 ((**v;;!s6!9~~22HMM-...41(61222r   c                    | j         s|                                  | j                            d| j        |fz             | j                            d|| j        f           | j                                        }t          |          dk    st          |d                   dk    r| j                            d           dS |d         d         }| j                            d|z             |S )	zBReturns the node for the given bookmark. None if it doesn't exist.s"   GET NODE reponame: %r bookmark: %rsF   SELECT node from bookmarkstonode WHERE bookmark = %s AND reponame = %srV   r   r   s   No matching bookmarkNs   Found node %rr|   )r&   rk   r   rl   s       r   getnodezsqlindexapi.getnode   s     	OO1T]H4MM	
 	
 	
 	/dm, 	 	
 	
 	

 ((**v;;!s6!9~~22HMM12224ay|&-...r   c                    | j         s|                                  | j                            d| j        |fz             t          |          }| j                            d| j        |f           | j                                        }i }|D ]D}t          |          dk    r| j                            d|z             3|d         ||d         <   E|S )Ns&   QUERY BOOKMARKS reponame: %r query: %rsS   SELECT bookmark, node from bookmarkstonode WHERE reponame = %s AND bookmark LIKE %srV      s   Bad row returned: %sr   r   )
r#   rD   r!   rX   r   r   r   rB   r}   r~   )r&   queryr   rt   rows        r   getbookmarkszsqlindexapi.getbookmarks   s     	OO58NN	
 	
 	
 (..2M5) 	 	
 	
 	

 ((**	 	' 	'C3xx1}}5;<<< #AIc!fr   c                     | j         s|                                  | j                            d| j        ||fz             | j                            d|| j        |f           d S )NsH   INSERT METADATA, QUERY BOOKMARKS reponame: %r node: %r, jsonmetadata: %ssP   UPDATE nodesmetadata SET optional_json_metadata=%s WHERE reponame=%s AND node=%srV   rj   )r&   rl   jsonmetadatas      r   saveoptionaljsonmetadataz$sqlindexapi.saveoptionaljsonmetadata
  s     	OO0 }dL1	2	
 	
 	
 	' $-6 	 	
 	
 	
 	
 	
r   )r   r   )__name__
__module____qualname____doc__r   rD   rI   rM   rT   rh   rm   rw   rz   r   r   r   r   __classcell__)r0   s   @r   r   r      s         ( ( ( ( ( (B" " "H    
$ $ $% % %N
 
 

 
 
      $  (  *
 
 
 
 
 
 
r   r   c                   *    e Zd ZdZddZddZddZdS )r9   ziEnsure that all values being returned are returned as python string
    (versus the default byte arrays).Nc                      t          |          S rL   strr&   valuedscs      r   _STRING_to_pythonz!CustomConverter._STRING_to_python       5zzr   c                      t          |          S rL   r   r   s      r   _VAR_STRING_to_pythonz%CustomConverter._VAR_STRING_to_python#  r   r   c                      t          |          S rL   r   r   s      r   _BLOB_to_pythonzCustomConverter._BLOB_to_python&  r   r   rL   )r   r   r   r   r   r   r   r3   r   r   r9   r9     sZ        ) )           r   r9   )r   r   r=   rF   mysql.connectorr5    r   r   r   r6   
conversionMySQLConverterr9   r3   r   r   <module>r      s     				              ~
 ~
 ~
 ~
 ~
(# ~
 ~
 ~
B    eo0?     r   