Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.4 #14

Merged
merged 31 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ec7c1cd
bump version and date
ThierryO Jun 28, 2016
8f47aa0
bump package version and date
ThierryO Jul 4, 2016
be1018b
connect_result() uses a PostgreSQL database
ThierryO Jul 4, 2016
7a758d3
check_dbtable() uses a PostgreSQL database
ThierryO Jul 4, 2016
a192116
check_dbtable_variable() uses a PostgreSQL database
ThierryO Jul 4, 2016
1ed69ab
use git_connect() with PostgreSQL database
ThierryO Jul 4, 2016
5253613
use a src_postgres() connection instead of a dbConnect()
ThierryO Jul 4, 2016
477a7b5
odbc_get_id() works with src_postgres() instead of RODBC
ThierryO Jul 4, 2016
81a4e93
bugfix: use the con element from channel in check_dbtable()
ThierryO Jul 5, 2016
c1c2d4a
odbc_connect() reads parameters from PostgreSQL database
ThierryO Jul 5, 2016
f1d4c02
update git_connect() to new database structure
ThierryO Sep 14, 2016
6692987
update odbc_connect() to new database structure
ThierryO Sep 16, 2016
6df7d9d
update get_nbn_key() and related functions
ThierryO Sep 28, 2016
3bdf832
add is_chartor()
ThierryO Nov 21, 2016
b6dd29b
fix linters
ThierryO Jan 18, 2017
5f4f12a
fix unit tests
ThierryO Jan 18, 2017
85e9cbf
update R versions
ThierryO Jan 18, 2017
013bd42
skip db related tests on CRAN
ThierryO Jan 18, 2017
48e9004
skip database related tests on CRAN
ThierryO Jan 18, 2017
ace629b
fix skip_on_cran() in check_id() tests
ThierryO Jan 18, 2017
d40b0fb
Merge branch 'develop' into postgresql
ThierryO Jan 18, 2017
b23ab04
ignore code coverage on database related lines
ThierryO Jan 18, 2017
e04c6d3
Merge remote-tracking branch 'origin/postgresql' into postgresql
ThierryO Jan 18, 2017
a996b47
bump package date
ThierryO Jan 18, 2017
148d24b
Merge pull request #12 from ThierryO/postgresql
ThierryO Jan 18, 2017
0a8729d
can't use R functions in filter on tbl()
ThierryO Feb 15, 2017
b90d243
simplify build chain
ThierryO Jul 28, 2017
f22260c
Merge pull request #13 from inbo/hotfix
ThierryO Jul 28, 2017
d43ba72
update README
ThierryO Jul 28, 2017
faa25d6
bump date
ThierryO Jul 28, 2017
e9c4e23
remove references to develop from README
ThierryO Jul 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Package: n2khelper
Title: Auxiliary Functions for the Analysis and Reporting of the Natura 2000
Monitoring
Version: 0.2
Date: 2016-05-30
Version: 0.4
Date: 2017-07-28
Authors@R: c(person("Thierry", "Onkelinx", email = "thierry.onkelinx@inbo.be", role = c("aut", "cre")))
Description: Functions for importing the raw data, creating analysis dataset
and running the analysis for the Common Breeding Bird Survey in Flanders.
Description: Auxiliary functions for analysing Natura 2000 monitoring data.
Depends:
R (>= 3.2.0)
Imports:
Expand All @@ -15,6 +14,9 @@ Imports:
lubridate,
methods,
assertthat,
DBI,
tidyr,
lazyeval,
RODBC
License: GPL-3
LazyData: true
Expand Down Expand Up @@ -45,6 +47,7 @@ Collate:
'git_connection.R'
'git_recent.R'
'git_sha.R'
'is_chartor.R'
'is_git_repo.R'
'list_files_git.R'
'match_nbn_key.R'
Expand Down
19 changes: 18 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export(check_single_logical)
export(check_single_numeric)
export(check_single_posix)
export(check_single_probability)
export(connect_nbn)
export(connect_result)
export(cut_date)
export(get_nbn_key)
export(get_nbn_key_multi)
export(get_nbn_name)
export(git_connect)
export(git_connection)
export(is.chartor)
export(is_git_repo)
export(match_nbn_key)
export(odbc_connect)
Expand All @@ -38,26 +40,39 @@ exportMethods(write_delim_git)
importClassesFrom(git2r,cred_ssh_key)
importClassesFrom(git2r,cred_user_pass)
importClassesFrom(git2r,git_repository)
importFrom(DBI,dbExistsTable)
importFrom(DBI,dbGetInfo)
importFrom(DBI,dbGetQuery)
importFrom(DBI,dbListFields)
importFrom(RODBC,odbcClose)
importFrom(RODBC,odbcDriverConnect)
importFrom(RODBC,sqlClear)
importFrom(RODBC,sqlColumns)
importFrom(RODBC,sqlQuery)
importFrom(RODBC,sqlTables)
importFrom(assertthat,'on_failure<-')
importFrom(assertthat,assert_that)
importFrom(assertthat,has_name)
importFrom(assertthat,is.count)
importFrom(assertthat,is.flag)
importFrom(assertthat,is.string)
importFrom(assertthat,noNA)
importFrom(dplyr,"%>%")
importFrom(dplyr,collect)
importFrom(dplyr,count_)
importFrom(dplyr,data_frame)
importFrom(dplyr,filter_)
importFrom(dplyr,funs)
importFrom(dplyr,group_by_)
importFrom(dplyr,inner_join)
importFrom(dplyr,mutate_)
importFrom(dplyr,mutate_each_)
importFrom(dplyr,select_)
importFrom(dplyr,semi_join)
importFrom(dplyr,slice_)
importFrom(dplyr,src_postgres)
importFrom(dplyr,summarise_)
importFrom(dplyr,tbl)
importFrom(dplyr,ungroup)
importFrom(git2r,add)
importFrom(git2r,commit)
importFrom(git2r,config)
Expand All @@ -67,6 +82,7 @@ importFrom(git2r,hashfile)
importFrom(git2r,head)
importFrom(git2r,push)
importFrom(git2r,repository)
importFrom(lazyeval,interp)
importFrom(lubridate,is.Date)
importFrom(lubridate,is.POSIXt)
importFrom(lubridate,year)
Expand All @@ -80,6 +96,7 @@ importFrom(plyr,ddply)
importFrom(stats,aggregate)
importFrom(stats,as.formula)
importFrom(stats,na.fail)
importFrom(tidyr,spread_)
importFrom(utils,file_test)
importFrom(utils,read.delim)
importFrom(utils,read.table)
Expand Down
43 changes: 29 additions & 14 deletions R/check_dbtable.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
#' Check if a table is available in a given ODBC connection
#' @param error Indicates the behaviour when a table is missing. Gives an error
#' when error = TRUE (default). Return FALSE otherwise.
#' @param schema The schema. Defaults to 'dbo'
#' @param schema The schema. Defaults to 'public'
#' @inheritParams odbc_get_id
#' @export
#' @importFrom RODBC sqlTables
#' @importFrom assertthat assert_that is.string
#' @importFrom dplyr %>%
#' @importFrom DBI dbExistsTable dbGetInfo
#' @importFrom stats na.fail
#' @return TRUE when all tables are present in the ODBC connection.
check_dbtable <- function(table, schema = "dbo", channel, error = TRUE){
check_dbtable <- function(table, schema = "public", channel, error = TRUE){
# nocov start
table <- check_character(x = table, name = "table", na.action = na.fail)
if (length(table) == 0) {
stop("'table' must contain at least one value")
}
schema <- check_single_character(x = schema, name = "schema")
if (class(channel) != "RODBC") {
stop("channel is not an ODBC connection")
assert_that(is.string(schema))
if (has_name(channel, "con")) {
assert_that(inherits(channel$con, "DBIConnection"))
this_channel <- channel$con
} else {
assert_that(inherits(channel, "DBIConnection"))
this_channel <- channel
}
# nocov start

available <- sqlTables(channel = channel, schema = schema)$TABLE_NAME
check <- table %in% available
if (all(check)) {
test <- sapply(
table,
function(x){
c(schema, x) %>%
dbExistsTable(conn = this_channel)
}
)
if (all(test)) {
return(TRUE)
}
if (error) {
stop(
"Table(s) missing: ",
paste(table[!check], collapse = ", ")
)
names(test)[!test] %>%
paste(collapse = ", ") %>%
sprintf(
fmt = "Table(s) %s not found in schema %s on database %s",
schema,
dbGetInfo(this_channel)$dbname
) %>%
stop(call. = FALSE)
} else {
return(FALSE)
}
Expand Down
12 changes: 4 additions & 8 deletions R/check_dbtable_variable.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#' @param error Indicates the behaviour when a variable is missing. Gives an error when error = TRUE (default). Return FALSE otherwise.
#' @inheritParams odbc_get_id
#' @export
#' @importFrom RODBC sqlColumns
#' @importFrom DBI dbListFields
#' @importFrom stats na.fail
#' @return TRUE when all variables are present in the table.
check_dbtable_variable <- function(
table,
variable,
schema = "dbo",
schema = "public",
channel,
error = TRUE
){
Expand All @@ -24,12 +24,8 @@ check_dbtable_variable <- function(
check_dbtable(table = table, schema = schema, channel = channel, error = TRUE)
# nocov start

available <- sqlColumns(
channel = channel,
sqtable = table,
schema = schema
)$COLUMN_NAME
check <- variable %in% available
check <- variable %in%
dbListFields(conn = channel$con, name = c(schema, table))
if (all(check)) {
return(TRUE)
}
Expand Down
21 changes: 4 additions & 17 deletions R/check_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,18 @@
#' @inheritParams odbc_get_id
#' @export
#' @importFrom assertthat assert_that is.count
#' @importFrom RODBC sqlQuery
#' @importFrom DBI dbGetQuery
check_id <- function(value, variable, table, channel){
assert_that(is.count(value))
variable <- check_single_character(variable, name = "variable")
check_dbtable_variable(table = table, variable = variable, channel = channel)

# nocov start
sql <- paste("SELECT", variable, "FROM", table, "WHERE", variable, "=", value)
selection <- sqlQuery(
channel = channel,
query = sql,
stringsAsFactors = FALSE,
as.is = TRUE
selection <- dbGetQuery(
conn = channel$con,
statement = sql
)
if (!inherits(selection, "data.frame")) {
if (length(grep("Invalid column name", selection)) > 0) {
stop(
"The variable '", variable, "' doesn't exists in table '", table, "'"
)
}
if (length(grep("Conversion failed", selection))) {
stop(paste(selection, collapse = "\n"))
}
}

if (nrow(selection) == 0) {
return(FALSE)
} else {
Expand Down
49 changes: 25 additions & 24 deletions R/connect_result.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#' Opens an ODBC connection to the 'results' database
#' @export
#' @importFrom RODBC odbcDriverConnect
#' @param develop Logical value. Indicates the location ton the results database
#' @param username the username to connect to the database. Use a trusted connection when missing
#' @param password the password for the username. Ignored when using a trusted connection.
#' @importFrom dplyr src_postgres
#' @param username the username to connect to the database.
#' @param password the password for the username.
#' @param develop Logical value. Indicates the location of the results database
#' @importFrom assertthat assert_that is.flag noNA is.string
connect_result <- function(develop = TRUE, username, password){
connect_result <- function(username, password, develop = TRUE){
assert_that(is.flag(develop))
assert_that(noNA(develop))
assert_that(is.string(username))
assert_that(is.string(password))

dbname <- "n2kresult"
if (develop) {
server <- "INBODEV02\\development"
# nocov start
host <- "localhost"
# nocov end
} else {
stop("Production database not yet defined")
}
driver <- ifelse(.Platform$OS.type == "windows", "SQL Server", "FreeTDS")
if (missing(username)) {
authentication <- "Trusted_Connection=Yes;"
} else {
assert_that(is.string(username))
if (username == "") {
authentication <- "Trusted_Connection=Yes;"
} else {
assert_that(is.string(password))
authentication <- sprintf("Uid=%s;Pwd=%s", username, password)
}
}
connection <- sprintf(
"Driver=%s;Server=%s;Database=D0116on00_SoortenMeetnetAnalyse;%s",
driver,
server,
authentication
# nocov start
src_postgres(
host = host,
dbname = dbname,
user = username,
password = password
)
odbcDriverConnect(connection)
# nocov end
}

#' Open a trusted connection to the NBN database
#' @export
connect_nbn <- function(){
odbcDriverConnect(connection = nbn.dsn)
}
Loading