-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlabkey.getQueryInfo.R
More file actions
118 lines (99 loc) · 4.72 KB
/
labkey.getQueryInfo.R
File metadata and controls
118 lines (99 loc) · 4.72 KB
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
##
# Copyright (c) 2010-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.
##
##labkey.getLookupDetails
labkey.getLookupDetails <- function(baseUrl=NULL, folderPath, schemaName, queryName, lookupKey)
{
if(missing("lookupKey") )
{stop ("You must supply the key (name) value of a query field defined as a lookup type field.")}
lookupFields <- getQueryInfo(baseUrl=baseUrl, folderPath=folderPath, schemaName=schemaName, queryName=queryName,showDefaultView=FALSE, lookupKey=lookupKey)
return(lookupFields)
}
##Public getQueryDetails
labkey.getQueryDetails <- function(baseUrl=NULL, folderPath, schemaName, queryName)
{
queryDetails <- getQueryInfo(baseUrl=baseUrl, folderPath=folderPath, schemaName=schemaName, queryName=queryName,showDefaultView=FALSE)
return(queryDetails)
}
## Public getDefaultViewDetails
labkey.getDefaultViewDetails <- function(baseUrl=NULL, folderPath, schemaName, queryName)
{
viewDetails <- getQueryInfo(baseUrl=baseUrl, folderPath=folderPath, schemaName=schemaName, queryName=queryName,showDefaultView=TRUE)
return(viewDetails)
}
## internal routine that handles all of these
getQueryInfo <- function(baseUrl=NULL, folderPath, schemaName, queryName, showDefaultView=FALSE, lookupKey=NULL)
{
baseUrl=labkey.getBaseUrl(baseUrl)
## Validate required parameters
if (missing(folderPath)) stop (paste("A value must be specified for folderPath."))
if (missing(schemaName)) stop (paste("A value must be specified for schemaName."))
if (missing(queryName)) stop (paste("A value must be specified for queryName."))
if (is.null(lookupKey)==FALSE) {char <- nchar(lookupKey); if(char<1) {lookupKey<-NULL} }
params <- list("schemaName"=schemaName, "query.queryName"=queryName, "apiVersion"="8.3")
if (!is.null(lookupKey))
params <- c(params, list("fk"=lookupKey))
## Construct url
myurl <- labkey.buildURL(baseUrl, "query", "getQueryDetails.api", folderPath, params)
## Execute via our standard GET function
mydata <- labkey.get(myurl)
decode <- fromJSON(mydata, simplifyVector=FALSE, simplifyDataFrame=FALSE)
## If querying the default view, the metadata is in a differnt object in the json stream
if (showDefaultView==TRUE) {qcs<-decode$defaultView$columns}
else {qcs <- decode$columns}
## parsed JSON stream has two types of problems related to nulls:
## the value NULL as as named element of the parent node
## the absence of either a value or a name for some columns on some records
## etiher one can be detected by checking for is.null on a row-by row basis against the total set of column names
baseColumns <- c("name", "caption", "fieldKey", "type", "isNullable","isKeyField",
"isAutoIncrement", "isVersionField","isHidden","isSelectable",
"isUserEditable", "isReadOnly", "isMvEnabled","description")
lookupColumns <- c("keyColumn", "schemaName", "displayColumn", "queryName", "isPublic")
dmall <- matrix(nrow=0, ncol=20, byrow=TRUE)
if(length(qcs)>0)
{
for (j in 1:length(qcs))
{
dmqrow<- matrix(data=decode$name[[1]], nrow=1, ncol=1, byrow=FALSE)
for (nm in baseColumns) {
if (is.null(qcs[[j]][[nm]])) {qcs[[j]][[nm]] <- NA}
dmqrow<- matrix(data=cbind(dmqrow, qcs[[j]][[nm]]), nrow=1, byrow=FALSE)
}
if (is.null(qcs[[j]]$lookup))
{
lookupinfo <- matrix(data=cbind(NA,NA,NA,NA,NA), ncol=5, byrow=FALSE)
}
else
{
for (nm in lookupColumns) {
if (is.null(qcs[[j]]$lookup[[nm]])) {qcs[[j]]$lookup[[nm]] <- NA}
}
nm <- lookupColumns[1]
lookupinfo<- as.matrix(qcs[[j]]$lookup[[nm]], nrow=1, byrow=FALSE)
for (nm in lookupColumns[-1]) {
lookupinfo<- matrix(data=cbind(lookupinfo, qcs[[j]]$lookup[[nm]]), nrow=1, byrow=FALSE)
}
}
dmqrow<-cbind(dmqrow, lookupinfo)
dmall <- rbind(dmall,dmqrow)
}
}
dfall <- as.data.frame(dmall, stringsAsFactors=FALSE)
colnames(dfall)<-c("queryName", "fieldName", "caption", "fieldKey", "type", "isNullable","isKeyField",
"isAutoIncrement", "isVersionField","isHidden","isSelectable",
"isUserEditable", "isReadOnly", "isMvEnabled", "description",
"lookupKeyField","lookupSchemaName","lookupDisplayField", "lookupQueryName", "lookupIsPublic")
return(dfall)
}