def get(self, context, collection, id, query=None): """Gets an object from the database.""" c = context.conn.cursor() if id and type(id) != str and type(id) != unicode: id = unicode(id) fullObject = (id != None) if fullObject: statement = "select * from data where collection=? " else: statement = "select id from data where collection=? " values = [collection] if id: statement += " and id=?" values.append(id) if query: clauses, valueAdds = self.convertQueryToSQL(query) if len(clauses): statement += clauses if len(valueAdds): values += valueAdds c.execute(statement, values) try: if fullObject: result = c.fetchone() if result: parentid = None if result[2]: parentid = unicode(result[2]) predecessorid = None if result[3]: predecessorid = unicode(result[3]) wbo = storage.WBO(id=id, parentid=parentid, predecessorid=predecessorid, sortindex=result[4], payload=result[6], modified=result[5]) c.close() return wbo else: return None else: result = c.fetchall() c.close() return json.dumps([str(r[0]) for r in result]) except Exception, e: import traceback traceback.print_exc(e) raise storage.WeaveStorageException( "Error while accessing storage: %s" % e, e)
def collection_modification_date(self, context, collection): try: c = CONN.cursor() c.execute("select max(modified) from data where username=? and collection=?", (context.name, collection)) result = c.fetchone() c.close() return float(result[0])/100 except Exception, e: import traceback traceback.print_exc(e) raise storage.WeaveStorageException("Error while accessing storage: %s" % e, e)
def add_or_modify(self, context, collection, item, id=None, query=None): c = context.conn.cursor() wbo = storage.WBO(item) try: if id: if wbo.id and id != wbo.id: raise storage.WeaveStorageException( "ID mismatch: URL must match ID in JSON-encoded object" ) else: if not wbo.id: raise storage.WeaveStorageException( "Item passed to add_or_modify must have an 'id' value") else: id = wbo.id if wbo.payload: c.execute( "insert or replace into data values(?,?,?,?,?,?,?,?)", (collection, id, wbo.parentid, wbo.predecessorid, wbo.sortindex, wbo.modified, wbo.payload, len( wbo.payload))) else: params, vals = getWBOUpdateStatement(wbo) if len(params) != 0: # Make sure metadata update has something to do! statement = "update data set " + params + " where collection = ? and id = ?" vals.append(collection) vals.append(wbo.id) c.execute(statement, vals) c.execute("select * from data where collection=? and id=?", (collection, wbo.id)) result = c.fetchone() context.conn.commit() except storage.WeaveStorageException, we: c.close() raise we
def collection_counts(self, context): try: c = CONN.cursor() c.execute("select collection, count(*) as ct from data where username=? group by collection", (context.name,)) result = c.fetchall() c.close() retMap = {} for r in result: retMap[r[0]] = str(r[1]) # making this into a string because that's what the suite says to do. return retMap except Exception, e: import traceback traceback.print_exc(e) raise storage.WeaveStorageException("Error while accessing storage: %s" % e, e)
def collection_timestamps(self, context): try: c = CONN.cursor() c.execute("select collection, max(modified) as timestamp from data where username=? group by collection", (context.name,)) result = c.fetchall() c.close() retMap = {} for r in result: retMap[r[0]] = float(r[1])/100 return retMap except Exception, e: import traceback traceback.print_exc(e) raise storage.WeaveStorageException("Error while accessing storage: %s" % e, e)
except: try: # Note that modified is in centiseconds c.execute("""create table data \ (username TEXT NOT NULL, \ 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 ('username','collection','id'))""") except Exception, e: raise storage.WeaveStorageException("Fatal error: Unable to create table 'data' for storage: %s" % e) c.close() except storage.WeaveStorageException, e: print e sys.exit(1) class WeaveSQLiteStorageContext(object): def __init__(self, name): self.name = name class WeaveSQLiteStorage(object): """Implements the Weave Storage API using a sqlite database. >>> storage = WeaveSQLiteStorage()