WorkWithDB.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from sqlalchemy import (MetaData, Table, Column, Integer,
  2. DateTime, String, Text, ForeignKey,
  3. create_engine, func)
  4. from sqlalchemy.exc import ProgrammingError, OperationalError
  5. import os
  6. from module.MyMessageBox import show_dialog
  7. from PySide6.QtWidgets import QMessageBox
  8. from logger import logger
  9. meta = MetaData()
  10. cam_setting = Table("сam_setting", meta,
  11. Column('id', Integer, primary_key=True),
  12. Column('type', Integer, nullable=False),
  13. Column('mode', String, nullable=False),
  14. Column("selected_cam", String, nullable=False),
  15. Column('created', DateTime, default=func.now()),
  16. Column('update', DateTime,
  17. onupdate=func.current_timestamp())
  18. )
  19. list_personal = Table("list_personal", meta,
  20. Column('id', Integer, primary_key=True),
  21. Column('lastname', String, nullable=False),
  22. Column('firstname', String, nullable=False),
  23. Column('middlename', String, nullable=False),
  24. Column('created', DateTime, default=func.now()),
  25. Column('update', DateTime,
  26. onupdate=func.current_timestamp())
  27. )
  28. list_place = Table("list_place", meta,
  29. Column('id', Integer, primary_key=True),
  30. Column('name_place', String, nullable=False),
  31. Column('created', DateTime, default=func.now()),
  32. Column('update', DateTime, onupdate=func.current_timestamp())
  33. )
  34. list_propusk = Table("list_propusk", meta,
  35. Column("id", Integer, primary_key=True),
  36. Column("id_propusk", Integer, nullable=False),
  37. Column("date_from", DateTime, nullable=False),
  38. Column("date_to", DateTime, nullable=False),
  39. Column("personal", Integer, ForeignKey(
  40. "list_personal.id"), nullable=False),
  41. Column("place", Integer, ForeignKey(
  42. "list_place.id"), nullable=False),
  43. Column("receiving_man", Text, nullable=False),
  44. Column("purpose_visite", Text, nullable=False),
  45. Column("face", Text, nullable=False),
  46. Column("document", Text, nullable=False),
  47. Column("created", DateTime, default=func.now()),
  48. Column("update", DateTime, default=func.now(),
  49. onupdate=func.current_timestamp()))
  50. list_ussued_passes = Table("list_ussued_passes", meta,
  51. Column("id", Integer, primary_key=True),
  52. Column("used_pass", Integer, nullable=False),
  53. Column("id_propusk", Integer, ForeignKey(
  54. "list_propusk.id_propusk"), nullable=False),
  55. Column("created", DateTime, default=func.now()),
  56. Column("update", DateTime, default=func.now(), onupdate=func.current_timestamp()))
  57. def checking_path_db() -> str | None:
  58. if os.environ.get("DB_DIR"):
  59. return os.path.join(os.environ.get("DB_DIR"), "propusk.db")
  60. else:
  61. if os.environ.get("DEFAULT_PATH"):
  62. return os.path.join(
  63. os.environ.get("DEFAULT_PATH"), "propusk.db")
  64. else:
  65. logger.error(
  66. "Не правильно указан путь к базе данных, или вообще отсутствует")
  67. # show_dialog(
  68. # QMessageBox.Icon.Critical,
  69. # "Путь к бд",
  70. # "Не правильно указан путь к базе данных, или вообще отсутствует"
  71. # )
  72. engine = create_engine(F"sqlite:///{checking_path_db()}", echo=False)
  73. engine.logging_name = 'PropuskLogger'
  74. def init_db():
  75. path = checking_path_db()
  76. if not os.path.exists(os.path.dirname(path)):
  77. os.mkdir(os.path.dirname(path))
  78. meta.create_all(engine)
  79. def connect():
  80. return engine.connect()
  81. def check_error_sql(func):
  82. def wrapper(*arg, **args):
  83. try:
  84. return func(*arg, **args)
  85. except ProgrammingError as pe:
  86. show_dialog(QMessageBox.Icon.Critical, 'Ошибка', 'Произошла ошибка в работе БД')
  87. logger.error(pe)
  88. except OperationalError as oe:
  89. show_dialog(QMessageBox.Icon.Critical, 'Ошибка', 'Произошла ошибка в работе БД')
  90. logger.error(oe)
  91. return wrapper