Casting between char and SQLCHAR

This commit is contained in:
Scott Morgan 2014-08-06 20:07:11 +01:00
parent 56bfe3ae61
commit 260def77db

View File

@ -119,7 +119,7 @@ static int fail(lua_State *L, const SQLSMALLINT type, const SQLHANDLE handle) {
SQLINTEGER NativeError; SQLINTEGER NativeError;
SQLSMALLINT MsgSize, i; SQLSMALLINT MsgSize, i;
SQLRETURN ret; SQLRETURN ret;
char Msg[SQL_MAX_MESSAGE_LENGTH]; SQLCHAR Msg[SQL_MAX_MESSAGE_LENGTH];
luaL_Buffer b; luaL_Buffer b;
lua_pushnil(L); lua_pushnil(L);
@ -129,7 +129,7 @@ static int fail(lua_State *L, const SQLSMALLINT type, const SQLHANDLE handle) {
ret = SQLGetDiagRec(type, handle, i, State, &NativeError, Msg, ret = SQLGetDiagRec(type, handle, i, State, &NativeError, Msg,
sizeof(Msg), &MsgSize); sizeof(Msg), &MsgSize);
if (ret == SQL_NO_DATA) break; if (ret == SQL_NO_DATA) break;
luaL_addlstring(&b, Msg, MsgSize); luaL_addlstring(&b, (char*)Msg, MsgSize);
luaL_addchar(&b, '\n'); luaL_addchar(&b, '\n');
i++; i++;
} }
@ -363,7 +363,7 @@ static int cur_coltypes (lua_State *L) {
/* /*
** Creates two tables with the names and the types of the columns. ** Creates two tables with the names and the types of the columns.
*/ */
static void create_colinfo (lua_State *L, cur_data *cur) { static int create_colinfo (lua_State *L, cur_data *cur) {
SQLCHAR buffer[256]; SQLCHAR buffer[256];
SQLSMALLINT namelen, datatype, i; SQLSMALLINT namelen, datatype, i;
SQLRETURN ret; SQLRETURN ret;
@ -375,15 +375,21 @@ static void create_colinfo (lua_State *L, cur_data *cur) {
names = lua_gettop (L); names = lua_gettop (L);
for (i = 1; i <= cur->numcols; i++) { for (i = 1; i <= cur->numcols; i++) {
ret = SQLDescribeCol(cur->hstmt, i, buffer, sizeof(buffer), ret = SQLDescribeCol(cur->hstmt, i, buffer, sizeof(buffer),
&namelen, &datatype, NULL, NULL, NULL); &namelen, &datatype, NULL, NULL, NULL);
/*if (ret == SQL_ERROR) return fail(L, hSTMT, cur->hstmt);*/ if (ret == SQL_ERROR) {
lua_pushstring (L, buffer); lua_pop(L, 2);
return -1;
}
lua_pushstring (L, (char*)buffer);
lua_rawseti (L, names, i); lua_rawseti (L, names, i);
lua_pushstring(L, sqltypetolua(datatype)); lua_pushstring(L, sqltypetolua(datatype));
lua_rawseti (L, types, i); lua_rawseti (L, types, i);
} }
cur->colnames = luaL_ref (L, LUA_REGISTRYINDEX); cur->colnames = luaL_ref (L, LUA_REGISTRYINDEX);
cur->coltypes = luaL_ref (L, LUA_REGISTRYINDEX); cur->coltypes = luaL_ref (L, LUA_REGISTRYINDEX);
return 0;
} }
@ -391,25 +397,30 @@ static void create_colinfo (lua_State *L, cur_data *cur) {
** Creates a cursor table and leave it on the top of the stack. ** Creates a cursor table and leave it on the top of the stack.
*/ */
static int create_cursor (lua_State *L, int o, conn_data *conn, static int create_cursor (lua_State *L, int o, conn_data *conn,
const SQLHSTMT hstmt, const SQLSMALLINT numcols) { const SQLHSTMT hstmt, const SQLSMALLINT numcols)
cur_data *cur = (cur_data *) lua_newuserdata(L, sizeof(cur_data)); {
cur_data *cur = (cur_data *) lua_newuserdata(L, sizeof(cur_data));
luasql_setmeta (L, LUASQL_CURSOR_ODBC); luasql_setmeta (L, LUASQL_CURSOR_ODBC);
conn->cur_counter++; conn->cur_counter++;
/* fill in structure */
/* fill in structure */
cur->closed = 0; cur->closed = 0;
cur->conn = LUA_NOREF; cur->conn = LUA_NOREF;
cur->numcols = numcols; cur->numcols = numcols;
cur->colnames = LUA_NOREF; cur->colnames = LUA_NOREF;
cur->coltypes = LUA_NOREF; cur->coltypes = LUA_NOREF;
cur->hstmt = hstmt; cur->hstmt = hstmt;
lua_pushvalue (L, o); lua_pushvalue (L, o);
cur->conn = luaL_ref (L, LUA_REGISTRYINDEX); cur->conn = luaL_ref (L, LUA_REGISTRYINDEX);
/* make and store column information table */ /* make and store column information table */
create_colinfo (L, cur); if(create_colinfo (L, cur) < 0) {
lua_pop(L, 1);
return fail(L, hSTMT, cur->hstmt);
}
return 1; return 1;
} }
@ -453,7 +464,7 @@ static int conn_close (lua_State *L) {
*/ */
static int conn_execute (lua_State *L) { static int conn_execute (lua_State *L) {
conn_data *conn = (conn_data *) getconnection (L); conn_data *conn = (conn_data *) getconnection (L);
const char *statement = luaL_checkstring(L, 2); SQLCHAR *statement = (SQLCHAR*)luaL_checkstring(L, 2);
SQLHDBC hdbc = conn->hdbc; SQLHDBC hdbc = conn->hdbc;
SQLHSTMT hstmt; SQLHSTMT hstmt;
SQLSMALLINT numcols; SQLSMALLINT numcols;
@ -462,7 +473,7 @@ static int conn_execute (lua_State *L) {
if (error(ret)) if (error(ret))
return fail(L, hDBC, hdbc); return fail(L, hDBC, hdbc);
ret = SQLPrepare(hstmt, (char *) statement, SQL_NTS); ret = SQLPrepare(hstmt, statement, SQL_NTS);
if (error(ret)) { if (error(ret)) {
ret = fail(L, hSTMT, hstmt); ret = fail(L, hSTMT, hstmt);
SQLFreeHandle(hSTMT, hstmt); SQLFreeHandle(hSTMT, hstmt);
@ -583,9 +594,9 @@ static int create_connection (lua_State *L, int o, env_data *env, SQLHDBC hdbc)
*/ */
static int env_connect (lua_State *L) { static int env_connect (lua_State *L) {
env_data *env = (env_data *) getenvironment (L); env_data *env = (env_data *) getenvironment (L);
const char *sourcename = luaL_checkstring (L, 2); SQLCHAR *sourcename = (SQLCHAR*)luaL_checkstring (L, 2);
const char *username = luaL_optstring (L, 3, NULL); SQLCHAR *username = (SQLCHAR*)luaL_optstring (L, 3, NULL);
const char *password = luaL_optstring (L, 4, NULL); SQLCHAR *password = (SQLCHAR*)luaL_optstring (L, 4, NULL);
SQLHDBC hdbc; SQLHDBC hdbc;
SQLRETURN ret; SQLRETURN ret;
@ -595,8 +606,8 @@ static int env_connect (lua_State *L) {
return luasql_faildirect (L, "connection allocation error."); return luasql_faildirect (L, "connection allocation error.");
/* tries to connect handle */ /* tries to connect handle */
ret = SQLConnect (hdbc, (char *) sourcename, SQL_NTS, ret = SQLConnect (hdbc, sourcename, SQL_NTS,
(char *) username, SQL_NTS, (char *) password, SQL_NTS); username, SQL_NTS, password, SQL_NTS);
if (error(ret)) { if (error(ret)) {
ret = fail(L, hDBC, hdbc); ret = fail(L, hDBC, hdbc);
SQLFreeHandle(hDBC, hdbc); SQLFreeHandle(hDBC, hdbc);