update docstrings for isso.db

This commit is contained in:
Martin Zimmermann 2013-12-17 13:30:37 +01:00
parent c99fe3d583
commit a4213e4304
2 changed files with 29 additions and 17 deletions

View File

@ -11,6 +11,11 @@ from isso.db.spam import Guard
class SQLite3: class SQLite3:
"""DB-dependend wrapper around SQLite3.
Runs migration if `user_version` is older than `MAX_VERSION` and register
a trigger for automated orphan removal.
"""
MAX_VERSION = 1 MAX_VERSION = 1
@ -59,6 +64,8 @@ class SQLite3:
logger.info("migrate database from version %i to %i", self.version, to) logger.info("migrate database from version %i to %i", self.version, to)
# re-initialize voters blob due a bug in the bloomfilter signature
# which added older commenter's ip addresses to the current voters blob
if self.version == 0: if self.version == 0:
from isso.utils import Bloomfilter from isso.utils import Bloomfilter

View File

@ -10,13 +10,13 @@ class Comments:
"""Hopefully DB-independend SQL to store, modify and retrieve all """Hopefully DB-independend SQL to store, modify and retrieve all
comment-related actions. Here's a short scheme overview: comment-related actions. Here's a short scheme overview:
| tid (thread id) | cid (comment id) | parent | ... | likes | remote_addr | | tid (thread id) | id (comment id) | parent | ... | voters | remote_addr |
+-----------------+------------------+--------+-----+-------+-------------+ +-----------------+-----------------+--------+-----+--------+-------------+
| 1 | 1 | null | ... | BLOB | 127.0.0.0 | | 1 | 1 | null | ... | BLOB | 127.0.0.0 |
| 1 | 2 | 1 | ... | BLOB | 127.0.0.0 | | 1 | 2 | 1 | ... | BLOB | 127.0.0.0 |
+-----------------+------------------+--------+-----+-------+-------------+ +-----------------+-----------------+--------+-----+--------+-------------+
The tuple (tid, cid) is unique and thus primary key. The tuple (tid, id) is unique and thus primary key.
""" """
fields = ['tid', 'id', 'parent', 'created', 'modified', 'mode', 'remote_addr', fields = ['tid', 'id', 'parent', 'created', 'modified', 'mode', 'remote_addr',
@ -34,9 +34,8 @@ class Comments:
def add(self, uri, c): def add(self, uri, c):
""" """
Add a new comment to the database and return public fields as dict. Add new comment to DB and return a mapping of :attribute:`fields` and
Initializes voter bloom array with provided :param:`remote_addr` and database values.
adds a new thread to the `main.threads` table.
""" """
self.db.execute([ self.db.execute([
'INSERT INTO comments (', 'INSERT INTO comments (',
@ -60,7 +59,9 @@ class Comments:
(uri, )).fetchone())) (uri, )).fetchone()))
def activate(self, id): def activate(self, id):
"""Activate comment id if pending and return comment for (path, id).""" """
Activate comment id if pending.
"""
self.db.execute([ self.db.execute([
'UPDATE comments SET', 'UPDATE comments SET',
' mode=1', ' mode=1',
@ -68,11 +69,9 @@ class Comments:
def update(self, id, data): def update(self, id, data):
""" """
Update an existing comment, but only writeable fields such as text, Update comment :param:`id` with values from :param:`data` and return
author, email, website and parent. This method should set the modified updated comment.
field to the current time.
""" """
self.db.execute([ self.db.execute([
'UPDATE comments SET', 'UPDATE comments SET',
','.join(key + '=' + '?' for key in data), ','.join(key + '=' + '?' for key in data),
@ -82,7 +81,10 @@ class Comments:
return self.get(id) return self.get(id)
def get(self, id): def get(self, id):
"""
Search for comment :param:`id` and return a mapping of :attr:`fields`
and values.
"""
rv = self.db.execute('SELECT * FROM comments WHERE id=?', (id, )).fetchone() rv = self.db.execute('SELECT * FROM comments WHERE id=?', (id, )).fetchone()
if rv: if rv:
return dict(zip(Comments.fields, rv)) return dict(zip(Comments.fields, rv))
@ -91,7 +93,7 @@ class Comments:
def fetch(self, uri, mode=5): def fetch(self, uri, mode=5):
""" """
Return all comments for `path` with `mode`. Return comments for :param:`uri` with :param:`mode`.
""" """
rv = self.db.execute([ rv = self.db.execute([
'SELECT comments.* FROM comments INNER JOIN threads ON', 'SELECT comments.* FROM comments INNER JOIN threads ON',
@ -175,7 +177,7 @@ class Comments:
def count(self, uri): def count(self, uri):
""" """
return count of comments for uri. Return comment count for :param:`uri`.
""" """
return self.db.execute([ return self.db.execute([
'SELECT COUNT(comments.id) FROM comments INNER JOIN threads ON', 'SELECT COUNT(comments.id) FROM comments INNER JOIN threads ON',
@ -183,6 +185,9 @@ class Comments:
(uri, )).fetchone() (uri, )).fetchone()
def purge(self, delta): def purge(self, delta):
"""
Remove comments older than :param:`delta`.
"""
self.db.execute([ self.db.execute([
'DELETE FROM comments WHERE mode = 2 AND ? - created > ?;' 'DELETE FROM comments WHERE mode = 2 AND ? - created > ?;'
], (time.time(), delta)) ], (time.time(), delta))