kbc_sqlite.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. # ----- Operating Cursor -----
  45. oc = {
  46. "dt":"", # DBType
  47. "dp":"", # DBPath
  48. "cp":[], # CurrentPath
  49. "pp":[], # PreviousPath
  50. "next_move":"",
  51. "tp":[], # TargetPath
  52. "tp_in":[], # ~ after command parameter "in"(&& before command parameter "to")
  53. "tp_to":[], # ~ after command parameter "to"
  54. "tp_attr":[] # ~ like "-name" in "edit -name oldName to newName"
  55. }
  56. # some thoughts:
  57. # class oc():
  58. # def read_oc(), def move_oc()
  59. # ----- Execute Methods -----
  60. def exec(dbPath:str="", sqls:str=""):
  61. if dbPath != "" and sqls != "":
  62. try:
  63. con = sqlite3.connect(dbPath)
  64. cur = con.cursor()
  65. cur.executescript(sqls)
  66. con.commit()
  67. con.close()
  68. except sqlite3.OperationalError:
  69. print(sqlite3.OperationalError.__name__)
  70. def exec_fetchall(dbPath:str="", sqls:str="", fetchAll:bool=True):
  71. if dbPath != "" and sqls != "":
  72. try:
  73. con = sqlite3.connect(dbPath)
  74. cur = con.cursor()
  75. cur.execute(sqls)
  76. con.commit()
  77. if fetchAll == True:
  78. return cur.fetchall()
  79. con.close()
  80. except sqlite3.OperationalError:
  81. print(sqlite3.OperationalError.__name__)
  82. def recordExist(dbPath:str, tableName:str, capitalize:bool=False, itemName:str="", returnBool:bool=True):
  83. if capitalize == True:
  84. tableName = tableName.capitalize()
  85. sqls = "SELECT name FROM {table} WHERE name='{name}';".format(table=tableName, name=itemName)
  86. ie = exec(sqls)
  87. if ie != [] and returnBool == False:
  88. return ie
  89. elif ie != [] and returnBool == True:
  90. return True
  91. elif ie == []:
  92. return False
  93. else:
  94. # kbc_alt.Err(errCode)
  95. print("err <Code>: unexpected error in existence check")
  96. if __name__ == "__main__":
  97. pass