Title: | Querying on 'OData' |
---|---|
Description: | Make querying on 'OData' easier. It exposes an 'ODataQuery' object that can be manipulated and provides features such as selection, filtering and ordering. |
Authors: | Laurent Verweijen [aut, cre] |
Maintainer: | Laurent Verweijen <[email protected]> |
License: | GPL-3 |
Version: | 0.5.3 |
Built: | 2024-11-29 08:53:34 UTC |
Source: | CRAN |
Create a combined filter
and_query(...) or_query(...) not_query(...)
and_query(...) or_query(...) not_query(...)
... |
Raw odata queries or query options. |
This function can be used with raw values or query options
Raw odata queries Raw OData can be passed as string. It's the responsibility of the caller that the argument is valid syntax and values are escaped.
Query options Query options can be passed as named parameters.
Query options should be of the following form: property.operator = value
Property should be a property of the entity or individual.
Operation can have any of the following values:
eq Whether property is equal to value.
ne Whether property is not equal to value.
gt Whether property is greater than value.
ge Whether property is greater than or equal to value.
lt Whether property is lower than value.
le Whether property is lower than or equal to value.
has Whether property has value as enumeration property.
startswith Whether property starts with value.
endswith Whether property ends with value.
contains Whether property contains value.
Value should be a string, double or boolean and will be escaped automatically.
https://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/
and_query("Column eq OtherColumn", FirstName.startswith = 'A', LastName.eq = 'Scott') or_query("ExpireDate eq null", ExpireDate.lt = "2020-07-07") not_query(or_query(Age.lt = 21, Age.gt = 65))
and_query("Column eq OtherColumn", FirstName.startswith = 'A', LastName.eq = 'Scott') or_query("ExpireDate eq null", ExpireDate.lt = "2020-07-07") not_query(or_query(Age.lt = 21, Age.gt = 65))
This turns an OData function into an R function Parameters are serialized to json. Scalar arguments should be passed as atomic vectors. Array or object arguments should be passed as list.
odata_function(url, metadata = c("none", "minimal", "all"), ...)
odata_function(url, metadata = c("none", "minimal", "all"), ...)
url |
Which url to fetch data from |
metadata |
Whether and how metadata is included |
... |
Arguments passed on to
|
An R function
Other retrieve:
retrieve_all()
,
retrieve_data()
,
retrieve_one()
R6 class that represents an OData query
This class has methods to build and navigate OData services:
Use methods such as $path()
and $get()
to find a path.
Use methods such as $select()
and $filter()
to make your query.
Use methods such as $retrieve()
, $all()
and $one()
to obtain
the results.
url
Generate (encoded) url
new()
Create a class representing a query.
ODataQuery$new( service, .resource = "", .query_options = list(), httr_args = list() )
service
The url of the endpoint to connect to. This url should not end with backslash.
.resource
Should not be used directly. Use $path() instead.
.query_options
Should not be used directly. Use methods such as $select(), $filter() and $query() instead.
httr_args
Additional parameters to pass to httr::GET
value
Read-only
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW")
print()
Print query, useful when debugging.
ODataQuery$print(top = 0, ...)
top
Number of records to print.
...
Additional parameters are passed to print
\dontrun{ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") service$print(10)$path("People")$print() }
path()
Supply path to the resource
ODataQuery$path(...)
...
Components that lead to resource path
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People")
get()
Query an individual record by ID parameters
ODataQuery$get(...)
...
ID-parameters (named or unnamed)
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") russellwhyte <- people_entity$get("russellwhyte")
func()
Path to an OData function
ODataQuery$func(fname, ...)
fname
Name of the function
...
Options passed to retrieve_data
closure
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") get_nearest_airport <- service$func('GetNearestAirport') \dontrun{ get_nearest_airport(lat = 33, lon = -118) }
query()
Supply custom query options that do not start with $
ODataQuery$query(...)
...
Named lists where the names are custom query options
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$query(filter = "FirstName eq 'scott'")$url
top()
Limit the number of results to n
ODataQuery$top(n = 10)
n
Number of records to return at most
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$top(10)
skip()
Skip first few items
ODataQuery$skip(n = 10)
n
Number of items to skip
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$skip(10)
select()
Select fields. If not present, all fields are returned.
ODataQuery$select(...)
...
Fields to select
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$select("FirstName", "LastName")
filter()
Apply filter to result
ODataQuery$filter(...)
...
Can be a raw odata query or query options. It's recommended to use
query options because these will automatically escape parameters.
The parameters are passed on to and_query
.
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$filter(FirstName.eq = 'Scott')
expand()
Expand on expansion properties
ODataQuery$expand(...)
...
Properties to extend on
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$expand("Friends")
orderby()
Order results by one or more keys
ODataQuery$orderby(...)
...
Keys to order by. To order in descending order, the key can be prefixed by a negative sign.
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$orderby('Concurrency') people_entity$orderby('-Concurrency')
search()
Search the entity
ODataQuery$search(s)
s
Search string as defined by the endpoint.
service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$search('Boise')
compute()
Compute properties
Add additional properties to query computed from other attributes.
ODataQuery$compute(...)
...
Named list of properties to compute
# Not really supported by this particular service. service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$compute(a = "5 MUL Concurrency")
retrieve()
Retrieve data
ODataQuery$retrieve(count = FALSE, ...)
count
Whether to include a count of the total number of records
...
Passed to retrieve_data
\dontrun{ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$retrieve() }
all()
Retrieve all data pages
Return concatenation of value of all pages
ODataQuery$all(...)
...
Passed to retrieve_all
\dontrun{ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$all() people_entity$all(jsonlite_args = list(simplifyVector = False)) }
one()
Retrieve individual
ODataQuery$one(...)
...
Passed to retrieve_one
\dontrun{ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$top(1)$one(default = NA) }
and_query()
for details.
## ------------------------------------------------ ## Method `ODataQuery$new` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") ## ------------------------------------------------ ## Method `ODataQuery$print` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") service$print(10)$path("People")$print() ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$path` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") ## ------------------------------------------------ ## Method `ODataQuery$get` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") russellwhyte <- people_entity$get("russellwhyte") ## ------------------------------------------------ ## Method `ODataQuery$func` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") get_nearest_airport <- service$func('GetNearestAirport') ## Not run: get_nearest_airport(lat = 33, lon = -118) ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$query` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$query(filter = "FirstName eq 'scott'")$url ## ------------------------------------------------ ## Method `ODataQuery$top` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$top(10) ## ------------------------------------------------ ## Method `ODataQuery$skip` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$skip(10) ## ------------------------------------------------ ## Method `ODataQuery$select` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$select("FirstName", "LastName") ## ------------------------------------------------ ## Method `ODataQuery$filter` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$filter(FirstName.eq = 'Scott') ## ------------------------------------------------ ## Method `ODataQuery$expand` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$expand("Friends") ## ------------------------------------------------ ## Method `ODataQuery$orderby` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$orderby('Concurrency') people_entity$orderby('-Concurrency') ## ------------------------------------------------ ## Method `ODataQuery$search` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$search('Boise') ## ------------------------------------------------ ## Method `ODataQuery$compute` ## ------------------------------------------------ # Not really supported by this particular service. service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$compute(a = "5 MUL Concurrency") ## ------------------------------------------------ ## Method `ODataQuery$retrieve` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$retrieve() ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$all` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$all() people_entity$all(jsonlite_args = list(simplifyVector = False)) ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$one` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$top(1)$one(default = NA) ## End(Not run)
## ------------------------------------------------ ## Method `ODataQuery$new` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") ## ------------------------------------------------ ## Method `ODataQuery$print` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") service$print(10)$path("People")$print() ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$path` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") ## ------------------------------------------------ ## Method `ODataQuery$get` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") russellwhyte <- people_entity$get("russellwhyte") ## ------------------------------------------------ ## Method `ODataQuery$func` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") get_nearest_airport <- service$func('GetNearestAirport') ## Not run: get_nearest_airport(lat = 33, lon = -118) ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$query` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$query(filter = "FirstName eq 'scott'")$url ## ------------------------------------------------ ## Method `ODataQuery$top` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$top(10) ## ------------------------------------------------ ## Method `ODataQuery$skip` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$skip(10) ## ------------------------------------------------ ## Method `ODataQuery$select` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$select("FirstName", "LastName") ## ------------------------------------------------ ## Method `ODataQuery$filter` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$filter(FirstName.eq = 'Scott') ## ------------------------------------------------ ## Method `ODataQuery$expand` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$expand("Friends") ## ------------------------------------------------ ## Method `ODataQuery$orderby` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$orderby('Concurrency') people_entity$orderby('-Concurrency') ## ------------------------------------------------ ## Method `ODataQuery$search` ## ------------------------------------------------ service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$search('Boise') ## ------------------------------------------------ ## Method `ODataQuery$compute` ## ------------------------------------------------ # Not really supported by this particular service. service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity <- service$path("People") people_entity$compute(a = "5 MUL Concurrency") ## ------------------------------------------------ ## Method `ODataQuery$retrieve` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$retrieve() ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$all` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$all() people_entity$all(jsonlite_args = list(simplifyVector = False)) ## End(Not run) ## ------------------------------------------------ ## Method `ODataQuery$one` ## ------------------------------------------------ ## Not run: service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW") people_entity$top(1)$one(default = NA) ## End(Not run)
Retrieve data. If data is paged, concatenate pages. Only return the value without metadata.
retrieve_all(url, ...)
retrieve_all(url, ...)
url |
Which url to fetch data from |
... |
Arguments passed on to
|
Other retrieve:
odata_function()
,
retrieve_data()
,
retrieve_one()
## Not run: url <- "https://services.odata.org/V4/TripPinServiceRW/People" retrieve_all(url) ## End(Not run)
## Not run: url <- "https://services.odata.org/V4/TripPinServiceRW/People" retrieve_all(url) ## End(Not run)
Retrieve data
retrieve_data( url, metadata = c("none", "minimal", "all"), httr_args = list(), jsonlite_args = list() )
retrieve_data( url, metadata = c("none", "minimal", "all"), httr_args = list(), jsonlite_args = list() )
url |
Which url to fetch data from |
metadata |
Whether and how metadata is included |
httr_args |
List of additional arguments passed on to httr::GET |
jsonlite_args |
List of additional arguments passed on to jsonlite::fromJSON |
Data including metadata
Other retrieve:
odata_function()
,
retrieve_all()
,
retrieve_one()
## Not run: url <- "https://services.odata.org/V4/TripPinServiceRW" retrieve_data(url) ## End(Not run)
## Not run: url <- "https://services.odata.org/V4/TripPinServiceRW" retrieve_data(url) ## End(Not run)
Retrieve single instance.
retrieve_one(url, default = stop("value not found"), ...)
retrieve_one(url, default = stop("value not found"), ...)
url |
Which url to fetch data from |
default |
The default if nothing was found. If not specified, an error is thrown in this case. |
... |
Arguments passed on to
|
Single value or default if none. If the result consists of multiple records, an error is thrown.
Other retrieve:
odata_function()
,
retrieve_all()
,
retrieve_data()
## Not run: url <- "https://services.odata.org/V4/TripPinServiceRW/People?$top=1" retrieve_one(url) url <- "https://services.odata.org/V4/TripPinServiceRW/People('russellwhyte')" retrieve_one(url) ## End(Not run)
## Not run: url <- "https://services.odata.org/V4/TripPinServiceRW/People?$top=1" retrieve_one(url) url <- "https://services.odata.org/V4/TripPinServiceRW/People('russellwhyte')" retrieve_one(url) ## End(Not run)
Macro to convert R to OData syntax
to_odata(expr) to_odata_(expr)
to_odata(expr) to_odata_(expr)
expr |
Expression to convert to OData |
to_odata takes unquote R code and quotes its input. Use !! to unquote an argument. to_odata_ requires its argument to be quoted already.
Only a subset of R is supported.
* arithmatic The operators +, -, *, / and
* strings (characters in R) toupper, tolower, startsWith, endsWith, nchar, paste, paste0, trimws
* arrays (lists in R) list, append, length
* Formulae become lambdas in OData (x ~ x$Name == "John")
Every unknown function is passed as is. If the function name is surrounded by percent signs it's treated as an infix operator.
to_odata(Field == value) address <- "Bakerstreet 4" to_odata(!!address %in% Adresses) to_odata(Friends$any(f ~ f$FirstName == 'John'))
to_odata(Field == value) address <- "Bakerstreet 4" to_odata(!!address %in% Adresses) to_odata(Friends$any(f ~ f$FirstName == 'John'))