conversion_rules: Create a conversion rule

Description Usage Arguments Details Examples

Description

Conversion rules define how to convert data between SQL and R values.

Usage

1
2
3
read_conversion_rule(condition, conversion, target_type)

write_conversion_rule(condition, conversion, target_type)

Arguments

condition

A function which accepts the data and additional data attributes and returns a logical indicating if it is able to convert the data.

conversion

A function which accepts data of a transfer type and transforms into data of type target_type.

target_type

A character vector of length one. For a read_conversion_rule this is an R class, for a write_conversion_rule, it is an SQL type name.

Details

Type mapping in dbj has four data type units: The R working type (RWT), The R transfer type (RTT), the Java transfer type (JTT) and the SQL storage Type (SST). The RWT is the type of a data.frame column you work with on the front end. For writing to or reading from a database, each RWT must be mapped to a SST. For performance reasons of the R to Java communication, conversions between these types must go through an associated RTT/JTT transfer type pair. A JTT is one of the Java raw types (boolean, byte, int, long, float, double) or String. An RTT is the R equivalent of the JTT. In summary, data transformation involves three conversions steps: RWT <-> RTT <-> JTT <-> SST. These steps are defined by the conersion rules.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Convert TIME data, fetched from JDBC as numeric milliseconds, to difftime.
read_conversion_rule(
  function(jdbc.type, ...) with(JDBC_SQL_TYPES,
    jdbc.type == TIME),
  function(data, ...) as.difftime(data / 1000, units = "secs"),
  function(...) "difftime"
)

#' # Convert difftime vectors into numeric vectors of milliseconds and create 
write_conversion_rule(
  function(data, ...) is.difftime(data),
  function(data, ...) as.numeric(data, units = "secs") * 1000,
  function(...) "TIME"
)

hoesler/dbj documentation built on May 17, 2019, 4:36 p.m.