Title: | 'BaseX' Client |
---|---|
Description: | 'BaseX' <https://basex.org> is a XML database engine and a compliant 'XQuery 3.1' processor with full support of 'W3C Update Facility'. This package is a full client-implementation of the client/server protocol for 'BaseX' and provides functionalities to create, manipulate and query on XML-data. |
Authors: | Ben Engbers [aut, cre] |
Maintainer: | Ben Engbers <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.2 |
Built: | 2025-02-22 02:54:09 UTC |
Source: | https://github.com/benengbers/rbasex |
Adds a new resource to the opened database.
Add(session, path, input)
Add(session, path, input)
session |
BasexClient instance-ID |
path |
Path |
input |
Additional input (optional) |
The input can be a UTF-8 encoded XML document, a binary resource, or any other data (such as JSON or CSV) that can be successfully converted to a resource by the server. The utility-function input_to_raw can be used to convert an arbitrary character vector to a stream. This method returns self invisibly, thus making it possible to chain together multiple method calls.
A list with two items
info Aditional info
success Boolean, indicating if the command was completed successfull
## Not run: Add(Session, "test", "<xml>Add</xml>") ## End(Not run)
## Not run: Add(Session, "test", "<xml>Add</xml>") ## End(Not run)
Binds a value to a variable.
Bind(query_obj, ...)
Bind(query_obj, ...)
query_obj |
QueryClass instance-ID |
... |
Binding Information |
Binding information can be provided in the following ways:
name, value Name and value for a variable.
name, value, type Name, value and type for a variable.
name, list(value) Name, list of values.
name, list(value), list(type) Name, list of values, list of types.
For a list of possibe types see https://docs.basex.org/wiki/Java_Bindings#Data_Types
This method returns self invisibly, thus making it possible to chain together multiple method calls.
Boolean value which indicates if the operation was executed successfull
## Not run: query_obj <- Query(Session, "declare variable $name external; for $i in 1 to 2 return element { $name } { $i }") Bind(query_obj, "$name", "number") print(Execute(query_obj)) query_obj <- Query(Session, "declare variable $name external; for $i in 3 to 4 return element { $name } { $i }") Bind(query_obj, "$name", "number", "xs:string") print(Execute(query_obj)) query_obj <- Query(Session, "declare variable $name external; for $t in collection('TestDB/Books')/book where $t/@author = $name return $t/@title/string()") Bind(query_obj, "$name", list("Walmsley", "Wickham")) print(Execute(query_obj)) query_obj <- Query(Session, "declare variable $name external; for $t in collection('TestDB/Books')/book where $t/@author = $name return $t/@title/string()") Bind(query_obj, "$name", list("Walmsley", "Wickham"), list("xs:string", "xs:string")) print(Execute(query_obj)) ## End(Not run)
## Not run: query_obj <- Query(Session, "declare variable $name external; for $i in 1 to 2 return element { $name } { $i }") Bind(query_obj, "$name", "number") print(Execute(query_obj)) query_obj <- Query(Session, "declare variable $name external; for $i in 3 to 4 return element { $name } { $i }") Bind(query_obj, "$name", "number", "xs:string") print(Execute(query_obj)) query_obj <- Query(Session, "declare variable $name external; for $t in collection('TestDB/Books')/book where $t/@author = $name return $t/@title/string()") Bind(query_obj, "$name", list("Walmsley", "Wickham")) print(Execute(query_obj)) query_obj <- Query(Session, "declare variable $name external; for $t in collection('TestDB/Books')/book where $t/@author = $name return $t/@title/string()") Bind(query_obj, "$name", list("Walmsley", "Wickham"), list("xs:string", "xs:string")) print(Execute(query_obj)) ## End(Not run)
Closes and unregisters the query with the specified ID
Close(query_obj)
Close(query_obj)
query_obj |
QueryClass instance-ID |
This method returns self invisibly, thus making it possible to chain together multiple method calls.
This function returns a list with the following items:
info Info
success A boolean, indicating if the command was completed successfull
Executes a database command or a query.
Command(...)
Command(...)
... |
The command or query to be executed. When used to execute a command, a SessionID and a string which contains the command, are to be passed. When used to execute a query, the QueryClass instance-ID is passed. |
For a list of database commands see https://docs.basex.org/wiki/Commands
'BaseX' can be used in a Standard mode or Query mode.
In the standard mode of the Clients, a database command can be sent to the server using the Command() function of the Session. The query mode of the Clients allows you to bind external variables to a query and evaluate the query in an iterative manner.
When used to execute commands in the Standard mode, this function returns a list with the following items:
result
info Aditional info
success A boolean, indicating if the command was completed successfull
When used to execute a query, it return the result as a list.
## Not run: Session <- NewBasexClient(user = <username>, password = "<password>") print(Command(Session, "info")$info) query_txt <- paste("for $i in 1 to 2", "return <xml>Text { $i }</xml>", sep = " ") query_obj <- Query(Session, query_txt) print(Command(query_obj)) ## End(Not run)
## Not run: Session <- NewBasexClient(user = <username>, password = "<password>") print(Command(Session, "info")$info) query_txt <- paste("for $i in 1 to 2", "return <xml>Text { $i }</xml>", sep = " ") query_obj <- Query(Session, query_txt) print(Command(query_obj)) ## End(Not run)
Binds a value to the context. The type will be ignored if the string is empty. The function returns no value.
Context(query_obj, value, type)
Context(query_obj, value, type)
query_obj |
QueryClass instance-ID |
value |
Value that should be boud to the context |
type |
The type will be ignored when the string is empty |
The type that is provided to the context, should be one of the standard-types. An alternative way is to parse the document information. This method returns self invisibly, thus making it possible to chain together multiple method calls.
## Not run: ctxt_query_txt <- "for $t in .//text() return string-length($t)" ctxt_query <- Query(Session, ctxt_query_txt) ctxt_txt <- paste0("<xml>", "<txt>Hi</txt>", "<txt>World</txt>", "</xml>") Context(ctxt_query, ctxt_txt, type = "document-node()") print(Execute(ctxt_query)) ## returns "2" "5" ctxt_query_txt <- "for $t in parse-xml(.)//text() return string-length($t)" Context(ctxt_query, ctxt_txt) print(Execute(ctxt_query)) ## End(Not run)
## Not run: ctxt_query_txt <- "for $t in .//text() return string-length($t)" ctxt_query <- Query(Session, ctxt_query_txt) ctxt_txt <- paste0("<xml>", "<txt>Hi</txt>", "<txt>World</txt>", "</xml>") Context(ctxt_query, ctxt_txt, type = "document-node()") print(Execute(ctxt_query)) ## returns "2" "5" ctxt_query_txt <- "for $t in parse-xml(.)//text() return string-length($t)" Context(ctxt_query, ctxt_txt) print(Execute(ctxt_query)) ## End(Not run)
Creates a new database with the specified name and input (may be empty).
Create(session, name, input)
Create(session, name, input)
session |
BasexClient instance-ID |
name |
Database name |
input |
Additional input, may be empty |
The input can be a UTF-8 encoded XML document, a binary resource, or any other data (such as JSON or CSV) that can be successfully converted to a resource by the server. 'Check' is a convenience command that combines OPEN and CREATE DB: If a database with the name input exists, and if there is no existing file or directory with the same name that has a newer timestamp, the database is opened. Otherwise, a new database is created; if the specified input points to an existing resource, it is stored as initial content. This method returns self invisibly, thus making it possible to chain together multiple method calls.
A list with two items
info Aditional info
success A boolean, indicating if the command was completed successfull
## Not run: Create(, "test", "<xml>Create test</xml>") Execute(Session, "Check test") Create(Session, "test2", "https://raw.githubusercontent.com/BaseXdb/basex/master/basex-api/src/test/resources/first.xml") Create(Session, "test3", "/home/username/Test.xml") ## End(Not run)
## Not run: Create(, "test", "<xml>Create test</xml>") Execute(Session, "Check test") Create(Session, "test2", "https://raw.githubusercontent.com/BaseXdb/basex/master/basex-api/src/test/resources/first.xml") Create(Session, "test3", "/home/username/Test.xml") ## End(Not run)
Executes a database command or a query.
Execute(...)
Execute(...)
... |
The command or query to be executed. When used to execute a command, a SessionID and a string which contains the command, are to be passed. When used to execute a query, the QueryClass instance-ID is passed. |
The 'Execute' command is obsolete and has been renamed to 'Command'. 'Execute' is being kept as convenience.
When used to execute commands in the Standard mode, this function returns a list with the following items:
result
info Aditional info
success A boolean, indicating if the command was completed successfull
When used to execute a query, it return the result as a list.
## Not run: Session <- NewBasexClient(user = <username>, password = "<password>") print(Execute(Session, "info")$info) query_txt <- paste("for $i in 1 to 2", "return <xml>Text { $i }</xml>", sep = " ") query_obj <- Query(Session, query_txt) print(Execute(query_obj)) ## End(Not run)
## Not run: Session <- NewBasexClient(user = <username>, password = "<password>") print(Execute(Session, "info")$info) query_txt <- paste("for $i in 1 to 2", "return <xml>Text { $i }</xml>", sep = " ") query_obj <- Query(Session, query_txt) print(Execute(query_obj)) ## End(Not run)
Executes a query and returns a list of vectors, each one representing a result as a string , prefixed by the 'XDM' (Xpath Data Model) Meta Data <https://www.xdm.org/>. Meta Data and results are seaparated by a '|'.
Full(query_obj)
Full(query_obj)
query_obj |
QueryClass instance-ID |
## Not run: query_txt <- "collection('/TestDB/Test.xml')" query_obj <- Query(Session, query_txt) print(Full(query_obj)) ## Return [[1]] [1] "2f" "/TestDB/Test.xml" [[2]] [1] "3c" "Line_1 line=\"1\">Content 1</Line_1" [[3]] [1] "2f" "/TestDB/Test.xml" [[4]] [1] "3c" "Line_2 line=\"2\">Content 2</Line_2" ## End(Not run)
## Not run: query_txt <- "collection('/TestDB/Test.xml')" query_obj <- Query(Session, query_txt) print(Full(query_obj)) ## Return [[1]] [1] "2f" "/TestDB/Test.xml" [[2]] [1] "3c" "Line_1 line=\"1\">Content 1</Line_1" [[3]] [1] "2f" "/TestDB/Test.xml" [[4]] [1] "3c" "Line_2 line=\"2\">Content 2</Line_2" ## End(Not run)
Current value for session$Intercept
GetIntercept(session)
GetIntercept(session)
session |
BasexClient instance-ID |
Current value
Current value from session$Success
GetSuccess(session)
GetSuccess(session)
session |
BasexClient instance-ID |
Current value
Returns a string with query compilation and profiling info.
Info(query_obj)
Info(query_obj)
query_obj |
QueryClass instance-ID |
If the query object has not been executed yet, an empty string is returned.
This function returns a list with the following items:
Info Info
success A boolean, indicating if the command was completed successfull
Convert input to a length-1 character vector.
input_to_raw(input)
input_to_raw(input)
input |
Character vector length 1 |
If input is a reference to a file, the number of bytes corresponding to the size is read. If it is an URL, the URL is read and converted to a 'Raw' vector. The function does not catch errors.
'Raw' vector
Indicates if there are any other results in the query-result.
More(query_obj)
More(query_obj)
query_obj |
QueryClass instance-ID |
Boolean
## Not run: Query_1 <- Query(Session, "collection('/TestDB/Test.xml')") iterResult <- c() while (More(Query_1)) { iterResult <- c(iterResult, Next(Query_1)) } print(iterResult) [[1]] [1] "0d" "<Line_1 line=\"1\">Content 1</Line_1>" [[2]] [1] "0d" "<Line_2 line=\"2\">Content 2</Line_2>" ## End(Not run)
## Not run: Query_1 <- Query(Session, "collection('/TestDB/Test.xml')") iterResult <- c() while (More(Query_1)) { iterResult <- c(iterResult, Next(Query_1)) } print(iterResult) [[1]] [1] "0d" "<Line_1 line=\"1\">Content 1</Line_1>" [[2]] [1] "0d" "<Line_2 line=\"2\">Content 2</Line_2>" ## End(Not run)
Create a BaseX-client
NewBasexClient(host = "localhost", port = 1984, user, password)
NewBasexClient(host = "localhost", port = 1984, user, password)
host , port
|
Host name and port-number |
user , password
|
User credentials |
This creates a BaseX-client. By default it listens to port 1984 on localhost. Username and password should be changed after the installation of 'BaseX'.
BasexClient-instance
## Not run: session <- NewBasexClient(user = <username>, password = "<password>") ## End(Not run)
## Not run: session <- NewBasexClient(user = <username>, password = "<password>") ## End(Not run)
Returns the next result when iterating over a query
Next(query_obj)
Next(query_obj)
query_obj |
QueryClass instance-ID |
## Not run: Query_1 <- Query(Session, "collection('TestDB/Test.xml')") iterResult <- c() while (More(Query_1)) { iterResult <- c(iterResult, Next(Query_1)) } print(iterResult) [[1]] [1] "0d" "<Line_1 line=\"1\">Content 1</Line_1>" [[2]] [1] "0d" "<Line_2 line=\"2\">Content 2</Line_2>" ## End(Not run)
## Not run: Query_1 <- Query(Session, "collection('TestDB/Test.xml')") iterResult <- c() while (More(Query_1)) { iterResult <- c(iterResult, Next(Query_1)) } print(iterResult) [[1]] [1] "0d" "<Line_1 line=\"1\">Content 1</Line_1>" [[2]] [1] "0d" "<Line_2 line=\"2\">Content 2</Line_2>" ## End(Not run)
Returns a string with all query serialization parameters, which can be assigned to the serializer option.
Options(query_obj)
Options(query_obj)
query_obj |
QueryClass instance-ID |
For a list of possibe types see https://docs.basex.org/wiki/Java_Bindings#Data_Types
This function returns a list with the following items:
Options Options
success A boolean, indicating if the command was completed successfull
Creates a new query instance and returns it's id.
Query(session, query_string)
Query(session, query_string)
session |
BasexClient instance-ID |
query_string |
query string |
If paste0() is used to create a multi-line statement, the lines must be separeted by a space or a newline \n-character.
Query_ID
## Not run: query_txt <- "for $i in 1 to 2 return <xml>Text { $i }</xml>" query_obj <- Query(Session, query_txt) print(Execute(query_obj)) ## End(Not run)
## Not run: query_txt <- "for $i in 1 to 2 return <xml>Text { $i }</xml>" query_obj <- Query(Session, query_txt) print(Execute(query_obj)) ## End(Not run)
The client can be used in 'standard' mode and in 'query' mode. Query mode is used to define queries, binding variables and for iterative evaluation.
new()
Initialize a new instance from QueryClass
QueryClass$new(query, Parent)
query
Query-string
Parent
The 'Parent' for this QueryClass-instance
QueryClass-instances can only be created by calling the 'Query'-method from the 'BasexClient'-class.
ExecuteQuery()
Executes a query.
QueryClass$ExecuteQuery()
Bind()
Binds a value to a variable.
QueryClass$Bind(...)
...
Binding Information
query_obj
QueryClass instance-ID
When using the primitive functions, this function can be chained.
Context()
Binds a value to the context. The type will be ignored if the string is empty.
QueryClass$Context(value, type)
value
Value that should be boud to the context
type
The type will be ignored when the string is empty
When using the primitive functions, this function can be chained.
Full()
Executes a query and returns a vector with all resulting items as strings, prefixed by the 'XDM' (Xpath Data Model) Meta Data <https://www.xdm.org/>.
QueryClass$Full()
More()
Indicates if there are any other results in the query-result.
QueryClass$More()
Next()
Returns the next result when iterating over a query
QueryClass$Next()
Info()
Returns a string with query compilation and profiling info.
QueryClass$Info()
Options()
Returns a string with all query serialization parameters, which can e.g. be assigned to the serializer option.
QueryClass$Options()
Updating()
Check if the query contains updating expressions.
QueryClass$Updating()
Close()
Closes and unregisters the query with the specified ID
QueryClass$Close()
When using the primitive functions, this function can be chained.
clone()
The objects of this class are cloneable with this method.
QueryClass$clone(deep = FALSE)
deep
Whether to make a deep clone.
'BaseX' is a robust, high-performance XML database engine and a highly compliant XQuery 3.1 processor with full support of the W3C Update and Full Text extensions.
The client can be used in 'standard' mode and in 'query' mode. Standard Mode is used for connecting to a server and sending commands.
'RBaseX' was developed using R6. For most of the public methods in the R6-classes, wrapper-functions are created. The differences in performance between R6-methods and wrapper-functions are minimal and slightly in advantage of the R6-version.
It is easy to use the R6-calls instead of the wrapper-functions. The only important difference is that in order to execute a query, you have to call ExecuteQuery() on a queryObject.
new()
Initialize a new client-session
BasexClient$new(host, port = 1984L, username, password)
host, port, username, password
Host-information and user-credentials
Command()
Execute a command
BasexClient$Command(command)
command
Command
For a list of database commands see https://docs.basex.org/wiki/Commands
Execute()
Execute a command
BasexClient$Execute(command)
command
Command
For a list of database commands see https://docs.basex.org/wiki/Commands. This function is replaced by 'Command' and is obsolete.
Query()
Create a new query-object
BasexClient$Query(query_string)
query_string
Query-string
A query-object has two fields. 'queryObject' is an ID for the new created 'QueryClass'-instance. 'success' holds the status from the last executed operation on the queryObject.
ID for the created query-object
Create()
Create a new database
BasexClient$Create(name, input)
name
Name
input
Initial content, Optional
Initial content can be offered as string, URL or file.
Add()
Add a new resouce at the specified path
BasexClient$Add(path, input)
path
Path
input
File, directory or XML-string
Replace()
Replace resource, adressed by path
BasexClient$Replace(path, input)
path
Path
input
File, directory or XML-string
Store()
Store binary content
BasexClient$Store(path, input)
path
Path
input
File, directory or XML-string
Binary content can be retrieved by executing a retrieve-command
set_intercept()
Toggles between using the ´success'-field, returned by the Execute-command or using regular error-handling (try-catch).
BasexClient$set_intercept(Intercept)
Intercept
Boolean
restore_intercept()
Restore the Intercept Toggles to the original value
BasexClient$restore_intercept()
get_intercept()
Get current Intercept
BasexClient$get_intercept()
get_socket()
Get the socket-ID
BasexClient$get_socket()
Socket-ID,
set_success()
Set the status success-from the last operation on the socket
BasexClient$set_success(Success)
Success
Boolean
This function is intended to be used by instances from the QueryClass
get_success()
Get the status success-from the last operation on the socket
BasexClient$get_success()
Boolean,
clone()
The objects of this class are cloneable with this method.
BasexClient$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run: Session <- BasexClient$new("localhost", 1984L, username = "<username>", password = "<password>") Session$Execute("Check test") Session$Execute("delete /") # Add resource Session$Add("test.xml", "<root/>") # Bindings ----- query_txt <- "declare variable $name external; for $i in 1 to 3 return element { $name } { $i }" query_obj <- Session$Query(query_txt) query_obj$queryObject$Bind("$name", "number") print(query_obj$queryObject$ExecuteQuery()) ## End(Not run)
## Not run: Session <- BasexClient$new("localhost", 1984L, username = "<username>", password = "<password>") Session$Execute("Check test") Session$Execute("delete /") # Add resource Session$Add("test.xml", "<root/>") # Bindings ----- query_txt <- "declare variable $name external; for $i in 1 to 3 return element { $name } { $i }" query_obj <- Session$Query(query_txt) query_obj$queryObject$Bind("$name", "number") print(query_obj$queryObject$ExecuteQuery()) ## End(Not run)
Replaces a resource with the specified input.
Replace(session, path, input)
Replace(session, path, input)
session |
BasexClient instance-ID |
path |
Path where to store the data |
input |
Replacement |
The input can be a UTF-8 encoded XML document, a binary resource, or any other data (such as JSON or CSV) that can be successfully converted to a resource by the server. This method returns self invisibly, thus making it possible to chain together multiple method calls.
A list with two items
info Aditional info
success A boolean, indicating if the command was completed successfull
## Not run: Replace(Session, "test", "<xml>Create test</xml>") ## End(Not run)
## Not run: Replace(Session, "test", "<xml>Create test</xml>") ## End(Not run)
Restore Intercept to original new value
RestoreIntercept(session)
RestoreIntercept(session)
session |
BasexClient instance-ID |
This method returns self invisibly, thus making it possible to chain together multiple method calls.
Converts the query-result to a frame. The query-result is either a list (sequence) or an array. If it is a list, 'cols' is needed to determine the number of columns.
result2frame(...)
result2frame(...)
... |
Query-result |
Return result from query as dataframe
Converts the query-result to a tibble. The query-result is either a list (sequence) or an array. If it is a list, 'cols' is needed to determine the number of columns.
result2tibble(...)
result2tibble(...)
... |
Query-result |
Return result from query as tibble
Assign a new value to session$Intercept
SetIntercept(session, intercept)
SetIntercept(session, intercept)
session |
BasexClient instance-ID |
intercept |
New Intercept value |
This method returns self invisibly, thus making it possible to chain together multiple method calls.
## Not run: SetIntercept(TRUE) ## End(Not run)
## Not run: SetIntercept(TRUE) ## End(Not run)
Assign a new value to session$Success
SetSuccess(session, success)
SetSuccess(session, success)
session |
BasexClient instance-ID |
success |
Success-indicator for the last operation on the socket |
## Not run: SetSuccess(TRUE) ## End(Not run)
## Not run: SetSuccess(TRUE) ## End(Not run)
All methods that are used by BasexClient and QueryClass
new()
Initialize a new socket
SocketClass$new(host, port = 1984L, username, password)
host, port, username, password
Host-information and credentials
finalize()
When releasing the session-object, close the socketConnection
SocketClass$finalize()
handShake()
Send input to the socket and return the response
SocketClass$handShake(input)
input
Input
Input is a raw vector, built up by converting all input to raw and concatenating the results
write_Byte()
Write 1 byte to the socket
SocketClass$write_Byte(Byte)
Byte
A vector length 1
clone()
The objects of this class are cloneable with this method.
SocketClass$clone(deep = FALSE)
deep
Whether to make a deep clone.
Stores a binary resource in the opened database.
Store(session, path, input)
Store(session, path, input)
session |
BasexClient instance-ID |
path |
Path where to store the data |
input |
Additional input, may be empty |
Use the database-command retrieve to retrieve the resource. The input can be a UTF-8 encoded XML document, a binary resource, or any other data (such as JSON or CSV) that can be successfully converted to a resource by the server. This method returns self invisibly, thus making it possible to chain together multiple method calls.
A list with two items
info Aditional info
success A boolean, indicating if the command was completed successfull
## Not run: Execute(Session, "DROP DB BinBase") testBin <- Execute(Session, "Check BinBase") bais <- raw() for (b in 252:255) bais <- c(bais, c(b)) %>% as.raw() test <- Store(Session, "test.bin", bais) print(test$success) baos <- Execute(Session, "retrieve test.bin") print(bais) print(baos$result) ## End(Not run)
## Not run: Execute(Session, "DROP DB BinBase") testBin <- Execute(Session, "Check BinBase") bais <- raw() for (b in 252:255) bais <- c(bais, c(b)) %>% as.raw() test <- Store(Session, "test.bin", bais) print(test$success) baos <- Execute(Session, "retrieve test.bin") print(bais) print(baos$result) ## End(Not run)
Check if the query contains updating expressions.
Updating(query_obj)
Updating(query_obj)
query_obj |
Query instance-ID |
Returns TRUE if the query contains updating expressions; FALSE otherwise.
This function returns a list with the following items:
result Result
success A boolean, indicating if the command was completed successfull