Skip to content

Commit

Permalink
Merge pull request #7 from teramonagi/feature/issue1
Browse files Browse the repository at this point in the history
First release(ver 0.1)
  • Loading branch information
teramonagi committed Dec 3, 2015
2 parents 506a375 + 1405a9c commit ddd23ff
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 19 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: RODBCDBI
Type: Package
Version: 0.1
Title: Provides access to databases through the ODBC interface
Title: Provides Access to Databases Through the ODBC Interface
Author: teramonagi
Maintainer: teramonagi <[email protected]>
Description: RODBCDBI is an implementation of R's DBI interface using ODBC as a
Maintainer: Nagi Teramo <[email protected]>
Description: An implementation of R's DBI interface using ODBC package as a
back-end. This allows R to connect to any DBMS that has a ODBC driver.
License: BSD
License: MIT + file LICENSE
Imports:
methods,
DBI,
Expand Down
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2015-2015
COPYRIGHT HOLDER: Nagi Teramo
36 changes: 32 additions & 4 deletions R/ODBCConnection.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ setClass(
slots=list(odbc="RODBC")
)

#' Execute a statement on a given database connection.
#'
#' @param conn An existing \code{\linkS4class{ODBCConnection}}
#' @param statement a character vector of length 1 containing SQL
#' @param ... Other parameters passed on to methods
#' @export
#' @rdname odbc-query
setMethod("dbSendQuery", "ODBCConnection", function(conn, statement, ...) {
Expand All @@ -21,22 +26,27 @@ setMethod("dbSendQuery", "ODBCConnection", function(conn, statement, ...) {
new("ODBCResult", connection=conn, sql=statement, state=env)
})

#' Get DBMS metadata.
#'
#' @param dbObj An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}}
#' @export
#' @rdname ODBCConnection-class
setMethod("dbGetInfo", "ODBCConnection", function(dbObj, ...) {odbcGetInfo(dbObj@odbc)})
setMethod("dbGetInfo", "ODBCConnection", function(dbObj, ...) {dbObj(dbObj@odbc)})


#' List fields in specified table.
#'
#' @param conn An existing \code{\linkS4class{RODBCConnection}}
#' @param conn An existing \code{\linkS4class{ODBCConnection}}
#' @param name a length 1 character vector giving the name of a table.
#' @export
#' @examples
#' \dontrun{
#' library(DBI)
#' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
#' dbWriteTable(con, "iris", iris, overwrite=TRUE)
#' dbListFields(con, "iris")
#' dbDisconnect(con)
#' }
setMethod("dbListFields", c("ODBCConnection", "character"), function(conn, name) {
sqlColumns(conn@odbc, name)$COLUMN_NAME
})
Expand All @@ -53,27 +63,32 @@ setMethod("dbListTables", "ODBCConnection", function(conn){
#'
#' @export
#' @rdname dbWriteTable
#' @param con,conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}
#' @param conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}
#' @param name a character string specifying a table name. ODBCConnection table names
#' are \emph{not} case sensitive, e.g., table names \code{ABC} and \code{abc}
#' are considered equal.
#' @param value a data.frame (or coercible to data.frame) object or a
#' file name (character). when \code{value} is a character, it is interpreted as a file name and its contents imported to ODBC.
#' @param overwrite logical. Should data be overwritten?
#' @param append logical. Should data be appended to an existing table?
#' @param ... additional arguments passed to the generic.
#' @export
#' @examples
#' \dontrun{
#' library(DBI)
#' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
#' dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE)
#' dbReadTable(con, "mtcars")
#' dbDisconnect(con)
#' }
setMethod("dbWriteTable", c("ODBCConnection", "character", "data.frame"), function(conn, name, value, overwrite=FALSE, append=FALSE, ...) {
sqlSave(conn@odbc, dat=value, tablename=name, safer=!overwrite, append=append, ...)
invisible(TRUE)
})

#' Does the table exist?
#'
#' @param conn An existing \code{\linkS4class{SQLiteConnection}}
#' @param conn An existing \code{\linkS4class{ODBCConnection}}
#' @param name String, name of table. Match is case insensitive.
#' @export
setMethod("dbExistsTable", c("ODBCConnection", "character"), function(conn, name) {
Expand Down Expand Up @@ -107,13 +122,15 @@ setMethod("dbRemoveTable", c("ODBCConnection", "character"), function(conn, name
#'
#' @param conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}
#' @param name a character string specifying a table name.
#' @param row.names a character string specifying a table name.
#' @param check.names If \code{TRUE}, the default, column names will be converted to valid R identifiers.
#' @param select.cols A SQL statement (in the form of a character vector of
#' length 1) giving the columns to select. E.g. "*" selects all columns,
#' "x,y,z" selects three columns named as listed.
#' @inheritParams DBI::rownamesToColumn
#' @export
#' @examples
#' \dontrun{
#' library(DBI)
#' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
#' dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE)
Expand All @@ -125,6 +142,7 @@ setMethod("dbRemoveTable", c("ODBCConnection", "character"), function(conn, name
#' dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8", row.names = FALSE)
#'
#' dbDisconnect(con)
#' }
setMethod("dbReadTable", c("ODBCConnection", "character"), function(conn, name, row.names = NA, check.names = TRUE, select.cols = "*") {
out <- dbGetQuery(conn, paste("SELECT", select.cols, "FROM", name), row.names = row.names)
if (check.names) {
Expand All @@ -133,6 +151,16 @@ setMethod("dbReadTable", c("ODBCConnection", "character"), function(conn, name,
out
})

#' Close a current session.
#'
#' @rdname dbDisconnect
#' @param conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}
#' @examples
#' \dontrun{
#' library(DBI)
#' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
#' dbDisconnect(con)
#' }
#' @export
setMethod("dbDisconnect", "ODBCConnection", function(conn) {
if (RODBC:::odbcValidChannel(conn@odbc)){
Expand Down
4 changes: 3 additions & 1 deletion R/ODBCDriver.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#' @export
#' @import methods DBI
#' @examples
#' library(DBI)
#' \dontrun{
#' #' library(DBI)
#' RODBCDBI::ODBC()
#' }
ODBC <- function() {new("ODBCDriver")}

#' ODBCDriver and methods.
Expand Down
9 changes: 6 additions & 3 deletions R/ODBCResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ is_done <- function(x) {
#' @inheritParams DBI::rownamesToColumn
#' @export
#' @rdname odbc-query
setMethod("dbFetch", "ODBCResult", function(res, n = -1, ..., row.names = NA) {
setMethod("dbFetch", "ODBCResult", function(res, n = -1, ...) {
result <- sqlQuery(res@connection@odbc, res@sql)
res
is_done(res) <- TRUE
Expand All @@ -58,9 +58,11 @@ setMethod("dbClearResult", "ODBCResult", function(res, ...) {
#'
#' See documentation of generics for more details.
#'
#' @param dbObj An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}}
#' @param res An object of class \code{\linkS4class{ODBCResult}}
#' @param ... Ignored. Needed for compatibility with generic
#' @examples
#' \dontrun{
#' library(DBI)
#' data(USArrests)
#' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
Expand All @@ -87,11 +89,12 @@ setMethod("dbClearResult", "ODBCResult", function(res, ...) {
#' names(dbGetInfo(con))
#'
#' dbDisconnect(con)
#' }
#' @name odbc-meta
NULL

#' @export
#' @rdname odbc-meta
#' @export
setMethod("dbGetRowCount", "ODBCResult", function(res, ...) {
unlist(dbGetQuery(res@connection, "SELECT count(*) FROM iris"))
})
Expand All @@ -105,6 +108,6 @@ setMethod("dbGetStatement", "ODBCResult", function(res, ...) {
#' @rdname odbc-meta
#' @export
setMethod("dbGetInfo", "ODBCResult", function(dbObj, ...) {
#
# mock implementation
NULL
})
4 changes: 3 additions & 1 deletion man/ODBC.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ ODBC()
This driver never needs to be unloaded and hence \code{dbUnload()} is a null-op.
}
\examples{
library(DBI)
\dontrun{
#' library(DBI)
RODBCDBI::ODBC()
}
}

5 changes: 5 additions & 0 deletions man/ODBCConnection-class.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
\usage{
\S4method{dbGetInfo}{ODBCConnection}(dbObj, ...)
}
\arguments{
\item{dbObj}{An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}}}
}
\description{
\code{ODBCConnection} objects are usually created by \code{\link[DBI]{dbConnect}}

Get DBMS metadata.
}
\keyword{internal}

23 changes: 23 additions & 0 deletions man/dbDisconnect.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/ODBCConnection.R
\docType{methods}
\name{dbDisconnect,ODBCConnection-method}
\alias{dbDisconnect,ODBCConnection-method}
\title{Close a current session.}
\usage{
\S4method{dbDisconnect}{ODBCConnection}(conn)
}
\arguments{
\item{conn}{a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}}
}
\description{
Close a current session.
}
\examples{
\dontrun{
library(DBI)
con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
dbDisconnect(con)
}
}

2 changes: 1 addition & 1 deletion man/dbExistsTable-ODBCConnection-character-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
\S4method{dbExistsTable}{ODBCConnection,character}(conn, name)
}
\arguments{
\item{conn}{An existing \code{\linkS4class{SQLiteConnection}}}
\item{conn}{An existing \code{\linkS4class{ODBCConnection}}}

\item{name}{String, name of table. Match is case insensitive.}
}
Expand Down
4 changes: 3 additions & 1 deletion man/dbListFields-ODBCConnection-character-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
\S4method{dbListFields}{ODBCConnection,character}(conn, name)
}
\arguments{
\item{conn}{An existing \code{\linkS4class{RODBCConnection}}}
\item{conn}{An existing \code{\linkS4class{ODBCConnection}}}

\item{name}{a length 1 character vector giving the name of a table.}
}
\description{
List fields in specified table.
}
\examples{
\dontrun{
library(DBI)
con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
dbWriteTable(con, "iris", iris, overwrite=TRUE)
dbListFields(con, "iris")
dbDisconnect(con)
}
}

4 changes: 4 additions & 0 deletions man/dbReadTable-ODBCConnection-character-method.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

\item{name}{a character string specifying a table name.}

\item{row.names}{a character string specifying a table name.}

\item{check.names}{If \code{TRUE}, the default, column names will be converted to valid R identifiers.}

\item{select.cols}{A SQL statement (in the form of a character vector of
Expand All @@ -33,6 +35,7 @@ Note that the data.frame returned by \code{dbReadTable} only has
primitive data, e.g., it does not coerce character data to factors.
}
\examples{
\dontrun{
library(DBI)
con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE)
Expand All @@ -45,4 +48,5 @@ dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8", row.names = FALSE)

dbDisconnect(con)
}
}

10 changes: 9 additions & 1 deletion man/dbWriteTable.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@
overwrite = FALSE, append = FALSE, ...)
}
\arguments{
\item{conn}{a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}}

\item{name}{a character string specifying a table name. ODBCConnection table names
are \emph{not} case sensitive, e.g., table names \code{ABC} and \code{abc}
are considered equal.}

\item{value}{a data.frame (or coercible to data.frame) object or a
file name (character). when \code{value} is a character, it is interpreted as a file name and its contents imported to ODBC.}

\item{con,conn}{a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}}
\item{overwrite}{logical. Should data be overwritten?}

\item{append}{logical. Should data be appended to an existing table?}

\item{...}{additional arguments passed to the generic.}
}
\description{
Write a local data frame or file to the database.
}
\examples{
\dontrun{
library(DBI)
con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE)
dbReadTable(con, "mtcars")
dbDisconnect(con)
}
}

4 changes: 4 additions & 0 deletions man/odbc-meta.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
\item{res}{An object of class \code{\linkS4class{ODBCResult}}}

\item{...}{Ignored. Needed for compatibility with generic}

\item{dbObj}{An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}}}
}
\description{
See documentation of generics for more details.
}
\examples{
\dontrun{
library(DBI)
data(USArrests)
con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!")
Expand Down Expand Up @@ -50,4 +53,5 @@ names(dbGetInfo(con))

dbDisconnect(con)
}
}

12 changes: 10 additions & 2 deletions man/odbc-query.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
\alias{dbFetch,ODBCResult-method}
\alias{dbHasCompleted,ODBCResult-method}
\alias{dbSendQuery,ODBCConnection-method}
\title{Execute a SQL statement on a database connection}
\title{Execute a statement on a given database connection.}
\usage{
\S4method{dbSendQuery}{ODBCConnection}(conn, statement, ...)

\S4method{dbFetch}{ODBCResult}(res, n = -1, ..., row.names = NA)
\S4method{dbFetch}{ODBCResult}(res, n = -1, ...)

\S4method{dbHasCompleted}{ODBCResult}(res, ...)

\S4method{dbClearResult}{ODBCResult}(res, ...)
}
\arguments{
\item{conn}{An existing \code{\linkS4class{ODBCConnection}}}

\item{statement}{a character vector of length 1 containing SQL}

\item{...}{Other parameters passed on to methods}

\item{res}{Code a \linkS4class{ODBCResult} produced by \code{\link[DBI]{dbSendQuery}}.}

\item{n}{Number of rows to return. If less than zero returns all rows.}
}
\description{
Execute a statement on a given database connection.

To retrieve results a chunk at a time, use \code{dbSendQuery},
\code{dbFetch}, then \code{ClearResult}. Alternatively, if you want all the
results (and they'll fit in memory) use \code{dbGetQuery} which sends,
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library("testthat")
library("RODBCDBI")
test_check("RODBCDBI")
if (identical(Sys.getenv("NOT_CRAN"), "true")){
test_check("RODBCDBI")
}

0 comments on commit ddd23ff

Please sign in to comment.