|
| 1 | +# |
| 2 | +# |
| 3 | +# @Author -- Jingtang Zhang |
| 4 | +# @Date -- 2018.7.13, Hangzhou |
| 5 | +# @Update -- 2018.8.2, Hangzhou |
| 6 | +# |
| 7 | +# @description The definition and part of implementation of class 'RDolphinDB' |
| 8 | +# @description The comments with [#'] is used for generating documents automatically |
| 9 | +# |
| 10 | +# In R CMD, type devtools::document() to generate documents. |
| 11 | +# A function with '@export' will be output to NAMESPACE, |
| 12 | +# which can be called outside the package. |
| 13 | +# |
| 14 | +# To generate document for package, modify file DESCRIPTION. |
| 15 | +# To generate documents for functions, modify comment below. |
| 16 | +# Then use devtools::document() to override old documents. |
| 17 | +# |
| 18 | + |
| 19 | +setClass("RDolphinDB", slots = list(connected = "logical")) |
| 20 | + |
| 21 | +#' @title dbConnect |
| 22 | +#' |
| 23 | +#' @description Method of getting connection with DolphinDB server. |
| 24 | +#' @description (If the input contains 'username' and 'password', a log-in job will be done) |
| 25 | +#' @param conn The connector object. |
| 26 | +#' @param host The host running the DolphinDB server. |
| 27 | +#' @param port The port running the DolphinDB server. |
| 28 | +#' @param username The username to login onto server. (NULL if default) |
| 29 | +#' @param password The password to login onto server. (NULL if default) |
| 30 | +#' @return Return the connector object with status of connection. |
| 31 | +#' @export |
| 32 | +#' @examples |
| 33 | +#' conn <- dbConnect(DolphinDB(), "localhost", 8848) # Without log-in |
| 34 | +#' # conn <- dbConnect(DolphinDB(), "localhost", 8848, "username", "password") |
| 35 | +#' |
| 36 | +#' if (conn@connected == TRUE) { |
| 37 | +#' # TO DO ... |
| 38 | +#' # dbRun(...) |
| 39 | +#' } |
| 40 | +setGeneric( |
| 41 | + "dbConnect", |
| 42 | + function(conn, host, port, username = NULL, password = NULL) { |
| 43 | + standardGeneric("dbConnect") |
| 44 | + } |
| 45 | +) |
| 46 | +setMethod( |
| 47 | + "dbConnect", |
| 48 | + signature(conn = "RDolphinDB"), |
| 49 | + function(conn, host, port, username = NULL, password = NULL) { |
| 50 | + # Check input |
| 51 | + if (!is.null(username)) { |
| 52 | + if (!is.character(username)) { |
| 53 | + print("'username' should be a character") |
| 54 | + return (NULL) |
| 55 | + } |
| 56 | + } |
| 57 | + if (!is.null(password)) { |
| 58 | + if (!is.character(password)) { |
| 59 | + print("'password' should be a character") |
| 60 | + return (NULL) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + # Connect |
| 65 | + xxdb_stat <- Connect(host, port) |
| 66 | + conn@connected <- xxdb_stat |
| 67 | + |
| 68 | + # Log In |
| 69 | + if ((!is.null(username)) && (!is.null(password))) { |
| 70 | + DDB_RPC("login", list(username, password)) |
| 71 | + } |
| 72 | + |
| 73 | + return (conn) |
| 74 | + } |
| 75 | +) |
| 76 | + |
| 77 | +#' @title dbRun |
| 78 | +#' |
| 79 | +#' @description Method of running scripts on DolphinDB. |
| 80 | +#' @param conn The connector object. |
| 81 | +#' @param script The script running the DolphinDB server. |
| 82 | +#' @return Return the excuted result of script from server in R type. |
| 83 | +#' @export |
| 84 | +#' @examples |
| 85 | +#' result <- dbRun(conn, "matrix(1.5 2.5 3.5, 4 NULL 6)") |
| 86 | +#' print(result) |
| 87 | +setGeneric( |
| 88 | + "dbRun", |
| 89 | + function(conn, script) { |
| 90 | + standardGeneric("dbRun") |
| 91 | + } |
| 92 | +) |
| 93 | +setMethod( |
| 94 | + "dbRun", |
| 95 | + signature(conn = "RDolphinDB"), |
| 96 | + function(conn, script) { |
| 97 | + type <- RunScript(script) |
| 98 | + DDB_GetEntity(type) |
| 99 | + } |
| 100 | +) |
| 101 | + |
| 102 | +#' @title dbRpc |
| 103 | +#' |
| 104 | +#' @description Method of running remote procedure call. |
| 105 | +#' @param conn The connector object. |
| 106 | +#' @param func The function name running the DolphinDB server. |
| 107 | +#' @param args The list containing all arguments needed to be uploaded to server. |
| 108 | +#' @return Return the excuted result of function from server in R type. |
| 109 | +#' @export |
| 110 | +#' @examples |
| 111 | +#' result <- dbRpc(conn, "concat", list("hello", "world")) |
| 112 | +#' print(result) |
| 113 | +setGeneric( |
| 114 | + "dbRpc", |
| 115 | + function(conn, func, args) { |
| 116 | + standardGeneric("dbRpc") |
| 117 | + } |
| 118 | +) |
| 119 | +setMethod( |
| 120 | + "dbRpc", |
| 121 | + signature(conn = "RDolphinDB"), |
| 122 | + function(conn, func, args) { |
| 123 | + # Check input |
| 124 | + if (class(func) != "character") { |
| 125 | + print("Error: parameter 'func' must be a string") |
| 126 | + return (NULL) |
| 127 | + } |
| 128 | + if (class(args) != "list") { |
| 129 | + print("Error: parameter 'args' must be a list") |
| 130 | + return (NULL) |
| 131 | + } |
| 132 | + # Execute RPC |
| 133 | + res_entity <- DDB_RPC(func, args) |
| 134 | + return (res_entity) |
| 135 | + } |
| 136 | +) |
| 137 | + |
| 138 | +#' @title dbUpload |
| 139 | +#' |
| 140 | +#' @description Method of uploading arguments to DolphinDB. |
| 141 | +#' @param conn The connector object. |
| 142 | +#' @param keys The variable names. (Cannot be repeated) |
| 143 | +#' @param args The list containing all arguments needed to be uploaded to server. |
| 144 | +#' @return Return the excuted result of uploading. |
| 145 | +#' @export |
| 146 | +#' @examples |
| 147 | +#' result <- dbUpload(conn, c("x", "y"), list(2.5L, "hello")) |
| 148 | +#' if (result == TRUE) { |
| 149 | +#' r_x <- dbRun(conn, "x") |
| 150 | +#' r_y <- dbRun(conn, "y") |
| 151 | +#' print(r_x) |
| 152 | +#' print(r_y) |
| 153 | +#' } |
| 154 | +setGeneric( |
| 155 | + "dbUpload", |
| 156 | + function(conn, keys, args) { |
| 157 | + standardGeneric("dbUpload") |
| 158 | + } |
| 159 | +) |
| 160 | +setMethod( |
| 161 | + "dbUpload", |
| 162 | + signature(conn = "RDolphinDB"), |
| 163 | + function(conn, keys, args) { |
| 164 | + # Check input |
| 165 | + if (!is.character(keys)) { |
| 166 | + print("'Keys' should be a character vector") |
| 167 | + return (NULL) |
| 168 | + } |
| 169 | + if (!is.list(args)) { |
| 170 | + print("'args' should be uploaded in a list") |
| 171 | + return (NULL) |
| 172 | + } |
| 173 | + if (length(unique(keys)) != length(args)) { |
| 174 | + print("Repeat keys in 'Keys'") |
| 175 | + return (NULL) |
| 176 | + } |
| 177 | + if (!DDB_UploadObjectCheck(args)) { |
| 178 | + return (NULL) |
| 179 | + } |
| 180 | + # Write header |
| 181 | + writable <- RunUploadInit(keys) |
| 182 | + if (writable == FALSE) { |
| 183 | + return (NULL) |
| 184 | + } |
| 185 | + # Upload entity |
| 186 | + DDB_UploadEntity(args) |
| 187 | + # Get status |
| 188 | + res_stat <- ReceiveHeader() |
| 189 | + return (res_stat) |
| 190 | + } |
| 191 | +) |
| 192 | + |
| 193 | +#' @title dbClose |
| 194 | +#' |
| 195 | +#' @description Method of closing connection with DolphinDB server. |
| 196 | +#' @param conn The connector object. |
| 197 | +#' @export |
| 198 | +#' @examples |
| 199 | +#' dbClose(conn) |
| 200 | +setGeneric( |
| 201 | + "dbClose", |
| 202 | + function(conn) { |
| 203 | + standardGeneric("dbClose") |
| 204 | + } |
| 205 | +) |
| 206 | +setMethod( |
| 207 | + "dbClose", |
| 208 | + signature(conn = "RDolphinDB"), |
| 209 | + function(conn) { |
| 210 | + DisConnect() |
| 211 | + object@connected <- FALSE |
| 212 | + } |
| 213 | +) |
| 214 | + |
| 215 | +obj <- new("RDolphinDB") |
| 216 | + |
| 217 | +#' @title DolphinDB |
| 218 | +#' |
| 219 | +#' @description Method to getting an object of connector. |
| 220 | +#' @export |
| 221 | +#' @examples |
| 222 | +#' conn <- DolphinDB() |
| 223 | +#' conn <- dbConnect(conn, "localhost", 8848) |
| 224 | +#' |
| 225 | +#' # Recommanded |
| 226 | +#' conn <- dbConnect(DolphinDB(), "localhost", 8848) |
| 227 | +DolphinDB <- function() { |
| 228 | + return (obj) |
| 229 | +} |
| 230 | + |
0 commit comments