Skip to content

Commit

Permalink
Add a helper function for copying strings in ODBC backend too
Browse files Browse the repository at this point in the history
This will be reused for XML and CLOB types soon.
  • Loading branch information
vadz committed Sep 14, 2017
1 parent 556f5d3 commit 11a8982
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
9 changes: 9 additions & 0 deletions include/soci/odbc/soci-odbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ struct odbc_standard_use_type_backend : details::standard_use_type_backend,
details::exchange_type type_;
char *buf_;
SQLLEN indHolder_;

private:
// Copy string data to buf_ and set size, sqlType and cType to the values
// appropriate for strings.
void copy_from_string(std::string const& s,
SQLLEN& size,
SQLSMALLINT& sqlType,
SQLSMALLINT& cType);

};

struct odbc_vector_use_type_backend : details::vector_use_type_backend,
Expand Down
25 changes: 18 additions & 7 deletions src/backends/odbc/standard-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,8 @@ void* odbc_standard_use_type_backend::prepare_for_bind(
case x_stdstring:
{
std::string const& s = exchange_type_cast<x_stdstring>(data_);
sqlType = SQL_VARCHAR;
cType = SQL_C_CHAR;
size = s.size();
buf_ = new char[size+1];
memcpy(buf_, s.c_str(), size);
buf_[size++] = '\0';
indHolder_ = SQL_NTS;

copy_from_string(s, size, sqlType, cType);
}
break;
case x_stdtm:
Expand Down Expand Up @@ -128,6 +123,22 @@ void* odbc_standard_use_type_backend::prepare_for_bind(
return buf_ ? buf_ : data_;
}

void odbc_standard_use_type_backend::copy_from_string(
std::string const& s,
SQLLEN& size,
SQLSMALLINT& sqlType,
SQLSMALLINT& cType
)
{
sqlType = SQL_VARCHAR;
cType = SQL_C_CHAR;
size = s.size();
buf_ = new char[size+1];
memcpy(buf_, s.c_str(), size);
buf_[size++] = '\0';
indHolder_ = SQL_NTS;
}

void odbc_standard_use_type_backend::bind_by_pos(
int &position, void *data, exchange_type type, bool /* readOnly */)
{
Expand Down

0 comments on commit 11a8982

Please sign in to comment.