-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
158 lines (132 loc) · 3.42 KB
/
db.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from psycopg2 import sql
from psycopg2.extras import RealDictCursor
from psycopg2.pool import SimpleConnectionPool
user = 'postgres'
pw = '12345'
address = '192.168.0.47'
db = 'postgres'
port = '5432'
schema = 'public'
pool = SimpleConnectionPool(
1, 5,
user=user,
password=pw,
host=address,
port=port,
database=db
)
def _get_dict_cursor(conn):
return conn.cursor(
cursor_factory=RealDictCursor
)
def _get_connection():
return pool.getconn()
def _put_connection(conn):
pool.putconn(conn)
def get_markets():
conn = _get_connection()
cur = _get_dict_cursor(conn)
query = sql.SQL(
"SELECT * FROM {}.markets;"
).format(
*map(sql.Identifier, (schema,))
)
cur.execute(query)
records = cur.fetchall()
_put_connection(conn)
return records
def clear_markets():
conn = _get_connection()
cur = _get_dict_cursor(conn)
query = sql.SQL(
"SELECT * FROM {}.markets;"
).format(
*map(sql.Identifier, (schema,))
)
cur.execute(query)
records = cur.fetchall()
_put_connection(conn)
return records
def add_market(designation, description):
conn = self._get_connection()
cur = conn.cursor()
query = f'INSERT INTO {self.schema}.projects (designation, description) ' \
'VALUES (%s, %s) ' \
'RETURNING project_id;'
params = (designation, description)
cur.execute(query, params)
record = cur.fetchone()
conn.commit()
self._put_connection(conn)
return next(iter(record), None)
def add_markets(market_list):
conn = _get_connection()
cur = conn.cursor()
args_str = ','.join(
cur.mogrify(
"(%s)",
(market_name,)
).decode("utf-8")
for market_name in market_list)
cur.execute(
f'INSERT INTO {schema}.markets '
'(market_name) '
f'VALUES {args_str} '
'RETURNING market_id;'
)
records = [next(iter(record))
for record in cur.fetchall() if len(record) > 0]
conn.commit()
_put_connection(conn)
return records
def get_companies():
conn = _get_connection()
cur = _get_dict_cursor(conn)
query = sql.SQL(
"SELECT * FROM {}.companies;"
).format(
*map(sql.Identifier, (schema,))
)
cur.execute(query)
records = cur.fetchall()
_put_connection(conn)
return records
def add_companies(company_list):
conn = _get_connection()
cur = conn.cursor()
args_str = ','.join(
cur.mogrify(
"(%s)",
(company_name,)
).decode("utf-8")
for company_name in company_list)
cur.execute(
f'INSERT INTO {schema}.companies '
'(company_name) '
f'VALUES {args_str} '
'RETURNING company_id, company_name;'
)
records = cur.fetchall()
conn.commit()
_put_connection(conn)
return records
def add_figures(figure_list):
conn = _get_connection()
cur = conn.cursor()
args_str = ','.join(
cur.mogrify(
"(%s)",
(market_name,)
).decode("utf-8")
for market_name in figure_list)
cur.execute(
f'INSERT INTO {schema}.markets '
'(market_name) '
f'VALUES {args_str} '
'RETURNING market_id;'
)
records = [next(iter(record))
for record in cur.fetchall() if len(record) > 0]
conn.commit()
_put_connection(conn)
return records