dbcon: Connect to Moodle database

Description Usage Arguments Details Value Examples

Description

These are functions for opening and closing database connections. A session is a convenience that keeps track of a connection for you, so you don't have to pass it to function calls. However, you can create a connection object outside of a session if you wish.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
create_db_con(host, port, dbname, username, password = getPass::getPass(msg =
  "Database password:"), driver = RMySQL::MySQL())

start_db_session(host, port, dbname, username, password = getPass::getPass(msg
  = "Database password:"), driver = RMySQL::MySQL())

end_db_session()

get_session_con()

set_session_con(con)

Arguments

host

The address of the database host.

port

The port on which to access the database.

dbname

The name of the database to access.

username

Your username to access the database.

password

Your password to access the database. Generally, passwords should not be saved in plain text code files. Thus, the default is to prompt the user interactively.

driver

Driver specifying communication protocol for the type of database being used. See dbplyr for supported databases.

con

A database connection object.

Details

These functions are wrappers around dbplyr and pool objects. These underlying tools neatly open and close individual connections as needed and translate queries into a variety of different database lanaguages, so the code is database-independent.

You do not need to open and close connections constantly. You can leave a connection open as long as you want (unless the database kicks you off), but it's best to close it when you are done for the day.

Value

create_db_con returns a dbplyr pool connection object.

start_db_session silently returns the old, closed session connection, or NULL if there was not a previous connection.

end_db_session silently returns the session connection after closing it, or NULL if there was no connection.

get_session_con returns the current session's connection object.

set_session_con returns the previous session's connection object after replacing the session connection.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Not run: 

## Start a session that will pass connection object for you

# You will be prompted for password
start_db_session(
  host = "localhost",
  port = 3306,
  dbname = "moodle",
  username = "moodle-ro"
)

# Do a bunch of stuff
fetch_enrolled_users(72)

# End the session when you are done for the day
end_db_session()

## Create pool connection object you will pass yourself

# You will be prompted for password
con <- create_db_con(
  host = "localhost",
  port = 3306,
  dbname = "moodle",
  username = "moodle-ro"
)

# Do a bunch of stuff
fetch_enrolled_users(72, con = con)

# Close the connection when you are done for the day
pool::poolClose(con)

## End(Not run)

TheFridayInstitute/fimoodler documentation built on May 28, 2019, 9:37 a.m.