Ñò ÅEBMc @s½ddkZddkZddkZddkZddkZddkZddkZd„Zde fd„ƒYZ de fd„ƒYZ d„Z e djoddkZeiƒndS( iÿÿÿÿNcCsGyti|ƒWn/tj o#}|itijoqC‚nXdS(N(tostmakedirstOSErrorterrnotEEXIST(tpathtexc((s./usr/local/www/weave/storage_peruser_sqlite.pytmkdir_ps t WeaveSQLitePerUserStorageContextcBseZd„Zd„ZRS(cCs!||_t|ƒdjo#d|d|d|df}nUt|ƒdjod|d|df}n&t|ƒdjod|d}ntii|ƒpt|ƒn|d|}tii|ƒoti|ƒ|_nti|ƒ|_|i ƒ|ii ƒi d ƒdS( Nisuserdbs/%s/%s/%siiisuserdbs/%s/%s/_suserdbs/%s/_/_t/sPRAGMA synchronous=0( tnametlenRRtexistsRtsqlite3tconnecttconntcreatetcursortexecute(tselfR tdbDirtdbName((s./usr/local/www/weave/storage_peruser_sqlite.pyt__init__s # cCs7|iiƒ}|idƒ|iiƒ|iƒdS(Nshcreate table data (collection TEXT NOT NULL, id VARBINARY(64) NOT NULL default '', parentid VARBINARY(64) default NULL, predecessorid VARBINARY(64) default NULL, sortindex INT(11) default NULL, modified INT(11) default NULL, payload BLOB, payload_size INT(11) default NULL, PRIMARY KEY ('collection','id'))(RRRtcommittclose(Rtc((s./usr/local/www/weave/storage_peruser_sqlite.pyR/s   (t__name__t __module__RR(((s./usr/local/www/weave/storage_peruser_sqlite.pyRs tWeavePerUserSQLiteStoragecBseeZdZd„Zd„Zd d„Zd„Zd„Zd„Z d d d„Z d d d„Z RS( s÷Implements the Weave Storage API using a sqlite database. >>> storage = WeavePerUserSQLiteStorage() >>> ctx = storage.get_context("unit_test") Normally you will create an object with an id and a payload, which can be a string or a JSON-encodable object. The return value from add_or_modify will be a timestamp. If an object is passed int as the payload, it will be serialized as JSON automatically, and deserialized on the way back out. >>> timestamp = storage.add_or_modify(ctx, "collection", {'id':'def','payload':{'a':'1','b':2}}) >>> type(timestamp) >>> wbo = storage.get(ctx, "collection", 'def') >>> wbo.payload u'{"a": "1", "b": 2}' Calls to an object with the same ID replace it. >>> ts = storage.add_or_modify(ctx, "collection", {'id':'replaceme','payload':{'a':'1'}}) >>> ts = storage.add_or_modify(ctx, "collection", {'id':'replaceme','payload':{'a':'2'}}) >>> wbo = storage.get(ctx, "collection", 'replaceme') >>> wbo.payload u'{"a": "2"}' You can update the metadata by calling again without a payload >>> ts = storage.add_or_modify(ctx, "collection", {'id':'updateme','sortindex':5,'payload':{'a':'1'}}) >>> ts = storage.add_or_modify(ctx, "collection", {'id':'updateme','sortindex':4}) >>> wbo = storage.get(ctx, "collection", 'updateme') >>> wbo.sortindex 4 #>>> storage.add_or_modify(ctx, "collection", {'id':'hello'}) #Traceback (most recent call last): # ... #WeaveStorageException: Item passed to add_or_modify must have an 'payload' value Attempts to create an object without an id or payload will fail. >>> storage.add_or_modify(ctx, "collection", {'payload':{'a':'1','b':2}}) Traceback (most recent call last): ... WeaveStorageException: Item passed to add_or_modify must have an 'id' value cCs t|ƒS(N(R(RR ((s./usr/local/www/weave/storage_peruser_sqlite.pyt get_contextrscCs»g}g}d|jop|idƒ|ddidƒ}|t7}|idig}|D] }|dq^~ƒƒ|idƒnd|jo&|idƒ|i|ddƒnd |jo&|id ƒ|i|d dƒnd |jo&|id ƒ|i|d dƒnd |jo&|idƒ|i|d dƒnd|jo0|idƒ|it|ddƒdƒnd|jo0|idƒ|it|ddƒdƒnd|jol|dd}|djo|idƒqH|djo|idƒqH|djo|idƒqHnd|joS|idt|ddƒƒd|jo#|idt|ddƒƒq¨ndi|ƒ|fS( s®Given a map of parameters, creates a SQL statement fragment encoding the parameters as WHERE, LIMIT, and/or OFFSET clauses. Returns a tuple of (sql fragment, values tuple)tidss and id in (it,t?t)tparentids and parentid = ?t predecessorids and predecessorid = ?t index_aboves and sortindex > ?t index_belows and sortindex < ?tnewers and modified > ?idtolders and modified < ?tsorttindexs order by sortindex desc tnewests order by modified desc toldests order by modified tlimits limit %dtoffsets offset %dt(tappendtsplittjointfloattint(Rtqueryt statementstvaluestidValst_[1]tits((s./usr/local/www/weave/storage_peruser_sqlite.pytconvertQueryToSQLusN   1          #  #      'cCsN|iiƒ}|o6t|ƒtjo#t|ƒtjot|ƒ}n|dj}|o d}nd}|g}|o|d7}|i|ƒn|oO|i|ƒ\} } t| ƒo|| 7}nt| ƒo|| 7}qðn|i ||ƒy|o°|i ƒ} | o•d} | dot| dƒ} nd} | dot| dƒ} nt i d|d| d| d | d d | d d | dƒ}|i ƒ|SdSnI|iƒ} |i ƒtig}| D]}|t|dƒqá~ƒSWnDtj o8}ddk}|i|ƒt id||ƒ‚nXdS(s!Gets an object from the database.s&select * from data where collection=? s'select id from data where collection=? s and id=?iitidR"R#t sortindexitpayloaditmodifiediiiÿÿÿÿNs!Error while accessing storage: %s(RRttypetstrtunicodetNoneR/R;R Rtfetchonet storage_basetWBORtfetchalltjsontdumpst Exceptiont tracebackt print_exctWeaveStorageException(Rtcontextt collectionR<R4Rt fullObjectt statementR6tclausest valueAddstresultR"R#twboR8trteRK((s./usr/local/www/weave/storage_peruser_sqlite.pytget±sN-         <   6  cCs™yN|iiƒ}|id|fƒ|iƒ}|iƒt|dƒdSWnDtj o8}ddk}|i|ƒt i d||ƒ‚nXdS(Ns1select max(modified) from data where collection=?iidiÿÿÿÿs!Error while accessing storage: %s( RRRRDRR2RJRKRLRERM(RRNRORRTRWRK((s./usr/local/www/weave/storage_peruser_sqlite.pytcollection_modification_dateäs    cCs¸ym|iiƒ}|idƒ|iƒ}|iƒh}x*|D]"}t|dƒd||dRR"R#R=R?R tgetWBOUpdateStatementR/RDRRRJRKRLR2R3ttime(RRNROtitemR<R4RRUtparamstvalsRQRTtweRWRK((s./usr/local/www/weave/storage_peruser_sqlite.pyt add_or_modifysB   !#        cCsŽ|iiƒ}yÌd}|g}|o|d7}|i|ƒn|oO|i|ƒ\}} t|ƒo||7}nt| ƒo|| 7}q™nti|ƒti|ƒ|i||ƒ|iƒ|ii ƒWn¬t i j o} |iƒ| ‚n†t i j o)} |iƒt i d| | ƒ‚nOtj oB} |iƒddk} | i| ƒt i d| | ƒ‚nXdS(Ns$delete from data where collection=? s and id=?sbOperational error while performing delete: Was SQLite3 compiled with support for DELETE_LIMIT?: %siÿÿÿÿs%Error accessing object for delete: %s(RRR/R;R tloggingterrorRRRRERMR tOperationalErrorRJRKRL(RRNROR<R4RRQR6RRRSRbtsqeRWRK((s./usr/local/www/weave/storage_peruser_sqlite.pytdelete;s:             N( RRt__doc__RR;RCRXRYR[R\RcRh(((s./usr/local/www/weave/storage_peruser_sqlite.pyRAs/  < 3  ,cCs-g}g}|io!|idƒ|i|iƒn|io!|idƒ|i|iƒn|io!|idƒ|i|iƒn|ioD|idƒ|idƒ|i|iƒ|it|iƒƒn|ip |io+|io!|idƒ|i|iƒndi|ƒ|fS(sûReturns a tuple containing the update clause of a SQL statement, and the values needed for that statement, to update a WBO. e.g. for {'id':'abc', 'parentid': 'def', 'sortindex': 5}, ('parentid = ?,sortindex = ?,modified = ?', ['def', 5, ...]) s parentid = ?spredecessorid = ?s sortindex = ?s payload = ?spayloadSize = ?s modified = ?R(R"R/R#R=R>R R?R1(RUtupdatesR`((s./usr/local/www/weave/storage_peruser_sqlite.pyR]`s(          t__main__(RdR^RHRER tos.pathRRRtobjectRRR]Rtdoctestttestmod(((s./usr/local/www/weave/storage_peruser_sqlite.pyts        )ÿ %