[freetds] Status of UTF-8 support in 0.83 dev

James K. Lowden jklowden at freetds.org
Thu Aug 14 11:53:23 EDT 2008


Hi Sebastien, 

> James K. Lowden wrote:
> > "Wide" characters have a fixed width, usually 16 bits.  UTF-8 has a
> > variable width and for the most part can be treated like ASCII
> > (strlen(3) et al.).  Declaring UTF-8 as SQL_C_WCHAR incorrectly tells
> > the driver there's a character in every two bytes.
> 
> Oh take care, actually there are 2 type specifications in
> SQLBindParameter:
> 
> SQLRETURN SQLBindParameter(
>       SQLHSTMT     StatementHandle,
>       SQLUSMALLINT     ParameterNumber,
>       SQLSMALLINT     InputOutputType,
>       SQLSMALLINT     ValueType,          -- This is the C type
>       SQLSMALLINT     ParameterType,      -- This is the SQL equivalent
>       SQLULEN     ColumnSize,
>       SQLSMALLINT     DecimalDigits,
>       SQLPOINTER     ParameterValuePtr,
>       SQLINTEGER     BufferLength,
>       SQLLEN *     StrLen_or_IndPtr);
> 
> For sure, when using (char *) / UTF-8, ValueType *must* be SQL_C_CHAR,
> not SQL_C_WCHAR... (that one is for wchar_t).
> 
> I was talking about ParameterType...
> 
> Should it be  SQL_W[VAR]CHAR  or  SQL_[VAR]CHAR  when binding UTF-8?

Oh, I see.  Your question surprises me, then, because IIUC the client-side
encoding doesn't matter for ParameterType.  ParameterType describes the
data type of the parameter as used in the query, not as represented in the
client buffer.  (I"m just an ODBC student.  Correct me if I'm wrong.) 

For instance, any of these pairs works:

	ValueType	ParameterType 
	-------------	-------------
	SQL_C_CHAR	SQL_DECIMAL
	SQL_C_CHAR	SQL_VARCHAR
	SQL_C_DOUBLE	SQL_FLOAT	

For character data parameters, the choice of SQL_W[VAR]CHAR or
SQL_[VAR]CHAR is governed by the data type of the parameter as understood
by the server.  For stored procedures, the parameter type is obvious: it's
declared within the procedure text.  For placeholders in open SQL text, I
would say the type is always SQL_[VAR]CHAR.  

That is, if your query is, 

	CREATE TABLE t (name nvarchar(30))
	INSERT t values( ? )	

You would use SQL_[VAR]CHAR.  

Now, what to do when what you really want is equivalent to "INSERT t
values( N'string' )"?  I assume it's still SQL_[VAR]CHAR, but I won't know
for sure until either Frediano explains or I figure out how sp_execute
works.  

One thing you could try is SQLDescribeParam with various combinations
using Microsoft's driver.  We would follow whatever it says/expects.  

Regards, 

--jkl



More information about the FreeTDS mailing list