DBTools.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. from typing import Any
  2. from sqlalchemy import create_engine, Column, Integer, String
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import sessionmaker
  5. # test.db.cxhy.cn
  6. # localhost
  7. engine = create_engine('mysql+pymysql://yixue:Lai123@test.db.cxhy.cn/yixue', pool_pre_ping=True)
  8. Base = declarative_base()
  9. class DBBase(Base):
  10. __abstract__ = True
  11. def __repr__(self):
  12. result = self.__class__.__name__ + "("
  13. for attr in self.__dict__:
  14. if attr.startswith("_") or attr.startswith("__") or attr == "__tablename__":
  15. continue
  16. result += attr + "=" + str(getattr(self, attr)) + " "
  17. result += ")"
  18. return result
  19. def to_json_object(self, **kwargs: Any):
  20. obj = {}
  21. for attr in self.__dict__:
  22. if attr.startswith("_") or attr.startswith("__") or attr == "__tablename__":
  23. continue
  24. obj[attr] = getattr(self, attr)
  25. for key, value in kwargs.items():
  26. obj[key] = value
  27. return obj
  28. @classmethod
  29. def new_session(cls):
  30. Session = sessionmaker(bind=engine)
  31. session = Session()
  32. return session
  33. @classmethod
  34. def insert(cls, **kwargs: Any):
  35. ss = cls.new_session()
  36. obj = cls()
  37. for key in kwargs.keys():
  38. if hasattr(obj, key):
  39. setattr(obj, key, kwargs[key])
  40. ss.add(obj)
  41. ss.commit()
  42. ss.close()
  43. @classmethod
  44. def insert_all(cls, items: []):
  45. ss = cls.new_session()
  46. lst = []
  47. for item in items:
  48. obj = cls()
  49. for key in item.keys():
  50. if hasattr(obj, key):
  51. setattr(obj, key, item[key])
  52. lst.append(obj)
  53. ss.add_all(lst)
  54. ss.commit()
  55. ss.close()
  56. @classmethod
  57. def delete_by(cls, **kwargs: Any):
  58. ss = cls.new_session()
  59. data = ss.query(cls).filter_by(**kwargs).all()
  60. for dt in data:
  61. ss.delete(dt)
  62. ss.commit()
  63. ss.close()
  64. @classmethod
  65. def query_by(cls, **kwargs: Any):
  66. ss = cls.new_session()
  67. data = ss.query(cls).filter_by(**kwargs).all()
  68. ss.close()
  69. return data
  70. @classmethod
  71. def query_first_by(cls, **kwargs: Any):
  72. ss = cls.new_session()
  73. data = ss.query(cls).filter_by(**kwargs).first()
  74. ss.close()
  75. return data
  76. # 也是查询,但是不关闭session,用于后续继续执行其他如update操作
  77. @classmethod
  78. def query_keep(cls, **kwargs: Any):
  79. ss = cls.new_session()
  80. data = ss.query(cls).filter_by(**kwargs).all()
  81. return [data, ss]
  82. @classmethod
  83. def check_exist(cls, **kwargs: Any):
  84. return cls.query_first_by(**kwargs) is not None
  85. class DBTaiYangPianYi(DBBase):
  86. __tablename__ = 'taiyangpianyi'
  87. id = Column(Integer, primary_key=True)
  88. month = Column(Integer)
  89. day = Column(Integer)
  90. diff = Column(Integer)
  91. class DBUserInfo(DBBase):
  92. __tablename__ = 'user_info'
  93. id = Column(Integer, primary_key=True)
  94. name = Column(String)
  95. beizhu = Column(String)
  96. man = Column(Integer)
  97. leibie = Column(Integer)
  98. year = Column(Integer)
  99. month = Column(Integer)
  100. day = Column(Integer)
  101. hour = Column(Integer)
  102. minute = Column(Integer)
  103. sheng = Column(String)
  104. shi = Column(String)
  105. qu = Column(String)
  106. niangan = Column(String)
  107. nianzhi = Column(String)
  108. yuegan = Column(String)
  109. yuezhi = Column(String)
  110. rigan = Column(String)
  111. rizhi = Column(String)
  112. shigan = Column(String)
  113. shizhi = Column(String)
  114. customer = Column(String)
  115. joinTime = Column(String)
  116. enabled = Column(Integer)
  117. class DBWanNianLi(DBBase):
  118. __tablename__ = 'wannianli'
  119. id = Column(Integer, primary_key=True)
  120. nian = Column(Integer)
  121. yue = Column(Integer)
  122. ri = Column(Integer)
  123. nian_gan = Column(String)
  124. nian_zhi = Column(String)
  125. yue_gan = Column(String)
  126. yue_zhi = Column(String)
  127. ri_gan = Column(String)
  128. ri_zhi = Column(String)
  129. class DBShiShenJieDu(DBBase):
  130. __tablename__ = 'shishen_jiedu'
  131. id = Column(Integer, primary_key=True)
  132. arg1 = Column(String)
  133. arg2 = Column(String)
  134. guanxi = Column(String)
  135. tip1 = Column(String)
  136. tip2 = Column(String)
  137. tip3 = Column(String)
  138. tip4 = Column(String)
  139. tip5 = Column(String)
  140. class DBLuckDay(DBBase):
  141. __tablename__ = 'luck_day'
  142. id = Column(Integer, primary_key=True)
  143. year = Column(Integer)
  144. month = Column(Integer)
  145. day = Column(Integer)
  146. desc = Column(String)
  147. jiedu1 = Column(String)
  148. jiedu2 = Column(String)
  149. jiedu3 = Column(String)
  150. jiedu4 = Column(String)
  151. jiedu5 = Column(String)
  152. tip = Column(String)
  153. class DBGaoNeng(DBBase):
  154. __tablename__ = 'gaoneng_mid'
  155. id = Column(Integer, primary_key=True)
  156. arg1 = Column(String)
  157. arg2 = Column(String)
  158. arg3 = Column(String)
  159. wuxing1 = Column(String)
  160. wuxing2 = Column(String)
  161. wuxing3 = Column(String)
  162. guanxi = Column(String)
  163. desc = Column(String)
  164. father = Column(String)
  165. leibie = Column(String)
  166. hehua = Column(String)
  167. fangxiang = Column(String)
  168. tip = Column(String)
  169. class DBCustomUser(DBBase):
  170. __tablename__ = 'custom_user'
  171. id = Column(Integer, primary_key=True)
  172. user = Column(String)
  173. psd = Column(String)
  174. name = Column(String)
  175. sexy = Column(Integer)
  176. online = Column(Integer)