From 274b757c0626abcda3a0414451fc4d1b2bc766f9 Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 3 Mar 2014 20:57:27 +0000 Subject: Adds native support for time_duration as a variable type Pass/retrieve boost::posix_time ptime and time_duration into/out of the db tier --- libmysqlpp/column.cpp | 24 +++++++++++++++--------- libmysqlpp/command.cpp | 28 +++++++++++++++++----------- libmysqlpp/command.h | 4 ++-- libmysqlpp/selectcommand.cpp | 4 +++- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/libmysqlpp/column.cpp b/libmysqlpp/column.cpp index cd1d811..7ff365a 100644 --- a/libmysqlpp/column.cpp +++ b/libmysqlpp/column.cpp @@ -2,6 +2,7 @@ #include "selectcommand.h" #include "error.h" #include +#include MySQL::ColumnBase::ColumnBase(const char * name, unsigned int i) : DB::Column(name, i) @@ -94,19 +95,24 @@ namespace MySQL { h.null(); } else { - struct tm tm; - memset(&tm, 0, sizeof(tm)); - tm.tm_year = value.year - 1900; - tm.tm_mon = value.month - 1; - tm.tm_mday = value.day; - tm.tm_hour = value.hour; - tm.tm_min = value.minute; - tm.tm_sec = value.second; - h.timestamp(tm); + h.timestamp(boost::posix_time::ptime( + boost::gregorian::date(value.year, value.month, value.day), + boost::posix_time::time_duration(value.hour, value.minute, value.second) + boost::posix_time::microseconds(value.second_part))); + } + } + template <> void Column::apply(DB::HandleField & h) const + { + if (is_null) { + h.null(); + } + else { + h.interval( + boost::posix_time::time_duration(value.hour, value.minute, value.second) + boost::posix_time::microseconds(value.second_part)); } } template class Column; template class Column; template class Column; + template class Column; } diff --git a/libmysqlpp/command.cpp b/libmysqlpp/command.cpp index 3551ca7..04628ec 100644 --- a/libmysqlpp/command.cpp +++ b/libmysqlpp/command.cpp @@ -115,25 +115,31 @@ MySQL::Command::bindParamS(unsigned int n, const Glib::ustring & s) binds[n].is_unsigned = 0; } void -MySQL::Command::bindParamT(unsigned int n, const tm * v) +MySQL::Command::bindParamT(unsigned int n, const boost::posix_time::ptime & v) { binds[n].buffer_type = MYSQL_TYPE_DATETIME; binds[n].buffer = realloc(binds[n].buffer, sizeof(MYSQL_TIME)); MYSQL_TIME & ts = *static_cast(binds[n].buffer); - ts.year = v->tm_year + 1900; - ts.month = v->tm_mon + 1; - ts.day = v->tm_mday; - ts.hour = v->tm_hour; - ts.minute = v->tm_min; - ts.second = v->tm_sec; + ts.year = v.date().year(); + ts.month = v.date().month(); + ts.day = v.date().day(); + ts.hour = v.time_of_day().hours(); + ts.minute = v.time_of_day().minutes(); + ts.second = v.time_of_day().seconds(); + ts.second_part = v.time_of_day().total_microseconds() % 1000000; ts.neg = 0; } void -MySQL::Command::bindParamT(unsigned int n, time_t v) +MySQL::Command::bindParamT(unsigned int n, const boost::posix_time::time_duration & v) { - struct tm t; - gmtime_r(&v, &t); - bindParamT(n, &t); + binds[n].buffer_type = MYSQL_TYPE_TIME; + binds[n].buffer = realloc(binds[n].buffer, sizeof(MYSQL_TIME)); + MYSQL_TIME & ts = *static_cast(binds[n].buffer); + ts.hour = v.hours(); + ts.minute = v.minutes(); + ts.second = v.seconds(); + ts.second_part = v.total_microseconds() % 1000000; + ts.neg = (v.is_negative() ? 1 : 0); } void MySQL::Command::bindNull(unsigned int n) diff --git a/libmysqlpp/command.h b/libmysqlpp/command.h index 19a1323..21a2e09 100644 --- a/libmysqlpp/command.h +++ b/libmysqlpp/command.h @@ -24,8 +24,8 @@ namespace MySQL { void bindParamS(unsigned int, const Glib::ustring&); - void bindParamT(unsigned int, const tm*); - void bindParamT(unsigned int, time_t); + void bindParamT(unsigned int, const boost::posix_time::time_duration &); + void bindParamT(unsigned int, const boost::posix_time::ptime &); void bindNull(unsigned int); protected: diff --git a/libmysqlpp/selectcommand.cpp b/libmysqlpp/selectcommand.cpp index e6bf789..b361fb1 100644 --- a/libmysqlpp/selectcommand.cpp +++ b/libmysqlpp/selectcommand.cpp @@ -44,6 +44,9 @@ MySQL::SelectCommand::execute() case MYSQL_TYPE_DATETIME: columns.insert(boost::shared_ptr(new Column(fieldDefs[i].name, i, &fields[i]))); break; + case MYSQL_TYPE_TIME: + columns.insert(boost::shared_ptr(new Column(fieldDefs[i].name, i, &fields[i]))); + break; case MYSQL_TYPE_STRING: case MYSQL_TYPE_VAR_STRING: columns.insert(boost::shared_ptr(new StringColumn(fieldDefs[i].name, i, &fields[i], fieldDefs[i].length))); @@ -56,7 +59,6 @@ MySQL::SelectCommand::execute() case MYSQL_TYPE_SET: case MYSQL_TYPE_ENUM: case MYSQL_TYPE_GEOMETRY: - case MYSQL_TYPE_TIME: default: mysql_free_result(prepare_meta_result); throw Error("Unexpected type"); -- cgit v1.2.3