503
503
def __len__(self):
504
504
return len(self.host)
506
# TODO the whole history index stuff needs to be refactored
507
# make it simpler if possible, following this code is already too brain cracking for me
508
506
class FactosSqliteDB(databases.ChannelSqliteDB):
509
507
"""Class for manipulating the sqlite database in Supybot, with methods specific to the Factos
512
def __init__(self, filename, tableFact, tableHist, tableUser):
510
def __init__(self, filename, tableFact, tableHist, tableHistRedo, tableUser):
513
511
self.__parent = super(FactosSqliteDB, self)
514
512
self.__parent.__init__(filename)
516
514
self.tableFact = tableFact
517
515
self.tableHist = tableHist
516
self.tableHistRedo = tableHistRedo
518
517
self.tableUser = tableUser
519
518
tableVersion = databases.Table(tblVersionStruct, 'version')
520
519
# check database and update it if needed
617
616
"""Gets the next id for users."""
618
617
return self.newId('user_id', select='user_id', order='user_id DESC', **kwargs)
620
def newHistId(self, name, **kwargs):
619
def newHistId(self, name, prefix, **kwargs):
621
620
"""Gets the next id for a history record of a particular fact."""
622
key = 'hist_' + name # otherwise a fact called 'user_id' can screw everything.
623
id = self.newId(key, select='history_id', where='name', equal=name, default=1, order='history_id DESC',
621
key = prefix + name # otherwise a fact called 'user_id' can screw everything.
622
id = self.newId(key, select='history_id', where='name', equal=name, order='history_id DESC',
627
625
self.indexes[key] = id
630
def getHist(self, where=None, **kwargs):
628
def getHist(self, where=None, table=None, **kwargs):
631
629
"""Fetchs rows from the history table."""
634
return self.__parent.get(table=self.tableHist, where=where, order='history_id', **kwargs)
632
return self.__parent.get(table=table or self.tableHist, where=where, order='history_id DESC', **kwargs)
636
def addHist(self, values, channel=None):
634
def addHist(self, values, table=None, prefix='undo', channel=None):
637
635
"""Adds new history record."""
638
self.setup(self.tableHist, channel=channel, create=True)
639
values['history_id'] = self.newHistId(values['name'])
636
self.setup(table or self.tableHist, channel=channel, create=True)
637
id = self.newHistId(values['name'], prefix)
639
values['history_id'] = id
640
640
self._insert(values)
642
def updateHist(self, hist, current_id, new_id):
642
def updateHist(self, hist, history_id, undo=True, channel=None):
643
643
"""Updates history when performing an undo or redo operation, undo history has positive indexes,
644
644
while redo are negative."""
645
if self.setup(self.tableHist):
646
hist['history_id'] = new_id
648
self.update(hist, where='name=\'%s\' and history_id=\'%s\'' %(name, current_id))
647
table = self.tableHist
650
table = self.tableHistRedo
652
if self.setup(table, channel=channel):
651
654
id = self.indexes[key]
653
656
id = self.getOne(select='history_id', where='name', equal=name,
654
657
order='history_id DESC')
657
# we did an undo, reduce index by one
658
self.indexes[key] = id - 1
660
# we did a redo, increase index by one
661
self.indexes[key] = id + 1
659
self.indexes[key] = id - 1
660
self.delete('name=\'%s\' and history_id=\'%s\'' %(name, history_id))
662
table = self.tableHistRedo
665
table = self.tableHist
667
self.addHist(hist, table=table, prefix=action, channel=channel)
663
669
def getUserById(self, user_id):
664
670
"""Returns a user that matches user_id in the cache or database."""