ConnectionHandler classes are R6 instances that intent
to provide consistent manners to connect to relational database
instances on top of DatabaseConnector utilities. These are
designed for long running applications, such as Plumber APIs or Shiny
applications and would generally be encapsulated in other objects (such
as a DataMigrationManager).
Creating a connection handler only requires a connection details object
In applications such as Shiny apps that require many long running or concurrent requests, pooled connections are often required. In this case, a polled connection handler should be used. Instantiation is similar to above:
connectionDetails <- DatabaseConnector::createConnectionDetails("sqlite", server = "MyDb.sqlite")
connectionHandler <- PooledConnectionHandler$new(connectionDetails)These classes should behave identically in terms of queries as they
implement a common set of functions. See the pool::dbPool
class for information regarding pooled connections.
Submitting queries to a database is straightforward and uses
SqlRender parameterization, for example:
Similarly, SQL execution can occur
Note that the above queries render and translate using
SqlRender functionality. Should direct querying or
execution be required the functions
connectionHander$queryFunction and
connectionHandler$executeFunction can be used,
respectively.