Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/6f83df853a718a3f790634d5d26692ee to your computer and use it in GitHub Desktop.
Save borkdude/6f83df853a718a3f790634d5d26692ee to your computer and use it in GitHub Desktop.
A simple example for a SQLIte pod for Babashka
import json
import sqlite3
import sys
from bcoding import bencode, bdecode
def read():
return dict(bdecode(sys.stdin.buffer))
def write(obj):
sys.stdout.buffer.write(bencode(obj))
sys.stdout.flush()
def debug(msg):
with open("debug.log", "a") as f:
f.write(str(msg) + "\n")
def main():
while True:
msg = read()
op = msg["op"]
if op == "describe":
write(
{
"format": "json",
"vars": [{"namespace": "pod.sqlite", "name": "execute!"}],
}
)
elif op == "invoke":
var = msg["var"]
id = msg["id"]
args = json.loads(msg["args"])
conn = sqlite3.connect("bb.db")
c = conn.cursor()
result = None
if var == "pod.sqlite/execute!":
try:
result = c.execute(*args)
except Exception as e:
debug(e)
write({"value": json.dumps(result.fetchall()), "id": id, "status": ["done"]})
conn.commit()
conn.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment