Skip to content

Commit

Permalink
Use Rcpp automatic inclusion of _types header
Browse files Browse the repository at this point in the history
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
jjallaire committed Feb 3, 2015
1 parent 9a82897 commit 79a6a18
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ before_install:
install:
- ./travis-tool.sh install_github rstats-db/DBI
- ./travis-tool.sh install_github rstats-db/SQL
- ./travis-tool.sh install_r Rcpp
- ./travis-tool.sh install_github RcppCore/Rcpp
- ./travis-tool.sh install_deps
- R CMD INSTALL .

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Suggests:
Imports:
DBI (>= 0.3.1.9004),
SQL,
Rcpp (>= 0.11.4)
Rcpp (>= 0.11.4.2)
License: LGPL (>= 2)
URL: https://github.com/rstats-db/RSQLite
BugReports: https://github.com/rstats-db/RSQLite/issues
Expand Down
6 changes: 0 additions & 6 deletions inst/include/RSQLite.h

This file was deleted.

7 changes: 7 additions & 0 deletions src/RSQLite_types.hpp
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__
2 changes: 1 addition & 1 deletion src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file was generated by Rcpp::compileAttributes
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include "../inst/include/RSQLite.h"
#include "RSQLite_types.hpp"
#include <Rcpp.h>

using namespace Rcpp;
Expand Down
73 changes: 73 additions & 0 deletions src/SqliteConnection.hpp
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__
Loading

3 comments on commit 79a6a18

@jeroen
Copy link
Member

@jeroen jeroen commented on 79a6a18 Feb 3, 2015

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.

@jjallaire
Copy link
Contributor Author

@jjallaire jjallaire commented on 79a6a18 Feb 3, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeroen
Copy link
Member

@jeroen jeroen commented on 79a6a18 Feb 3, 2015

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.

Please sign in to comment.