-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
137 lines (114 loc) · 3.53 KB
/
sql.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
import psycopg2
import json
import numpy as np
import pickle as pkl
from const import DB_CONNECTION_STRING
class TableCreation:
"""
configuration for the database-related stuff
"""
schema = 'local_polarization'
single_seed_table = 'query_result_single_seed'
single_seed_table_creation = """
CREATE TABLE IF NOT EXISTS {schema}.{table_name}
(
graph_path TEXT,
query INTEGER,
kappa NUMERIC,
k INTEGER,
C1 BYTEA,
C2 BYTEA,
best_beta REAL,
best_t REAL,
beta_array BYTEA,
ts BYTEA,
time_elapsed REAL,
runtime_info BYTEA
);
CREATE INDEX IF NOT EXISTS {schema}_{table_name}_idx ON {schema}.{table_name} (graph_path, query, kappa, k);
""".format(
table_name=single_seed_table,
schema=schema
)
seed_pair_table = 'query_result_seed_pair'
seed_pair_table_creation = """
CREATE TABLE IF NOT EXISTS {schema}.{table_name}
(
graph_path TEXT,
seed1 INTEGER,
seed2 INTEGER,
kappa NUMERIC,
k INTEGER,
C1 BYTEA,
C2 BYTEA,
best_beta REAL,
best_t REAL,
beta_array BYTEA,
ts BYTEA,
time_elapsed REAL,
runtime_info BYTEA
);
CREATE INDEX IF NOT EXISTS {schema}_{table_name}_idx ON {schema}.{table_name} (graph_path, seed1, seed2, kappa, k);
""".format(
table_name=seed_pair_table,
schema=schema
)
def init_db(debug=False, create_table=False):
"""
create connection, make a cursor and create the tables if needed
"""
conn = psycopg2.connect(DB_CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""CREATE SCHEMA IF NOT EXISTS {}""".format(TableCreation.schema)
)
sqls_to_execute = (
TableCreation.seed_pair_table_creation,
TableCreation.single_seed_table_creation,
)
if create_table:
for sql in sqls_to_execute:
cursor.execute(sql)
conn.commit()
if debug:
conn.set_trace_callback(print)
return conn, cursor
def record_exists(cursor, table, record):
"""record: dict"""
cursor.execute(
"""
SELECT 1 FROM
{schema}.{table_name}
WHERE
{filter_template}
""".format(
schema=TableCreation.schema,
table_name=table,
filter_template=' AND '.join(
map(lambda s: "{}=%s".format(s), record.keys())
)
),
tuple(record.values())
)
return cursor.fetchone() is not None
def insert_record(cursor, table, record):
# convert complex type to pickle
for k, v in record.items():
if isinstance(v, dict):
record[k] = json.dumps(v)
if isinstance(v, (list, tuple, set, np.ndarray)):
record[k] = pkl.dumps(v)
cursor.execute(
"""
INSERT INTO
{schema}.{table_name} ({fields})
VALUES
({placeholders})
""".format(
schema=TableCreation.schema,
table_name=table,
fields=', '.join(record.keys()),
placeholders=', '.join(['%s'] * len(record))
),
tuple(record.values())
)