From a40d0d0e901d0fc9d1f71bd6269e85586bd0ef12 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 13 Jun 2008 14:56:11 +0000 Subject: Address Tomeu review comments. --- (limited to 'places.py') diff --git a/places.py b/places.py index 821c894..1277835 100644 --- a/places.py +++ b/places.py @@ -39,69 +39,79 @@ class SqliteStore(object): db_path = os.path.join(activity.get_activity_root(), 'data', 'places.db') - self._con = sqlite3.connect(db_path) - cur = self._con.cursor() - - cur.execute('select * from sqlite_master where name == "places"') - if cur.fetchone() == None: - cur.execute("""create table places ( - uri text, - title text, - bookmark boolean, - gecko_flags integer, - visits integer, - last_visit timestamp - ); - """) + self._connection = sqlite3.connect(db_path) + cursor = self._connection.cursor() + + cursor.execute('select * from sqlite_master where name == "places"') + if cursor.fetchone() == None: + cursor.execute("""create table places ( + uri text, + title text, + bookmark boolean, + gecko_flags integer, + visits integer, + last_visit timestamp + ); + """) else: self._cleanup() def search(self, text): - cur = self._con.cursor() + cursor = self._connection.cursor() - text = '%' + text + '%' - cur.execute('select * from places where uri like ? or title like ? ' \ - 'order by visits desc limit 0, ?', - (text, text, self.MAX_SEARCH_MATCHES)) + try: + text = '%' + text + '%' + cursor.execute('select uri, title, bookmark, gecko_flags, ' \ + 'visits, last_visit from places ' \ + 'where uri like ? or title like ? ' \ + 'order by visits desc limit 0, ?', + (text, text, self.MAX_SEARCH_MATCHES)) - result = [self._place_from_row(row) for row in cur] - - cur.close() + result = [self._place_from_row(row) for row in cursor] + finally: + cursor.close() return result def add_place(self, place): - cur = self._con.cursor() - - cur.execute('insert into places values (?, ?, ?, ?, ?, ?)', \ - (place.uri, place.title, place.bookmark, - place.gecko_flags, place.visits, place.last_visit)) - - self._con.commit() - cur.close() + cursor = self._connection.cursor() + + try: + cursor.execute('insert into places (uri, title, bookmark, ' \ + 'gecko_flags, visits, last_visit) ' \ + 'values (?, ?, ?, ?, ?, ?)', \ + (place.uri, place.title, place.bookmark, + place.gecko_flags, place.visits, place.last_visit)) + self._connection.commit() + finally: + cursor.close() def lookup_place(self, uri): - cur = self._con.cursor() - cur.execute('select * from places where uri=?', (uri,)) + cursor = self._connection.cursor() - row = cur.fetchone() - if row: - return self._place_from_row(row) - else: - return None + try: + cursor.execute('select uri, title, bookmark, gecko_flags,visits, ' \ + 'last_visit from places where uri=?', (uri,)) - cur.close() + row = cursor.fetchone() + if row: + return self._place_from_row(row) + else: + return None + finally: + cursor.close() def update_place(self, place): - cur = self._con.cursor() + cursor = self._connection.cursor() - cur.execute('update places set title=?, gecko_flags=?, ' - 'visits=?, last_visit=?, bookmark=? where uri=?', - (place.title, place.gecko_flags, place.visits, - place.last_visit, place.bookmark, place.uri)) - - self._con.commit() - cur.close() + try: + cursor.execute('update places set title=?, gecko_flags=?, ' + 'visits=?, last_visit=?, bookmark=? where uri=?', + (place.title, place.gecko_flags, place.visits, + place.last_visit, place.bookmark, place.uri)) + self._connection.commit() + finally: + cursor.close() def _place_from_row(self, row): place = Place() @@ -112,13 +122,14 @@ class SqliteStore(object): return place def _cleanup(self): - cur = self._con.cursor() - - date = datetime.now() - timedelta(days=self.EXPIRE_DAYS) - cur.execute('delete from places where last_visit < ?', (date,)) - - self._con.commit() - cur.close() + cursor = self._connection.cursor() + + try: + date = datetime.now() - timedelta(days=self.EXPIRE_DAYS) + cursor.execute('delete from places where last_visit < ?', (date,)) + self._connection.commit() + finally: + cursor.close() def get_store(): global _store -- cgit v0.9.1