/
query_examples.py
306 lines (251 loc) · 9.24 KB
/
query_examples.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#
# Copyright (c) 2015-2018 LabKey Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Examples using the Query.py API
Sample data from the New Study tutorial on labkey.org:
https://www.labkey.org/Documentation/wiki-page.view?name=studySetupManual
"""
from labkey.api_wrapper import APIWrapper
from labkey.exceptions import (
RequestError,
QueryNotFoundError,
ServerContextError,
ServerNotFoundError,
)
from labkey.query import Pagination, QueryFilter
from requests.exceptions import Timeout
import copy
print("Create a server context")
labkey_server = "localhost:8080"
project_name = "ModuleAssayTest" # Project folder name
context_path = "labkey"
api = APIWrapper(labkey_server, project_name, context_path, use_ssl=False)
schema = "lists"
table = "Demographics"
column1 = "Group Assignment"
column2 = "Participant ID"
###################
# Test basic select_rows
###################
result = api.query.select_rows(schema, table)
if result is not None:
print(result["rows"][0])
print("select_rows: There are " + str(result["rowCount"]) + " rows.")
else:
print("select_rows: Failed to load results from " + schema + "." + table)
###################
# Test error handling
###################
# catch base error
try:
result = api.query.select_rows(schema, "badtable")
print(result)
except RequestError:
print("Caught base error")
# catch table not found error
try:
result = api.query.select_rows(schema, "badtable")
print(result)
except QueryNotFoundError:
print("Caught bad table")
# catch schema error
try:
result = api.query.select_rows("badSchema", table)
print(result)
except QueryNotFoundError:
print("Caught bad schema")
# catch SSL error
ssl_api = APIWrapper(labkey_server, project_name, context_path, use_ssl=True)
try:
result = ssl_api.query.select_rows(schema, table)
print(result)
except ServerContextError:
print("Caught SSL Error")
# catch bad context path
bad_api = APIWrapper(labkey_server, project_name, "", use_ssl=False)
try:
result = bad_api.query.select_rows(schema, table)
print(result)
except ServerNotFoundError:
print("Caught context path")
# catch bad folder path error
bad_api = APIWrapper(labkey_server, "bad_project_name", context_path, use_ssl=False)
try:
result = bad_api.query.select_rows(schema, table)
print(result)
except ServerNotFoundError:
print("Caught bad folder name")
###################
# Test some parameters of select_rows
###################
result = api.query.select_rows(
schema,
table,
max_rows=5,
offset=10,
include_total_count=True,
include_details_column=True,
include_update_column=True,
)
if result is not None:
print("select_rows: There are " + str(len(result["rows"])) + " rows.")
print("select_rows: There are " + str(result["rowCount"]) + " total rows.")
print("select_rows: Response API version [" + str(result["formatVersion"]) + "].")
column_statement = "select_rows: Included columns: "
for column in result["columnModel"]:
column_statement = column_statement + " " + column["header"] + ", "
print(column_statement)
row = result["rows"][0]
dataIndex = result["metaData"]["id"]
print("select_rows: The first row Key is: " + str(row[dataIndex]))
else:
print("select_rows: Failed to load results from " + schema + "." + table)
###################
# Test get all results
###################
result = api.query.select_rows(schema, table, show_rows=Pagination.ALL, include_total_count=True)
if result is not None:
print("select_rows: There are " + str(len(result["rows"])) + " rows.")
print("select_rows: There are " + str(result["rowCount"]) + " total rows.")
else:
print("select_rows: Failed to load results from " + schema + "." + table)
###################
# Test sort and select columns
###################
result = api.query.select_rows(
schema,
table,
max_rows=5,
offset=10,
include_total_count=False,
columns=",".join([column1, column2]),
sort=column1 + ", -" + column2,
) # use '-' to sort descending
if result is not None:
print("select_rows: There are " + str(result["rowCount"]) + " rows.")
print("select_rows: " + table)
for row in result["rows"]:
print("\t" + str(row[column1]) + ", " + str(row[column2]))
else:
print("select_rows: Failed to load results from " + schema + "." + table)
###################
# Test basic filters
###################
filters = [
QueryFilter(column1, "Group 2: HIV-1 Negative"),
QueryFilter("Height (inches)", "50, 70", QueryFilter.Types.BETWEEN),
QueryFilter("Country", "Germany;Uganda", QueryFilter.Types.IN),
]
result = api.query.select_rows(schema, table, filter_array=filters)
if result is not None:
print("select_rows: There are " + str(result["rowCount"]) + " rows.")
else:
print("select_rows: Failed to load results from " + schema + "." + table)
###################
# Test update_rows
###################
rows = result["rows"]
test_row_idx = 1
original_value = rows[test_row_idx]
column3 = "Country"
test_row = {"Key": original_value["Key"], column3: "Pangea"}
print("update_rows: original value [ " + original_value[column3] + " ]")
update_result = api.query.update_rows(schema, table, [test_row])
print("update_rows: updated value [ " + update_result["rows"][0][column3] + " ]")
update_result = api.query.update_rows(schema, table, [original_value])
print("update_rows: reset value [ " + update_result["rows"][0][column3] + " ]")
###################
# Test insert_rows & delete_rows
###################
test_row = copy.copy(original_value)
test_row["Key"] = None
test_row["Country"] = "Antarctica"
all_rows = api.query.select_rows(schema, table)
print("Insert Rows: Initials row count [ " + str(all_rows["rowCount"]) + " ]")
insert_result = api.query.insert_rows(schema, table, [test_row])
print("Insert Rows: New rowId [ " + str(insert_result["rows"][0]["Key"]) + " ]")
all_rows = api.query.select_rows(schema, table)
print("Insert Rows: after row count [ " + str(all_rows["rowCount"]) + " ]")
test_row = insert_result["rows"][0]
deleteResult = api.query.delete_rows(schema, table, [test_row])
print("Delete Rows: deleted rowId [ " + str(deleteResult["rows"][0]["Key"]) + " ]")
all_rows = api.query.select_rows(schema, table)
print("Delete Rows: after row count [ " + str(all_rows["rowCount"]) + " ]")
###################
# Test truncate_table
###################
truncate_info = api.query.truncate_table(schema, table)
print("Delete all rows in table: [ " + str(truncate_info["deletedRows"]) + " ] rows deleted")
###################
# Test execute_sql
###################
sql = "select * from lists.demographics"
# base execute_sql
sql_result = api.query.execute_sql(schema, sql)
if sql_result is not None:
print("execute_sql: There are " + str(sql_result["rowCount"]) + " rows.")
else:
print("execute_sql: Failed to load results from " + schema + "." + table)
# paging
sql_result = api.query.execute_sql(
schema, sql, max_rows=5, offset=10, sort=(column1 + ", -" + column2)
)
if sql_result is not None:
print("execute_sql: There are " + str(len(sql_result["rows"])) + " rows.")
print("execute_sql: There are " + str(sql_result["rowCount"]) + " total rows.")
print("execute_sql: " + table)
for row in sql_result["rows"]:
print("\t" + str(row[column1]) + ", " + str(row[column2]))
else:
print("execute_sql: Failed to load results from " + schema + "." + table)
# Save query within the session
sql_result = api.query.execute_sql(schema, sql, max_rows=5, offset=10, save_in_session=True)
print("execute_sql: query saved as [ " + sql_result["queryName"] + " ]")
# set timeout
try:
sql_result = api.query.execute_sql(schema, sql, timeout=0.001)
print("execute_sql did not timeout")
except Timeout:
print("Caught Timeout")
###################
# Test QC State Definitions
###################
# Create new QC state definitions
qc_states = [
{
"label": "needs verification",
"description": "please look at this",
"publicData": False,
},
{"label": "approved", "publicData": True},
]
result = api.query.insert_rows("core", "qcstate", qc_states)
for row in result["rows"]:
print("Created QC state: " + row["label"])
result = api.query.select_rows("core", "qcstate")
# Update a QC state definitions
original_value = result["rows"][1]
test_row = {"RowId": original_value["RowId"], "label": "Updated Label"}
update_result = api.query.update_rows("core", "qcstate", [test_row])
print("Updated label: approved -> " + update_result["rows"][0]["label"])
# Delete all unused QC state definitions
result = api.query.select_rows("core", "qcstate")
for row in result["rows"]:
print("Deleting QC state: " + row["Label"])
try:
api.query.delete_rows("core", "qcstate", [row])
except ServerContextError as e:
print(e.message)