Skip to content

Commit c61d1fe

Browse files
committed
Share more code
1 parent ad3e300 commit c61d1fe

3 files changed

Lines changed: 29 additions & 23 deletions

File tree

src/org/labkey/remoteapi/query/BaseQueryCommand.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,26 @@ public JSONObject getJsonObject()
211211

212212
return json;
213213
}
214+
215+
protected void addOffsetAndMaxRows(JSONObject json, String offsetName, String maxRowsName)
216+
{
217+
if (getOffset() > 0)
218+
json.put(offsetName, getOffset());
219+
220+
if (getMaxRows() >= 0)
221+
json.put(maxRowsName, getMaxRows());
222+
}
223+
224+
protected JSONObject addSortAndParams(JSONObject json)
225+
{
226+
if (null != getSorts() && !getSorts().isEmpty())
227+
json.put("query.sort", Sort.getSortQueryStringParam(getSorts()));
228+
229+
for (Map.Entry<String, String> entry : getQueryParameters().entrySet())
230+
{
231+
json.put("query.param." + entry.getKey(), entry.getValue());
232+
}
233+
234+
return json;
235+
}
214236
}

src/org/labkey/remoteapi/query/BaseSelectRowsCommand.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,12 @@ public JSONObject getJsonObject()
9898
JSONObject json = super.getJsonObject();
9999

100100
// Note: SelectRows and ExecuteSql both support sort and param, but they differ in how they're conveyed (URL vs. JSON)
101-
102-
if (null != getSorts() && !getSorts().isEmpty())
103-
json.put("query.sort", Sort.getSortQueryStringParam(getSorts()));
104-
105-
for (Map.Entry<String, String> entry : getQueryParameters().entrySet())
106-
{
107-
json.put("query.param." + entry.getKey(), entry.getValue());
108-
}
101+
addSortAndParams(json);
109102

110103
// Note: SelectRows and ExecuteSql both support offset and maxRows, but the property names are different
104+
addOffsetAndMaxRows(json,"query.offset", "query.maxRows");
111105

112-
if (getOffset() > 0)
113-
json.put("query.offset", getOffset());
114-
115-
if (getMaxRows() >= 0)
116-
json.put("query.maxRows", getMaxRows());
117-
else
106+
if (!json.has("query.maxRows"))
118107
json.put("query.showRows", "all");
119108

120109
if (null != getFilters())

src/org/labkey/remoteapi/query/ExecuteSqlCommand.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,7 @@ public JSONObject getJsonObject()
207207
JSONObject json = super.getJsonObject();
208208
json.put("schemaName", getSchemaName());
209209
json.put("sql", getWafEncoding() ? EncodeUtils.wafEncode(getSql()) : getSql());
210-
if (getMaxRows() >= 0)
211-
json.put("maxRows", getMaxRows());
212-
if (getOffset() > 0)
213-
json.put("offset", getOffset());
210+
addOffsetAndMaxRows(json, "offset", "maxRows");
214211
json.put("includeDetailsColumn", isIncludeDetailsColumn());
215212
json.put("saveInSession", isSaveInSession());
216213

@@ -221,13 +218,11 @@ public JSONObject getJsonObject()
221218
protected Map<String, Object> createParameterMap()
222219
{
223220
Map<String, Object> params = super.createParameterMap();
221+
JSONObject sortAndParams = addSortAndParams(new JSONObject());
224222

225-
if (null != getSorts() && !getSorts().isEmpty())
226-
params.put("query.sort", Sort.getSortQueryStringParam(getSorts()));
227-
228-
for (Map.Entry<String, String> entry : getQueryParameters().entrySet())
223+
if (!sortAndParams.isEmpty())
229224
{
230-
params.put("query.param." + entry.getKey(), entry.getValue());
225+
params.putAll(sortAndParams.toMap());
231226
}
232227

233228
return params;

0 commit comments

Comments
 (0)