kbc_sqlite.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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="", style:str="", creator:str="", createdTime:str="datetime('now')", relatedBoard:str="", relatedClass:str="", statement:str="=-10") -> None:
  6. self.id = "null"
  7. self.type = type
  8. self.name = name
  9. self.dscrp = dscrp
  10. self.style = style
  11. self.creator = creator
  12. self.createdTime = createdTime
  13. self.relatedBoard = relatedBoard
  14. self.relatedClass = relatedClass
  15. self.statement = statement
  16. # WHERE {swc}.format(swc=select_where)
  17. def select(self, selectColumn:str="name"):
  18. 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)
  19. def add(self):
  20. 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)
  21. def delete(self):
  22. 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)
  23. def edit(self, editColumn:str="", editValue:str=""):
  24. 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)
  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.statement};".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.statement};".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.statement};".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. "steps":[],
  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="", fetchAll:bool=False):
  61. if dbPath != "" and sqls != "":
  62. try:
  63. con = sqlite3.connect(dbPath)
  64. cur = con.cursor()
  65. cur.execute(sqls)
  66. con.commit()
  67. if fetchAll == True:
  68. return cur.fetchall()
  69. con.close()
  70. except sqlite3.OperationalError:
  71. print(sqlite3.OperationalError.__name__)
  72. def recordExist(dbPath:str, tableName:str, capitalize:bool=False, itemName:str="", returnBool:bool=True):
  73. if capitalize == True:
  74. tableName = tableName.capitalize()
  75. sqls = "SELECT name FROM {table} WHERE name='{name}';".format(table=tableName, name=itemName)
  76. ie = exec(sqls)
  77. if ie != [] and returnBool == False:
  78. return ie
  79. elif ie != [] and returnBool == True:
  80. return True
  81. elif ie == []:
  82. return False
  83. else:
  84. # kbc_alt.Err(errCode)
  85. print("err <Code>: unexpected error in existence check")
  86. if __name__ == "__main__":
  87. pass