From d6fba8bad6277eb2a19e94f93a7a4baf74b29ef3 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Wed, 9 Feb 2011 17:57:03 -0700 Subject: [PATCH] persist_data: make SQLTable a context manager This can be used for more control over the underlying transactions. Unlike the context manager of, say, a file object, we can still use the object even after the end of a given with block, as the context manager exit only ensures we've committed to the database, not that we have closed the database. Signed-off-by: Chris Larson --- lib/bb/persist_data.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/bb/persist_data.py b/lib/bb/persist_data.py index aa2ede4b937..af96b76f401 100644 --- a/lib/bb/persist_data.py +++ b/lib/bb/persist_data.py @@ -55,6 +55,13 @@ class SQLTable(collections.MutableMapping): def _execute(self, *query): return self.cursor.execute(*query) + def __enter__(self): + self.cursor.__enter__() + return self + + def __exit__(self, *excinfo): + self.cursor.__exit__(*excinfo) + def __getitem__(self, key): data = self._execute("SELECT * from %s where key=?;" % self.table, [key]) -- 2.47.2