### Install Exasol R Package from GitHub Source: https://github.com/exasol/r-exasol/blob/main/README.md Installs the exasol R package directly from its GitHub repository using the devtools package. This command compiles and builds the package on your system, requiring necessary development tools and dependencies to be pre-installed. ```r devtools::install_github("EXASOL/r-exasol") ``` -------------------------------- ### Load Exasol R Package Source: https://github.com/exasol/r-exasol/blob/main/README.md Loads the installed exasol R package into the current R session, making its functions available for use. This is a standard R command to access package functionalities. ```r library(exasol) ``` -------------------------------- ### CMake Project Setup and Dependencies Source: https://github.com/exasol/r-exasol/blob/main/tests/cpp/CMakeLists.txt This snippet configures the basic CMake project, sets the C++ standard, includes directories, and fetches external dependencies like Catch2 for testing and OpenSSL for SSL support. It ensures these dependencies are available for the build. ```cmake cmake_minimum_required(VERSION 3.19) project(r_exasol) set(CMAKE_CXX_STANDARD 14) include_directories(../../src /usr/share/R/include/) include(FetchContent) include(CTest) find_package(OpenSSL REQUIRED) FetchContent_Declare( catch GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v2.13.6) # CMake 3.14+ FetchContent_MakeAvailable(catch) ``` -------------------------------- ### Manage Transactions in Exasol with R Source: https://context7.com/exasol/r-exasol/llms.txt This code demonstrates explicit transaction management in Exasol using R. It covers starting a transaction, performing multiple write operations, committing the transaction, and handling potential errors with a rollback mechanism. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", autocommit = "N") # Begin transaction dbBegin(con) # Perform multiple operations dbWriteTable(con, "test.temp1", data.frame(id = 1:10, value = rnorm(10))) dbWriteTable(con, "test.temp2", data.frame(id = 1:10, label = letters[1:10])) # Commit transaction dbCommit(con) # Transaction with rollback on error dbBegin(con) tryCatch({ dbWriteTable(con, "test.data1", data.frame(x = 1:5)) dbWriteTable(con, "test.data2", data.frame(y = 6:10)) # Simulate error condition if (some_condition) { stop("Business logic error") } dbCommit(con) }, error = function(e) { print(paste("Error occurred:", e$message)) dbRollback(con) }) dbDisconnect(con) ``` -------------------------------- ### Establish r-exasol Database Connection Source: https://github.com/exasol/r-exasol/blob/main/doc/developer_guide/developer_guide.md This function initiates a connection to the Exasol database using RODBC. It is a core part of the r-exasol library's setup process. The function `EXANewConnection` internally calls `odbcDriverConnect` to establish the connection. ```r EXANewConnection <- function (...) { ... odbcDriverConnect(...) } ``` -------------------------------- ### Read and Write Data with r-exasol Source: https://github.com/exasol/r-exasol/blob/main/doc/developer_guide/developer_guide.md These are the primary functions for data exchange between R and Exasol DB. `exa.readData()` is used for retrieving data, and `exa.writeData()` is for sending data to the database. Standard SQL functions like `dbGetQuery()` may internally utilize these methods for optimized data transfer. ```r exa.readData() exa.writeData() ``` -------------------------------- ### Access Package Documentation Source: https://github.com/exasol/r-exasol/blob/main/README.md Provides instructions on how to access the documentation for the exasol R package and its individual functions directly within R. This is essential for understanding available commands and their parameters. ```r # display package documentation with examples for each method ?exasol # display documentation of individual commands with Exasol-specific parameters ?dbConnect ``` -------------------------------- ### Connect to Exasol Database Source: https://github.com/exasol/r-exasol/blob/main/README.md Demonstrates how to establish a connection to an Exasol database using the exasol R package. It shows two common methods: connecting via an ODBC DSN and connecting directly using host, port, and credentials with encryption enabled. ```r # connect to Exasol DB with an ODBC DSN con <- dbConnect("exa", dsn="ExaSolo", schema="test") # OR connect to Exasol DB running on default port (8563) with a hostname, default 'sys' user and default schema ('SYS'), using an encryption channel con <- dbConnect("exa", exahost = ":8563", uid = "sys", pwd = "", encryption = "Y") ``` -------------------------------- ### Query Exasol Database and Fetch Subset of Results Source: https://github.com/exasol/r-exasol/blob/main/README.md Sends a SQL query to the Exasol database and returns a result set handler. It then demonstrates fetching a specified number of rows (e.g., 2) from this result set into an R data.frame, allowing for more controlled data retrieval. ```r #send a query and return a result set handler, then fetch 2 rows res <- dbSendQuery(con, "SELECT * FROM test.mytab") df <- dbFetch(res, 2) ``` -------------------------------- ### Create and Execute R UDF Scripts in Exasol Source: https://context7.com/exasol/r-exasol/llms.txt This snippet shows how to deploy R functions as User Defined Functions (UDFs) in Exasol. It covers creating both SET-EMITS and SCALAR-RETURNS UDFs, generating test data, and executing these UDFs within the database. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Create schema and test data odbcQuery(con, "CREATE SCHEMA test") odbcQuery(con, "CREATE TABLE test.twogroups (groupid INT, val DOUBLE)") # Generate test data valsMean0 <- rnorm(10, 0) valsMean50 <- rnorm(10, 50) twogroups <- data.frame( group = rep(1:2, each = 10), value = c(valsMean0, valsMean50) ) exa.writeData(con, twogroups, tableName = "test.twogroups") # Create UDF script that computes mean for each group mymean_script <- exa.createScript( con, "test.mymean", function(data) { data$next_row(NA) # Read all values from this group data$emit(data$groupid[[1]], mean(data$val)) }, inType = "SET", outType = "EMITS", inArgs = c("groupid INT", "val DOUBLE"), outArgs = c("groupid INT", "mean DOUBLE") ) # Execute the UDF script with grouping results <- mymean_script("groupid", "val", table = "test.twogroups", groupBy = "groupid") print(results) # Create SCALAR-RETURNS UDF (processes row by row) double_value <- exa.createScript( con, "test.doubler", function(data) { return(data$val * 2) }, inType = "SCALAR", outType = "RETURNS", inArgs = c("val DOUBLE"), outArgs = c("DOUBLE") ) # Execute scalar UDF doubled <- double_value("val", table = "test.twogroups") print(doubled) dbDisconnect(con) ``` -------------------------------- ### Connect to Exasol via Hostname Source: https://context7.com/exasol/r-exasol/llms.txt Establishes a connection to an Exasol database using hostname and credentials. It supports IP ranges, custom ports, SSL certificates, and encryption settings. Requires the 'exasol' library. ```r library(exasol) # Connect with hostname, encryption enabled (default), and custom schema con <- dbConnect( "exa", exahost = "192.168.1.10..15:8563", # IP range with port uid = "sys", pwd = "exasol_password", schema = "sales", encryption = "Y", autocommit = "Y", querytimeout = "0" ) # Connection metadata is available cat("Connected to:", con@db_host, "port:", con@db_port, "\n") cat("User:", con@db_user, "\n") cat("Encrypted:", con@encrypted, "\n") dbDisconnect(con) ``` -------------------------------- ### Query Exasol Database and Fetch Results Source: https://github.com/exasol/r-exasol/blob/main/README.md Executes a SQL query against the Exasol database and fetches the entire result set into an R data.frame. This is a straightforward way to retrieve query results. ```r # send a query and read the result into a data.frame df <- dbGetQuery(con, "SELECT * FROM test.mytab") ``` -------------------------------- ### Platform-Specific Linker and Compiler Options Source: https://github.com/exasol/r-exasol/blob/main/tests/cpp/CMakeLists.txt This CMake snippet provides platform-specific compile and link options for the 'r_exasol_tests' target. It includes directives for Windows (using wsock32 and ws2_32) and other systems (enabling address sanitizer, setting warning levels, and disabling frame pointer omission). ```cmake if (WIN32) target_compile_options (r_exasol_tests PRIVATE -Wa,-mbig-obj) target_link_libraries(r_exasol_tests PRIVATE Catch2::Catch2 wsock32 ws2_32 OpenSSL::SSL) else() target_compile_options (r_exasol_tests PRIVATE -fno-omit-frame-pointer -fsanitize=address -Wall -Wextra -pedantic -Wno-deprecated) target_link_options (r_exasol_tests PRIVATE -fno-omit-frame-pointer -fsanitize=address) target_link_libraries(r_exasol_tests PRIVATE Catch2::Catch2 OpenSSL::SSL) endif() ``` -------------------------------- ### List Tables in Exasol Source: https://github.com/exasol/r-exasol/blob/main/README.md Retrieves a list of all available tables within the connected Exasol database. The function returns the table names as a character vector. ```r # list all tables in Exasol (returns a character vector). dbListTables(con) ``` -------------------------------- ### Send Query with Result Handler for Incremental Fetching Source: https://context7.com/exasol/r-exasol/llms.txt Sends a SQL query to Exasol and returns a result set handler for incremental fetching. This allows for chunked data retrieval, which is efficient for large result sets. Requires the 'exasol' library. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Send query and get result handler res <- dbSendQuery(con, "SELECT * FROM sales.transactions") # Fetch data in chunks chunk1 <- dbFetch(res, n = 100) # First 100 rows print(nrow(chunk1)) chunk2 <- dbFetch(res, n = 100) # Next 100 rows print(nrow(chunk2)) # Fetch all remaining rows remaining <- dbFetch(res, n = -1) print(nrow(remaining)) # Clean up result set dbClearResult(res) dbDisconnect(con) ``` -------------------------------- ### List Exasol Database Tables Source: https://context7.com/exasol/r-exasol/llms.txt Retrieves all tables from the Exasol database, optionally filtered by schema, returning fully qualified table names. Requires the 'exasol' library and an active database connection. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", schema = "SYS") # List all tables in the database all_tables <- dbListTables(con) print(head(all_tables, 10)) # List tables in a specific schema schema_tables <- dbListTables(con, schema = "sales") print(schema_tables) dbDisconnect(con) ``` -------------------------------- ### Connect to Exasol via DSN Source: https://context7.com/exasol/r-exasol/llms.txt Establishes a connection to an Exasol database using a preconfigured ODBC Data Source Name (DSN). It supports encrypted access and automatic schema selection. Requires the 'exasol' library. ```r library(exasol) # Connect using a configured DSN with default encryption con <- dbConnect("exa", dsn = "ExaSolo", schema = "test") # Verify connection print(con@db_version) print(con@current_schema) # Disconnect when done dbDisconnect(con) ``` -------------------------------- ### Execute SQL Query and Fetch Results Source: https://context7.com/exasol/r-exasol/llms.txt Sends a SQL query to the Exasol database and retrieves the entire result set as an R data frame. It utilizes high-speed parallel transfer for efficient data retrieval. Requires the 'exasol' library. ```r library(exasol) con <- dbConnect("exa", exahost = "192.168.1.10:8563", uid = "sys", pwd = "exasol") # Execute query and get all results customers <- dbGetQuery(con, "SELECT * FROM sales.customers WHERE country = 'USA'") # Work with the data frame print(nrow(customers)) print(colnames(customers)) print(head(customers, 5)) # Complex query with joins query <- " SELECT c.customer_id, c.name, SUM(o.amount) as total FROM sales.customers c JOIN sales.orders o ON c.customer_id = o.customer_id WHERE o.order_date >= '2023-01-01' GROUP BY c.customer_id, c.name ORDER BY total DESC " results <- dbGetQuery(con, query) print(results) dbDisconnect(con) ``` -------------------------------- ### High-Speed Data Read with EXPORT TO CSV Source: https://context7.com/exasol/r-exasol/llms.txt Executes a SQL query and reads results using Exasol's optimized parallel transfer channel (EXPORT TO CSV). This method is significantly faster than standard ODBC transfers. Requires the 'exasol' library. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Read data with high-speed transfer tables <- exa.readData(con, "SELECT * FROM EXA_ALL_TABLES") # Work with results print(nrow(tables)) print(colnames(tables)) print(tables[1,]) print(tables$TABLE_NAME[1]) # Complex query with custom encoding sales_data <- exa.readData( con, "SELECT product_id, SUM(quantity) as total_qty, SUM(revenue) as total_rev FROM sales.fact_sales WHERE sale_date BETWEEN '2023-01-01' AND '2023-12-31' GROUP BY product_id HAVING SUM(revenue) > 1000 ORDER BY total_rev DESC", encoding = "UTF-8" ) print(head(sales_data, 20)) dbDisconnect(con) ``` -------------------------------- ### Read Table API Source: https://context7.com/exasol/r-exasol/llms.txt Reads an entire database table or a subset with optional ordering and row limits, returning results as a data frame. ```APIDOC ## Read Table ### Description Reads an entire database table or a subset with optional ordering and row limits, returning results as a data frame. ### Method `dbReadTable(con, name, schema = NULL, order_col = NULL, limit = NULL)` ### Parameters #### Path Parameters * `con` (connection object) - An active Exasol database connection. * `name` (string) - The name of the table. * `schema` (string, optional) - The schema name if not included in `name`. * `order_col` (string, optional) - Column(s) to order the results by (e.g., "column1 DESC, column2 ASC"). * `limit` (integer, optional) - Maximum number of rows to return. ### Request Example ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", schema = "sales") # Read entire table customers <- dbReadTable(con, "sales.customers") print(nrow(customers)) # Read with ordering ordered_data <- dbReadTable( con, "customers", schema = "sales", order_col = "customer_id DESC", limit = 100 ) print(head(ordered_data, 10)) # Read without specifying schema (uses fully qualified name) products <- dbReadTable(con, "sales.products", limit = 50) print(products) dbDisconnect(con) ``` ``` -------------------------------- ### Write Table API Source: https://context7.com/exasol/r-exasol/llms.txt Writes a data frame to a database table, automatically creating the table if it doesn't exist with proper schema and column types. ```APIDOC ## Write Table ### Description Writes a data frame to a database table, automatically creating the table if it doesn't exist with proper schema and column types. ### Method `dbWriteTable(con, name, value, overwrite = FALSE, append = FALSE, field_types = NULL, writeCols = NULL)` ### Parameters #### Path Parameters * `con` (connection object) - An active Exasol database connection. * `name` (string) - The name of the target table, optionally including the schema (e.g., "schema.table"). * `value` (data.frame) - The data frame to be written. * `overwrite` (boolean, optional) - If TRUE, existing table will be dropped and recreated. Defaults to FALSE. * `append` (boolean, optional) - If TRUE, data will be appended to the existing table. Defaults to FALSE. * `field_types` (character vector, optional) - A vector of SQL data types to use for table creation. * `writeCols` (character vector, optional) - A vector of column names to write to the table. ### Request Example ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Create data frame employee_data <- data.frame( employee_id = 1:5, name = c("Alice", "Bob", "Charlie", "Diana", "Eve"), salary = c(75000, 82000, 68000, 91000, 77000), hire_date = as.Date(c("2020-01-15", "2019-06-22", "2021-03-10", "2018-11-05", "2020-09-18")), stringsAsFactors = FALSE ) # Write to new table (creates schema and table if needed) dbWriteTable(con, "hr.employees", employee_data) # Append data to existing table more_employees <- data.frame( employee_id = 6:8, name = c("Frank", "Grace", "Henry"), salary = c(79000, 85000, 72000), hire_date = as.Date(c("2022-02-14", "2021-07-30", "2023-01-09")) ) dbWriteTable(con, "hr.employees", more_employees, overwrite = FALSE, append = TRUE) # Overwrite existing table dbWriteTable(con, "hr.employees", employee_data, overwrite = TRUE) # Write with specific column types field_types <- c("INT", "VARCHAR(100)", "DECIMAL(10,2)", "DATE") dbWriteTable(con, "hr.staff", employee_data, field_types = field_types) # Write to specific columns partial_data <- data.frame( employee_id = 9:10, name = c("Ivy", "Jack") ) dbWriteTable(con, "hr.employees", partial_data, writeCols = c("employee_id", "name")) dbDisconnect(con) ``` ``` -------------------------------- ### Check Table Existence API Source: https://context7.com/exasol/r-exasol/llms.txt Checks whether a table exists in the database, returning a boolean value for conditional logic. ```APIDOC ## Check Table Existence ### Description Checks whether a table exists in the database, returning a boolean value for conditional logic. ### Method `dbExistsTable(con, name, schema = NULL)` ### Parameters #### Path Parameters * `con` (connection object) - An active Exasol database connection. * `name` (string) - The name of the table. * `schema` (string, optional) - The schema name if not included in `name`. ### Request Example ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", schema = "sales") # Check if table exists if (dbExistsTable(con, "sales.customers")) { print("Table exists, reading data...") data <- dbReadTable(con, "sales.customers") } else { print("Table does not exist, creating it...") # Create table logic here } # Check with schema parameter exists <- dbExistsTable(con, "orders", schema = "sales") print(paste("sales.orders exists:", exists)) # Check non-existent table exists_test <- dbExistsTable(con, "sales.nonexistent_table") print(paste("sales.nonexistent_table exists:", exists_test)) dbDisconnect(con) ``` ``` -------------------------------- ### Define r_exasol_tests Executable Target Source: https://github.com/exasol/r-exasol/blob/main/tests/cpp/CMakeLists.txt This CMake snippet defines the 'r_exasol_tests' executable target, listing all the source files required for the tests. It includes various C++ source and header files from the project's 'src' directory, as well as test-specific files and mock implementations. ```cmake add_executable(r_exasol_tests ../../src/r_exasol/connection/protocol/http/reader/http_chunk_reader.cpp ../../src/r_exasol/connection/protocol/http/reader/http_chunk_reader.h ../../src/r_exasol/connection/protocol/http/writer/http_chunk_writer.cpp ../../src/r_exasol/connection/protocol/http/writer/http_chunk_writer.h ../../src/r_exasol/connection/protocol/http/conn/http_connection_establisher.h ../../src/r_exasol/connection/protocol/http/conn/http_connection_establisher.cpp ../../src/r_exasol/connection/protocol/https/conn/https_connection_establisher.h ../../src/r_exasol/connection/protocol/https/conn/https_connection_establisher.cpp ../../src/r_exasol/connection/protocol/common.h ../../src/r_exasol/connection/protocol/common.cpp ../../src/r_exasol/connection/protocol/chunk.h ../../src/r_exasol/connection/protocol/meta_info_reader.h ../../src/r_exasol/connection/protocol/meta_info_reader.cpp ../../src/r_exasol/connection/connection_controller.cpp ../../src/r_exasol/connection/connection_factory_impl.h ../../src/r_exasol/connection/connection_factory_impl.cpp ../../src/r_exasol/connection/connection_controller.h ../../src/r_exasol/connection/connection_establisher.h ../../src/r_exasol/connection/connection_info.h ../../src/r_exasol/connection/error_handler.h ../../src/r_exasol/connection/async_executor/async_executor.h ../../src/r_exasol/connection/async_executor/async_executor_session_info.h ../../src/r_exasol/connection/reader.h ../../src/r_exasol/connection/writer.h ../../src/r_exasol/connection/connection_factory.h ../../src/r_exasol/connection/socket/socket.h ../../src/r_exasol/connection/socket/socket_impl.h ../../src/r_exasol/connection/socket/socket_impl.cpp ../../src/r_exasol/connection/socket/ssl_socket_impl.h ../../src/r_exasol/connection/socket/ssl_socket_impl.cpp ../../src/r_exasol/ssl/certificate.cpp ../../src/r_exasol/debug_print/debug_printer.cpp ../../src/r_exasol/debug_print/debug_printer.h #../../src/exasol.c main.cpp algo.cpp http_tests.cpp connection_tests.cpp async_tests.cpp test_utils.h mocks/AsyncExecutorMock.cpp mocks/AsyncExecutorMock.h mocks/AsyncSessionMock.cpp mocks/AsyncSessionMock.h mocks/CustomAsyncExecutorMock.cpp mocks/CustomAsyncExecutorMock.h mocks/CustomAsyncSessionMock.cpp mocks/CustomAsyncSessionMock.h) ``` -------------------------------- ### Read Table Data from Exasol Source: https://context7.com/exasol/r-exasol/llms.txt Reads an entire database table or a subset with optional ordering and row limits, returning results as a data frame. Supports reading with or without specifying the schema. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", schema = "sales") # Read entire table customers <- dbReadTable(con, "sales.customers") print(nrow(customers)) # Read with ordering ordered_data <- dbReadTable( con, "customers", schema = "sales", order_col = "customer_id DESC", limit = 100 ) print(head(ordered_data, 10)) # Read without specifying schema (uses fully qualified name) products <- dbReadTable(con, "sales.products", limit = 50) print(products) dbDisconnect(con) ``` -------------------------------- ### High-Speed Data Write Source: https://context7.com/exasol/r-exasol/llms.txt Writes a data frame to an Exasol table using an optimized parallel transfer channel for efficient streaming. ```APIDOC ## High-Speed Data Write ### Description Writes a data frame to an Exasol table using the optimized parallel transfer channel (IMPORT FROM CSV), streaming data efficiently to the database. ### Method `exa.writeData(con, data, tableName, encoding = "UTF-8")` ### Parameters #### Path Parameters * `con` (connection object) - An active Exasol database connection. * `data` (data.frame) - The data frame to be written. * `tableName` (string) - The name of the target table, optionally including the schema (e.g., "schema.table"). * `encoding` (string, optional) - The character encoding to use for the data transfer. Defaults to "UTF-8". ### Request Example ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Create schema and table odbcQuery(con, "CREATE SCHEMA test") odbcQuery(con, "CREATE TABLE test.sales_data ( product_id INT, quantity DOUBLE, revenue DECIMAL(18,2), sale_date DATE )") # Generate example data sales_df <- data.frame( product_id = sample(1:100, 1000, replace = TRUE), quantity = rnorm(1000, mean = 10, sd = 3), revenue = runif(1000, min = 50, max = 500), sale_date = sample(seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "day"), 1000, replace = TRUE) ) # Write data with high-speed transfer exa.writeData(con, sales_df, tableName = "test.sales_data") # Write with custom encoding exa.writeData(con, sales_df, "test.sales_data", encoding = "UTF-8") # Verify data was written count <- dbGetQuery(con, "SELECT COUNT(*) FROM test.sales_data") print(count) dbDisconnect(con) ``` ``` -------------------------------- ### List Table Fields API Source: https://context7.com/exasol/r-exasol/llms.txt Retrieves column names from a database table or result set, returning them in ordinal position order. ```APIDOC ## List Table Fields ### Description Retrieves column names from a database table or result set, returning them in ordinal position order. ### Method `dbListFields(con, name, schema = NULL)` ### Parameters #### Path Parameters * `con` (connection object or result object) - An active Exasol database connection or a result object from `dbSendQuery`. * `name` (string, optional) - The name of the table if `con` is a connection object. Not used if `con` is a result object. * `schema` (string, optional) - The schema name if `con` is a connection object and the schema is not included in `name`. ### Request Example ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # List fields from a table columns <- dbListFields(con, "sales.customers") print(columns) # List fields with schema specified separately columns2 <- dbListFields(con, "orders", schema = "sales") print(columns2) # List fields from a query result res <- dbSendQuery(con, "SELECT customer_id, name, country FROM sales.customers") result_columns <- dbListFields(res) print(result_columns) dbClearResult(res) dbDisconnect(con) ``` ``` -------------------------------- ### Define and Add Test Execution Command Source: https://github.com/exasol/r-exasol/blob/main/tests/cpp/CMakeLists.txt This CMake snippet defines the command to execute the project's tests using a Python script. It sets the test executable to 'python3' and specifies the path to the 'python_tests.py' script, then adds this as a named test 'r_exasol_tests' to be run by CTest. ```cmake set(TEST_EXEC "python3" "${CMAKE_SOURCE_DIR}/python/python_tests.py") add_test(NAME r_exasol_tests COMMAND ${TEST_EXEC}) ``` -------------------------------- ### Change Current Schema in Exasol using R Source: https://context7.com/exasol/r-exasol/llms.txt This snippet illustrates how to change the active database schema for an R connection to Exasol. It demonstrates initializing a connection to a specific schema, checking the current schema, switching to a different schema, and querying tables from the new active schema. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", schema = "SYS") # Check current schema print(con@current_schema) # Change to different schema con <- dbCurrentSchema(con, setSchema = "sales") print(con@current_schema) # Query current schema from database con <- dbCurrentSchema(con) # Now queries without schema qualification use the current schema tables <- dbListTables(con) # Lists tables in 'sales' schema print(tables) dbDisconnect(con) ``` -------------------------------- ### Write Data Frame to Exasol Table Source: https://context7.com/exasol/r-exasol/llms.txt Writes a data frame to a database table, automatically creating the table if it doesn't exist with proper schema and column types. Supports appending, overwriting, specifying column types, and writing to specific columns. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Create data frame employee_data <- data.frame( employee_id = 1:5, name = c("Alice", "Bob", "Charlie", "Diana", "Eve"), salary = c(75000, 82000, 68000, 91000, 77000), hire_date = as.Date(c("2020-01-15", "2019-06-22", "2021-03-10", "2018-11-05", "2020-09-18")), stringsAsFactors = FALSE ) # Write to new table (creates schema and table if needed) dbWriteTable(con, "hr.employees", employee_data) # Append data to existing table more_employees <- data.frame( employee_id = 6:8, name = c("Frank", "Grace", "Henry"), salary = c(79000, 85000, 72000), hire_date = as.Date(c("2022-02-14", "2021-07-30", "2023-01-09")) ) dbWriteTable(con, "hr.employees", more_employees, overwrite = FALSE) # Overwrite existing table dbWriteTable(con, "hr.employees", employee_data, overwrite = TRUE) # Write with specific column types field_types <- c("INT", "VARCHAR(100)", "DECIMAL(10,2)", "DATE") dbWriteTable(con, "hr.staff", employee_data, field_types = field_types) # Write to specific columns partial_data <- data.frame( employee_id = 9:10, name = c("Ivy", "Jack") ) dbWriteTable(con, "hr.employees", partial_data, writeCols = c("employee_id", "name")) dbDisconnect(con) ``` -------------------------------- ### Remove Table Conditionally with R Source: https://context7.com/exasol/r-exasol/llms.txt Demonstrates how to conditionally check for and remove a table in Exasol using R. It first checks if 'test.old_data' exists and, if so, removes it. Finally, it disconnects from the database. ```r if (dbExistsTable(con, "test.old_data")) { dbRemoveTable(con, "test.old_data") print("Table removed successfully") } dbDisconnect(con) ``` -------------------------------- ### High-Speed Data Write to Exasol Source: https://context7.com/exasol/r-exasol/llms.txt Writes a data frame to an Exasol table using the optimized parallel transfer channel (IMPORT FROM CSV), streaming data efficiently to the database. This method is suitable for large datasets and offers control over encoding. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Create schema and table odbcQuery(con, "CREATE SCHEMA test") odbcQuery(con, "CREATE TABLE test.sales_data ( product_id INT, quantity DOUBLE, revenue DECIMAL(18,2), sale_date DATE )") # Generate example data sales_df <- data.frame( product_id = sample(1:100, 1000, replace = TRUE), quantity = rnorm(1000, mean = 10, sd = 3), revenue = runif(1000, min = 50, max = 500), sale_date = sample(seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "day"), 1000, replace = TRUE) ) # Write data with high-speed transfer exa.writeData(con, sales_df, tableName = "test.sales_data") # Write with custom encoding exa.writeData(con, sales_df, "test.sales_data", encoding = "UTF-8") # Verify data was written count <- dbGetQuery(con, "SELECT COUNT(*) FROM test.sales_data") print(count) dbDisconnect(con) ``` -------------------------------- ### Remove Table API Source: https://context7.com/exasol/r-exasol/llms.txt Removes a table from the database with optional cascade deletion of foreign key constraints. ```APIDOC ## Remove Table ### Description Removes a table from the database with optional cascade deletion of foreign key constraints. ### Method `dbRemoveTable(con, name, schema = NULL, cascade = FALSE)` ### Parameters #### Path Parameters * `con` (connection object) - An active Exasol database connection. * `name` (string) - The name of the table to remove. * `schema` (string, optional) - The schema name if not included in `name`. * `cascade` (boolean, optional) - If TRUE, referencing foreign key constraints will also be deleted. Defaults to FALSE. ### Request Example ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Remove a table dbRemoveTable(con, "test.temp_data") # Remove table with cascade (deletes referencing foreign keys) dbRemoveTable(con, "test.parent_table", cascade = TRUE) # Remove table with schema specified separately dbRemoveTable(con, "temp_results", schema = "test") dbDisconnect(con) ``` ``` -------------------------------- ### List Table Fields from Exasol Source: https://context7.com/exasol/r-exasol/llms.txt Retrieves column names from a database table or result set, returning them in ordinal position order. This function can be used with table names or directly with a query result object. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # List fields from a table columns <- dbListFields(con, "sales.customers") print(columns) # List fields with schema specified separately columns2 <- dbListFields(con, "orders", schema = "sales") print(columns2) # List fields from a query result res <- dbSendQuery(con, "SELECT customer_id, name, country FROM sales.customers") result_columns <- dbListFields(res) print(result_columns) dbClearResult(res) dbDisconnect(con) ``` -------------------------------- ### Check Table Existence in Exasol Source: https://context7.com/exasol/r-exasol/llms.txt Checks whether a table exists in the database, returning a boolean value for conditional logic. This function can check for tables with or without specifying the schema. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo", schema = "sales") # Check if table exists if (dbExistsTable(con, "sales.customers")) { print("Table exists, reading data...") data <- dbReadTable(con, "sales.customers") } else { print("Table does not exist, creating it...") # Create table logic here } # Check with schema parameter exists <- dbExistsTable(con, "orders", schema = "sales") print(paste("sales.orders exists:", exists)) # Check non-existent table exists_test <- dbExistsTable(con, "sales.nonexistent_table") print(paste("sales.nonexistent_table exists:", exists_test)) dbDisconnect(con) ``` -------------------------------- ### Disconnect from Exasol Database Source: https://github.com/exasol/r-exasol/blob/main/README.md Closes the active connection to the Exasol database. This is an important step to release database resources and ensure proper session termination. ```r # disconnect dbDisconnect(con) ``` -------------------------------- ### Remove Table from Exasol Source: https://context7.com/exasol/r-exasol/llms.txt Removes a table from the database with optional cascade deletion of foreign key constraints. This function can also be used to remove tables where the schema is specified separately. ```r library(exasol) con <- dbConnect("exa", dsn = "ExaSolo") # Remove a table dbRemoveTable(con, "test.temp_data") # Remove table with cascade (deletes referencing foreign keys) dbRemoveTable(con, "test.parent_table", cascade = TRUE) # Remove table with schema specified separately dbRemoveTable(con, "temp_results", schema = "test") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.