SEXP dbExistsTable(SEXP dbi_conn_sexp, SEXP tableName_sexp) { SEXP ans; if(TYPEOF(dbi_conn_sexp) != EXTPTRSXP || dbi_conn_sexp == R_NilValue) { return R_NilValue; } DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp)); if(!conn) { // throw bad_connection_object REprintf("bad database connection.\n"); return R_NilValue; } const char* tableName = CHAR(STRING_ELT(tableName_sexp,0)); PROTECT(ans = allocVector(LGLSXP,1)); LOGICAL(ans)[0] = static_cast<int>(conn->existsTable(tableName)); UNPROTECT(1); return ans; }
// return number of rows written to database // using both overwrite and append is a bad design decision // there should just be overwrite, which will drop the table if it exists // append should be automatic if the table exists, and should fail if the row formats don't match up // having both overwrite and append just complicates the logic of this function SEXP dbWriteTable(SEXP dbi_conn_sexp, SEXP tableName_sexp, SEXP value_sexp, SEXP writeRowNames_sexp, SEXP overWrite_sexp, SEXP append_sexp) { SEXP ans; int rows; if(TYPEOF(dbi_conn_sexp) != EXTPTRSXP || dbi_conn_sexp == R_NilValue) { return R_NilValue; } DatabaseConnection* conn = reinterpret_cast<DatabaseConnection*>(R_ExternalPtrAddr(dbi_conn_sexp)); if(!conn) { // throw bad_connection_object REprintf("bad database connection.\n"); return ScalarInteger(0); } if(TYPEOF(tableName_sexp) != STRSXP) { REprintf("ERROR: tableName is not a string.\n"); return ScalarInteger(0); } const char* tableName = CHAR(STRING_ELT(tableName_sexp,0)); const bool writeRowNames = static_cast<bool>(LOGICAL(writeRowNames_sexp)[0]); const bool overWrite = static_cast<bool>(LOGICAL(overWrite_sexp)[0]); if(conn->existsTable(tableName) && overWrite) { if(!conn->removeTable(tableName)) { REprintf("could not remove existing table (aborting).\n"); return ScalarInteger(0); } } try { rows = conn->writeTable(tableName, value_sexp, writeRowNames); } catch (MapToTypeNotImplemented& e) { REprintf("%s\n",e.what()); return R_NilValue; } PROTECT(ans = allocVector(INTSXP,1)); INTEGER(ans)[0] = rows; UNPROTECT(1); return ans; }