Skip to content
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

Add user interrupt check while waiting for query results to be ready. #193

Merged
merged 6 commits into from Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
@@ -1,11 +1,12 @@
Package: RPostgres
Title: 'Rcpp' Interface to 'PostgreSQL'
Version: 1.1.1
Date: 2018-05-05
Version: 1.1.2
Date: 2018-08-25
Authors@R: c(
person("Hadley", "Wickham", role = "aut"),
person("Jeroen", "Ooms", role = "aut"),
person("Kirill", "M\u00fcller", role = c("aut", "cre"), email = "krlmlr+r@mailbox.org", comment = c(ORCID = "0000-0002-1416-3412")),
person("Mateusz", "\u017b\u00f3\u0142tak", role = c("ctb")),
Copy link
Member

Choose a reason for hiding this comment

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

In this project, we acknowledge in the NEWS.md file. I'll add an acknowledgment for all contributors to r-dbi repos to the upcoming DBI blog post too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to NEWS.md in d6dcba9

person("RStudio", role = "cph"),
person("R Consortium", role = "fnd"),
person("Tomoaki", "Nishiyama", role = "ctb",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,3 +1,7 @@
# RPostgres 1.1.2 (2018-08-25)

- Add user interrupt check while waiting for query results to be ready. Allows safe interruption of long-running queries.

# RPostgres 1.1.1 (2018-05-05)

- Add support for `bigint` argument to `dbConnect()`, supported values are `"integer64"`, `"integer"`, `"numeric"` and `"character"`. Large integers are returned as values of that type (r-dbi/DBItest#133).
Expand Down
31 changes: 31 additions & 0 deletions src/PqResultImpl.cpp
Expand Up @@ -12,6 +12,7 @@ PqResultImpl::PqResultImpl(DbResult* pRes, PGconn* pConn, const std::string& sql
cache(pSpec_),
complete_(false),
ready_(false),
data_ready_(false),
nrows_(0),
rows_affected_(0),
group_(0),
Expand Down Expand Up @@ -329,6 +330,7 @@ bool PqResultImpl::bind_row() {
PQsendQueryPrepared(pConn_, "", cache.nparams_, &c_params[0],
&lengths[0], &formats[0], 0) :
PQsendQueryPrepared(pConn_, "", 0, NULL, NULL, NULL, 0);
data_ready_ = false;

if (!success)
conn_stop("Failed to send query");
Expand Down Expand Up @@ -379,6 +381,35 @@ bool PqResultImpl::step_run() {
LOG_VERBOSE;

if (pRes_) PQclear(pRes_);

// Check user interrupts while waiting for the data to be ready
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
if (!data_ready_) {
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
int socket, ret;
fd_set input;
timeval timeout = {0, 0};

socket = PQsocket(pConn_);
if (socket < 0) {
stop("Failed to get connection socket");
}
FD_ZERO(&input);
FD_SET(socket, &input);

do {
timeout.tv_sec = 1;
ret = select(socket + 1, &input, NULL, NULL, &timeout);
if (ret == 0) {
checkUserInterrupt();
} else if(ret < 0) {
stop("select() on the connection failed");
}
if (!PQconsumeInput(pConn_)) {
stop("Failed to consume input from the server");
}
} while (PQisBusy(pConn_));
data_ready_ = true;
}

pRes_ = PQgetResult(pConn_);

// We're done, but we need to call PQgetResult until it returns NULL
Expand Down
1 change: 1 addition & 0 deletions src/PqResultImpl.h
Expand Up @@ -37,6 +37,7 @@ class PqResultImpl : boost::noncopyable, public PqResultSource {
// State
bool complete_;
bool ready_;
bool data_ready_;
int nrows_;
int rows_affected_;
List params_;
Expand Down