-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathrecords.py
175 lines (150 loc) · 4.63 KB
/
records.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import json
from db import connection as db_conn
def _json_or_none(value):
return json.dumps(value) if value is not None else None
def list_records_from_table(
conn,
table_oid,
limit=None,
offset=None,
order=None,
filter=None,
group=None,
return_record_summaries=False,
table_record_summary_templates=None,
):
"""
Get records from a table.
The order definition objects should have the form
{"attnum": <int>, "direction": <text>}
Only data from which the user is granted `SELECT` is returned.
Args:
tab_id: The OID of the table whose records we'll get.
limit: The maximum number of rows we'll return.
offset: The number of rows to skip before returning records from
following rows.
order: An array of ordering definition objects.
filter: An array of filter definition objects.
group: An array of group definition objects.
return_record_summaries: Whether to return self record summaries.
table_record_summary_templates: A dict of record summary templates, per table.
"""
result = db_conn.exec_msar_func(
conn,
'list_records_from_table',
table_oid,
limit,
offset,
_json_or_none(order),
_json_or_none(filter),
_json_or_none(group),
return_record_summaries,
_json_or_none(table_record_summary_templates),
).fetchone()[0]
return result
def get_record_from_table(
conn,
record_id,
table_oid,
return_record_summaries=False,
table_record_summary_templates=None,
):
"""
Get single record from a table by its primary key
Only data from which the user is granted `SELECT` is returned.
Args:
record_id: The primary key value of the record.
table_id: The OID of the table whose record we'll get.
return_record_summaries: Whether to return self record summaries.
table_record_summary_templates: A dict of record summary templates, per table.
"""
result = db_conn.exec_msar_func(
conn,
'get_record_from_table',
table_oid,
record_id,
return_record_summaries,
_json_or_none(table_record_summary_templates),
).fetchone()[0]
return result
def search_records_from_table(
conn,
table_oid,
search=[],
limit=10,
return_record_summaries=False,
table_record_summary_templates=None,
):
"""
Get records from a table, according to a search specification
Only data from which the user is granted `SELECT` is returned.
Args:
tab_id: The OID of the table whose records we'll get.
search: A list of dictionaries defining a search.
limit: The maximum number of rows we'll return.
return_record_summaries: Whether to return self record summaries.
table_record_summary_templates: A dict of record summary templates, per table.
The search definition objects should have the form
{"attnum": <int>, "literal": <text>}
"""
search = search or []
result = db_conn.exec_msar_func(
conn,
'search_records_from_table',
table_oid,
json.dumps(search),
limit,
return_record_summaries,
_json_or_none(table_record_summary_templates),
).fetchone()[0]
return result
def delete_records_from_table(conn, record_ids, table_oid):
"""
Delete records from table by id.
Args:
tab_id: The OID of the table whose record we'll delete.
record_ids: A list of primary values
The table must have a single primary key column.
"""
return db_conn.exec_msar_func(
conn,
'delete_records_from_table',
table_oid,
json.dumps(record_ids),
).fetchone()[0]
def add_record_to_table(
conn,
record_def,
table_oid,
return_record_summaries=False,
table_record_summary_templates=None,
):
"""Add a record to a table."""
result = db_conn.exec_msar_func(
conn,
'add_record_to_table',
table_oid,
json.dumps(record_def),
return_record_summaries,
_json_or_none(table_record_summary_templates),
).fetchone()[0]
return result
def patch_record_in_table(
conn,
record_def,
record_id,
table_oid,
return_record_summaries=False,
table_record_summary_templates=None,
):
"""Update a record in a table."""
result = db_conn.exec_msar_func(
conn,
'patch_record_in_table',
table_oid,
record_id,
json.dumps(record_def),
return_record_summaries,
_json_or_none(table_record_summary_templates),
).fetchone()[0]
return result