Installation and set up of R6 rocker object

Nikolaus Pawlowski


Installation

Installation of current released version from CRAN

install.packages("rocker")

Installation of current development version from GitHub

install.packages("devtools")
devtools::install_github("nikolaus77/rocker")

New rocker class object

Create new rocker database handling object

Option 1

db <- rocker::newDB() # New database handling object
#> dctr | New object

Option 2

db <- rocker::rocker$new() # New database handling object
#> dctr | New object

Terminal output

Controlling terminal output

db <- rocker::newDB(verbose = TRUE) # New database handling object
#> dctr | New object
db$setupPostgreSQL()
#> Dctr | Driver load RPostgres
db$unloadDriver()
#> dctr | Driver unload RPostgres
db$verbose <- FALSE # Terminal output off
db$setupPostgreSQL()
db$unloadDriver()
db$verbose <- TRUE # Terminal output on (default)
db$setupPostgreSQL()
#> Dctr | Driver load RPostgres
db$unloadDriver()
#> dctr | Driver unload RPostgres

Structure of terminal output

Dctr | Driver load RSQLite
D                          = Driver     (D = loaded,    d = not set)
 c                         = Connection (C = opened,    c = closed)
  t                        = Transation (T = active,    t = no tranastion)
   r                       = Result     (R = available, r = no result)
       Driver load RSQLite = Message text

Optional object ID

Optionally, rocker object can be labeled with an ID. This can be helpful in case terminal output of multiple rocker objects need to be distinguished.

db1 <- rocker::newDB(id = "myDB 1") # New database handling object with ID
#> myDB 1 | dctr | New object id myDB 1
db2 <- rocker::newDB(id = "myDB 2") # New database handling object with ID
#> myDB 2 | dctr | New object id myDB 2
db1$setupPostgreSQL()
#> myDB 1 | Dctr | Driver load RPostgres
db2$setupMariaDB()
#> myDB 2 | Dctr | Driver load RMariaDB
db1$unloadDriver()
#> myDB 1 | dctr | Driver unload RPostgres
db2$unloadDriver()
#> myDB 2 | dctr | Driver unload RMariaDB
db1$id <- NULL # Remove ID
db1$setupSQLite()
#> Dctr | Driver load RSQLite
db1$unloadDriver()
#> dctr | Driver unload RSQLite
db1$id <- "newID 1" # Add new ID
db1$setupSQLite()
#> newID 1 | Dctr | Driver load RSQLite
db1$unloadDriver()
#> newID 1 | dctr | Driver unload RSQLite

Object properties

Object properties are stored in the info field and can be displayed by print function.

db <- rocker::newDB() # New database handling object
#> dctr | New object
db$setupPostgreSQL()
#> Dctr | Driver load RPostgres
db$info
#> $package
#> [1] "RPostgres"
#> 
#> $host
#> [1] "127.0.0.1"
#> 
#> $port
#> [1] "5432"
#> 
#> $dbname
#> [1] "mydb"
db
#> object          
#>   id            null
#>   verbose       true
#>   validateQuery null
#> database        
#>   package       RPostgres
#>   host          127.0.0.1
#>   port          5432
#>   dbname        mydb
#> status          
#>   driver        true
#>   connection    false
#>   transaction   false
#>   result        false
db$print()
#> object          
#>   id            null
#>   verbose       true
#>   validateQuery null
#> database        
#>   package       RPostgres
#>   host          127.0.0.1
#>   port          5432
#>   dbname        mydb
#> status          
#>   driver        true
#>   connection    false
#>   transaction   false
#>   result        false
print(db)
#> object          
#>   id            null
#>   verbose       true
#>   validateQuery null
#> database        
#>   package       RPostgres
#>   host          127.0.0.1
#>   port          5432
#>   dbname        mydb
#> status          
#>   driver        true
#>   connection    false
#>   transaction   false
#>   result        false
db$unloadDriver()
#> dctr | Driver unload RPostgres

Connection validation – Is the earlier opened database connection still open?

db <- rocker::newDB() # New database handling object
#> dctr | New object
db$setupSQLite()
#> Dctr | Driver load RSQLite
db$print()
#> object          
#>   id            null
#>   verbose       true
#>   validateQuery null
#> database        
#>   package       RSQLite
#>   dbname        :memory:
#> status          
#>   driver        true
#>   connection    false
#>   transaction   false
#>   result        false

During connection setup, a validateQuery is looked up automatically.

db$connect()
#> DCtr | Database connected
db$print()
#> object          
#>   id            null
#>   verbose       true
#>   validateQuery SELECT 1
#> database        
#>   package       RSQLite
#>   dbname        :memory:
#> status          
#>   driver        true
#>   connection    true
#>   transaction   false
#>   result        false

Discovered validateQuery

db$validateQuery
#> [1] "SELECT 1"

Validate connection

db$validateCon()
#> DCtr | Connection valid true
#> [1] TRUE

If required, validateQuery can be defined manually.

db$validateQuery <- "SELECT 2"
db$validateCon()
#> DCtr | Connection valid true
#> [1] TRUE
db$print()
#> object          
#>   id            null
#>   verbose       true
#>   validateQuery SELECT 2
#> database        
#>   package       RSQLite
#>   dbname        :memory:
#> status          
#>   driver        true
#>   connection    true
#>   transaction   false
#>   result        false

Clean up

db$disconnect()
#> Dctr | Database disconnected
db$validateCon()
#> Dctr | Connection valid false
#> [1] FALSE
db$unloadDriver()
#> dctr | Driver unload RSQLite

Further help

Please read the documentation of rocker class.

help("rocker-package")
help("rocker-R6-class")
help("rocker-S3-functions")
help("rocker-README")

Reading of DBI package documentation is recommended.