-
Notifications
You must be signed in to change notification settings - Fork 110
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
OdbcResult: Ability to explicitely describe parameters #863
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -126,17 +126,54 @@ setMethod("dbGetRowsAffected", "OdbcResult", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' @param param_description A data.frame with per-parameter attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' overrides. Argument is optional; if used it must have columns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' * param_index Index of parameter in query ( beginning with 1 ). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' * data_type Integer corresponding to the parameter SQL Data Type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' See \code{\link{ODBC_TYPE}}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' * column_size Size of parameter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' * decimal_digits Either precision or the scale of the parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' depending on type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+129
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' For more information see the [ODBC SQLBindParam documentation](https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlbindparameter-function?view=sql-server-ver16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' @examples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' \dontrun{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' library(odbc) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' # Writing UNICODE into a VARCHAR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' # column with SQL server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' DBI::dbRemoveTable(conn, "#tmp") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' dbExecute(conn, "CREATE TABLE #tmp (col1 VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8);") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' res <- dbSendQuery(conn, "INSERT INTO #tmp SELECT ? COLLATE Latin1_General_100_CI_AI_SC_UTF8") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' description <- data.frame(param_index = 1, data_type = ODBC_TYPE("WVARCHAR"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' column_size = 5, decimal_digits = NA_integer_) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' dbBind(res, params = list("\u2915"), param_description = description) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' dbClearResult(res) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' DBI::dbReadTable(conn, "#tmp") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+140
to
+153
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Some style edits I'd make:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' @rdname OdbcResult | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' @inheritParams DBI::dbBind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' @inheritParams DBI-tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#' @export | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setMethod("dbBind", "OdbcResult", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
function(res, params, ..., batch_rows = getOption("odbc.batch_rows", NA)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
function(res, params, ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
param_description = data.frame(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
batch_rows = getOption("odbc.batch_rows", NA)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params <- as.list(params) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (length(params) == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return(invisible(res)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (nrow(param_description)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Aligning with above. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!all(c("param_index", "data_type", "column_size", "decimal_digits") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
%in% colnames(param_description))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cli::cli_abort( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"param_description data.frame does not have necessary columns." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+168
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result_describe_parameters(res@ptr, param_description) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (is.na(batch_rows)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
batch_rows <- length(params[[1]]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <unordered_map> | ||
#include <string> | ||
#include <sql.h> | ||
#include <sqlext.h> | ||
#include <sqlucode.h> | ||
#include "utils.h" | ||
#define INSTANTIATE_SQL_DATA_TYPE(TYPE) _the_map[#TYPE] = SQL_##TYPE; | ||
|
||
using namespace odbc; | ||
const std::unordered_map< std::string, int >& generateOdbcTypes() { | ||
static std::unordered_map< std::string, int > _the_map; | ||
INSTANTIATE_SQL_DATA_TYPE(CHAR); | ||
INSTANTIATE_SQL_DATA_TYPE(VARCHAR) | ||
INSTANTIATE_SQL_DATA_TYPE(LONGVARCHAR); | ||
|
||
INSTANTIATE_SQL_DATA_TYPE(WCHAR); | ||
INSTANTIATE_SQL_DATA_TYPE(WVARCHAR); | ||
INSTANTIATE_SQL_DATA_TYPE(WLONGVARCHAR); | ||
|
||
INSTANTIATE_SQL_DATA_TYPE(DECIMAL); | ||
INSTANTIATE_SQL_DATA_TYPE(NUMERIC); | ||
INSTANTIATE_SQL_DATA_TYPE(SMALLINT); | ||
INSTANTIATE_SQL_DATA_TYPE(INTEGER); | ||
INSTANTIATE_SQL_DATA_TYPE(REAL); | ||
INSTANTIATE_SQL_DATA_TYPE(FLOAT); | ||
INSTANTIATE_SQL_DATA_TYPE(DOUBLE); | ||
|
||
INSTANTIATE_SQL_DATA_TYPE(BIT); | ||
INSTANTIATE_SQL_DATA_TYPE(TINYINT); | ||
INSTANTIATE_SQL_DATA_TYPE(BIGINT); | ||
|
||
INSTANTIATE_SQL_DATA_TYPE(BINARY); | ||
INSTANTIATE_SQL_DATA_TYPE(VARBINARY); | ||
INSTANTIATE_SQL_DATA_TYPE(LONGVARBINARY); | ||
|
||
INSTANTIATE_SQL_DATA_TYPE(TYPE_DATE); | ||
INSTANTIATE_SQL_DATA_TYPE(TYPE_TIME); | ||
INSTANTIATE_SQL_DATA_TYPE(TYPE_TIMESTAMP); | ||
return _the_map; | ||
} | ||
|
||
//' Access the definition of the named SQL Data Type. | ||
//' | ||
//' @description Helper method returning the integer of the | ||
//' [SQL Data Type](https://learn.microsoft.com/en-us/sql/odbc/reference/appendixes/sql-data-types?view=sql-server-ver16) | ||
//' named in the argument. | ||
//' | ||
//' @seealso <https://github.com/microsoft/ODBC-Specification/blob/master/Windows/inc/sql.h> | ||
//' @rdname odbcDataType. | ||
//' @examples | ||
//' \dontrun{ | ||
//' library(odbc) | ||
//' ODBC_TYPE("LONGVARCHAR") | ||
//' } | ||
//' @export | ||
// [[Rcpp::export]] | ||
int ODBC_TYPE(const std::string& type) { | ||
static const std::unordered_map< std::string, int >& map = | ||
generateOdbcTypes(); | ||
auto itr = map.find(type); | ||
if (itr != map.end()) | ||
{ | ||
return (int)itr->second; | ||
} | ||
utils::raise_error("Could not map input string to ODBC type"); | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, just to situate more comfily in this package's namespace, this may be better formatted as: