# -*- coding: ISO-8859-1 -*- ################################## # @program smon # @description simulation monitor # @copyright Copyright “(c)2009 Centre National de la Recherche Scientifique CNRS. # All Rights Reserved” # @svn_file $Id: db.py 2585 2013-03-24 04:09:22Z jripsl $ # @version $Rev: 2585 $ # @lastrevision $Date: 2013-03-24 05:09:22 +0100 (Sun, 24 Mar 2013) $ # @license CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) ################################## from logger import * class CDatabase(): _conn=None @classmethod def connect(self): # When a database is accessed by multiple connections, and one of the processes # modifies the database, the SQLite database is locked until that transaction is # committed. The timeout parameter specifies how long the connection should wait # for the lock to go away until raising an exception. The default for the timeout # parameter is 5.0 (five seconds). # # more info here => http://www.sqlite.org/faq.html#q5 # # we increase the timeout so we are able to use sqlite3 to run manual # query without stopping the daemon # l__timeout=120 # 2 mn # Note # by default, sqlite is in autocommit mode, # but the sqlite3 python module is *not* in autocommit mode by default # we don't want autocommit mode, so we leave it at its default, which will result in a plain "BEGIN" statement # (If you want autocommit mode, then set isolation_level to None) # open connection self._conn=sqlite3.connect(self.get_DB_name(),l__timeout) self._conn.row_factory=sqlite3.Row # this is for "by name" colums indexing self.createTables() @classmethod def get_DB_name(self): return SMON.dbfile @classmethod def disconnect(self): if self.isConnected(): self._conn.close() self._conn=None @classmethod def isConnected(self): if (self._conn==None): return False else: return True @classmethod def createTables(self): c=self._conn.cursor() c.execute("create table if not exists message (id INTEGER PRIMARY KEY, name TEXT, timestamp TEXT, nested_level INT, simulation_id INT") c.execute("create index if not exists idx_message_1 on message (timestamp)") self._conn.commit() c.close()