Giter Club home page Giter Club logo

dbpath's Introduction

dbpath

R build status

dbpath is an R library for creating database connections via a single string (url).

Install

remotes::install_github("machow/dbpath")

Examples

library(dbpath)

sql_url <- dbpath("postgresql+RPostgres://some_user:some_password@localhost:5432")
sql_url
[1] "postgresql+RPostgres://some_user:****@localhost:5432"

You can use the dbpath output with either DBI::dbConnect, or dplyr::tbl to create a remote connection.

# get a database connection
con <- DBI::dbConnect(sql_url)

# get a database table called mtcars
tbl_mtcars <- dplyr::tbl(sql_url, "mtcars")
tbl_mtcars

URL Format

dbpath URLs follow the format below.

<dialect>+<driver>://<username>:<password>@<host>:<port>/<database>

Here's an example using mysql:

mysql_url <- "mysql+RMariaDB://root:some_password@localhost"

In this case, we're connecting to the mysql dialect, using R's MariaDB package as a driver.

The code below shows how it translates to making the connection manually.

# dbpath
DBI::dbConnect(mysql_url)

# manual
DBI::dbConnect(
  RMariaDB::MariaDB(),
  user = "root",
  password = "some_password",
  host = "localhost"
  )

Behind the scenes, dbpath uses driver hooks to know that if RMariaDB is the driver, then we need its MariaDB() object. Note that the RMariaDB in mysql+RMariaDB is optional!

Interoperability with Python

dbpath's approach is based on python's SQLAlchemy library. This means that you can use the same string across languages!

<!-- python example -->
R python
# one string to rule them all
sql_url = "postgresql://user:password@localhost:port/dbname"
library(dbpath)
DBI::dbconnection(sql_url)
import sqlalchemy
sqlalchemy.create_engine(sql_url)

Configuring Driver Selection

The code below adds a custom driver for SQLite.

library(dbpath)

driver_registry$set(my_driver = function () RSQLite::SQLite)

# Note the 3 slashes, rather than two, meaning no user name, password, or host
sqlite_url <- "sqlite+my_driver:///:memory:"

sqlite_url
[1] "sqlite+my_driver://:****@/:memory:"
DBI::dbConnect(sqlite_url)
<SQLiteConnection>
  Path: :memory:
  Extensions: TRUE

Available Driver Hooks

# see available drivers
driver_registry$get()

# see defaults for when no driver is specified
driver_defaults$get()

Here are the current driver defaults:

 postgresql       mysql     mariadb 
"RPostgres"  "RMariaDB"  "RMariaDB" 

Configuring Driver Connections

dbpath uses an s3 method called dbpath_params to get a list of parameters to pass to DBI::dbConnect (or dplyr::tbl).

url <- dbpath("postgresql://a_user:a_password@localhost/dbname")

dbpath_params(url)
$drv
<PqDriver>

$user
[1] "a_user"

$password
[1] "a_password"

$host
[1] "localhost"

$port
[1] ""

$dbname
[1] "dbname"

In order to support a new driver type, you can register an s3 method for it. The function should return a list of parameters, whose names are the arguments that would be passed to DBI::dbConnect.

dbpath_params.PqDriver <- function(driver, url) {
  list(
    drv = driver
    user = url$user,
    password = url$password,
    host = url$host,
    port = url$port,
    
    # use PqDriver specific argument: dbname
    dbname = url$database
  )
}

You can get a specific drivers parameters by passing it as the first argument to dbpath_params:

driver <- RPostgres::Postgres()
class(driver)                        # <PqDriver>

dbpath_params(driver, url)

dbpath's People

Contributors

machow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.