forked from barskern/pandoc-filter-runsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunsql.py
More file actions
executable file
·64 lines (51 loc) · 1.78 KB
/
runsql.py
File metadata and controls
executable file
·64 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
import sys
from os import getenv
import MySQLdb
import sqlparse
from panflute import *
import sqlparse
def action(options, data, element, doc):
doc.host = str(doc.get_metadata('host', default='127.0.0.1'))
doc.user = str(doc.get_metadata('user', default='root'))
doc.passwd = str(doc.get_metadata('passwd', default='toor'))
doc.db = str(doc.get_metadata('db', default='db'))
db = MySQLdb.connect(
host=doc.host,
user=doc.user,
passwd=doc.passwd,
db=doc.db
)
if isinstance(options, dict):
no_result = options.get("no_result", False)
else:
# If options is not a dict, it is the actual data
no_result = False
data = str(options)
err = None
cur = db.cursor()
try:
cur.execute(data)
except MySQLdb.IntegrityError as e:
err = convert_text(f"Error: {e}")
fmt_data = sqlparse.format(data, reindent=True, keyword_case="upper")
if no_result:
if err:
return [CodeBlock(fmt_data, classes=["sql"]), *err]
else:
return [
CodeBlock(fmt_data, classes=["sql"]),
*convert_text(f"Query successfully: {cur.rowcount} row{'s' if cur.rowcount > 1 else ''} affected"),
]
cells = [
TableRow(*[TableCell(Plain(Str(str(v)))) for v in row])
for row in cur.fetchall()
]
column_names = TableRow(*[TableCell(Plain(Str(i[0]))) for i in cur.description])
table = Table(*cells, header=column_names)
if err:
return [CodeBlock(fmt_data, classes=["sql"]), *err, table]
else:
return [CodeBlock(fmt_data, classes=["sql"]), table]
if __name__ == "__main__":
run_filter(yaml_filter, tag="runsql", function=action)