[hamster-applet] optimizations to the swap code to have it a bit more



commit 78a7e1304d1d991870490ef61d2bdb8bcffaf3ec
Author: Toms Bauģis <toms baugis gmail com>
Date:   Mon Apr 20 22:15:39 2009 +0100

    optimizations to the swap code to have it a bit more
    responsive
---
 hamster/db.py          |   26 +++++++++++++++-----------
 hamster/preferences.py |    6 ++++--
 hamster/storage.py     |    4 ++--
 3 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/hamster/db.py b/hamster/db.py
index 5f98510..1a8628b 100644
--- a/hamster/db.py
+++ b/hamster/db.py
@@ -25,6 +25,7 @@ try:
     import sqlite3 as sqlite
 except ImportError:
     try:
+        print "Using sqlite2"
         from pysqlite2 import dbapi2 as sqlite
     except ImportError:
         print "Error: Neither sqlite3 nor pysqlite2 found"
@@ -577,13 +578,12 @@ class Storage(hamster.storage.Storage):
         self.execute("delete from categories where id = ?", (id, ))
         
     
-    def __swap_activities(self, id1, id2):
+    def __swap_activities(self, id1, priority1, id2, priority2):
         """ swaps nearby activities """
         # TODO - 2 selects and 2 updates is wrong we could live without selects
-        priority1 = self.fetchone("select activity_order from activities where id = ?", (id1,))[0]
-        priority2 = self.fetchone("select activity_order from activities where id = ?", (id2,))[0]
-        self.execute("update activities set activity_order = ? where id = ?", (priority1, id2) )
-        self.execute("update activities set activity_order = ? where id = ?", (priority2, id1) )
+        self.execute(["update activities set activity_order = ? where id = ?",
+                      "update activities set activity_order = ? where id = ?"],
+                      [(priority1, id2), (priority2, id1)])
 
     def __add_activity(self, name, category_id = None):
         # first check that we don't have anything like that yet
@@ -647,17 +647,21 @@ class Storage(hamster.storage.Storage):
             return None
 
     def execute(self, statement, params = ()):
+        """execute sql statement. optionally you can give multiple statements
+        to save on cursor creation and closure"""
         con = self.connection
         cur = con.cursor()
-
-        if hamster.trace_sql:
-            print statement, params
-
-        res = cur.execute(statement, params)
+        
+        if type(statement) == list:
+            for i in range(len(statement)):
+                if hamster.trace_sql:
+                    print statement[i], params[i]
+         
+                res = cur.execute(statement[i], params[i])
 
         con.commit()
         cur.close()
-
+        
     def run_fixtures(self):
         # defaults
         work_category = {"name": _("Work"),
diff --git a/hamster/preferences.py b/hamster/preferences.py
index 560b31c..7210425 100755
--- a/hamster/preferences.py
+++ b/hamster/preferences.py
@@ -583,7 +583,8 @@ class PreferencesEditor:
 
         #previous item
         prev_iter = get_prev(self.selection, model)
-        storage.swap_activities(model[iter][0], model[prev_iter][0])
+        storage.swap_activities(model[iter][0], model[iter][3],
+                                model[prev_iter][0], model[prev_iter][3])
         model.move_before(iter, prev_iter)
 
         self.activity_changed(self.selection, model)
@@ -592,7 +593,8 @@ class PreferencesEditor:
         (model, iter) = self.selection.get_selected()
 
         next_iter = model.iter_next(iter)
-        storage.swap_activities(model[iter][0], model[next_iter][0])
+        storage.swap_activities(model[iter][0], model[iter][3],
+                                model[next_iter][0], model[next_iter][3])
         self.activity_store.move_after(iter, next_iter)
 
         self.activity_changed(self.selection, model)
diff --git a/hamster/storage.py b/hamster/storage.py
index f69047d..f56fbeb 100644
--- a/hamster/storage.py
+++ b/hamster/storage.py
@@ -93,8 +93,8 @@ class Storage(object):
             self.dispatch('activity_updated', ())
         return changed
 
-    def swap_activities(self, id1, id2):
-        res = self.__swap_activities(id1, id2)
+    def swap_activities(self, id1, priority1, id2, priority2):
+        res = self.__swap_activities(id1, priority1, id2, priority2)
         self.dispatch('activity_updated', ())
         return res
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]