Wide strings

They wanted to make the database Internationalization(I18N) friendly. It seemed so easy.  Just use the wstring data type instead of the string.  But…

On Windows wchar_r ( the character type conatined withing wstring) is 16 bits long.  On Linux, with a nod to Solaris, it is 32bits long.

OK, that should be no problem.  Nothing says they have to be the same when running…Actually, that is a lie.  The Databases (We are dealing with Oracle and MSSQL Server) only deal well with 16 bit wide strings.  Yes, Oracle can be made to comply, but it is more system administration than we want to make our customers do.

This format is known as UCS2. If has been replaced by UTF-16, but the issues still exist.

The solution was to create a string that is forced to be the Data type we want.  Since ODBC already does the work in the headers to figure out what the right size string is, we can use the SQLWCHAR type.  This is a little bit of a bastardization:

namespace std {
template <>
class char_traits<SQLWCHAR>
{
public:
typedef SQLWCHAR  char_type;
static SQLWCHAR * copy(SQLWCHAR* __s1, SQLWCHAR const* __s2,
std::size_t  __n)
{
std::copy(__s2, __s2 + __n, __s1);

};
static SQLWCHAR * move (SQLWCHAR* __s1, SQLWCHAR const* __s2,
std::size_t  __n)
{
return static_cast<SQLWCHAR*>(std::memmove(__s1, __s2,
__n * sizeof(char_type)));
};

static void assign(SQLWCHAR* __s, std::size_t  __n, SQLWCHAR __a){
std::fill_n(__s, __n, __a);
};
static void assign(SQLWCHAR& __c1,  const SQLWCHAR& __c2){
__c1 = __c2;
};
};
};
typedef basic_string<SQLWCHAR> sqlstring;

[Update]  When I implemented this, I changed the character datatype tor char16, and the string type to string16, since there was nothing SQL specific of the code.  I also divorced us from any dependencies on SQL headers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.