-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvance.py
More file actions
84 lines (79 loc) · 3.05 KB
/
Advance.py
File metadata and controls
84 lines (79 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from psycopg2.extensions import cursor
class Advance1:
def __init__(self, pool):
self.__pool = pool
self.__create_function()
def __create_function(self):
conn = self.__pool.get_conn()
cur: cursor = conn.cursor()
cur.execute('drop function if exists get_enterprise_order(enterprise_name varchar);')
cur.execute('''create function get_enterprise_order(enterprise_name varchar)
returns table
(
product_model varchar,
quantity int,
country varchar,
city varchar,
industry varchar,
price numeric,
total_price numeric
)
as
$$
begin
RETURN QUERY
select o.product_model as products,
o.quantity as quantity,
e.country as country,
e.city as city,
e.industry as industry,
p.unit_price as price,
p.unit_price * o.quantity as total_price
from orders o
join enterprise e on o.enterprise = e.name
join product p on p.model = o.product_model
where e.name = enterprise_name;
end;
$$ language plpgsql;''')
cur.execute('drop function if exists get_center_stock(center_name varchar);')
cur.execute('''create function get_center_stock(center_name varchar) --供应中心
returns table
(
product varchar,
qt int,
price numeric,
total_price numeric
)
as
$$
begin
RETURN QUERY
select s.model as product,
s.quantity - s.current_quantity as qt,
p.unit_price as price,
(s.quantity - s.current_quantity) * p.unit_price as total_price
from stock s
join product p on s.model = p.model
where s.center = center_name
and (s.quantity - s.current_quantity) <> 0;
end
$$ language plpgsql;''')
conn.commit()
cur.close()
conn.close()
def get_enterprise_order(self, enterprise_name):
conn = self.__pool.get_conn()
cur: cursor = conn.cursor()
cur.execute("select * from get_enterprise_order('%s')" % enterprise_name)
result = cur.fetchall()
cur.close()
conn.close()
return result
def get_center_stock(self, center_name):
conn = self.__pool.get_conn()
cur: cursor = conn.cursor()
cur.execute("select * from get_center_stock('%s');" % center_name)
result = cur.fetchall()
cur.close()
conn.close()
return result