o ]Lb&@sddlmZddlZddlZddlZddlZddlZddlm Z ddl m Z ddZ Gdd d e j Z Gd d d ejjjZdS) )absolute_importN)pycompat)indexapicCs6|dd}|dd}|dr|ddd}|S)N_s\_%s\%*)replaceendswith)patternr @/usr/lib/python3/dist-packages/hgext/infinitepush/sqlindexapi.py_convertbookmarkpatterns   rcseZdZdZ  dfdd ZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZZS) sqlindexapiz< Sql backend for infinitepush index. See schema.sql ,xc sttt|||_|||||d|_d|_d|_|stj}t j |dt |_ |j |d|_| |_| |_dS)N)shostsportsdatabasesuserspassword)filenameF)superr__init__reponamesqlargssqlconn sqlcursorosdevnulllogging basicConfig getLoggerlogsetLevel _connected _waittimeout _locktimeout) selfrhostportdatabaseuserpasswordlogfileloglevel waittimeout locktimeout __class__r rr#s$     zsqlindexapi.__init__cCs|jrtd|jrtdd} ztjjd i|j|_|jt d|j_ Wntjj j yD|d8}|dkr=t dYnwq|jjd |j}|j|_|jd ||jd |jd|_dS) NsSQL connection already opens*SQL cursor already open without connectionTFrrg?s%ssSET wait_timeout=%ssSET innodb_lock_wait_timeout=%sr )rrindexexceptionrmysql connectorconnectrset_converter_classCustomConverter autocommiterrorsErrortimesleep converterescaper"cursorexecuter#r!)r$retryr,r r r sqlconnectDs6    zsqlindexapi.sqlconnectcCsVttd|j|jWdn1swYd|_d|_dS)z(Cleans up the metadata store connection.signoreN)warningscatch_warnings simplefilterrcloserr$r r rrEhs     zsqlindexapi.closecCs|js||SN)r!rArFr r r __enter__qszsqlindexapi.__enter__cCs$|dur |jdS|jdSrG)rcommitrollback)r$exc_typeexc_valexc_tbr r r__exit__vszsqlindexapi.__exit__c Cs|js||jd|j|f|jjd||jfd|D]N}|jjd|||jfd|}| }| d| }t | d}t | d|}|jjd|| |||||||jf dqdS) NsADD BUNDLE %r %rs5INSERT INTO bundles(bundle, reponame) VALUES (%s, %s)paramsssINSERT INTO nodestobundle(node, bundle, reponame) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE bundle=VALUES(bundle)s committerrscommitter_datesINSERT 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!rArinforrr?hexextrar(getintdate descriptionp1p2) r$bundleidnodesctxctxrS author_namecommitter_name author_datecommitter_dater r r addbundle|s>  zsqlindexapi.addbundlecCsB|js||jd|j||f|jjd|||jfddS)zRTakes a bookmark name and hash, and records mapping in the metadata store.s&ADD BOOKMARKS %r bookmark: %r node: %rssINSERT INTO bookmarkstonode(bookmark, node, reponame) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE node=VALUES(node)rONr!rArrQrrr?)r$bookmarknoder r r addbookmarks   zsqlindexapi.addbookmarkcCsh|js|g}g}t|D]\}}|d||||jfqd|}|jj d||ddS)Ns (%s, %s, %s),siINSERT INTO bookmarkstonode(bookmark, node, reponame) VALUES %s ON DUPLICATE KEY UPDATE node=VALUES(node)rO) r!rAr iteritemsappendextendrjoinrr?)r$ bookmarksargsvaluesrcrdr r raddmanybookmarkss   zsqlindexapi.addmanybookmarkscCsJ|js||jd||D]}t|}|jjd||jfdqdS)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. sDELETE BOOKMARKS: %ssFDELETE from bookmarkstonode WHERE bookmark LIKE (%s) and reponame = %srON)r!rArrQrrr?r)r$patternsr r r rdeletebookmarksszsqlindexapi.deletebookmarkscC|js||jd|j|f|jjd||jfd|j}t|dks0t|ddkr8|jddS|dd}|jd||S) zAReturns the bundleid for the bundle that contains the given node.sGET BUNDLE %r %rsBSELECT bundle from nodestobundle WHERE node = %s AND reponame = %srOrrsNo matching nodeNsFound bundle %r r!rArrQrrr?fetchalllen)r$rdresultbundler r r getbundles   zsqlindexapi.getbundlecCrq) zBReturns the node for the given bookmark. None if it doesn't exist.s"GET NODE reponame: %r bookmark: %rsFSELECT node from bookmarkstonode WHERE bookmark = %s AND reponame = %srOrrsNo matching bookmarkNs Found node %rrr)r$rcrurdr r rgetnodes     zsqlindexapi.getnodecCs|js||jd|j|ft|}|jjd|j|fd|j}i}|D]}t |dkr;|jd|q*|d||d<q*|S)Ns&QUERY BOOKMARKS reponame: %r query: %rsSSELECT bookmark, node from bookmarkstonode WHERE reponame = %s AND bookmark LIKE %srOsBad row returned: %srr) r!rArrQrrrr?rsrt)r$queryrurkrowr r r getbookmarkss$   zsqlindexapi.getbookmarkscCsB|js||jd|j||f|jjd||j|fddS)NsHINSERT METADATA, QUERY BOOKMARKS reponame: %r node: %r, jsonmetadata: %ssPUPDATE nodesmetadata SET optional_json_metadata=%s WHERE reponame=%s AND node=%srOrb)r$rd jsonmetadatar r rsaveoptionaljsonmetadata s   z$sqlindexapi.saveoptionaljsonmetadata)rr)__name__ __module__ __qualname____doc__rrArErHrNrarernrprwrxr|r~ __classcell__r r r.rrs"!$ 'rc@s.eZdZdZd ddZd ddZd ddZdS) r6ziEnsure that all values being returned are returned as python string (versus the default byte arrays).NcCt|SrGstrr$valuedscr r r_STRING_to_python#z!CustomConverter._STRING_to_pythoncCrrGrrr r r_VAR_STRING_to_python&rz%CustomConverter._VAR_STRING_to_pythoncCrrGrrr r r_BLOB_to_python)rzCustomConverter._BLOB_to_pythonrG)rrrrrrrr r r rr6s   r6) __future__rrrr:rBmysql.connectorr2 mercurialrrrrr3 conversionMySQLConverterr6r r r rs