]> Softwares of Agnibho - librevax.git/blob - patient.py
Database dump
[librevax.git] / patient.py
1 # LibreVax
2 # Copyright (C) 2024 Dr. Agnibho Mondal
3 # This file is part of LibreVax.
4 # LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6 # You should have received a copy of the GNU General Public License along with LibreVax. If not, see <https://www.gnu.org/licenses/>.
7
8 def create(cursor, data):
9 try:
10 cursor.execute("INSERT INTO patients (cid, mid, name, dob, sex, address, contact, idproof, idnum, status, short, note, next, data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (data["cid"], data["mid"], data["name"], data["dob"], data["sex"], data["address"], data["contact"], data["idproof"], data["idnum"], data["status"], "", "", "", ""))
11 if(cursor.rowcount<=0):
12 return (False, "Failed to create record.")
13 else:
14 return (True, cursor.lastrowid)
15 except Exception as e:
16 return (False, e)
17
18 def read(cursor, pid):
19 try:
20 result=cursor.execute("SELECT * FROM patients WHERE pid=?", (pid,))
21 return (True, result.fetchone())
22 except Exception as e:
23 return (False, e)
24
25 def update(cursor, pid, data):
26 try:
27 cursor.execute("UPDATE patients SET cid=?, mid=?, name=?, dob=?, sex=?, address=?, contact=?, idproof=?, idnum=?, status=? WHERE pid=?", (data["cid"], data["mid"], data["name"], data["dob"], data["sex"], data["address"], data["contact"], data["idproof"], data["idnum"], data["status"], pid))
28 if(cursor.rowcount<=0):
29 return (False, "Failed to update record.")
30 else:
31 return (True, cursor.lastrowid)
32 except Exception as e:
33 return (False, e)
34
35 def delete(cursor, pid):
36 try:
37 cursor.execute("DELETE FROM patients WHERE pid=?", (pid,))
38 if(cursor.rowcount<=0):
39 return (False, "Failed to delete record.")
40 else:
41 return (True, cursor.lastrowid)
42 except Exception as e:
43 return (False, e)
44
45 def list(cursor, filter={"cid":"%", "name":"%", "next":"%"}, mid=None):
46 try:
47 filter={k:v for k,v in filter.items() if v}
48 if(mid is None):
49 result=cursor.execute("SELECT * FROM patients WHERE cid LIKE ? AND name LIKE ? AND next LIKE ? ORDER BY cid DESC", ("%"+filter.get("cid", "")+"%", "%"+filter.get("name", "")+"%", "%"+filter.get("next", "")+"%"))
50 else:
51 result=cursor.execute("SELECT * FROM patients WHERE cid LIKE ? AND name LIKE ? AND next LIKE ? AND mid=? ORDER BY cid DESC", ("%"+filter.get("cid", "")+"%", "%"+filter.get("name", "")+"%", "%"+filter.get("next", "")+"%", mid))
52 #if(filter=="cid"):
53 # result=cursor.execute("SELECT * FROM patients WHERE cid LIKE ? AND mid LIKE ?", (term, mid))
54 #elif(filter=="name"):
55 # result=cursor.execute("SELECT * FROM patients WHERE name LIKE ? AND mid LIKE ?", (term, mid))
56 #else:
57 # if(mid is not None):
58 # result=cursor.execute("SELECT * FROM patients WHERE mid=?", (mid,))
59 # else:
60 # result=cursor.execute("SELECT * FROM patients")
61 return (True, result.fetchall())
62 except Exception as e:
63 return (False, e)
64
65 def note(cursor, pid, data):
66 try:
67 cursor.execute("UPDATE patients SET short=?, note=?, next=? WHERE pid=?", (data["short"], data["note"], data["next"], pid))
68 if(cursor.rowcount<=0):
69 return (False, "Failed to update note.")
70 else:
71 return (True, cursor.lastrowid)
72 except Exception as e:
73 return (False, e)
74
75 def last(cursor, col=None, mid=""):
76 try:
77 if(col=="cid"):
78 result=cursor.execute("SELECT * FROM patients WHERE mid LIKE ? ORDER BY cid DESC LIMIT 1", ("%"+mid+"%",))
79 else:
80 result=cursor.execute("SELECT * FROM patients WHERE mid LIKE ? ORDER BY pid DESC LIMIT 1", ("%"+mid+"%",))
81 return (True, result.fetchone())
82 except Exception as e:
83 raise(e)
84 return (False, e)