-
Notifications
You must be signed in to change notification settings - Fork 78
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 Date, POSIXct and hms types #333
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ec7553d
handle DT_DATE, DT_DATETIME and DT_TIME
anderic1 ffb94d5
add global extended_types flag
anderic1 fc7f0e2
add tests
anderic1 b39016b
move hms to suggests
anderic1 b7c8526
add warnings and treat blobs as NA
anderic1 4f22014
improve documentation and extended_types FALSE by default
anderic1 8cfa58f
add tests
anderic1 cf0fbae
Merge branch 'master' into alt_types
anderic1 e29776f
allow enabling globally
anderic1 2a6d520
explicit types instead of auto
anderic1 cf5c01f
use boost string compare
anderic1 4bb3e97
add origin and regenerate documentation
anderic1 c7ffc61
remove possibility to enable globally
anderic1 379bd2a
regenerate documentation
anderic1 f09fb5d
condition on hms being available
anderic1 6d29b55
revert not calling new_hms
anderic1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 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 |
---|---|---|
|
@@ -3,14 +3,27 @@ | |
#include "integer64.h" | ||
#include "affinity.h" | ||
#include <boost/limits.hpp> | ||
#include <boost/date_time/gregorian/gregorian.hpp> | ||
#include <boost/date_time/posix_time/posix_time.hpp> | ||
#include <boost/algorithm/string.hpp> | ||
|
||
SqliteColumnDataSource::SqliteColumnDataSource(sqlite3_stmt* stmt_, const int j_) : | ||
|
||
SqliteColumnDataSource::SqliteColumnDataSource(sqlite3_stmt* stmt_, const int j_, bool with_alt_types_) : | ||
DbColumnDataSource(j_), | ||
stmt(stmt_) | ||
stmt(stmt_), | ||
with_alt_types(with_alt_types_) | ||
{ | ||
} | ||
|
||
DATA_TYPE SqliteColumnDataSource::get_data_type() const { | ||
|
||
if (with_alt_types) { | ||
DATA_TYPE decl_dt = get_decl_data_type(); | ||
if (decl_dt == DT_DATE || decl_dt == DT_DATETIME || decl_dt == DT_TIME) { | ||
return decl_dt; | ||
} | ||
} | ||
|
||
const int field_type = get_column_type(); | ||
switch (field_type) { | ||
case SQLITE_INTEGER: | ||
|
@@ -39,7 +52,7 @@ DATA_TYPE SqliteColumnDataSource::get_data_type() const { | |
} | ||
|
||
DATA_TYPE SqliteColumnDataSource::get_decl_data_type() const { | ||
return datatype_from_decltype(sqlite3_column_decltype(get_stmt(), get_j())); | ||
return datatype_from_decltype(sqlite3_column_decltype(get_stmt(), get_j()), with_alt_types); | ||
} | ||
|
||
bool SqliteColumnDataSource::is_null() const { | ||
|
@@ -80,13 +93,59 @@ SEXP SqliteColumnDataSource::fetch_blob() const { | |
} | ||
|
||
double SqliteColumnDataSource::fetch_date() const { | ||
// No such data type | ||
return 0.0; | ||
|
||
namespace bg = boost::gregorian; | ||
|
||
int dt = get_column_type(); | ||
|
||
if (dt == SQLITE_TEXT) { | ||
const char* dtstr = reinterpret_cast<const char*>(sqlite3_column_text(get_stmt(), get_j())); | ||
double dateval; | ||
|
||
try { | ||
bg::date dt(bg::from_simple_string(dtstr)); | ||
bg::date_duration delta = dt - bg::date(1970, 1, 1); | ||
dateval = static_cast<double>(delta.days()); | ||
} catch (...) { | ||
Rcpp::warning("Unknown string format, NA is returned."); | ||
dateval = NA_REAL; | ||
} | ||
return dateval; | ||
} else if (dt == SQLITE_BLOB) { | ||
Rcpp::warning("Cannot convert blob, NA is returned."); | ||
return NA_REAL; | ||
} else { | ||
return static_cast<double>(sqlite3_column_int(get_stmt(), get_j())); | ||
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. What happens if the underlying data is an integer or a blob? |
||
} | ||
} | ||
|
||
double SqliteColumnDataSource::fetch_datetime_local() const { | ||
// No such data type | ||
return 0.0; | ||
|
||
namespace bp = boost::posix_time; | ||
namespace bg = boost::gregorian; | ||
namespace bd = boost::date_time; | ||
|
||
int dt = get_column_type(); | ||
|
||
if (dt == SQLITE_TEXT) { | ||
const char* dtstr = reinterpret_cast<const char*>(sqlite3_column_text(get_stmt(), get_j())); | ||
double dateval; | ||
try { | ||
bp::ptime dttm(bd::parse_delimited_time<bp::ptime>(dtstr, ' ')); | ||
bp::time_duration delta = dttm - bp::ptime(bg::date(1970, 1, 1), bp::seconds(0)); | ||
dateval = delta.total_microseconds() * 1e-6; | ||
} catch (...) { | ||
Rcpp::warning("Unknown string format, NA is returned."); | ||
dateval = NA_REAL; | ||
} | ||
return dateval; | ||
} else if (dt == SQLITE_BLOB) { | ||
Rcpp::warning("Cannot convert blob, NA is returned."); | ||
return NA_REAL; | ||
} else { | ||
return sqlite3_column_double(get_stmt(), get_j()); | ||
} | ||
|
||
} | ||
|
||
double SqliteColumnDataSource::fetch_datetime() const { | ||
|
@@ -95,15 +154,46 @@ double SqliteColumnDataSource::fetch_datetime() const { | |
} | ||
|
||
double SqliteColumnDataSource::fetch_time() const { | ||
// No such data type | ||
return 0.0; | ||
|
||
|
||
namespace bp = boost::posix_time; | ||
|
||
int dt = get_column_type(); | ||
|
||
if (dt == SQLITE_TEXT) { | ||
const char *tmstr = reinterpret_cast<const char*>(sqlite3_column_text(get_stmt(), get_j())); | ||
double dateval; | ||
try { | ||
bp::time_duration secs(bp::duration_from_string(tmstr)); | ||
dateval = secs.total_microseconds() * 1e-6; | ||
} catch (...) { | ||
Rcpp::warning("Unknown string format, NA is returned."); | ||
dateval = NA_REAL; | ||
} | ||
return dateval; | ||
} else if (dt == SQLITE_BLOB) { | ||
Rcpp::warning("Cannot convert blob, NA is returned."); | ||
return NA_REAL; | ||
} else { | ||
return sqlite3_column_double(get_stmt(), get_j()); | ||
} | ||
} | ||
|
||
|
||
DATA_TYPE SqliteColumnDataSource::datatype_from_decltype(const char* decl_type) { | ||
DATA_TYPE SqliteColumnDataSource::datatype_from_decltype(const char* decl_type, bool with_alt_types) { | ||
if (decl_type == NULL) | ||
return DT_BOOL; | ||
|
||
if (with_alt_types) { | ||
if (boost::iequals(decl_type, "datetime") || boost::iequals(decl_type, "timestamp")) { | ||
return DT_DATETIME; | ||
} else if (boost::iequals(decl_type, "date")) { | ||
return DT_DATE; | ||
} else if (boost::iequals(decl_type, "time")) { | ||
return DT_TIME; | ||
} | ||
} | ||
|
||
char affinity = sqlite3AffinityType(decl_type); | ||
|
||
switch (affinity) { | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does this mean that a declared data type overrides what's stored in the columns?