Prepares and runs an SQL query for the history database. Parameters ---------- sql : str Any filtering expressions to go after SELECT ... FROM ... params : tuple Parameters passed to the SQL query (to replace "?") raw, output : bool
(
self,
sql: str,
params: tuple,
raw: bool = True,
output: bool = False,
latest: bool = False,
)
| 361 | ## Methods for retrieving history: |
| 362 | ## ------------------------------- |
| 363 | def _run_sql( |
| 364 | self, |
| 365 | sql: str, |
| 366 | params: tuple, |
| 367 | raw: bool = True, |
| 368 | output: bool = False, |
| 369 | latest: bool = False, |
| 370 | ) -> Iterable[tuple[int, int, InOrInOut]]: |
| 371 | """Prepares and runs an SQL query for the history database. |
| 372 | |
| 373 | Parameters |
| 374 | ---------- |
| 375 | sql : str |
| 376 | Any filtering expressions to go after SELECT ... FROM ... |
| 377 | params : tuple |
| 378 | Parameters passed to the SQL query (to replace "?") |
| 379 | raw, output : bool |
| 380 | See :meth:`get_range` |
| 381 | latest : bool |
| 382 | Select rows with max (session, line) |
| 383 | |
| 384 | Returns |
| 385 | ------- |
| 386 | Tuples as :meth:`get_range` |
| 387 | """ |
| 388 | toget = "source_raw" if raw else "source" |
| 389 | sqlfrom = "history" |
| 390 | if output: |
| 391 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" |
| 392 | toget = "history.%s, output_history.output" % toget |
| 393 | if latest: |
| 394 | toget += ", MAX(session * 128 * 1024 + line)" |
| 395 | this_querry = "SELECT session, line, %s FROM %s " % (toget, sqlfrom) + sql |
| 396 | cur = self.db.execute(this_querry, params) |
| 397 | if latest: |
| 398 | cur = (row[:-1] for row in cur) |
| 399 | if output: # Regroup into 3-tuples, and parse JSON |
| 400 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur) |
| 401 | return cur |
| 402 | |
| 403 | @only_when_enabled |
| 404 | @catch_corrupt_db |