1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
6
"""Journal using twisted.enterprise.row RDBMS support.
8
You're going to need the following table in your database::
10
| CREATE TABLE journalinfo
14
| INSERT INTO journalinfo VALUES (0);
18
from __future__ import nested_scopes
21
from twisted.internet import defer
27
# constants for command list
28
INSERT, DELETE, UPDATE = range(3)
31
class RowJournal(base.Journal):
32
"""Journal that stores data 'snapshot' in using twisted.enterprise.row.
34
Use this as the reflector instead of the original reflector.
36
It may block on creation, if it has to run recovery.
39
def __init__(self, log, journaledService, reflector):
40
self.reflector = reflector
43
base.Journal.__init__(self, log, journaledService)
45
def updateRow(self, obj):
46
"""Mark on object for updating when sync()ing."""
47
self.commands.append((UPDATE, obj))
49
def insertRow(self, obj):
50
"""Mark on object for inserting when sync()ing."""
51
self.commands.append((INSERT, obj))
53
def deleteRow(self, obj):
54
"""Mark on object for deleting when sync()ing."""
55
self.commands.append((DELETE, obj))
57
def loadObjectsFrom(self, tableName, parentRow=None, data=None, whereClause=None, forceChildren=0):
58
"""Flush all objects to the database and then load objects."""
60
d.addCallback(lambda result: self.reflector.loadObjectsFrom(
61
tableName, parentRow=parentRow, data=data, whereClause=whereClause,
62
forceChildren=forceChildren))
66
"""Commit changes to database."""
68
raise ValueError, "sync already in progress"
69
comandMap = {INSERT : self.reflector.insertRowSQL,
70
UPDATE : self.reflector.updateRowSQL,
71
DELETE : self.reflector.deleteRowSQL}
73
for kind, obj in self.commands:
74
sqlCommands.append(comandMap[kind](obj))
78
d = self.reflector.dbpool.runInteraction(self._sync, self.latestIndex, sqlCommands)
79
d.addCallback(self._syncDone)
82
return defer.succeed(1)
84
def _sync(self, txn, index, commands):
85
"""Do the actual database synchronization."""
88
txn.update("UPDATE journalinfo SET commandIndex = %d" % index)
90
def _syncDone(self, result):
94
def getLastSnapshot(self):
95
"""Return command index of last snapshot."""
96
conn = self.reflector.dbpool.connect()
97
cursor = conn.cursor()
98
cursor.execute("SELECT commandIndex FROM journalinfo")
99
return cursor.fetchall()[0][0]