kbc_sqlite.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import sqlite3
  2. # ----- Sqlite Methods -----
  3. def exec_one(dbPath:str, commands:list, fetchall:bool=False):
  4. con = sqlite3.connect(dbPath)
  5. cur = con.cursor()
  6. cur.execute(commands)
  7. con.commit()
  8. # if fetchall == input, return 0?
  9. if fetchall == True:
  10. re = cur.fetchall()
  11. return re
  12. cur.close()
  13. con.close()
  14. return 0
  15. def isExist(dbPath:str, tableName:str, capitalize:bool=False, itemName:str="", returnBool:bool=True):
  16. # [todo 4] 这里面.capitalize()后面需要根据config.toml里面的内容判断
  17. # 可能也不用, 因为KBCLEV的表名和本身并无关系
  18. if capitalize == True:
  19. tableName = tableName.capitalize()
  20. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=itemName)
  21. ie = exec_one(dbPath, sqls)
  22. if ie != [] and returnBool == False:
  23. return ie
  24. elif ie != [] and returnBool == True:
  25. return True
  26. elif ie == []:
  27. return False
  28. else:
  29. # Alt.Err(errCode)
  30. print("err <Code>: unexpected error in existence check")
  31. # ----- Record_main(DB record as a class) -----
  32. class RM():
  33. def __init__(self, type:str="", name:str="", dscrp:str="", creator:str="", relatedBoard:str="", relatedClass:str="", state:int=-10) -> None:
  34. self.id = "null"
  35. self.type = type
  36. self.name = name
  37. self.dscrp = dscrp
  38. self.creator = creator
  39. self.createdTime = "datetime('now')"
  40. self.relatedBoard = relatedBoard
  41. self.relatedClass = relatedClass
  42. self.state = state
  43. def select(self, selectColumn:str="name"):
  44. return "SELECT {sc} FROM compact_main WHERE type='{s.type}' AND name='{s.name}' AND realatedBoard='{s.relatedBoard}' AND relatedClass='{s.relatedClass}' AND state={s.state};".format(sc=selectColumn, s=self)
  45. def add(self):
  46. return "INSERT INTO compact_main VALUES({a.id}, '{a.type}', '{a.name}', '{a.dscrp}', '{a.creator}', '{a.createdTime}', '{a.relatedBoard}', '{a.relatedClass}', {a.state});".format(a=self)
  47. def delete(self): # 1. withstate:int=10? 2. withStatement >= or > or <?
  48. return "UPDATE compact_main SET state=-10 WHERE type='{d.type}' AND name='{d.name}' AND relatedBoard='{d.relatedBoard}' AND relatedClass='{d.relatedClass}' AND state={d.state};".format(d=self)
  49. def edit(self, editColumn:str="", editValue:str=""):
  50. return "UPDATE compact_main SET {ec}='{ev}' WHERE type='{e.type}' AND name='{e.name}' AND relatedBoard='{e.relatedBoard}' AND relatedClass='{e.relatedClass}' AND state={e.state};".format(ec=editColumn, ev=editValue, e=self)
  51. def edit_state(self, editValue:int=-10):
  52. return "UPDATE compact_main SET state={ev} WHERE type='{e.type}' AND name='{e.name}' AND relatedBoard='{e.relatedBoard}' AND relatedClass='{e.relatedClass}' AND state={e.state};".format(ev=editValue, e=self)
  53. def move(self, moveColumn:str="", moveValue:str=""):
  54. return "UPDATE compact_main SET {mc}='{mv}' WHERE type='{m.type}' AND name='{m.name}' AND relatedBoard='{m.relatedBoard}' AND relatedClass='{m.relatedClass}' AND state={m.state};".format(mc=moveColumn, mv=moveValue, m=self)
  55. def back(self, selectColumn:str="name"):
  56. return "SELECT {sc} FROM compact_main WHERE type='{s.type}' AND name='{s.name}' AND realatedBoard='{s.relatedBoard}' AND relatedClass='{s.relatedClass}' AND state={s.state};".format(sc=selectColumn, s=self)
  57. def export(self, selectColumn:str="name"):
  58. return "SELECT {sc} FROM compact_main WHERE type='{s.type}' AND name='{s.name}' AND realatedBoard='{s.relatedBoard}' AND relatedClass='{s.relatedClass}' AND state={s.state};".format(sc=selectColumn, s=self)
  59. def get_styles():
  60. pass
  61. # ----- Record_log_action(DB record as a class) -----
  62. class RLA():
  63. pass
  64. # ----- Auto generate DB from config.toml -----
  65. def genDB():
  66. # model IE?
  67. # over write?
  68. # exec
  69. pass