Add example
This commit is contained in:
parent
fb6e739aee
commit
98d4f81432
2 changed files with 81 additions and 0 deletions
20
examples/pod-lispyclouds-sqlite/README.md
Normal file
20
examples/pod-lispyclouds-sqlite/README.md
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# pod-lispyclouds-sqlite
|
||||||
|
|
||||||
|
To run this:
|
||||||
|
|
||||||
|
- Install python3 and sqlite3
|
||||||
|
- Create a virtualenv: `python3 -m venv ~/.virtualenvs/babashka`
|
||||||
|
- Switch to it: `source ~/.virtualenvs/babashka/bin/activate`
|
||||||
|
- Run: `pip install bcoding` to install the bencode lib
|
||||||
|
- Create a new db: `sqlite3 /tmp/babashka.db "create table foo (foo int);"`
|
||||||
|
|
||||||
|
Then run as pod:
|
||||||
|
|
||||||
|
``` clojure
|
||||||
|
(babashka.pods/load-pod ["./pod-lispyclouds-sqlite.py"])
|
||||||
|
(require '[pod.lispyclouds.sqlite :as sqlite])
|
||||||
|
(sqlite/execute! "create table if not exists foo ( int foo )")
|
||||||
|
(sqlite/execute! "delete from foo")
|
||||||
|
(sqlite/execute! "insert into foo values (1), (2)")
|
||||||
|
(sqlite/execute! "select * from foo") ;;=> ([1] [2])
|
||||||
|
```
|
||||||
61
examples/pod-lispyclouds-sqlite/pod-lispyclouds-sqlite.py
Executable file
61
examples/pod-lispyclouds-sqlite/pod-lispyclouds-sqlite.py
Executable file
|
|
@ -0,0 +1,61 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
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("/tmp/debug.log", "a") as f:
|
||||||
|
f.write(str(msg) + "\n")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
while True:
|
||||||
|
msg = read()
|
||||||
|
debug("msg", msg)
|
||||||
|
|
||||||
|
op = msg["op"]
|
||||||
|
|
||||||
|
if op == "describe":
|
||||||
|
write(
|
||||||
|
{
|
||||||
|
"format": "json",
|
||||||
|
"namespaces": [{"name": "pod.lispyclouds.sqlite",
|
||||||
|
"vars": [{"name": "execute!"}]}]}
|
||||||
|
)
|
||||||
|
elif op == "invoke":
|
||||||
|
var = msg["var"]
|
||||||
|
id = msg["id"]
|
||||||
|
args = json.loads(msg["args"])
|
||||||
|
debug(args)
|
||||||
|
conn = sqlite3.connect("/tmp/babashka.db")
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
result = None
|
||||||
|
|
||||||
|
if var == "pod.lispyclouds.sqlite/execute!":
|
||||||
|
try:
|
||||||
|
result = c.execute(*args)
|
||||||
|
except Exception as e:
|
||||||
|
debug(e)
|
||||||
|
|
||||||
|
value = json.dumps(result.fetchall())
|
||||||
|
debug("value", value)
|
||||||
|
|
||||||
|
write({"value": value, "id": id, "status": ["done"]})
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue