Connect to the database, and create tables if necessary.
(self)
| 243 | |
| 244 | @catch_corrupt_db |
| 245 | def init_db(self): |
| 246 | """Connect to the database, and create tables if necessary.""" |
| 247 | if not self.enabled: |
| 248 | self.db = DummyDB() |
| 249 | return |
| 250 | |
| 251 | # use detect_types so that timestamps return datetime objects |
| 252 | kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) |
| 253 | kwargs.update(self.connection_options) |
| 254 | self.db = sqlite3.connect(self.hist_file, **kwargs) |
| 255 | self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
| 256 | primary key autoincrement, start timestamp, |
| 257 | end timestamp, num_cmds integer, remark text)""") |
| 258 | self.db.execute("""CREATE TABLE IF NOT EXISTS history |
| 259 | (session integer, line integer, source text, source_raw text, |
| 260 | PRIMARY KEY (session, line))""") |
| 261 | # Output history is optional, but ensure the table's there so it can be |
| 262 | # enabled later. |
| 263 | self.db.execute("""CREATE TABLE IF NOT EXISTS output_history |
| 264 | (session integer, line integer, output text, |
| 265 | PRIMARY KEY (session, line))""") |
| 266 | self.db.commit() |
| 267 | # success! reset corrupt db count |
| 268 | self._corrupt_db_counter = 0 |
| 269 | |
| 270 | def writeout_cache(self): |
| 271 | """Overridden by HistoryManager to dump the cache before certain |