Create a class representing a connection to a MareFrame DB

mfdb(schema_name,
         db_params = list(),
         destroy_schema = FALSE,
         save_temp_tables = FALSE)

    mfdb_disconnect(mdb)

Arguments

schema_name

This can be one of:

  1. Postgresql schema name

  2. A file path ending with ".sqlite" to connect to a SQLite file database

  3. A file path ending with ".duckdb" to connect to a DuckDB file database

If connecting to a SQLite/DuckDB database, db_params should remain empty ( schema_name will be used as a dbname).

If connecting to a Postgres database, it can be used to store any number of case studies, by storing them in separate schemas. This parameter defines the schema to connect to, and can contain any lower case characters or underscore.

db_params

Extra parameters to supply to DBI::dbConnect. By default it will search for a "mf" database locally, but you can override any of the parameters, in particular host, dbname, user, password. See ?RPostgres::Postgres for more information.

If dbname looks like a SQLite database filename, then MFDB will use the RSQLite driver. If dbdir is set, then MFDB will use the duckdb driver. Otherwise, RPostgres will be used.

db_params can also be supplied by environment variable, for example if a MFDB_DBNAME environment variable is set then it will be used instead of any dbname supplied here.

destroy_schema

Optional boolean. If true, all mfdb tables will be destroyed when connecting. This allows you to start populating your case study from scratch if required. The function will return NULL, you need to call mfdb again to connect, at which point the mfdb tables will be recreated and you can populate with data again.

save_temp_tables

Optional boolean. If true, any temporary tables will be made permanent for later inspection.

mdb

Database connection created by mfdb().

Value

A 'mfdb' object representing the DB connection

Examples

# Connect to a SQLite database file
mdb <- mfdb(tempfile(fileext = '.sqlite'))
#> 2022-11-16 12:34:25 INFO:mfdb:Creating schema from scratch
#> 2022-11-16 12:34:25 INFO:mfdb:Taxonomy market_category no updates to make
#> 2022-11-16 12:34:25 INFO:mfdb:Schema up-to-date
mfdb_disconnect(mdb)

if (FALSE) # NB: Requires a PostgreSQL installation, see README

# Connect to local DB, as the "examples" case study
mdb <- mfdb('examples')
mfdb_disconnect(mdb)
#> Warning: Already disconnected

# Connect to remote server, will prompt for username/password
if (interactive()) {
    mdb <- mfdb('examples', db_params = list(host = "mfdb.rhi.hi.is"))
}