-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathudf.R
More file actions
156 lines (137 loc) · 4.4 KB
/
Copy pathudf.R
File metadata and controls
156 lines (137 loc) · 4.4 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
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
#' Methods for working with Azure Cosmos DB user-defined functions
#'
#' @param object A Cosmos DB container object, as obtained by `get_cosmos_container` or `create_cosmos_container`, or for `delete_udf.cosmos_udf`, the function object.
#' @param funcname The name of the user-defined function.
#' @param body For `create_udf` and `replace_udf`, the body of the function. This can be either a character string containing the source code, or the name of a source file.
#' @param confirm For `delete_udf`, whether to ask for confirmation before deleting.
#' @param ... Optional arguments passed to lower-level functions.
#' @details
#' These are methods for working with user-defined functions (UDFs) in Azure Cosmos DB using the core (SQL) API. In the Cosmos DB model, UDFs are written in JavaScript and associated with a container.
#'
#' @return
#' For `get_udf` and `create_udf`, an object of class `cosmos_udf`. For `list_udfs`, a list of such objects.
#' @seealso
#' [cosmos_container], [get_stored_procedure]
#' @examples
#' \dontrun{
#'
#' endp <- cosmos_endpoint("https://myaccount.documents.azure.com:443/", key="mykey")
#' db <- get_cosmos_database(endp, "mydatabase")
#'
#' # importing the Star Wars data from dplyr
#' cont <- endp %>%
#' get_cosmos_database(endp, "mydatabase") %>%
#' create_cosmos_container(db, "mycontainer", partition_key="sex")
#'
#' create_udf(cont, "times2", "function(x) { return 2*x; }")
#'
#' list_udfs(cont)
#'
#' # UDFs in queries are prefixed with the 'udf.' identifier
#' query_documents(cont, "select udf.times2(c.height) t2 from cont c")
#'
#' delete_udf(cont, "times2")
#'
#' }
#' @rdname cosmos_udf
#' @export
get_udf <- function(object, ...)
{
UseMethod("get_udf")
}
#' @rdname cosmos_udf
#' @export
get_udf.cosmos_container <- function(object, funcname, ...)
{
path <- file.path("udfs", funcname)
res <- do_cosmos_op(object, path, "udfs", path, ...)
udf <- process_cosmos_response(res)
as_udf(udf, object)
}
#' @rdname cosmos_udf
#' @export
list_udfs <- function(object, ...)
{
UseMethod("list_udfs")
}
#' @export
list_udfs.cosmos_container <- function(object, ...)
{
res <- do_cosmos_op(object, "udfs", "udfs", "", ...)
atts <- if(inherits(res, "response"))
process_cosmos_response(res)$UserDefinedFunctions
else unlist(lapply(process_cosmos_response(res), `[[`, "UserDefinedFunctions"), recursive=FALSE)
lapply(atts, as_udf, container=object)
}
#' @rdname cosmos_udf
#' @export
create_udf <- function(object, ...)
{
UseMethod("create_udf")
}
#' @rdname cosmos_udf
#' @export
create_udf.cosmos_container <- function(object, funcname, body, ...)
{
if(is.character(body) && length(body) == 1 && file.exists(body))
body <- readLines(body)
body <- list(id=funcname, body=paste0(body, collapse="\n"))
res <- do_cosmos_op(object, "udfs", "udfs", "", body=body, encode="json", http_verb="POST", ...)
udf <- process_cosmos_response(res)
invisible(as_udf(udf, object))
}
#' @rdname cosmos_udf
#' @export
replace_udf <- function(object, ...)
{
UseMethod("replace_udf")
}
#' @rdname cosmos_udf
#' @export
replace_udf.cosmos_container <- function(object, funcname, body, ...)
{
body <- list(id=funcname, body=body)
path <- file.path("udfs", funcname)
res <- do_cosmos_op(object, path, "udfs", path, body=body, encode="json", http_verb="PUT", ...)
udf <- process_cosmos_response(res)
invisible(as_udf(udf, object))
}
#' @rdname cosmos_udf
#' @export
replace_udf.cosmos_udf <- function(object, body, ...)
{
replace_udf.cosmos_container(object$container, object$id, body, ...)
}
#' @rdname cosmos_udf
#' @export
delete_udf <- function(object, ...)
{
UseMethod("delete_udf")
}
#' @rdname cosmos_udf
#' @export
delete_udf.cosmos_container <- function(object, funcname, confirm=TRUE, ...)
{
if(!delete_confirmed(confirm, funcname, "stored procedure"))
return(invisible(NULL))
path <- file.path("udfs", funcname)
res <- do_cosmos_op(object, path, "udfs", path, http_verb="DELETE", ...)
invisible(process_cosmos_response(res))
}
#' @rdname cosmos_udf
#' @export
delete_udf.cosmos_udf <- function(object, ...)
{
delete_udf(object$container, object$id, ...)
}
#' @export
print.cosmos_udf <- function(x, ...)
{
cat("Cosmos DB SQL user defined function '", x$id, "'\n", sep="")
invisible(x)
}
as_udf <- function(udf, container)
{
udf$container <- container
structure(udf, class="cosmos_udf")
}