kbc_sqlite.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import sqlite3
  2. import kbc_alt
  3. # ----- Record_main(DB record as a class) -----
  4. class RM():
  5. def __init__(self, type:str="", name:str="", dscrp:str="", creator:str="", createdTime:str="datetime('now')", relatedBoard:str="", relatedClass:str="", state:int=-10) -> None:
  6. self.id = "null"
  7. self.type = type
  8. self.name = name
  9. self.dscrp = dscrp
  10. self.creator = creator
  11. self.createdTime = createdTime
  12. self.relatedBoard = relatedBoard
  13. self.relatedClass = relatedClass
  14. self.state = state
  15. def select(self, selectColumn:str="name"):
  16. 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)
  17. def add(self):
  18. 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)
  19. def delete(self): # 1. withstate:int=10? 2. withStatement >= or > or <?
  20. 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)
  21. def edit(self, editColumn:str="", editValue:str=""):
  22. 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)
  23. def edit_state(self, editValue:int=-10):
  24. 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)
  25. def move(self, moveColumn:str="", moveValue:str=""):
  26. 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)
  27. def back(self, selectColumn:str="name"):
  28. 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)
  29. def export(self, selectColumn:str="name"):
  30. 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)
  31. def get_style():
  32. pass
  33. # ----- Record_log_action(DB record as a class) -----
  34. class RLA():
  35. pass
  36. # ----- DB as a class -----
  37. class DB():
  38. def __init__(self) -> None:
  39. pass
  40. def dbExist():
  41. pass
  42. def reGenerate():
  43. pass
  44. def direct():
  45. pass
  46. # ----- Operating Cursor -----
  47. oc = {
  48. "dt":str, # DBType
  49. "dp":str, # DBPath
  50. "cp":list, # CurrentPath
  51. "pp":list, # PreviousPath
  52. "next_move":str,
  53. "tp":list, # TargetPath
  54. "tp_in":list, # ~ after command parameter "in"(&& before command parameter "to")
  55. "tp_to":list, # ~ after command parameter "to"
  56. "tp_attr":str # ~ like "-name" in "edit -name oldName to newName"
  57. }
  58. # some thoughts:
  59. # class oc():
  60. # def get_oc(), def move_oc()
  61. # ----- Execute Methods -----
  62. def execute(dbPath):
  63. def dec(origin_func):
  64. pass
  65. con = sqlite3.connect(dbPath)
  66. cur = con.cursor()
  67. cur.execute(commands)
  68. con.commit()
  69. # if fetchall == input, return 0?
  70. if fetchall == True:
  71. re = cur.fetchall()
  72. return re
  73. cur.close()
  74. con.close()
  75. def dec():
  76. pass
  77. @execute(oc["dp"])
  78. def exec_one(dbPath:str, commands:list, fetchall:bool=False):
  79. pass
  80. @execute(oc["dp"])
  81. def exec_many():
  82. pass
  83. def recordExist(dbPath:str, tableName:str, capitalize:bool=False, itemName:str="", returnBool:bool=True):
  84. # [todo 4] 这里面.capitalize()后面需要根据config.toml里面的内容判断
  85. # 可能也不用, 因为KBCLEV的表名和本身并无关系
  86. if capitalize == True:
  87. tableName = tableName.capitalize()
  88. sqls = "SELECT name FROM {table} WHERE name='{name}';".format(table=tableName, name=itemName)
  89. ie = exec_one(dbPath, sqls)
  90. if ie != [] and returnBool == False:
  91. return ie
  92. elif ie != [] and returnBool == True:
  93. return True
  94. elif ie == []:
  95. return False
  96. else:
  97. # kbc_alt.Err(errCode)
  98. print("err <Code>: unexpected error in existence check")
  99. # ----- Master process -----
  100. def master():
  101. pass