1 """ sqlite event store """
3 from sqlite3 import connect, OperationalError
5 __all__ = "fetch", "initdb", "stow"
9 SCHEMA = """create table if not exists events (
12 peeraddr text not null,
13 is_incoming int not null default TRUE,
24 """alter table events add column
25 is_incoming int not null default TRUE"""
27 except OperationalError:
34 k: kwargs[k] if k in kwargs else v
36 ("is_incoming", True),
44 assert len(kwargs) <= len(parms)
46 """insert or ignore into events
47 (tstamp, imei, peeraddr, proto, packet, is_incoming)
49 (:when, :imei, :peeraddr, :proto, :packet, :is_incoming)
56 def fetch(imei, protos, backlog):
58 protosel = ", ".join(["?" for _ in range(len(protos))])
61 f"""select packet from events
62 where proto in ({protosel}) and imei = ?
63 order by tstamp desc limit ?""",
64 protos + (imei, backlog),
66 result = [row[0] for row in cur]