Teradata Package for R Function Reference | 17.00 - 17.00 - td_set_context - Teradata Package for R

Teradata® Package for R Function Reference

Product
Teradata Package for R
Release Number
17.00
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B700-4007-090K
Language
English (United States)

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

    1. the connection object "con" is created using tdplyr::NativeDriver() (as in Example 2), 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 (because dbConnect on NativeDriver object initializes the context), the previous context (that is created by dbConnect) is NOT overwritten and all nonpersistent work tables remain in the session.

    2. the connection object "con" is created using teradatasql::TeradataDriver() (as in Example 3) object (For more information on this, please check documentation from Teradata SQL Driver for R), then

      1. the 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.

      2. 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
Specifies a DBIConnection object with a Teradata or a TeradataConnection subclass.

temp.database

Optional Argument
Specifies the database name which needs to be used to create temporary database objects (table/view) for processing data. Such objects are garbage collected at the end of the session. If a user wants to specifically use a known database for temporary objects, the database name can be specified using this argument.
Note:

  1. Make sure the user has privilege to create objects in this database, otherwise the functions that create temporary objects will fail.

  2. If this argument is not specified, default database of the connection is used for temporary objects.

  3. To get the temporary database name, use function td_get_context().

Default: NULL
Types: character

Value

Returns NULL invisibly

See Also

td_remove_context, td_create_context, td_get_context, dbConnect

Examples

# Example 1: Create a new context and switch its temporary database using
#            td_set_context() function without removing nonpersistent work
#            tables.
con <- td_create_context(host = "<dbcname>", uid = "<tduser>",
                         pwd = "<tdpwd>", dType = "native")

# Get the current context information.
td_get_context()

# Change temporary database.
td_set_context(con, temp.database = "database_for_transient_objects")

# Get the current and changed context information.
td_get_context()

# Remove the current context.
td_remove_context()

# Example 2: Use dbConnect() function to connect to Advanced SQL Engine
#            through tdplyr NativeDriver() and set context using different
#            temporary database to change "temp.database" of an existing
#            context without removing nonpersistent work tables.
con <- DBI::dbConnect(tdplyr::NativeDriver(), "<dbcname>", "<tduser>",
                      "<tdpwd>")

# Set context using different temporary database.
td_set_context(con, temp.database = "database_for_transient_objects")

# Remove the current context.
td_remove_context()

# Example 3: 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>"}')

# Set context using different temporary database.
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 as follows.
con <- td_get_context()$connection

# Remove the current context.
td_remove_context()

# Example 4: Managing multiple connections with td_set_context() function.
con1 <- DBI::dbConnect(teradatasql::TeradataDriver(),
                       '{"host":"<dbcname>","user":"<tduser>","password":"<tdpwd>"}')
con2 <- DBI::dbConnect(teradatasql::TeradataDriver(),
                       '{"host":"<dbcname>","user":"<tduser>","password":"<tdpwd>"}')

# Initialize the context with 'con1'.
td_set_context(con = con1, temp.database = "database_for_transient_objects")

# Get the current context information.
td_get_context()

# Initialize the context with 'con2'. This overwrites current context and
# 'con1' is disconnected.
td_set_context(con = con2)

# Get the current context information.
td_get_context()

# Remove the current context.
# Note: This performs garbage collection and disconnects current context
#       connection i.e., 'con2'.
td_remove_context()