diff options
author | randomdan <randomdan@localhost> | 2011-02-02 10:49:10 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-02-02 10:49:10 +0000 |
commit | b081a82d9348e3ac77b0a12c9c456f838a693f4a (patch) | |
tree | 39b52ea24ced19f596b047d69eaa9357feb1c28a | |
parent | Fix type of rows counter (diff) | |
download | libdbpp-odbc-b081a82d9348e3ac77b0a12c9c456f838a693f4a.tar.bz2 libdbpp-odbc-b081a82d9348e3ac77b0a12c9c456f838a693f4a.tar.xz libdbpp-odbc-b081a82d9348e3ac77b0a12c9c456f838a693f4a.zip |
Remove compose functions on column data and add a handle function for type safe data passing
Use new handle function to get type safe data from ODBC
Add a datetime option to variables
-rw-r--r-- | libodbcpp/column.cpp | 114 | ||||
-rw-r--r-- | libodbcpp/column.h | 38 |
2 files changed, 39 insertions, 113 deletions
diff --git a/libodbcpp/column.cpp b/libodbcpp/column.cpp index 4d071e1..8caf9c8 100644 --- a/libodbcpp/column.cpp +++ b/libodbcpp/column.cpp @@ -9,15 +9,13 @@ ODBC::Column::Column(SelectCommand * sc, const Glib::ustring & n, unsigned int i) : colNo(i), name(n), - selectCmd(sc), - composeCache(NULL) + selectCmd(sc) { bindLen = 0; } ODBC::Column::~Column() { - delete composeCache; } void @@ -41,8 +39,6 @@ ODBC::CharArrayColumn::resize(SQLHANDLE hStmt) void ODBC::Column::onScroll() { - delete composeCache; - composeCache = NULL; } bool @@ -68,89 +64,6 @@ ODBC::Column::bind() } } -#define SIMPLEFORMATTER(ctype, deffmtstr) \ - int \ - ODBC::ctype::writeToBuf(char ** buf, const char * fmt) const \ - { \ - return asprintf(buf, fmt, data); \ - } \ - int \ - ODBC::ctype::writeToBuf(char ** buf) const \ - { \ - return writeToBuf(buf, deffmtstr); \ - } \ - const Glib::ustring & \ - ODBC::ctype::compose() const \ - { \ - if (!composeCache) { \ - composeCache = new Glib::ustring(Glib::ustring::compose("%1", data)); \ - } \ - return *composeCache; \ - } \ - Glib::ustring \ - ODBC::ctype::compose(const Glib::ustring & fmt) const \ - { \ - return Glib::ustring::compose(fmt, data); \ - } -SIMPLEFORMATTER(FloatingPointColumn, "%g"); -SIMPLEFORMATTER(SignedIntegerColumn, "%ld"); -#ifdef COMPLETENESS -SIMPLEFORMATTER(UnsignedIntegerColumn, "%lu"); -#endif - -int -ODBC::CharArrayColumn::writeToBuf(char ** buf, const char * fmt) const -{ - return asprintf(buf, fmt, &data[0]); -} -int -ODBC::CharArrayColumn::writeToBuf(char ** buf) const -{ - return writeToBuf(buf, "%s"); -} -const Glib::ustring & -ODBC::CharArrayColumn::compose() const -{ - if (!composeCache) { - composeCache = new Glib::ustring(&data.front(), &data[bindLen]); - } - return *composeCache; -} -Glib::ustring -ODBC::CharArrayColumn::compose(const Glib::ustring & fmt) const -{ - return Glib::ustring::compose(fmt, &data.front()); -} -int -ODBC::TimeStampColumn::writeToBuf(char ** buf, const char * fmt) const -{ - *buf = (char *)malloc(300); - struct tm t; - t << data; - return strftime(*buf, 300, fmt, &t); -} -int -ODBC::TimeStampColumn::writeToBuf(char ** buf) const -{ - return writeToBuf(buf, "%F %T"); -} -Glib::ustring -ODBC::TimeStampColumn::compose(const Glib::ustring & fmt) const -{ - char buf[300]; - struct tm t; - t << data; - int len = strftime(buf, sizeof(buf), fmt.c_str(), &t); - return Glib::ustring(buf, len); -} -const Glib::ustring & -ODBC::TimeStampColumn::compose() const -{ - if (!composeCache) { - composeCache = new Glib::ustring(Glib::ustring(compose("%F %T"))); - } - return *composeCache; -} ODBC::TimeStampColumn::operator tm() const { struct tm t; @@ -176,3 +89,28 @@ void operator << (struct tm & target, const SQL_TIMESTAMP_STRUCT & src) target.tm_min = src.minute; target.tm_sec = src.second; } + +void +ODBC::SignedIntegerColumn::apply(ODBC::HandleField & h) const +{ + if (isNull()) return h.null(); + h.integer(data); +} +void +ODBC::FloatingPointColumn::apply(ODBC::HandleField & h) const +{ + if (isNull()) return h.null(); + h.floatingpoint(data); +} +void +ODBC::CharArrayColumn::apply(ODBC::HandleField & h) const +{ + if (isNull()) return h.null(); + h.string(data); +} +void +ODBC::TimeStampColumn::apply(ODBC::HandleField & h) const +{ + if (isNull()) return h.null(); + h.timestamp(data); +} diff --git a/libodbcpp/column.h b/libodbcpp/column.h index 1ea4091..86d4507 100644 --- a/libodbcpp/column.h +++ b/libodbcpp/column.h @@ -8,6 +8,14 @@ namespace ODBC { class SelectCommand; + class HandleField { + public: + virtual void null() = 0; + virtual void string(const std::vector<char> &) = 0; + virtual void integer(SQLINTEGER) = 0; + virtual void floatingpoint(SQLDOUBLE) = 0; + virtual void timestamp(const SQL_TIMESTAMP_STRUCT &) = 0; + }; class Column : public virtual Bind { public: Column(SelectCommand *, const Glib::ustring &, unsigned int); @@ -34,18 +42,14 @@ namespace ODBC { virtual operator struct tm () const { throw std::bad_cast(); } virtual operator SQL_TIMESTAMP_STRUCT () const { throw std::bad_cast(); } - virtual const Glib::ustring & compose() const = 0; - virtual Glib::ustring compose(const Glib::ustring & fmt) const = 0; - virtual int writeToBuf(char ** buf) const = 0; - virtual int writeToBuf(char ** buf, const char * fmt) const = 0; bool isNull() const; + virtual void apply(HandleField &) const = 0; const unsigned int colNo; const Glib::ustring name; const SelectCommand * selectCmd; protected: virtual const Param * meAsAParam() const = 0; - mutable Glib::ustring * composeCache; }; class CharArrayColumn : public Column, public Param { public: @@ -63,12 +67,9 @@ namespace ODBC { virtual void * rwDataAddress() { return &data.front(); } void operator=(const Glib::ustring & d); void resize(SQLHANDLE); - virtual const Glib::ustring & compose() const; - virtual Glib::ustring compose(const Glib::ustring & fmt) const; - virtual int writeToBuf(char ** buf) const; - virtual int writeToBuf(char ** buf, const char * fmt) const; virtual operator std::string () const { return std::string(&data.front(), bindLen); } virtual operator Glib::ustring () const { return std::string(&data.front(), bindLen); } + virtual void apply(HandleField &) const; protected: virtual const Param * meAsAParam() const { return this; } CharArray data; @@ -80,24 +81,17 @@ namespace ODBC { virtual SQLSMALLINT ctype() const { return SignedIntegerParam::ctype(); } virtual SQLULEN size() const { return SignedIntegerParam::size(); } virtual void * rwDataAddress() { return &data; } - virtual const Glib::ustring & compose() const; - virtual Glib::ustring compose(const Glib::ustring & fmt) const; - virtual int writeToBuf(char ** buf) const; - virtual int writeToBuf(char ** buf, const char * fmt) const; virtual operator int () const { return data; } virtual operator long () const { return data; } virtual operator long long () const { return data; } virtual const Param * meAsAParam() const { return this; } + virtual void apply(HandleField &) const; }; #ifdef COMPLETENESS class UnsignedIntegerColumn : public Column, public UnsignedIntegerParam { public: UnsignedIntegerColumn(SelectCommand * sc, const Glib::ustring & n, unsigned int i) : Column(sc, n, i) { } - virtual const Glib::ustring & compose() const; - virtual Glib::ustring compose(const Glib::ustring & fmt) const; - virtual int writeToBuf(char ** buf) const; - virtual int writeToBuf(char ** buf, const char * fmt) const; virtual const Param * meAsAParam() const { return this; } }; #endif @@ -108,13 +102,10 @@ namespace ODBC { virtual SQLSMALLINT ctype() const { return FloatingPointParam::ctype(); } virtual SQLULEN size() const { return FloatingPointParam::size(); } virtual void * rwDataAddress() { return &data; } - virtual const Glib::ustring & compose() const; - virtual Glib::ustring compose(const Glib::ustring & fmt) const; - virtual int writeToBuf(char ** buf) const; - virtual int writeToBuf(char ** buf, const char * fmt) const; virtual operator double () const { return data; } virtual operator float () const { return data; } virtual const Param * meAsAParam() const { return this; } + virtual void apply(HandleField &) const; }; class TimeStampColumn : public Column, public TimeStampParam { public: @@ -123,13 +114,10 @@ namespace ODBC { virtual SQLSMALLINT ctype() const { return TimeStampParam::ctype(); } virtual SQLULEN size() const { return TimeStampParam::size(); } virtual void * rwDataAddress() { return &data; } - virtual const Glib::ustring & compose() const; - virtual Glib::ustring compose(const Glib::ustring & fmt) const; - virtual int writeToBuf(char ** buf) const; - virtual int writeToBuf(char ** buf, const char * fmt) const; virtual operator struct tm () const; virtual operator SQL_TIMESTAMP_STRUCT () const { return data; } virtual const Param * meAsAParam() const { return this; } + virtual void apply(HandleField &) const; }; } |