kbc_sqlite.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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="", statement: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.statement = statement
  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.statement};".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.statement});".format(a=self)
  19. def delete(self):
  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.statement};".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.statement};".format(ec=editColumn, ev=editValue, e=self)
  23. def move(self, moveColumn:str="", moveValue:str=""):
  24. 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.statement};".format(mc=moveColumn, mv=moveValue, m=self)
  25. def back(self, selectColumn:str="name"):
  26. 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.statement};".format(sc=selectColumn, s=self)
  27. def export(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.statement};".format(sc=selectColumn, s=self)
  29. def get_style():
  30. pass
  31. # ----- Record_log_action(DB record as a class) -----
  32. class RLA():
  33. pass
  34. # ----- DB as a class -----
  35. class DB():
  36. def __init__(self) -> None:
  37. pass
  38. def dbExist():
  39. pass
  40. def reGenerate():
  41. pass
  42. # ----- Operating Cursor -----
  43. oc = {
  44. "dt":"", # DBType
  45. "dp":"", # DBPath
  46. "cp":[], # CurrentPath
  47. "pp":[], # PreviousPath
  48. "steps":[],
  49. "tp":[], # TargetPath
  50. "tp_in":[], # ~ after command parameter "in"(&& before command parameter "to")
  51. "tp_to":[], # ~ after command parameter "to"
  52. "tp_attr":[] # ~ like "-name" in "edit -name oldName to newName"
  53. }
  54. # some thoughts:
  55. # class oc():
  56. # def read_oc(), def move_oc()
  57. # ----- Execute Methods -----
  58. def exec(dbPath:str="", sqls:str="", fetchAll:bool=False):
  59. if dbPath != "" and sqls != "":
  60. try:
  61. con = sqlite3.connect(dbPath)
  62. cur = con.cursor()
  63. cur.execute(sqls)
  64. con.commit()
  65. if fetchAll == True:
  66. return cur.fetchall()
  67. con.close()
  68. except sqlite3.OperationalError:
  69. print(sqlite3.OperationalError.__name__)
  70. def recordExist(dbPath:str, tableName:str, capitalize:bool=False, itemName:str="", returnBool:bool=True):
  71. if capitalize == True:
  72. tableName = tableName.capitalize()
  73. sqls = "SELECT name FROM {table} WHERE name='{name}';".format(table=tableName, name=itemName)
  74. ie = exec(sqls)
  75. if ie != [] and returnBool == False:
  76. return ie
  77. elif ie != [] and returnBool == True:
  78. return True
  79. elif ie == []:
  80. return False
  81. else:
  82. # kbc_alt.Err(errCode)
  83. print("err <Code>: unexpected error in existence check")
  84. if __name__ == "__main__":
  85. pass