-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcustomReports.py
More file actions
64 lines (54 loc) · 2.28 KB
/
customReports.py
File metadata and controls
64 lines (54 loc) · 2.28 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
import sqlite3
import os
class CustomReports:
def __init__(self, database):
self.database = database
def migrate(self, dbConnection, db_schema_version):
if db_schema_version < 7:
dbConnection.execute('''CREATE TABLE reports
(report_id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
sql_text TEXT,
parameters TEXT,
active INTEGER default 1)''')
def injectData(self, dbConnection, data):
for datum in data:
dbConnection.execute(
"INSERT INTO reports VALUES (?,?,?,'',1)",
(datum["report_id"], datum["name"], datum["sql_text"]))
def readOnlyConnect(self):
return sqlite3.connect('file:' + self.database + '?mode=ro', uri=True)
def customSQL(self, sql):
# open as read only
with self.readOnlyConnect() as c:
cur = c.cursor()
cur.execute(sql)
header = [i[0] for i in cur.description]
rows = [list(i) for i in cur.fetchall()]
return (header, rows)
def customReport(self, report_id):
with self.readOnlyConnect() as c:
data = c.execute("SELECT * FROM reports WHERE (report_id=?)",
(report_id, )).fetchone()
if data:
title = data[1]
sql = data[2]
(header, rows) = self.customSQL(sql)
return (title, sql, header, rows)
return ("Couldn't find report", "", None, None)
def saveCustomSQL(self, dbConnection, sql, name):
try:
dbConnection.execute("INSERT INTO reports VALUES (NULL,?,?,?,1)",
(name, sql, ""))
return ""
except sqlite3.IntegrityError:
return "Report already exists with that name"
def get_report_list(self, dbConnection):
report_list = []
for row in dbConnection.execute(
'''SELECT report_id, name
FROM reports
WHERE (active = ?)
ORDER BY name''', (1, )):
report_list.append((row[0], row[1]))
return report_list