kbc_sqlite.py 4.0 KB

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