Skip to content

Commit

Permalink
everything fucked up
Browse files Browse the repository at this point in the history
  • Loading branch information
bold84 committed Mar 15, 2024
1 parent 01075b7 commit e9e296e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 47 deletions.
5 changes: 3 additions & 2 deletions include/soci/odbc/soci-odbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ struct odbc_standard_use_type_backend : details::standard_use_type_backend,
int position_;
void *data_;
details::exchange_type type_;
std::unique_ptr<details::odbc_char_type[]> buf_;
std::unique_ptr<char[]> buf_;
SQLLEN indHolder_;

private:
Expand Down Expand Up @@ -303,7 +303,8 @@ struct odbc_vector_use_type_backend : details::vector_use_type_backend,
void *data_;
details::exchange_type type_;
int position_;
details::odbc_char_type *buf_; // generic buffer
//details::odbc_char_type *buf_; // generic buffer
char* buf_; // generic buffer
std::size_t colSize_; // size of the string column (used for strings)
// used for strings only
std::size_t maxSize_;
Expand Down
58 changes: 39 additions & 19 deletions src/backends/odbc/standard-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ void* odbc_standard_use_type_backend::prepare_for_bind(
sqlType = SQL_NUMERIC;
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
size = max_bigint_length;
buf_ = std::make_unique<odbc_char_type[]>(size);
#ifndef SOCI_ENABLE_UNICODE
snprintf(reinterpret_cast<char *>(buf_.get()), size, "%" LL_FMT_FLAGS "d",
exchange_type_cast<x_long_long>(data_));
#else
swprintf(buf_.get(), size, L"%" LL_FMT_FLAGS L"d",
exchange_type_cast<x_long_long>(data_));
#endif
buf_ = std::make_unique<char[]>(size);

snprintf(reinterpret_cast<char*>(buf_.get()), size, "%" LL_FMT_FLAGS "d",
exchange_type_cast<x_long_long>(data_));

//#ifndef SOCI_ENABLE_UNICODE
// snprintf(reinterpret_cast<char *>(buf_.get()), size, "%" LL_FMT_FLAGS "d",
// exchange_type_cast<x_long_long>(data_));
//#else
// swprintf(buf_.get(), size, L"%" LL_FMT_FLAGS L"d",
// exchange_type_cast<x_long_long>(data_));
//#endif
indHolder_ = SQL_NTS;
}
else // Normal case, use ODBC support.
Expand All @@ -63,14 +67,18 @@ void* odbc_standard_use_type_backend::prepare_for_bind(
sqlType = SQL_NUMERIC;
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
size = max_bigint_length;
buf_ = std::make_unique<odbc_char_type[]>(size);
#ifndef SOCI_ENABLE_UNICODE
buf_ = std::make_unique<char[]>(size);

snprintf(reinterpret_cast<char*>(buf_.get()), size, "%" LL_FMT_FLAGS "u",
exchange_type_cast<x_unsigned_long_long>(data_));

/*#ifndef SOCI_ENABLE_UNICODE
snprintf(reinterpret_cast<char *>(buf_.get()), size, "%" LL_FMT_FLAGS "u",
exchange_type_cast<x_unsigned_long_long>(data_));
#else
swprintf(buf_.get(), size, L"%" LL_FMT_FLAGS L"u",
exchange_type_cast<x_unsigned_long_long>(data_));
#endif
#endif*/
indHolder_ = SQL_NTS;
}
else // Normal case, use ODBC support.
Expand All @@ -87,13 +95,23 @@ void* odbc_standard_use_type_backend::prepare_for_bind(
break;

case x_char:
#ifdef SOCI_ENABLE_UNICODE
sqlType = SQL_WCHAR;
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
size = sizeof(odbc_char_type) * 2;
buf_ = std::make_unique<char[]>(sizeof(odbc_char_type) * 2);
buf_[0] = toUtf16(exchange_type_cast<x_char>(data_));
buf_[1] = L'\0';
indHolder_ = SQL_NTS;
#else
sqlType = SQL_CHAR;
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
size = sizeof(odbc_char_type) * 2;
buf_ = std::make_unique<odbc_char_type[]>(size);
buf_ = std::make_unique<char[]>(size);
buf_[0] = exchange_type_cast<x_char>(data_);
buf_[1] = L'\0';
buf_[1] = '\0';
indHolder_ = SQL_NTS;
#endif // SOCI_ENABLE_UNICODE
break;
case x_stdstring:
{
Expand All @@ -108,7 +126,7 @@ void* odbc_standard_use_type_backend::prepare_for_bind(

sqlType = SQL_TIMESTAMP;
cType = SQL_C_TIMESTAMP;
buf_ = std::make_unique<odbc_char_type[]>(sizeof(TIMESTAMP_STRUCT));
buf_ = std::make_unique<char[]>(sizeof(TIMESTAMP_STRUCT));
size = 19; // This number is not the size in bytes, but the number
// of characters in the date if it was written out
// yyyy-mm-dd hh:mm:ss
Expand Down Expand Up @@ -166,18 +184,20 @@ void odbc_standard_use_type_backend::copy_from_string(
size = s.size();
sqlType = size >= ODBC_MAX_COL_SIZE ? SQL_LONGVARCHAR : SQL_VARCHAR;
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
buf_ = std::make_unique<odbc_char_type[]>(size + 1);
buf_ = std::make_unique<char[]>(size + 1);
std::copy(s.begin(), s.end(), buf_.get());
buf_[size] = '\0';
indHolder_ = SQL_NTS;
#else
std::wstring ws = toUtf16(s);
size = ws.size();
sqlType = size >= ODBC_MAX_COL_SIZE ? SQL_WLONGVARCHAR : SQL_WVARCHAR;
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
buf_ = std::make_unique<odbc_char_type[]>(size + sizeof(odbc_char_type));
std::copy(ws.begin(), ws.end(), buf_.get());
buf_[size] = L'\0';
cType = SQL_WCHAR;
std::size_t const bufSize = (size + 1) * sizeof(SQLWCHAR);
buf_ = std::make_unique<char[]>(bufSize);
SQLWCHAR* const buf = reinterpret_cast<SQLWCHAR*>(buf_.get());
std::copy(ws.begin(), ws.end(), buf);
buf[size] = L'\0';
indHolder_ = SQL_NTS;
#endif
}
Expand Down
78 changes: 52 additions & 26 deletions src/backends/odbc/vector-use-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void* odbc_vector_use_type_backend::prepare_for_bind(SQLUINTEGER &size,
sqlType = SQL_NUMERIC;
cType = SQL_C_CHAR;
size = max_bigint_length;
buf_ = new details::odbc_char_type[size * vsize];
buf_ = new char[size * vsize];
data = buf_;
}
else // Normal case, use ODBC support.
Expand All @@ -104,7 +104,7 @@ void* odbc_vector_use_type_backend::prepare_for_bind(SQLUINTEGER &size,
sqlType = SQL_NUMERIC;
cType = SQL_C_CHAR;
size = max_bigint_length;
buf_ = new details::odbc_char_type[size * vsize];
buf_ = new char[size * vsize];
data = buf_;
}
else // Normal case, use ODBC support.
Expand Down Expand Up @@ -137,19 +137,26 @@ void* odbc_vector_use_type_backend::prepare_for_bind(SQLUINTEGER &size,

prepare_indicators(vsize);

size = sizeof(char) * 2;
buf_ = new details::odbc_char_type[size * vsize];
size = sizeof(details::odbc_char_type) * 2;

details::odbc_char_type *pos = buf_;
buf_ = new char[size * vsize];

//buf_ = new details::odbc_char_type[size * vsize];

details::odbc_char_type *pos = reinterpret_cast<details::odbc_char_type*>(buf_);

for (std::size_t i = 0; i != vsize; ++i)
{
*pos++ = (*vp)[i];
*pos++ = details::toUtf16((*vp)[i]);
*pos++ = 0;
}

#ifdef SOCI_ENABLE_UNICODE
sqlType = SQL_WCHAR;
#else
sqlType = SQL_CHAR;
cType = SQL_C_CHAR;
#endif // SOCI_ENABLE_UNICODE
cType = SOCI_ODBC_SQL_C_CHAR_TYPE;
data = buf_;
}
break;
Expand All @@ -160,7 +167,7 @@ void* odbc_vector_use_type_backend::prepare_for_bind(SQLUINTEGER &size,
std::size_t maxSize = 0;
std::size_t const vecSize = get_vector_size(type_, data_);
prepare_indicators(vecSize);
for (std::size_t i = 0; i != vecSize; ++i)
for (std::size_t i = 0; i < vecSize; ++i)
{
std::size_t sz = vector_string_value(type_, data_, i).length();
set_sqllen_from_vector_at(i, static_cast<long>(sz));
Expand All @@ -169,22 +176,39 @@ void* odbc_vector_use_type_backend::prepare_for_bind(SQLUINTEGER &size,

maxSize++; // For terminating nul.

buf_ = new details::odbc_char_type[maxSize * vecSize];
memset(buf_, 0, maxSize * vecSize);
//buf_ = new details::odbc_char_type[maxSize * vecSize];
const std::size_t bufSize = maxSize * vecSize * sizeof(details::odbc_char_type);

buf_ = new char[bufSize];
memset(buf_, 0, bufSize);

details::odbc_char_type *pos = buf_;
details::odbc_char_type *pos = reinterpret_cast<details::odbc_char_type *>(buf_);
for (std::size_t i = 0; i != vecSize; ++i)
{
std::string& value = vector_string_value(type_, data_, i);

#ifdef SOCI_ENABLE_UNICODE
const std::wstring wValue = toUtf16(value);
//memcpy(pos, wValue.c_str(), (wValue.length()) * sizeof(details::odbc_char_type));
//wmemcpy(pos, wValue.c_str(), wValue.length());
std::copy(wValue.begin(), wValue.end(), pos);
#else
memcpy(pos, value.c_str(), value.length());
#endif // SOCI_ENABLE_UNICODE

pos += maxSize;
}

data = buf_;
size = static_cast<SQLINTEGER>(maxSize);
size = static_cast<SQLUINTEGER>(maxSize * sizeof(details::odbc_char_type));

#ifdef SOCI_ENABLE_UNICODE
sqlType = size >= ODBC_MAX_COL_SIZE ? SQL_WLONGVARCHAR : SQL_WVARCHAR;
cType = SQL_C_WCHAR;
#else
sqlType = size >= ODBC_MAX_COL_SIZE ? SQL_LONGVARCHAR : SQL_VARCHAR;
cType = SQL_C_CHAR;
#endif // SOCI_ENABLE_UNICODE
}
break;
case x_stdtm:
Expand All @@ -194,7 +218,7 @@ void* odbc_vector_use_type_backend::prepare_for_bind(SQLUINTEGER &size,

prepare_indicators(vp->size());

buf_ = new details::odbc_char_type[sizeof(TIMESTAMP_STRUCT) * vp->size()];
buf_ = new char[sizeof(TIMESTAMP_STRUCT) * vp->size()];

sqlType = SQL_TYPE_TIMESTAMP;
cType = SQL_C_TYPE_TIMESTAMP;
Expand Down Expand Up @@ -303,7 +327,7 @@ void odbc_vector_use_type_backend::pre_use(indicator const *ind)

std::vector<std::tm> &v(*vp);

details::odbc_char_type *pos = buf_;
char *pos = buf_;
std::size_t const vsize = v.size();
for (std::size_t i = 0; i != vsize; ++i)
{
Expand Down Expand Up @@ -331,19 +355,19 @@ void odbc_vector_use_type_backend::pre_use(indicator const *ind)
case x_long_long:
if (use_string_for_bigint())
{
std::vector<long long> *vp
= static_cast<std::vector<long long> *>(data_);
std::vector<long long> *vp = static_cast<std::vector<long long> *>(data_);
std::vector<long long> &v(*vp);

details::odbc_char_type *pos = buf_;
char *pos = buf_;
std::size_t const vsize = v.size();
for (std::size_t i = 0; i != vsize; ++i)
{
#ifdef SOCI_ENABLE_UNICODE
swprintf(pos, max_bigint_length, L"%" LL_FMT_FLAGS L"d", v[i]);
#else
snprintf(reinterpret_cast<char*>(pos), max_bigint_length, "%" LL_FMT_FLAGS "d", v[i]);
#endif // SOCI_ENABLE_UNICODE
//#ifdef SOCI_ENABLE_UNICODE
// swprintf(pos, max_bigint_length, L"%" LL_FMT_FLAGS L"d", v[i]);
//#else
// snprintf(reinterpret_cast<char*>(pos), max_bigint_length, "%" LL_FMT_FLAGS "d", v[i]);
//#endif // SOCI_ENABLE_UNICODE
pos += max_bigint_length;
}

Expand All @@ -358,15 +382,17 @@ void odbc_vector_use_type_backend::pre_use(indicator const *ind)
= static_cast<std::vector<unsigned long long> *>(data_);
std::vector<unsigned long long> &v(*vp);

details::odbc_char_type *pos = buf_;
char *pos = buf_;
std::size_t const vsize = v.size();
for (std::size_t i = 0; i != vsize; ++i)
{
#ifdef SOCI_ENABLE_UNICODE
swprintf(pos, max_bigint_length, L"%" LL_FMT_FLAGS L"u", v[i]);
#else
snprintf(reinterpret_cast<char*>(pos), max_bigint_length, "%" LL_FMT_FLAGS "u", v[i]);
#endif // SOCI_ENABLE_UNICODE

//#ifdef SOCI_ENABLE_UNICODE
// swprintf(pos, max_bigint_length, L"%" LL_FMT_FLAGS L"u", v[i]);
//#else
// snprintf(reinterpret_cast<char*>(pos), max_bigint_length, "%" LL_FMT_FLAGS "u", v[i]);
//#endif // SOCI_ENABLE_UNICODE

pos += max_bigint_length;
}
Expand Down

0 comments on commit e9e296e

Please sign in to comment.