kbc_sqlite.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. sqls = "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. return sqls
  46. def add(self):
  47. sqls = "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)
  48. return sqls
  49. def delete(self): # 1. withstate:int=10? 2. withStatement >= or > or <?
  50. sqls = "UPDATE compact_main SET state=-10 WHERE type='{d.type}' AND name='{d.name}' AND relatedBoard='{d.relatedBoard}' AND relatedClass='{d.relatedClass}' AND state=10;".format(d=self)
  51. return sqls
  52. def edit(self, editColumn:str="", editValue:str=""):
  53. sqls = "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)
  54. return sqls
  55. def edit_state(self, editColumn:str="", editValue:int=-10):
  56. sqls = "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)
  57. return sqls
  58. def move(self, moveColumn:str="", moveValue:str=""):
  59. sqls = "UPDATE compact_main SET {mc}='{mv}' WHERE type='{m.type}' AND name='{m.name}' AND relatedBoard='{m.relatedBoard}' AND relatedClass='{m.relatedClass}' AND state=10;".format(mc=moveColumn, mv=moveValue, m=self)
  60. return sqls
  61. def back(self, selectColumn:str="name"):
  62. sqls = "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)
  63. return sqls
  64. def export(self, selectColumn:str="name"):
  65. sqls = "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)
  66. return sqls
  67. # ----- Record_log_action(DB record as a class) -----
  68. class RLA():
  69. pass
  70. # ----- Auto generate DB from config.toml -----
  71. def genDB():
  72. # model IE?
  73. # over write?
  74. # exec
  75. pass