Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Shared sql connection functionality #8

Merged
merged 4 commits into from

2 participants

@cortex

This allows shared connection settings to be placed in a separate file. This is useful for sharing username/password for several connections in one file so it is possible to omit that from version control.

R/sql.reader.R
@@ -78,6 +78,15 @@
sql.reader <- function(data.file, filename, variable.name)
{
database.info <- ProjectTemplate:::translate.dcf(filename)
+
+ if (! is.null(database.info[['connection']]))
+ {
+ connection_filename <- paste("data/", database.info[['connection']],".sql-connection", sep="")
+ connection.info <- ProjectTemplate:::translate.dcf(connection_filename)
+
+ # Allow .sql to override options defined in .connection
+ database.info <- c(database.info, connection.info)
@johnmyleswhite Owner

I'm a little confused by this combination. Could you give an example?

@cortex
cortex added a note

The idea here is that the settings specified in the .sql file will override the settings in the .sql-connection file. The most common case (for which I wrote this) would be that they are not actually overlapping, like:

maindb.sql-connection
type: mysql
user: username
password: xxxyyy
host: 127.0.0.1
port: 3309
dbname: stats

pageviews.sql
connection: maindb
query: select * from pageviews where timestamp > "2012-01-01"

...but since we are using combine, it is also possible to override stuff you might need for some specific databases, even when it is specified in the sql-connection file. For example:

users.sql
connection: maindb
dbname:social
query: select * from users

salaries.sql
connection: maindb
dbname:economy
user:finance
password:secret
query: select * from salaries

Of course it would be possible to make the composition not allow overrides, or only allow it for certain fields, but I this approach seemed more flexible to me.

@cortex
cortex added a note

...I just test-ran this code again and realized that the c() call actually does not work the way I have described here, I'll get back with corrected code.

@cortex
cortex added a note

Ok, I am pretty new to R so this was an interesting exercise in trying to figure out the data types. It turns out that the code actually does work the way I described, just not for the reason I thought. When running c with the arguments in that order, it will produce a list that has the repeated values multiple times, but when accessing that element using database.info[[''type']], only the correct element is returned. This looks a bit strange in the code I guess, but it does work. If we want to eliminate the duplicated attributes we can add this:

database.info <- database.info[!duplicated(names(database.info))]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@johnmyleswhite johnmyleswhite merged commit 7e5a9a4 into johnmyleswhite:master
@johnmyleswhite

I've also merged this. Sorry for the delay. I thought I would have time to test these, but I'm too swamped with work. If something goes wrong, I'll reset them and them reopen this issue to figure out issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 0 deletions.
  1. +9 −0 R/sql.reader.R
  2. +6 −0 man/sql.reader.Rd
View
9 R/sql.reader.R
@@ -79,6 +79,15 @@ sql.reader <- function(data.file, filename, variable.name)
{
database.info <- ProjectTemplate:::translate.dcf(filename)
+ if (! is.null(database.info[['connection']]))
+ {
+ connection_filename <- paste("data/", database.info[['connection']],".sql-connection", sep="")
+ connection.info <- ProjectTemplate:::translate.dcf(connection_filename)
+
+ # Allow .sql to override options defined in .connection
+ database.info <- modifyList(connection.info, database.info)
+ }
+
if (! (database.info[['type']] %in% c('mysql', 'sqlite', 'odbc', 'postgres', 'oracle')))
{
warning('Only databases reachable through RMySQL, RSQLite, RODBC ROracle or RPostgreSQL are currently supported.')
View
6 man/sql.reader.Rd
@@ -23,6 +23,12 @@
accessed. All tables from the database, one specific
tables or one specific query against any set of tables
may be executed to generate a data set.
+
+ It is also possible to place shared connection settings
+ in a separate file using the option "connection". The
+ shared connection configuration is placed in files
+ named *.sql-connection in the data/ directory.
+
}
\details{
Examples of the DCF format and settings used in a .sql
Something went wrong with that request. Please try again.