Decode keys in a database table
decode_fks.RdWhen querying a table from a database, it is helpful to decode the primary and foreign keys in the data frame, i.e. to translate from keys to values. This function provides that service.
Usage
decode_fks(
.df = NULL,
db_table_name,
collect = FALSE,
conn = NULL,
schema = schema_from_conn(conn),
fk_parent_tables = get_all_fk_tables(conn = conn, schema = schema),
.child_table = PFUPipelineTools::dm_fk_colnames$child_table,
.child_fk_cols = PFUPipelineTools::dm_fk_colnames$child_fk_cols,
.parent_key_cols = PFUPipelineTools::dm_fk_colnames$parent_key_cols,
.pk_suffix = PFUPipelineTools::key_col_info$pk_suffix,
.y_joining_suffix = ".y"
)Arguments
- .df
A data frame whose foreign keys are to be decoded. Default is
NULL, meaning that.dfshould be downloaded fromdb_table_nameatconnbefore decoding foreign keys.- db_table_name
The string name of the database table where
.dfis to be uploaded.- collect
A boolean that tells whether to download the decoded table (returning an in-memory data frame produced by calling
dplyr::collect()) or atbl(a reference to a database query to be executed bydplyr::collect()). Default isFALSE. Applies only when.dfisNULL. Note that to prevent downloads fromconn, supply values forschemaandfk_parent_tables, whose default values will download both admobject and the foreign key parent tables from the database atconn.- conn
An optional database connection. Necessary only for the default values of
.df,schema, andfk_parent_tables. Also necessary ifcollect = TRUE. Default isNULL.- schema
The data model (
dmobject) for the database inconn. See details.- fk_parent_tables
A named list of all parent tables for the foreign keys in
db_table_name. See details.- .child_table, .child_fk_cols, .parent_key_cols
- .pk_suffix
- .y_joining_suffix
The y column name suffix for
left_join(). Default is ".y".
Details
schema is a data model (dm object) for the CL-PFU database.
It can be obtained from calling schema_from_conn().
The default is schema_from_conn(conn = conn),
which downloads the dm object from conn.
To save time, pre-compute the dm object and
supply in the schema argument.
fk_parent_tables is a named list of tables,
some of which are foreign key (fk) parent tables for db_table_name
containing the mapping between fk values (usually strings)
and fk keys (usually integers).
fk_parent_tables is treated as a store from which foreign key tables
are retrieved by name when needed.
An appropriate value for fk_parent_tables can be obtained
from get_all_fk_tables().
There are two three ways to use this function.
Offline:
supply a value for .df and values for schema and fk_parent_tables.
In offline mode, conn is not needed and
no connection to a database at conn will be made.
Download:
accept the default value for .df (NULL)
and supply a conn.
In download mode, db_table_name will be downloaded
from the database at conn.
Fast download:
supply values for all of conn, schema and fk_parent_tables.
schema and fk_parent_tables()
can be obtained by calling
schema_from_conn(conn = conn) and
get_all_fk_tables(conn = conn, schema = schema),
respectively.
In fast download mode,
execution time is reduced, because the database at conn
has already been queried for schema and fk_parent_tables.
Fast download mode quickens multiple downloads to the same database.
See also
encode_fks() for the reverse operation.