/*-------
 * Module:			odbcapi25w.c
 *
 * Description:		This module contains UNICODE routines
 *
 * Classes:			n/a
 *
 * API functions:	SQLColAttributesW, SQLErrorW, SQLGetConnectOptionW,
			SQLSetConnectOptionW
 *-------
 */

#include "psqlodbc.h"
#include <stdio.h>
#include <string.h>

#include "pgapifunc.h"
#include "connection.h"
#include "statement.h"

RETCODE  SQL_API SQLErrorW(HENV EnvironmentHandle,
           HDBC ConnectionHandle, HSTMT StatementHandle,
           SQLWCHAR *Sqlstate, SQLINTEGER *NativeError,
           SQLWCHAR *MessageText, SQLSMALLINT BufferLength,
           SQLSMALLINT *TextLength)
{
	RETCODE	ret;
	SWORD	tlen, buflen;
	char	*qst = NULL, *mtxt = NULL;

	mylog("[SQLErrorW]");
	if (Sqlstate)
		qst = malloc(8);
	buflen = 0;
	if (MessageText && BufferLength > 0)
	{
		buflen = BufferLength * 3 + 1;
		mtxt = malloc(buflen);
	}
	ret = PGAPI_Error(EnvironmentHandle, ConnectionHandle, StatementHandle,
           	qst, NativeError, mtxt, buflen, &tlen);
	if (qst)
#if defined(MAC64) && defined(MARKLOGIC)
		utf8_to_ucs4(qst, strlen(qst), Sqlstate, 5);
#else
		utf8_to_ucs2(qst, strlen(qst), Sqlstate, 5);
#endif
	if (NULL != mtxt)
	{
#if defined(MAC64) && defined(MARKLOGIC)
		SQLULEN	tulen = utf8_to_ucs4(mtxt, tlen, MessageText, BufferLength);
#else
		SQLULEN	tulen = utf8_to_ucs2(mtxt, tlen, MessageText, BufferLength);
#endif
		if (NULL != TextLength)
			*TextLength = tulen;
		free(mtxt);
	}
	if (qst)
		free(qst);
	return ret;
}

RETCODE  SQL_API SQLGetConnectOptionW(HDBC ConnectionHandle,
           SQLUSMALLINT Option, PTR Value)
{
	mylog("[SQLGetConnectOptionW]");
	CC_set_in_unicode_driver((ConnectionClass *) ConnectionHandle);
	return PGAPI_GetConnectOption(ConnectionHandle, Option, Value, NULL, 64);
} 

RETCODE  SQL_API SQLSetConnectOptionW(HDBC ConnectionHandle,
           SQLUSMALLINT Option, SQLUINTEGER Value)
{
	mylog("[SQLSetConnectionOptionW]");
if (!ConnectionHandle)	return SQL_ERROR;
	CC_set_in_unicode_driver((ConnectionClass *) ConnectionHandle);
	return PGAPI_SetConnectOption(ConnectionHandle, Option, Value);
}

RETCODE SQL_API SQLColAttributesW(
    HSTMT           hstmt,
    SQLUSMALLINT       icol,
    SQLUSMALLINT       fDescType,
    PTR         rgbDesc,
    SQLSMALLINT        cbDescMax,
    SQLSMALLINT 	  *pcbDesc,
    SQLINTEGER 		  *pfDesc)
{
	mylog("[SQLColAttributesW]");
	return PGAPI_ColAttributes(hstmt, icol, fDescType, rgbDesc,
		cbDescMax, pcbDesc, pfDesc);
}
