From 685587ddb7af3a943a1dd5837fcfeeb779cf15b1 Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 30 Aug 2010 17:34:02 +0000 Subject: Remove lots of overkill variations of string stuff and have a vector deal with the memory --- libodbcpp/bind.cpp | 7 ------- libodbcpp/bind.h | 10 ++++++---- libodbcpp/column.cpp | 23 ++++++++++++----------- libodbcpp/command.h | 6 +++--- libodbcpp/param.cpp | 26 ++------------------------ libodbcpp/selectcommand.cpp | 6 +++--- 6 files changed, 26 insertions(+), 52 deletions(-) diff --git a/libodbcpp/bind.cpp b/libodbcpp/bind.cpp index 2e40975..07e9d35 100644 --- a/libodbcpp/bind.cpp +++ b/libodbcpp/bind.cpp @@ -7,11 +7,4 @@ ODBC::BindBase::BindBase() : { } -ODBC::Bind::~Bind() -{ - if (value) { - delete[] value; - } -} - diff --git a/libodbcpp/bind.h b/libodbcpp/bind.h index 7dfbf34..5baa53a 100644 --- a/libodbcpp/bind.h +++ b/libodbcpp/bind.h @@ -2,6 +2,7 @@ #define ODBC_BIND_H #include +#include namespace ODBC { class Command; @@ -19,13 +20,14 @@ namespace ODBC { class Bind { public: virtual ~Bind() {} - t value; + mutable t value; }; + typedef std::vector SQLCHARVEC; template <> - class Bind { + class Bind { public: - virtual ~Bind(); - unsigned char * value; + virtual ~Bind() {} + mutable SQLCHARVEC value; }; } diff --git a/libodbcpp/column.cpp b/libodbcpp/column.cpp index 8237535..6e517a6 100644 --- a/libodbcpp/column.cpp +++ b/libodbcpp/column.cpp @@ -34,7 +34,6 @@ ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, long long); ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, int); ODBC_DEFAULT_COLUMN_CAST(SQLDOUBLE, double); ODBC_DEFAULT_COLUMN_CAST(SQLDOUBLE, float); -ODBC_DEFAULT_COLUMN_CAST(SQLCHAR*, const unsigned char *); ODBC::Column::operator Glib::ustring() const { return Glib::ustring((const char *)((dynamic_cast& >(*this)).value)); } @@ -75,11 +74,13 @@ namespace ODBC { REBIND(double, bindParamF) REBIND(float, bindParamF) REBIND(SQL_TIMESTAMP_STRUCT, bindParamT) - template<> void _Column::rebind(Command * cmd, unsigned int col) const \ + + template<> + void + _Column::rebind(Command * cmd, unsigned int col) const \ { - cmd->bindParamS(col, (const char *)value); + cmd->bindParamS(col, &value[0]); } - template <> int _Column::writeToBuf(char ** buf, const char * fmt) const @@ -136,30 +137,30 @@ namespace ODBC { } template <> int - _Column::writeToBuf(char ** buf, const char * fmt) const + _Column::writeToBuf(char ** buf, const char * fmt) const { - return asprintf(buf, fmt, value); + return asprintf(buf, fmt, &value[0]); } template <> int - _Column::writeToBuf(char ** buf) const + _Column::writeToBuf(char ** buf) const { return writeToBuf(buf, "%s"); } template <> const Glib::ustring & - _Column::compose() const + _Column::compose() const { if (!composeCache) { - composeCache = new Glib::ustring((const char *)value); + composeCache = new Glib::ustring((const char *)&value[0]); } return *composeCache; } template <> Glib::ustring - _Column::compose(const Glib::ustring & fmt) const + _Column::compose(const Glib::ustring & fmt) const { - return Glib::ustring::compose(fmt, value); + return Glib::ustring::compose(fmt, &value[0]); } template <> int diff --git a/libodbcpp/command.h b/libodbcpp/command.h index 1348441..830f573 100644 --- a/libodbcpp/command.h +++ b/libodbcpp/command.h @@ -18,12 +18,12 @@ namespace ODBC { void bindParamI(unsigned int i, unsigned int val); void bindParamI(unsigned int i, long unsigned int val); void bindParamI(unsigned int i, long long unsigned int val); + void bindParamF(unsigned int i, double val); void bindParamF(unsigned int i, float val); - void bindParamS(unsigned int i, const char *); - void bindParamS(unsigned int i, const char *, size_t); - void bindParamS(unsigned int i, const std::string &); + void bindParamS(unsigned int i, const Glib::ustring &); + void bindParamT(unsigned int i, const struct tm *); void bindParamT(unsigned int i, const SQL_TIMESTAMP_STRUCT &); void bindParamT(unsigned int i, time_t); diff --git a/libodbcpp/param.cpp b/libodbcpp/param.cpp index c4d2223..ff10d3a 100644 --- a/libodbcpp/param.cpp +++ b/libodbcpp/param.cpp @@ -31,10 +31,10 @@ ODBC::Param::makeParam(ODBC::Param*& p) void ODBC::Param::bind(SQLHANDLE hStmt, SQLUINTEGER col, SQLSMALLINT ctype, SQLSMALLINT stype, - SQLINTEGER colsize, SQLINTEGER dp, const void* value, size_t buflen) + SQLINTEGER colsize, SQLINTEGER dp, const void * value, size_t buflen) { RETCODE rc = SQLBindParameter(hStmt, col, SQL_PARAM_INPUT, ctype, stype, - colsize, dp, (void*)value, buflen, &bindLen); + colsize, dp, const_cast(value), buflen, &bindLen); if (rc != SQL_SUCCESS) { throw Error(rc, SQL_HANDLE_STMT, hStmt, "%s: Bind for column %lu", __FUNCTION__, col); @@ -114,18 +114,6 @@ ODBC::Command::bindParamS(unsigned int i, const Glib::ustring & val) throw Error("%s: Bind out of bounds", __FUNCTION__); } void -ODBC::Command::bindParamS(unsigned int i, const char * val, size_t len) -{ - if (i < params.size()) { - _Param* p = Param::makeParam(params[i]); - p->value.assign(val); - p->bindLen = len; - p->bind(this->hStmt, i + 1, SQL_C_CHAR, SQL_CHAR, 0, 0, p->value.data(), p->value.bytes()); - return; - } - throw Error("%s: Bind out of bounds", __FUNCTION__); -} -void ODBC::Command::bindParamT(unsigned int i, const struct tm * val) { if (i < params.size()) { @@ -171,16 +159,6 @@ ODBC::Command::bindParamI(unsigned int i, unsigned int val) bindParamI(i, (long long unsigned int)val); } void -ODBC::Command::bindParamS(unsigned int i, const char * val) -{ - bindParamS(i, val, strlen(val)); -} -void -ODBC::Command::bindParamS(unsigned int i, const std::string & val) -{ - bindParamS(i, val.c_str(), val.length()); -} -void ODBC::Command::bindParamF(unsigned int i, float val) { bindParamF(i, (double)val); diff --git a/libodbcpp/selectcommand.cpp b/libodbcpp/selectcommand.cpp index 97102ab..88a071f 100644 --- a/libodbcpp/selectcommand.cpp +++ b/libodbcpp/selectcommand.cpp @@ -87,9 +87,9 @@ ODBC::SelectCommand::execute() case SQL_VARCHAR: case SQL_LONGVARCHAR: { - _Column* s = new _Column(colName, col); - s->value = new SQLCHAR[bindSize + 1]; - s->bind(hStmt, sqlcol, SQL_C_CHAR, s->value, bindSize + 1); + _Column* s = new _Column(colName, col); + s->value.resize(bindSize + 1); + s->bind(hStmt, sqlcol, SQL_C_CHAR, &s->value[0], bindSize + 1); columns[col] = s; break; } -- cgit v1.2.3