-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Rcpp automatic inclusion of _types header
We use the fact that Rcpp will now automatically include a specially named _types header in RcppExports.cpp to eliminate inst/include/RSQLite.h. It's contents are now broken up into three files in src: SqliteConnection.hpp, SqliteResult.hpp, and SqliteUtils.hpp
- Loading branch information
Showing
11 changed files
with
239 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef __RSQLSITE_TYPES__ | ||
#define __RSQLSITE_TYPES__ | ||
|
||
#include "SqliteResult.hpp" | ||
#include "SqliteConnection.hpp" | ||
|
||
#endif // __RSQLSITE_TYPES__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifndef __RSQLSITE_SQLITE_CONNECTION__ | ||
#define __RSQLSITE_SQLITE_CONNECTION__ | ||
|
||
#include <Rcpp.h> | ||
#include <boost/noncopyable.hpp> | ||
#include <boost/shared_ptr.hpp> | ||
#include "sqlite3.h" | ||
|
||
#include "SqliteUtils.hpp" | ||
|
||
// Connection ------------------------------------------------------------------ | ||
|
||
// Reference counted wrapper for a sqlite3* connnection which will keep the | ||
// connection alive as long as there are references to this object alive. | ||
|
||
// convenience typedef for shared_ptr to SqliteConnectionWrapper | ||
class SqliteConnectionWrapper; | ||
typedef boost::shared_ptr<SqliteConnectionWrapper> SqliteConnectionPtr; | ||
|
||
class SqliteConnectionWrapper : boost::noncopyable { | ||
public: | ||
// Create a new connection handle | ||
SqliteConnectionWrapper(std::string path, bool allow_ext, | ||
int flags, std::string vfs = "") | ||
: pConn_(NULL) { | ||
|
||
// Get the underlying database connection | ||
int rc = sqlite3_open_v2(path.c_str(), &pConn_, flags, vfs.size() ? vfs.c_str() : NULL); | ||
if (rc != SQLITE_OK) { | ||
Rcpp::stop("Could not connect to database:\n%s", getException()); | ||
} | ||
if (allow_ext) { | ||
sqlite3_enable_load_extension(pConn_, 1); | ||
} | ||
} | ||
|
||
void copy_to(SqliteConnectionPtr pDest) { | ||
sqlite3_backup* backup = sqlite3_backup_init(pDest->conn(), "main", | ||
pConn_, "main"); | ||
|
||
int rc = sqlite3_backup_step(backup, -1); | ||
if (rc != SQLITE_DONE) { | ||
Rcpp::stop("Failed to copy all data:\n%s", getException()); | ||
} | ||
rc = sqlite3_backup_finish(backup); | ||
if (rc != SQLITE_OK) { | ||
Rcpp::stop("Could not finish copy:\n%s", getException()); | ||
} | ||
} | ||
|
||
virtual ~SqliteConnectionWrapper() { | ||
try { | ||
sqlite3_close_v2(pConn_); | ||
} catch(...) {} | ||
} | ||
|
||
|
||
// Get access to the underlying sqlite3* | ||
sqlite3* conn() const { return pConn_; } | ||
|
||
// Get the last exception as a string | ||
std::string getException() const { | ||
if (pConn_ != NULL) | ||
return std::string(sqlite3_errmsg(pConn_)); | ||
else | ||
return std::string(); | ||
} | ||
|
||
private: | ||
sqlite3* pConn_; | ||
}; | ||
|
||
#endif // __RSQLSITE_SQLITE_CONNECTION__ |
Oops, something went wrong.
79a6a18
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.
Cool, is this a new feature in Rcpp? I had a hard time figuring out how to automatically include the type headers in RcppExports.
79a6a18
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.
79a6a18
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.
Cool that makes sense. Much more elegant this way.