Skip to content
This repository has been archived by the owner on Apr 14, 2018. It is now read-only.

Commit

Permalink
Getting compute() to work as part of #4
Browse files Browse the repository at this point in the history
  • Loading branch information
imanuelcostigan committed Oct 17, 2014
1 parent 609f9f6 commit c83ae9b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions R/DBConnection.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ setMethod("dbSendQuery",
statement)
} else
{

s <- .jcall(conn@jc, "Ljava/sql/Statement;", "createStatement")
.verify.JDBC.result(s, "Unable to create simple JDBC statement ",
statement)
Expand Down
27 changes: 23 additions & 4 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ tbl.src_sqlserver <- function (src, from, ...)
dplyr::tbl_sql("sqlserver", src = src, from = from, ...)

#' @export
head.tbl_sqlserver <- function(x, n = 6L, ...) {
head.tbl_sqlserver <- function (x, n = 6L, ...) {
assertthat::assert_that(length(n) == 1, n > 0L)
build_query(x)$fetch(n)
}

#' @export
compute.tbl_sqlserver <- function (x, name = dplyr:::random_table_name(),
temporary = TRUE, ...)
{
name <- paste0(if (temporary) sql("#"), name)
db_save_query(x$src$con, x$query$sql, name = name, temporary = temporary)
update(dplyr::tbl(x$src, name), group_by = dplyr::groups(x))
}


# DB backend methods ------------------------------------------------------------------
db_list_tables.SQLServerConnection <- function (con)
Expand All @@ -55,17 +64,27 @@ db_query_fields.SQLServerConnection <- function (con, sql, ...)
db_query_rows.SQLServerConnection <- function(con, sql, ...)
{
qry <- build_sql(sql, con = con)
res <- dbGetQuery(con, qry)
dbSendQuery(con, qry)
qry <- build_sql("SELECT @@ROWCOUNT")
res <- dbGetQuery(con, qry)
as.integer(res)
as.integer(dbGetQuery(con, qry))
}

db_save_query.SQLServerConnection <- function (con, sql, name, temporary = TRUE,
...)
{
tt_sql <- build_sql("SELECT * ", "INTO ", ident(name),
" FROM (", sql, ") AS MASTER")
js <- J(con@jc, "createStatement")
J(js, "execute", as.character(tt_sql)[1])
name
}

db_explain.SQLServerConnection <- function (con, sql, ...)
{
stop ('SQL Server does not provide an explain statement.', call. = FALSE)
# Though may be possible to use SHOWPLAN
# http://msdn.microsoft.com/en-us/library/ms187735.aspx
# Maybe use same strategy as db_query_rows
}

# SQL backend methods --------------------------------------------------------------
Expand Down

0 comments on commit c83ae9b

Please sign in to comment.