connect.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import psycopg2
  2. class DataConsumer():
  3. """To query database"""
  4. def __init__(self, credentials):
  5. self.conn = psycopg2.connect(credentials)
  6. def __del__(self):
  7. self.conn.close()
  8. def getProductSet(self):
  9. cursor = self.conn.cursor()
  10. cursor.execute("SELECT * from Product")
  11. rows = cursor.fetchall()
  12. cursor.close()
  13. return rows
  14. def addProduct(self, name):
  15. cursor = self.conn.cursor()
  16. cursor.execute("INSERT INTO Product (pro_name) VALUES (%s)", (name,))
  17. cursor.close()
  18. def getCompanyNames(self):
  19. cursor = self.conn.cursor()
  20. cursor.execute("SELECT com_name FROM Company")
  21. ans = []
  22. for t in cursor:
  23. ans.append(t[0])
  24. cursor.close()
  25. return ans
  26. def getOrdersOfCompany(self, company):
  27. cursor = self.conn.cursor()
  28. cursor.execute("SELECT ord_id, ord_date FROM bookorder NATURAL JOIN company WHERE com_name = %s", (company,))
  29. rows = cursor.fetchall()
  30. cursor.close()
  31. return rows
  32. def getItemsOfOrder(self, bookId):
  33. cursor = self.conn.cursor()
  34. cursor.execute("SELECT ori_id, ori_quantity, pro_name, ori_deliveryduedate FROM ordereditem NATURAL JOIN product WHERE ord_id = %s", (bookId,))
  35. rows = cursor.fetchall()
  36. cursor.close()
  37. return rows
  38. def getWarehousesOfCompany(self, company):
  39. cursor = self.conn.cursor()
  40. cursor.execute("SELECT war_id, add_number, add_name, add_city FROM warehouse AS w JOIN company AS c ON w.com_id = c.com_id JOIN postaladdress AS p ON w.add_id = p.add_id WHERE com_name = %s", (company,))
  41. rows = cursor.fetchall()
  42. cursor.close()
  43. return rows
  44. def addOrder(self, company, houseId, proList, proQuantityList, dueDate):
  45. # Debug : work under progress
  46. print('add order not implemented yet')
  47. print(company)
  48. print(houseId)
  49. print(proList)
  50. print(proQuantityList)
  51. print(dueDate)
  52. # Create cursor to execute SQL statements
  53. cursor = self.conn.cursor()
  54. # Create book order
  55. cursor.execute("INSERT INTO bookorder(ord_date, com_id) SELECT %s, com_id FROM company WHERE com_name = %s", (dueDate, company))
  56. # Retrieve id of the newly created bookorder
  57. cursor.execute("SELECT CURRVAL('bookorder_ord_id_seq')")
  58. ordId = cursor.fetchone()[0]
  59. # Add ordered items
  60. for k in range(len(proList)):
  61. proName = proList[k]
  62. proQuantity = proQuantityList[k]
  63. cursor.execute("INSERT INTO ordereditem(ori_quantity, ori_deliveryduedate, pro_id, ord_id, war_id) SELECT %s, %s, pro_id, %s, %s FROM product WHERE pro_name = %s", (proQuantity, dueDate, ordId, houseId, proName))
  64. cursor.close()
  65. def commit(self):
  66. """Make the changes to the database persistent"""
  67. self.conn.commit()