当代数据库管理系统课程实验二
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
9.0 KiB

  1. from sqlalchemy import create_engine,MetaData
  2. from sqlalchemy import Integer,String,ForeignKey,Column,TEXT,LargeBinary
  3. from sqlalchemy.orm import relationship
  4. from sqlalchemy.ext.declarative import declarative_base
  5. from sqlalchemy.orm import sessionmaker
  6. from sqlalchemy import PrimaryKeyConstraint,UniqueConstraint
  7. import sqlalchemy
  8. import re
  9. import time
  10. import jieba.analyse
  11. from jieba import cut_for_search
  12. Base=declarative_base()
  13. class con:
  14. def connect():
  15. '''Returns a connection and a metadata object'''
  16. # We connect with the help of the PostgreSQL URL
  17. url = 'postgresql://stu10205501415:Stu10205501415@dase-cdms-2022-pub.pg.rds.aliyuncs.com:5432/stu10205501415'
  18. # The return value of create_engine() is our connection object
  19. con = create_engine(url, client_encoding='utf8')
  20. # We then bind the connection to MetaData()
  21. meta = MetaData(bind=con)
  22. return con, meta
  23. class User(Base):
  24. __tablename__ = 'user'
  25. user_id = Column(TEXT, primary_key=True, comment="主键")
  26. password = Column(TEXT, nullable=False, comment="密码")
  27. balance = Column(Integer, nullable=False, comment="")
  28. token = Column(TEXT, comment="缓存的令牌")
  29. terminal = Column(TEXT, comment="终端代码")
  30. # class Store(Base):
  31. # __tablename__ = 'store'
  32. # store_id = Column(TEXT, primary_key=True,comment="主键")
  33. # stock_level = Column(Integer, comment = "货存")
  34. class Store_Book(Base):
  35. __tablename__ = 'store_book'
  36. store_id = Column(TEXT, comment="主键")
  37. book_id = Column(TEXT, comment="主键")
  38. book_info = Column(TEXT, comment="书籍信息")
  39. stock_level = Column(Integer, comment = "货存")
  40. __table_args__ = (
  41. PrimaryKeyConstraint('store_id', 'book_id'),
  42. )
  43. class User_Store(Base):
  44. __tablename__ = 'user_store'
  45. # id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  46. user_id = Column(
  47. TEXT,
  48. # ForeignKey(
  49. # "user.user_id",
  50. # ondelete="CASCADE",
  51. # onupdate="CASCADE",
  52. # ),
  53. nullable=False,
  54. comment="user外键"
  55. )
  56. store_id = Column(
  57. TEXT,
  58. # ForeignKey(
  59. # "store.store_id",
  60. # ondelete="CASCADE",
  61. # onupdate="CASCADE",
  62. # ),
  63. nullable=False,
  64. comment="store外键"
  65. )
  66. # 多对多关系的中间表必须使用联合唯一约束,防止出现重复数据
  67. __table_args__ = (
  68. PrimaryKeyConstraint('store_id', 'user_id'),
  69. )
  70. class New_Order(Base):
  71. __tablename__ = 'new_order'
  72. order_id = Column(TEXT, primary_key = True, comment = '订单id')
  73. user_id = Column(
  74. TEXT,
  75. ForeignKey(
  76. "user.user_id",
  77. ondelete="CASCADE",
  78. onupdate="CASCADE",
  79. ),
  80. nullable=False,
  81. comment="user外键"
  82. )
  83. store_id = Column(
  84. TEXT,
  85. # ForeignKey(
  86. # "store.store_id",
  87. # ondelete="CASCADE",
  88. # onupdate="CASCADE",
  89. # ),
  90. nullable=False,
  91. comment="store外键"
  92. )
  93. creat_time = Column(TEXT, nullable=False,comment="订单创建时间")
  94. status = Column(Integer,nullable=False,comment="订单状态")
  95. class New_Order_Detail(Base):
  96. __tablename__ = 'new_order_detail'
  97. order_id = Column(TEXT, comment='订单id')
  98. book_id = Column(TEXT, comment='订单书籍')
  99. count = Column(Integer, comment='购买书籍数')
  100. price = Column(Integer, comment='单价')
  101. __table_args__ = (
  102. PrimaryKeyConstraint('order_id','book_id'),
  103. )
  104. class Book(Base):
  105. __tablename__ = 'book'
  106. book_id = Column(TEXT, primary_key=True)
  107. title = Column(TEXT, nullable=False)
  108. author = Column(TEXT)
  109. publisher = Column(TEXT)
  110. original_title = Column(TEXT)
  111. translator = Column(TEXT)
  112. pub_year = Column(TEXT)
  113. pages = Column(Integer)
  114. original_price = Column(Integer) # 原价
  115. currency_unit = Column(TEXT)
  116. binding = Column(TEXT)
  117. isbn = Column(TEXT)
  118. author_intro = Column(TEXT)
  119. book_intro = Column(TEXT)
  120. content = Column(TEXT)
  121. tags = Column(TEXT)
  122. picture = Column(LargeBinary)
  123. # 搜索标题表
  124. class Search_title(Base):
  125. __tablename__ = 'search_title'
  126. search_id = Column(Integer, nullable=False)
  127. title = Column(TEXT, nullable=False)
  128. book_id = Column(TEXT, ForeignKey('book.book_id'), nullable=False)
  129. __table_args__ = (
  130. PrimaryKeyConstraint('search_id', 'title'),
  131. {},
  132. )
  133. # 搜索标签表
  134. class Search_tags(Base):
  135. __tablename__ = 'search_tags'
  136. search_id = Column(Integer, nullable=False)
  137. tags = Column(TEXT, nullable=False)
  138. book_id = Column(TEXT, ForeignKey('book.book_id'), nullable=False)
  139. __table_args__ = (
  140. PrimaryKeyConstraint('search_id', 'tags'),
  141. {},
  142. )
  143. # 搜索作者表
  144. class Search_author(Base):
  145. __tablename__ = 'search_author'
  146. search_id = Column(Integer, nullable=False)
  147. author = Column(TEXT, nullable=False)
  148. book_id = Column(TEXT, ForeignKey('book.book_id'), nullable=False)
  149. __table_args__ = (
  150. PrimaryKeyConstraint('search_id', 'author'),
  151. {},
  152. )
  153. # 搜索书本内容表
  154. class Search_book_intro(Base):
  155. __tablename__ = 'search_book_intro'
  156. search_id = Column(Integer, nullable=False)
  157. book_intro = Column(TEXT, nullable=False)
  158. book_id = Column(TEXT, ForeignKey('book.book_id'), nullable=False)
  159. __table_args__ = (
  160. PrimaryKeyConstraint('search_id', 'book_intro'),
  161. {},
  162. )
  163. def insert_tags(session):
  164. row = session.query(Book).all()
  165. for i in row:
  166. tags = i.tags
  167. for j in tags:
  168. row_tmp = session.query(Search_tags).filter(Search_tags.tags==j).order_by(Search_tags.search_id.desc()).first()
  169. if row_tmp is None:
  170. max_num = 0
  171. else:
  172. max_num = row_tmp.search_id + 1
  173. new_search_tags = Search_tags(search_id=max_num,tags=j,book_id=i.book_id)
  174. session.add(new_search_tags)
  175. session.commit()
  176. def insert_author(session):
  177. row = session.query(Book).all()
  178. for i in row:
  179. tmp = i.author
  180. if tmp == None:
  181. j = '作者不详'
  182. else:
  183. j = tmp
  184. row_tmp = session.query(Search_author).filter(Search_author.author==j).order_by(Search_author.search_id.desc()).first()
  185. if row_tmp is None:
  186. max_num = 0
  187. else:
  188. max_num = row_tmp.search_id + 1
  189. # print(max_num, j, i.book_id)
  190. new_search_author = Search_author(search_id=max_num,author=j,book_id=i.book_id)
  191. session.add(new_search_author)
  192. session.commit()
  193. def insert_title(session):
  194. row = session.query(Book).all()
  195. for i in row:
  196. tmp = i.title
  197. # print(tmp)
  198. tmp = re.sub(r'[\(\[\{(【][^))】]*[\)\]\{\\)]\s?', '', tmp)
  199. tmp = re.sub(r'[^\w\s]', '', tmp)
  200. # 处理空标题
  201. if len(tmp) == 0:
  202. continue
  203. # 搜索引擎模式,在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。
  204. seg_list = cut_for_search(tmp)
  205. sig_list = []
  206. tag = 0
  207. for k in seg_list:
  208. sig_list.append(k)
  209. if k == tmp:
  210. tag = 1
  211. if tag == 0:
  212. sig_list.append(tmp)
  213. for j in sig_list:
  214. if j == "" or j == " ":
  215. continue
  216. row_tmp = session.query(Search_title).filter(Search_title.title==j).order_by(Search_title.search_id.desc()).first()
  217. if row_tmp is None:
  218. max_num = 0
  219. else:
  220. max_num = row_tmp.search_id + 1
  221. # print(max_num, j, i.book_id)
  222. new_search_title = Search_title(search_id=max_num,title=j,book_id=i.book_id)
  223. session.add(new_search_title)
  224. session.commit()
  225. def insert_book_intro(session):
  226. row = session.query(Book).all()
  227. for i in row:
  228. tmp = i.book_intro
  229. if tmp != None:
  230. # print(tmp)
  231. # 采用textrank进行分词
  232. keywords_textrank = jieba.analyse.textrank(tmp)
  233. # print(keywords_textrank)
  234. # keywords_tfidf = jieba.analyse.extract_tags(tmp)
  235. # print(keywords_tfidf)
  236. for j in keywords_textrank:
  237. row_tmp = session.query(Search_book_intro).filter(Search_book_intro.book_intro==j).order_by(Search_title.search_id.desc()).first()
  238. if row_tmp is None:
  239. max_num = 0
  240. else:
  241. max_num = row_tmp.search_id + 1
  242. # print(max_num, j, i.book_id)
  243. new_search_book_intro = Search_book_intro(search_id=max_num,book_intro=j,book_id=i.book_id)
  244. session.add(new_search_book_intro)
  245. session.commit()
  246. engine, meta = con.connect()
  247. Base.metadata.bind = engine
  248. DBSession = sessionmaker(bind=engine)
  249. session = DBSession()
  250. insert_tags(session=session)
  251. insert_author(session=session)
  252. insert_title(session=session)
  253. insert_book_intro(session=session)