1 | # -*- coding: ISO-8859-1 -*- |
---|
2 | |
---|
3 | ################################## |
---|
4 | # @program smon |
---|
5 | # @description simulation monitor |
---|
6 | # @copyright Copyright â(c)2009 Centre National de la Recherche Scientifique CNRS. |
---|
7 | # All Rights Reservedâ |
---|
8 | # @svn_file $Id: db.py 2585 2013-03-24 04:09:22Z jripsl $ |
---|
9 | # @version $Rev: 2585 $ |
---|
10 | # @lastrevision $Date: 2013-03-24 05:09:22 +0100 (Sun, 24 Mar 2013) $ |
---|
11 | # @license CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) |
---|
12 | ################################## |
---|
13 | |
---|
14 | from logger import * |
---|
15 | |
---|
16 | class CDatabase(): |
---|
17 | _conn=None |
---|
18 | |
---|
19 | @classmethod |
---|
20 | def connect(self): |
---|
21 | |
---|
22 | # When a database is accessed by multiple connections, and one of the processes |
---|
23 | # modifies the database, the SQLite database is locked until that transaction is |
---|
24 | # committed. The timeout parameter specifies how long the connection should wait |
---|
25 | # for the lock to go away until raising an exception. The default for the timeout |
---|
26 | # parameter is 5.0 (five seconds). |
---|
27 | # |
---|
28 | # more info here => http://www.sqlite.org/faq.html#q5 |
---|
29 | # |
---|
30 | # we increase the timeout so we are able to use sqlite3 to run manual |
---|
31 | # query without stopping the daemon |
---|
32 | # |
---|
33 | l__timeout=120 # 2 mn |
---|
34 | |
---|
35 | # Note |
---|
36 | # by default, sqlite is in autocommit mode, |
---|
37 | # but the sqlite3 python module is *not* in autocommit mode by default |
---|
38 | # we don't want autocommit mode, so we leave it at its default, which will result in a plain "BEGIN" statement |
---|
39 | # (If you want autocommit mode, then set isolation_level to None) |
---|
40 | |
---|
41 | # open connection |
---|
42 | self._conn=sqlite3.connect(self.get_DB_name(),l__timeout) |
---|
43 | self._conn.row_factory=sqlite3.Row # this is for "by name" colums indexing |
---|
44 | |
---|
45 | self.createTables() |
---|
46 | |
---|
47 | @classmethod |
---|
48 | def get_DB_name(self): |
---|
49 | return SMON.dbfile |
---|
50 | |
---|
51 | @classmethod |
---|
52 | def disconnect(self): |
---|
53 | if self.isConnected(): |
---|
54 | self._conn.close() |
---|
55 | |
---|
56 | self._conn=None |
---|
57 | |
---|
58 | @classmethod |
---|
59 | def isConnected(self): |
---|
60 | if (self._conn==None): |
---|
61 | return False |
---|
62 | else: |
---|
63 | return True |
---|
64 | |
---|
65 | @classmethod |
---|
66 | def createTables(self): |
---|
67 | c=self._conn.cursor() |
---|
68 | |
---|
69 | c.execute("create table if not exists message (id INTEGER PRIMARY KEY, name TEXT, timestamp TEXT, nested_level INT, simulation_id INT") |
---|
70 | c.execute("create index if not exists idx_message_1 on message (timestamp)") |
---|
71 | |
---|
72 | self._conn.commit() |
---|
73 | c.close() |
---|