From c3da11f658e572d19ad00d6c5e6ff8ecb884bf66 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 2 May 2010 12:44:03 +0000 Subject: Ditch crazy timetypepair and just use an ODBC struct --- libodbcpp/column.cpp | 43 +++++++++++++++++++++++--------- libodbcpp/column.h | 5 +++- libodbcpp/command.h | 7 +++--- libodbcpp/makefile.in | 1 - libodbcpp/param.cpp | 32 +++++++++++++++--------- libodbcpp/selectcommand.cpp | 8 +++--- libodbcpp/timetypepair.cpp | 60 --------------------------------------------- libodbcpp/timetypepair.h | 30 ----------------------- 8 files changed, 63 insertions(+), 123 deletions(-) delete mode 100644 libodbcpp/timetypepair.cpp delete mode 100644 libodbcpp/timetypepair.h (limited to 'libodbcpp') diff --git a/libodbcpp/column.cpp b/libodbcpp/column.cpp index 14b12f8..85022eb 100644 --- a/libodbcpp/column.cpp +++ b/libodbcpp/column.cpp @@ -3,7 +3,6 @@ #include "column.h" #include "command.h" #include "error.h" -#include "timetypepair.h" ODBC::Column::Column(String n, unsigned int i) : colNo(i), @@ -35,13 +34,11 @@ ODBC::Column::operator std::string() const { ODBC::Column::operator const char * () const { return (const char*)((dynamic_cast& >(*this)).value); } -ODBC::Column::operator const struct tm & () const { - const _Column& c = dynamic_cast& >(*this); - if (c.fresh) { - c.value.sql2c(); - c.fresh = false; - } - return c.value.c(); +ODBC::Column::operator struct tm () const { + const _Column& c = dynamic_cast& >(*this); + struct tm rtn; + rtn << c.value; + return rtn; } void @@ -67,7 +64,7 @@ namespace ODBC { REBIND(long long unsigned int, bindParamI) REBIND(double, bindParamF) REBIND(float, bindParamF) - REBIND(TimeTypePair, bindParamT) + REBIND(SQL_TIMESTAMP_STRUCT, bindParamT) REBIND(unsigned char *, bindParamS) template <> @@ -108,15 +105,37 @@ namespace ODBC { } template <> int - _Column::writeToBuf(char ** buf, const char * fmt) const + _Column::writeToBuf(char ** buf, const char * fmt) const { *buf = (char *)malloc(30); - return strftime(*buf, sizeof(buf), fmt, &value.c()); + struct tm t; + t << value; + return strftime(*buf, 30, fmt, &t); } template <> int - _Column::writeToBuf(char ** buf) const + _Column::writeToBuf(char ** buf) const { return writeToBuf(buf, "%F %T"); } } + +void operator << (SQL_TIMESTAMP_STRUCT & target, const struct tm & src) +{ + target.year = src.tm_year + 1900; + target.month = src.tm_mon + 1; + target.day = src.tm_mday; + target.hour = src.tm_hour; + target.minute = src.tm_min; + target.second = src.tm_sec; + target.fraction = 0; +} +void operator << (struct tm & target, const SQL_TIMESTAMP_STRUCT & src) +{ + target.tm_year = src.year - 1900; + target.tm_mon = src.month - 1; + target.tm_mday = src.day; + target.tm_hour = src.hour; + target.tm_min = src.minute; + target.tm_sec = src.second; +} diff --git a/libodbcpp/column.h b/libodbcpp/column.h index 616ca2d..992a958 100644 --- a/libodbcpp/column.h +++ b/libodbcpp/column.h @@ -20,7 +20,7 @@ namespace ODBC { operator const char * () const; operator std::string () const; operator String () const; - operator const struct tm & () const; + operator struct tm () const; virtual void rebind(Command *, unsigned int col) const = 0; virtual int writeToBuf(char ** buf) const = 0; virtual int writeToBuf(char ** buf, const char * fmt) const = 0; @@ -42,5 +42,8 @@ namespace ODBC { }; } +void operator << (SQL_TIMESTAMP_STRUCT & target, const struct tm &); +void operator << (struct tm &, const SQL_TIMESTAMP_STRUCT & target); + #endif diff --git a/libodbcpp/command.h b/libodbcpp/command.h index 8ef7249..8ea24d4 100644 --- a/libodbcpp/command.h +++ b/libodbcpp/command.h @@ -3,7 +3,6 @@ #include #include "connection.h" -#include "timetypepair.h" namespace ODBC { class Param; @@ -23,11 +22,11 @@ namespace ODBC { void bindParamS(unsigned int i, const char *); void bindParamS(unsigned int i, const unsigned char *); void bindParamS(unsigned int i, const unsigned char *, size_t); - void bindParamS(unsigned int i, std::string); - void bindParamS(unsigned int i, String); + void bindParamS(unsigned int i, const std::string &); + void bindParamS(unsigned int i, const String &); void bindParamT(unsigned int i, const struct tm *); + void bindParamT(unsigned int i, const SQL_TIMESTAMP_STRUCT &); void bindParamT(unsigned int i, time_t); - void bindParamT(unsigned int i, const TimeTypePair & p); const String sql; protected: diff --git a/libodbcpp/makefile.in b/libodbcpp/makefile.in index fc4c341..f98cdc9 100644 --- a/libodbcpp/makefile.in +++ b/libodbcpp/makefile.in @@ -11,7 +11,6 @@ OBJS = \ bind.o \ param.o \ column.o \ - timetypepair.o \ modifycommand.o \ selectcommand.o diff --git a/libodbcpp/param.cpp b/libodbcpp/param.cpp index 53b55b5..fb839c2 100644 --- a/libodbcpp/param.cpp +++ b/libodbcpp/param.cpp @@ -1,6 +1,7 @@ #include #include "param.h" #include "command.h" +#include "column.h" #include "error.h" #include @@ -125,14 +126,28 @@ ODBC::Command::bindParamS(unsigned int i, const unsigned char * val, size_t leng throw Error("%s: Bind out of bounds", __FUNCTION__); } void -ODBC::Command::bindParamT(unsigned int i, const TimeTypePair & val) +ODBC::Command::bindParamT(unsigned int i, const struct tm * val) +{ + if (i < params.size()) { + _Param* p = Param::makeParam(params[i]); + p->value << *val; + if (!p->bound) { + p->bind(this->hStmt, i + 1, SQL_C_TIMESTAMP, SQL_TYPE_TIMESTAMP, + sizeof(SQL_TIMESTAMP_STRUCT), 0, &p->value, sizeof(SQL_TIMESTAMP_STRUCT)); + } + return; + } + throw Error("%s: Bind out of bounds", __FUNCTION__); +} +void +ODBC::Command::bindParamT(unsigned int i, const SQL_TIMESTAMP_STRUCT & val) { if (i < params.size()) { - _Param* p = Param::makeParam(params[i]); + _Param* p = Param::makeParam(params[i]); p->value = val; if (!p->bound) { - p->bind(this->hStmt, i + 1, SQL_C_TYPE_TIMESTAMP, SQL_TYPE_TIMESTAMP, - sizeof(SQL_TIMESTAMP_STRUCT), 0, &p->value.sql(), sizeof(SQL_TIMESTAMP_STRUCT)); + p->bind(this->hStmt, i + 1, SQL_C_TIMESTAMP, SQL_TIMESTAMP, + sizeof(SQL_TIMESTAMP_STRUCT), 0, &p->value, sizeof(SQL_TIMESTAMP_STRUCT)); } return; } @@ -154,7 +169,7 @@ ODBC::Command::bindParamI(unsigned int i, unsigned int val) bindParamI(i, (long long unsigned int)val); } void -ODBC::Command::bindParamS(unsigned int i, String val) +ODBC::Command::bindParamS(unsigned int i, const String & val) { bindParamS(i, val.c_str(), val.size()); } @@ -171,7 +186,7 @@ ODBC::Command::bindParamS(unsigned int i, const char * val) bindParamS(i, (unsigned char *)val, strlen(val)); } void -ODBC::Command::bindParamS(unsigned int i, std::string val) +ODBC::Command::bindParamS(unsigned int i, const std::string & val) { bindParamS(i, (unsigned char *)val.c_str(), val.size()); } @@ -187,9 +202,4 @@ ODBC::Command::bindParamT(unsigned int i, time_t val) gmtime_r(&val, &t); bindParamT(i, &t); } -void -ODBC::Command::bindParamT(unsigned int i, const struct tm * val) -{ - bindParamT(i, TimeTypePair(*val)); -} diff --git a/libodbcpp/selectcommand.cpp b/libodbcpp/selectcommand.cpp index 92b4077..025141a 100644 --- a/libodbcpp/selectcommand.cpp +++ b/libodbcpp/selectcommand.cpp @@ -2,6 +2,7 @@ #include "error.h" #include "column.h" #include +#include ODBC::SelectCommand::SelectCommand(const Connection& c, String s) : Command(c, s) @@ -110,15 +111,14 @@ ODBC::SelectCommand::execute() columns[col] = i; break; } - case 11: + case SQL_TIMESTAMP: case SQL_DATETIME: case SQL_TYPE_TIME: case SQL_TYPE_DATE: case SQL_TYPE_TIMESTAMP: { - _Column* t = new _Column(colName, col); - t->bind(hStmt, sqlcol, SQL_C_TYPE_TIMESTAMP, &t->value.sql(), - sizeof(SQL_TIMESTAMP_STRUCT)); + _Column* t = new _Column(colName, col); + t->bind(hStmt, sqlcol, SQL_C_TIMESTAMP, &t->value, sizeof(SQL_TIMESTAMP_STRUCT)); columns[col] = t; break; } diff --git a/libodbcpp/timetypepair.cpp b/libodbcpp/timetypepair.cpp deleted file mode 100644 index 55373bb..0000000 --- a/libodbcpp/timetypepair.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "command.h" -#include - -ODBC::TimeTypePair::TimeTypePair() -{ - memset(&_c, 0, sizeof(_c)); - memset(&_sql, 0, sizeof(_sql)); -} -ODBC::TimeTypePair::TimeTypePair(const ODBC::TimeTypePair::SQL_TS& t) -{ - memset(&_c, 0, sizeof(_c)); - memset(&_sql, 0, sizeof(_sql)); - set(t); -} -ODBC::TimeTypePair::TimeTypePair(tm const& t) -{ - memset(&_c, 0, sizeof(_c)); - memset(&_sql, 0, sizeof(_sql)); - set(t); -} - -tm const& -ODBC::TimeTypePair::set(const ODBC::TimeTypePair::SQL_TS& t) -{ - _sql = t; - sql2c(); - return _c; -} - -void -ODBC::TimeTypePair::sql2c() const -{ - _c.tm_year = _sql.year - 1900; - _c.tm_mon = _sql.month - 1; - _c.tm_mday = _sql.day; - _c.tm_hour = _sql.hour; - _c.tm_min = _sql.minute; - _c.tm_sec = _sql.second; -} - -const ODBC::TimeTypePair::SQL_TS& -ODBC::TimeTypePair::set(tm const& t) -{ - _c = t; - c2sql(); - return _sql; -} - -void -ODBC::TimeTypePair::c2sql() const -{ - _sql.year = _c.tm_year + 1900; - _sql.month = _c.tm_mon + 1; - _sql.day = _c.tm_mday; - _sql.hour = _c.tm_hour; - _sql.minute = _c.tm_min; - _sql.second = _c.tm_sec; - _sql.fraction = 0; -} - diff --git a/libodbcpp/timetypepair.h b/libodbcpp/timetypepair.h deleted file mode 100644 index af77c0f..0000000 --- a/libodbcpp/timetypepair.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef ODBC_TIMETYPEPAIR_H -#define ODBC_TIMETYPEPAIR_H - -#include -#include - -namespace ODBC { - class TimeTypePair { - typedef SQL_TIMESTAMP_STRUCT SQL_TS; - public: - TimeTypePair (); - TimeTypePair (const tm&); - TimeTypePair (const SQL_TS&); - - const SQL_TS& set(const tm&); - const tm& set(const SQL_TS&); - SQL_TS& sql() { return _sql; } - tm& c() { return _c; } - const SQL_TS& sql() const { return _sql; } - const tm& c() const { return _c; } - void sql2c() const; - void c2sql() const; - private: - mutable SQL_TS _sql; - mutable tm _c; - }; -}; - -#endif - -- cgit v1.2.3