def deleteObject(self, key, bucket, commit=True): if self._bucket_exists(bucket): self.conn.execute( "DELETE from `{}` where key = ?".format(hash(bucket)), (key, )) if not self._has_keys(bucket): self.conn.execute("DROP TABLE IF EXISTS `{}`".format( hash(bucket))) if commit: self.conn.commit()
def deleteObject(self, key, bucket, commit=True): if self._bucket_exists(bucket): self.conn.execute("DELETE from `{}` where key = ?" .format(hash(bucket)), (key,)) if not self._has_keys(bucket): self.conn.execute("DROP TABLE IF EXISTS `{}`" .format(hash(bucket))) if commit: self.conn.commit()
def setObject(self, obj, key, bucket): if not self._bucket_exists(bucket): self._create_bucket(bucket) self.conn.execute( "REPLACE INTO `{}` (key, value) VALUES (?, ?)".format( hash(bucket)), (key, memoryview(compress(jsonify(obj))))) self.conn.commit()
def setObject(self, obj, key, bucket): if not self._bucket_exists(bucket): self._create_bucket(bucket) self.conn.execute("REPLACE INTO `{}` (key, value) VALUES (?, ?)" .format(hash(bucket)), (key, memoryview(compress(jsonify(obj))))) self.conn.commit()
def _has_keys(self, bucket): c = self.conn.execute("SELECT count(*) from `{}`".format(hash(bucket))) row = c.fetchone() if row[0]: return True else: return False
def _bucket_exists(self, bucket): c = self.conn.execute("SELECT name from sqlite_master " "WHERE type='table' and name=?", (hash(bucket),)) if c.fetchone(): return True else: return False
def _bucket_exists(self, bucket): c = self.conn.execute( "SELECT name from sqlite_master " "WHERE type='table' and name=?", (hash(bucket), )) if c.fetchone(): return True else: return False
def setObject(self, obj, key, bucket): if not self._bucket_exists(bucket): self._create_bucket(bucket) blob = sqlite3.Binary(compress(jsonify(obj).encode('utf-8'))) self.conn.execute("REPLACE INTO `{}` (key, value) VALUES (?, ?)" .format(hash(bucket)), (key, blob)) self.conn.commit()
def getObject(self, key, bucket): if not self._bucket_exists(bucket): return key, None c = self.conn.execute("SELECT key, value from `{}` WHERE key=?" .format(hash(bucket)), (key,)) row = c.fetchone() if row: k, v = row return (k, dejsonify(decompress(v).decode('utf-8'))) else: return key, None
def getObject(self, key, bucket): if not self._bucket_exists(bucket): return key, None c = self.conn.execute("SELECT key, value from `{}` WHERE key=?" .format(hash(bucket)), (key,)) row = c.fetchone() if row: k, v = row return (k, dejsonify(decompress(v))) else: return key, None
def _create_bucket(self, bucket): self.conn.execute("CREATE TABLE `{}` (key blob UNIQUE, value blob)" .format(hash(bucket)))
def updateObjectKey(self, bucket, oldkey, newkey): self.conn.execute( "UPDATE `{}` set key = ? where key=?".format(hash(bucket)), (newkey, oldkey)) self.conn.commit()
def getAllObjects(self, bucket): if not self._bucket_exists(bucket): return for k, v in self.conn.execute("SELECT key, value from `{}`" .format(hash(bucket))): yield (k, dejsonify(decompress(v)))
def updateObjectKey(self, bucket, oldkey, newkey): self.conn.execute("UPDATE `{}` set key = ? where key=?" .format(hash(bucket)), (newkey, oldkey)) self.conn.commit()
def _create_bucket(self, bucket): self.conn.execute( "CREATE TABLE `{}` (key blob UNIQUE, value blob)".format( hash(bucket)))
def getAllObjects(self, bucket): if not self._bucket_exists(bucket): return for k, v in self.conn.execute("SELECT key, value from `{}`".format( hash(bucket))): yield (k, dejsonify(decompress(v)))