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,
23 DB.execute("""alter table events add column
24 is_incoming int not null default TRUE""")
25 except OperationalError:
32 k: kwargs[k] if k in kwargs else v
41 assert len(kwargs) <= len(parms)
43 """insert or ignore into events
44 (tstamp, imei, peeraddr, proto, packet)
46 (:when, :imei, :peeraddr, :proto, :packet)
52 def fetch(imei, protos, backlog):
54 protosel = ", ".join(["?" for _ in range(len(protos))])
56 cur.execute(f"""select packet from events
57 where proto in ({protosel}) and imei = ?
58 order by tstamp desc limit ?""",
59 protos + (imei, backlog))
60 result = [row[0] for row in cur]