Description
A context must be initialized before executing analytic functions. Use the td_set_context
function to initialize or update a context using an already established connection.
Note:
If there is no previous context, then a new context is initialized.
If the context already exists, and
the connection object "con" is created using
tdplyr::NativeDriver()
(as in Example 4), then the context attribute "temp.database" is updated if it is different compared to that of previous context. Since the connection parameters host, user/uid and database (i.e. default schema) are same for both "con" and the connection object from the context (becausedbConnect
onNativeDriver
object initializes the context), the previous context (that is created bydbConnect
) is NOT overwritten and all nonpersistent work tables remain in the session.the connection object "con" is created using
teradatasql::TeradataDriver()
(as in Example 5) object (For more information on this, please check documentation from TeradataSQLDriver for R), thenthe previous context is overwritten and all nonpersistent work tables are removed if the connection parameters of "con" are different than those of the connection object from the previous context.
the previous context is NOT overwritten and all nonpersistent work tables remain in the session if the connection parameters are same for both "con" and the connection object from the previous context.
In this case, the new connection object should be retrieved using
td_get_context()$connection
before using it.
Usage
td_set_context(con, temp.database = NULL)
Arguments
con |
Required Argument |
temp.database |
Optional Argument
Default: NULL |
Value
Returns NULL invisibly
See Also
td_remove_context
, td_create_context
, td_get_context
,
dbConnect
Examples
# Establish an ODBC connection to Teradata Vantage using the dbConnect API. con <- DBI::dbConnect(odbc::odbc(), dsn = "TeradataDSN") # Example 1: Changing the temp.database of an existing context without removing # nonpersistent work tables. # Use td_set_context function to initialize a context with a given connection. td_set_context(con = con, temp.database = "database_for_transient_objects") # Use td_get_context function to get the current context information. context <- td_get_context() # Change the temp.database of an existing context. td_set_context(context$connection, temp.database = context$default.database) # Remove the current context. td_remove_context() # Example 2: Managing multiple connections with td_set_context function. con1 <- DBI::dbConnect(odbc::odbc(), dsn = "TeradataDSN") con2 <- DBI::dbConnect(odbc::odbc(), dsn = "TeradataDSN") # Initialize the context with con1. td_set_context(con = con1, temp.database = "database_for_transient_objects") # Initialize the context with con2. td_set_context(con = con2) # Remove the current context. # Note: This performs garbage collection and disconnects current context connection i.e., con2. td_remove_context() # Explicit call to dbDisconnect() disconnects the connection "con1". DBI::dbDisconnect(con1) # Example 3: Create a new ODBC context and switch its temporary database using # td_set_context function. con <- td_create_context(dsn = "TeradataDSN") # Use td_get_context function to get the current context information. td_get_context() # Use td_set_context function to change temporary database. td_set_context(con, temp.database = "database_for_transient_objects") # Use td_get_context function to get the current context information. td_get_context() # Remove the current context. td_remove_context() # Example 4: Use dbConnect function to connect to Advanced SQL Engine through Teradata SQL Driver # and set context using different temporary database. db <- DBI::dbConnect(tdplyr::NativeDriver(), "<dbcname>", "<tduser>", "<tdpwd>") # Use td_set_context function to set context using different temporary database. td_set_context(db, temp.database = "database_for_transient_objects") # Remove the current context. td_remove_context() # Example 5: Use dbConnect function to connect to Advanced SQL Engine directly through Teradata # SQL Driver (i.e. TeradataDriver()). con <- DBI::dbConnect (teradatasql::TeradataDriver(), '{"host":"<dbcname>","user":"<tduser>","password":"<tdpwd>"}') td_set_context(con, temp.database = "database_for_transient_objects") # The td_set_context function just sets the context and does not return the connection object. # Hence, get the connection object using td_get_context()$connection. con <- td_get_context()$connection # Remove the current context. td_remove_context()