How can I portably implement row-wise binding of arbitrary resultsets in ODBC while avoiding alignment issues?
I have a class which takes an SQL query, executes it, then binds every column in the resultset as SQL_C_WCHAR using row-wise binding.
right now the way I do it is to allocate a vector of char, and determine the pointers to give SQLBindColumn as follows:
- buffer for column 1 = &vec[0]
- buffer for length indicator of column 1 = &vec[0] + (sizeof(SQLW开发者_如何学编程CHAR) * length of column 1)
- buffer for column 2 = &vec[0] + (sizeof(SQLWCHAR) * length of column 1) + sizeof(length indicator)
- buffer for length indicator of column 2 = &vec[0] + (sizeof(SQLWCHAR) * length of column 1) + sizeof(length indicator) + (sizeof(SQLWCHAR) * length of column 2)
and so on
this is causing some alignment issues (on SPARC). I know I need to add some padding, but I don't know how to calculate how much portably.
The way I ended up dealing with it was to actually put the length indicators and the wchars in seperate, correctly typed, buffers. They don't actually have to live in the same buffer since all that ODBC does is add the 'struct size' to each address each time it wants to go the the next set.
I figured out the alignment needed for WCHAR and SQLLEN by allocating two small arrays on the stack and subtracting pointers between adjacent cells. Then I took the LCM of both of the alignments, and added padding to the buffers so each set would occupy a multiple of that space. The I fed ODBC this as a phony 'struct size'.
Row-wise binding is a pain in the backside. However, I thought the indicators (assuming you mean StrLen_or_IndPtr argument) can be specified in a separate array of SQLINTEGER/SQLLEN (depending on how new your ODBC is). Look for something like SQL_DESC_INDICATOR_PTR and there is another one for the length. You can set these separately from the data by setting fields in the descriptor. If you do this you will avoid alignment issues and keep your row data separate from the indicator/length.
Update: http://msdn.microsoft.com/en-us/library/ms711730(v=vs.85).aspx
Update2: Just ensure the integer values (like you indicator) are correctly aligned on a 4 byte boundary or whatever Sparc requires.
精彩评论