WithSqlite.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import sqlite3
  2. # [todo 0]
  3. # 1. 检查commands[5]是否为dbPath, 不是话报错(syntax error)
  4. # ↑↑↑ 但是总感觉输入检查应该放在Controller里面(也就是放在前端去检查)...
  5. # 前端检查语法, 冲突代回到后端去检查, 然后顺便就执行了
  6. # 2. 命令里面的<KB>, <CL>, <EV>都代入检查是否存在, 区分add/edit(INSERT INTO/UPDATE)
  7. # 3. 匹配SQL关键字
  8. # 4. 拼完以后进行to和in的判断
  9. # 5. 按正确的顺序拼SQL语句
  10. # 6. 执行
  11. dbPath = "dev.db"
  12. def Exec_one(dbPath, commands):
  13. con = sqlite3.connect(dbPath)
  14. cur = con.cursor()
  15. cur.execute(commands)
  16. con.commit()
  17. re = cur.fetchall()
  18. con.close()
  19. return re
  20. # [todo 4]
  21. def Exec_many():
  22. pass
  23. def IsExist(exec_commands, returnBool=True):
  24. tableName = str(exec_commands[1]).capitalize()
  25. ItemName = str(exec_commands[2])
  26. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=ItemName)
  27. ie = Exec_one(dbPath, sqls)
  28. if ie != [] and returnBool == False:
  29. return ie
  30. elif ie != [] and returnBool == True:
  31. return True
  32. elif ie == []:
  33. return False
  34. else:
  35. # Alt.Err(errCode)
  36. print("err <Code>: unexpected error in existence check")
  37. # [todo 4]
  38. def Secondary_response():
  39. pass
  40. class objBoard():
  41. def __init__(self, dbPath, previousPath, currentPath, boardName, newBoardName) -> None:
  42. self.dp = dbPath
  43. self.pp = previousPath
  44. self.cp = currentPath
  45. self.name = boardName
  46. self.newName = newBoardName
  47. def select_board(self, aliveOnly=True):# 不太对劲, 应该在IsExist的时候就已经可以得到结果了
  48. if aliveOnly == True:
  49. sqls = "SELECT name FROM Board WHERE name='{name}' AND status='alive';".format(name=self.name)
  50. elif aliveOnly == False:
  51. sqls = "SELECT name FROM Board WHERE name='{name}';".format(name=self.name)
  52. reserve = Exec_one(self.dp, sqls)
  53. # [todo 4]
  54. # return sqls
  55. def add_board(self):
  56. sqls = "INSERT INTO Board VALUES(null, '{name}', 'alive');".format(name=self.name)
  57. Exec_one(self.dp, sqls)
  58. def delete_board(self):
  59. sqls = "UPDATE Board SET status='deleted' WHERE name='{name}';".format(name=self.name)
  60. Exec_one(self.dp, sqls)
  61. def edit_board():
  62. # 修改了Board之后关联的分类和事件也要变, 而且要先修改关系最后变board名称
  63. # 1. 查找CL和EV里面和这个KB的关联
  64. # 2. 修改关联的KB-name
  65. # 3. 修改KB的name
  66. sqls = ""
  67. def move_board():
  68. print("err <Code>: syntax error")
  69. class objClass():
  70. def __init__(self, dbPath, previousPath, currentPath, className, newClName="") -> None:
  71. self.dp = dbPath
  72. self.pp = previousPath
  73. self.cp = currentPath
  74. self.name = className
  75. self.newName = newClName
  76. def select_class(self, aliveOnly=True):# 不太对劲, 应该在IsExist的时候就已经可以得到结果了
  77. if aliveOnly == True:
  78. sqls = "SELECT name FROM Class WHERE name='{name}' AND status='alive';".format(name=self.name)
  79. elif aliveOnly == False:
  80. sqls = "SELECT name FROM Class WHERE name='{name}';".format(name=self.name)
  81. reserve = Exec_one(self.dp, sqls)
  82. def add_class(self):
  83. sqls = "INSERT INTO Class VALUES(null, '{name}', 'alive');".format(name=self.name)
  84. Exec_one(self.dp, sqls)
  85. def delete_class(self):
  86. sqls = "UPDATE Class SET status='deleted' WHERE name='{name}';".format(name=self.name)
  87. Exec_one(self.dp, sqls)
  88. # [todo 3] 关联的EV和KB待处理
  89. def edit_class(self):
  90. sqls = "UPDATE Class SET name={newName} WHERE name='{name}';".format(newName=self.newName, name=self.name)
  91. Exec_one(self.dp, sqls)
  92. def move_class(self):
  93. # Why would someone want to do that?
  94. # cp[todo 4] or del ONLY: def copy_class():
  95. print("err <Code>: syntax error")
  96. class objEvent():
  97. def __init__(self, dbPath, previousPath, currentPath, eventName, newEventName, dscrp, newDscrp, classCreated, classCurrent) -> None:
  98. self.dp = dbPath
  99. self.pp = previousPath
  100. self.cp = currentPath
  101. self.name = eventName
  102. self.newName = newEventName
  103. self.dscrp = dscrp
  104. self.nd = newDscrp
  105. self.ccr = classCreated
  106. self.ccu = classCurrent
  107. def select_event(self):
  108. sqls = "SELECT * FROM Event WHERE name='{name}';".format(name=self.name)
  109. reserve = Exec_one(self.dp, sqls)
  110. def add_event(self):
  111. sqls = "INSERT INTO Event VALUES(null, '{name}', '{dscrp}', '{creator}', datetime('now'), '{classCreated}', '{classCurrent}', 'alive');"
  112. Exec_one(self.dp, sqls)
  113. def delete_event(self):
  114. sqls = "UPDATE Event SET status='deleted' WHERE name='{name}';".format(name=self.name)
  115. Exec_one(self.dp, sqls)
  116. def edit_event(self, target):
  117. # newName
  118. if target == "newName":
  119. sqls = "UPDATE Event SET name='{newName}' WHERE name='{name}';".format(newName=self.newName, name=self.name)
  120. # [todo 4] 更改之后自动跳转?
  121. # newDscrp && appendDscrp
  122. elif target == "newDscrp":
  123. sqls = "UPDATE Event SET dscrp='{newDscrp}' WHERE name='{name}';".format(newDscrp=self.nd, name=self.name)
  124. elif target == "appendDscrp":
  125. sqls = "SELECT dscrp FROM Event WHERE name='{name}'".format(name=self.name)
  126. reserve = Exec_one(self.dp, sqls)
  127. # some prosess
  128. sqls = reserve + ""
  129. Exec_one(self.dp, sqls)
  130. def move_event(self):
  131. sqls = "UPDATE Event SET classCurrent='{classCurrent}' WHERE name='{name}';".format(classCurrent=self.ccu, name=self.name)
  132. Exec_one(self.dp, sqls)
  133. # [todo 3]
  134. def bp():
  135. pass
  136. def home(): # IE就能解决了好像
  137. sqls = "SELECT name FROM sqlite_master WHERE type='table' AND name is NOT 'sqlite_sequence';"
  138. Exec_one(dbPath, sqls)
  139. # Regular Start
  140. def Regular(dbPath, exec_commands):
  141. # try:
  142. ie = IsExist(exec_commands)
  143. print("is exist: ", ie)
  144. tableName = str(exec_commands[1]).capitalize()
  145. # to的情况待处理
  146. itemName = str(exec_commands[2])
  147. exec_flag = False
  148. if ie == True:
  149. pass
  150. elif ie == False:
  151. pass
  152. elif exec_flag == True:
  153. pass
  154. # 有个数据库锁定的异常待处理(Multi-user)
  155. # Exec_one(dbPath, sqls)
  156. if __name__ == "__main__":
  157. while(1):
  158. sy_i = input("sql: ").split(" ")
  159. Regular(dbPath, sy_i)