diff options
author | Максим Зражевский <[email protected]> | 2011-12-19 02:43:21 +0800 |
---|---|---|
committer | Henrik Nord <[email protected]> | 2011-12-21 15:29:03 +0100 |
commit | c6e01fe15d02dc704e5a9e77b59d2ee2db235976 (patch) | |
tree | 0875a4d53cccd62cf89a09a32c8c61542d2fa169 | |
parent | 58336ee5c613de56f6d69562cd59b651eef734f0 (diff) | |
download | otp-c6e01fe15d02dc704e5a9e77b59d2ee2db235976.tar.gz otp-c6e01fe15d02dc704e5a9e77b59d2ee2db235976.tar.bz2 otp-c6e01fe15d02dc704e5a9e77b59d2ee2db235976.zip |
Add support for NULL value in odbc:param_query
Support atom 'null' in odbc:param_query as database NULL value
Fix "ODBC: received unexpected info:{tcp_closed, ...}" when connection is terminating.
Fix possible access violation with 64bit ODBC.
-rw-r--r-- | lib/odbc/c_src/odbcserver.c | 60 | ||||
-rw-r--r-- | lib/odbc/c_src/odbcserver.h | 2 | ||||
-rw-r--r-- | lib/odbc/src/odbc.erl | 31 |
3 files changed, 63 insertions, 30 deletions
diff --git a/lib/odbc/c_src/odbcserver.c b/lib/odbc/c_src/odbcserver.c index ab2d7fe210..6d4460014f 100644 --- a/lib/odbc/c_src/odbcserver.c +++ b/lib/odbc/c_src/odbcserver.c @@ -176,7 +176,7 @@ static void encode_column_dyn(db_column column, int column_nr, static void encode_data_type(SQLSMALLINT sql_type, SQLINTEGER size, SQLSMALLINT decimal_digits, db_state *state); static Boolean decode_params(db_state *state, byte *buffer, int *index, param_array **params, - int i, int j); + int i, int j, int num_param_values); /*------------- Erlang port communication functions ----------------------*/ @@ -212,6 +212,7 @@ static db_column * alloc_column_buffer(int n); static void free_column_buffer(db_column **columns, int n); static void free_params(param_array **params, int cols); static void clean_state(db_state *state); +static SQLLEN* alloc_strlen_indptr(int n, int val); /* ------------- Init/map/bind/retrive functions -------------------------*/ @@ -1157,7 +1158,7 @@ static db_result_msg encode_out_params(db_state *state, break; case SQL_C_BIT: ei_x_encode_atom(&dynamic_buffer(state), - ((Boolean*)values)[j]==TRUE?"true":"false"); + ((byte*)values)[j]==TRUE?"true":"false"); break; default: ei_x_encode_atom(&dynamic_buffer(state), "error"); @@ -1579,37 +1580,48 @@ static void encode_data_type(SQLSMALLINT sql_type, SQLINTEGER size, } static Boolean decode_params(db_state *state, byte *buffer, int *index, param_array **params, - int i, int j) + int i, int j, int num_param_values) { int erl_type, size; long bin_size, l64; long val; param_array* param; TIMESTAMP_STRUCT* ts; + char atomarray[MAXATOMLEN+1]; ei_get_type(buffer, index, &erl_type, &size); param = &(*params)[i]; + if(erl_type == ERL_ATOM_EXT) { + ei_decode_atom(buffer, index, atomarray); + if(strncmp(atomarray, "null", 4) == 0 ) { + param->offset += param->type.len; + + if(!param->type.strlen_or_indptr_array) + param->type.strlen_or_indptr_array = alloc_strlen_indptr(num_param_values, param->type.len); + + param->type.strlen_or_indptr_array[j] = SQL_NULL_DATA; + return TRUE; + } + } + switch (param->type.c) { case SQL_C_CHAR: if (binary_strings(state)) { ei_decode_binary(buffer, index, &(param->values.string[param->offset]), &bin_size); param->offset += param->type.len; - param->type.strlen_or_indptr_array[j] = SQL_NTS; } else { if(erl_type != ERL_STRING_EXT) { return FALSE; } ei_decode_string(buffer, index, &(param->values.string[param->offset])); param->offset += param->type.len; - param->type.strlen_or_indptr_array[j] = SQL_NTS; } break; case SQL_C_WCHAR: ei_decode_binary(buffer, index, &(param->values.string[param->offset]), &bin_size); param->offset += param->type.len; - param->type.strlen_or_indptr_array[j] = SQL_NTS; break; case SQL_C_TYPE_TIMESTAMP: ts = (TIMESTAMP_STRUCT*) param->values.string; @@ -1661,9 +1673,13 @@ static Boolean decode_params(db_state *state, byte *buffer, int *index, param_ar if((erl_type != ERL_ATOM_EXT)) { return FALSE; } - ei_decode_boolean(buffer, index, &(param->values.bool[j])); + if (strncmp((char*)atomarray,"true",4) == 0) + param->values.bool[j] = TRUE; + else if (strncmp((char*)atomarray,"false",5) == 0) + param->values.bool[j] = FALSE; + else + return -1; break; - default: return FALSE; } @@ -2014,6 +2030,18 @@ static void clean_state(db_state *state) nr_of_columns(state) = 0; } +/* Allocates and fill with default value StrLen_or_IndPtr array */ +static SQLLEN* alloc_strlen_indptr(int n, int val) +{ + int i; + SQLLEN* arr = (SQLLEN*)safe_malloc(n * sizeof(SQLLEN)); + + for( i=0; i < n; ++i ) + arr[i] = val; + + return arr; +} + /* ------------- Init/map/bind/retrive functions ------------------------*/ /* Prepare the state for a connection */ @@ -2118,7 +2146,7 @@ static void init_param_column(param_array *params, byte *buffer, int *index, (double *)safe_malloc(num_param_values * params->type.len); } else if(params->type.c == SQL_C_CHAR) { params->type.strlen_or_indptr_array - = (SQLLEN*)safe_malloc(num_param_values * sizeof(SQLINTEGER)); + = alloc_strlen_indptr(num_param_values, SQL_NTS); params->values.string = (byte *)safe_malloc(num_param_values * sizeof(byte)* params->type.len); @@ -2136,8 +2164,8 @@ static void init_param_column(param_array *params, byte *buffer, int *index, params->type.len = length+1; params->type.c = SQL_C_CHAR; params->type.col_size = (SQLUINTEGER)length; - params->type.strlen_or_indptr_array = - (SQLLEN*)safe_malloc(num_param_values * sizeof(SQLINTEGER)); + params->type.strlen_or_indptr_array + = alloc_strlen_indptr(num_param_values, SQL_NTS); params->values.string = (byte *)safe_malloc(num_param_values * sizeof(byte)* params->type.len); @@ -2159,8 +2187,8 @@ static void init_param_column(param_array *params, byte *buffer, int *index, params->type.len = (length+1)*sizeof(SQLWCHAR); params->type.c = SQL_C_WCHAR; params->type.col_size = (SQLUINTEGER)length; - params->type.strlen_or_indptr_array = - (SQLLEN*)safe_malloc(num_param_values * sizeof(SQLINTEGER)); + params->type.strlen_or_indptr_array + = alloc_strlen_indptr(num_param_values, SQL_NTS); params->values.string = (byte *)safe_malloc(num_param_values * sizeof(byte) * params->type.len); @@ -2201,10 +2229,10 @@ static void init_param_column(param_array *params, byte *buffer, int *index, case USER_BOOLEAN: params->type.sql = SQL_BIT; params->type.c = SQL_C_BIT; - params->type.len = sizeof(Boolean); + params->type.len = sizeof(byte); params->type.col_size = params->type.len; params->values.bool = - (Boolean *)safe_malloc(num_param_values * params->type.len); + (byte *)safe_malloc(num_param_values * params->type.len); break; } params->offset = 0; @@ -2411,7 +2439,7 @@ static param_array * bind_parameter_arrays(byte *buffer, int *index, } for (j = 0; j < num_param_values; j++) { - if(!decode_params(state, buffer, index, ¶ms, i, j)) { + if(!decode_params(state, buffer, index, ¶ms, i, j, num_param_values)) { /* An input parameter was not of the expected type */ free_params(¶ms, i); return params; diff --git a/lib/odbc/c_src/odbcserver.h b/lib/odbc/c_src/odbcserver.h index 56b6148777..a76cedf1af 100644 --- a/lib/odbc/c_src/odbcserver.h +++ b/lib/odbc/c_src/odbcserver.h @@ -156,7 +156,7 @@ typedef struct { byte *string; SQLINTEGER *integer; double *floating; - Boolean *bool; + byte *bool; }values; } param_array; diff --git a/lib/odbc/src/odbc.erl b/lib/odbc/src/odbc.erl index 36afd1abcf..e187679dc3 100644 --- a/lib/odbc/src/odbc.erl +++ b/lib/odbc/src/odbc.erl @@ -755,7 +755,10 @@ handle_info({'DOWN', _Ref, _Type, _Process, shutdown}, State) -> handle_info({'DOWN', _Ref, _Type, Process, Reason}, State) -> {stop, {stopped, {'EXIT', Process, Reason}}, State#state{reply_to = undefined}}; - + +handle_info({tcp_closed, Socket}, State = #state{odbc_socket=Socket, + state = disconnecting}) -> + {stop, normal, State}; %--------------------------------------------------------------------------- %% Catch all - throws away unknown messages (This could happen by "accident" %% so we do not want to crash, but we make a log entry as it is an @@ -942,9 +945,11 @@ fix_params({sql_bit, InOut, Values}) -> fix_params({'sql_timestamp', InOut, Values}) -> NewValues = case (catch - lists:map(fun({{Year,Month,Day},{Hour,Minute,Second}}) -> - {Year,Month,Day,Hour,Minute,Second} - end, Values)) of + lists:map( + fun({{Year,Month,Day},{Hour,Minute,Second}}) -> + {Year,Month,Day,Hour,Minute,Second}; + (null) -> null + end, Values)) of Result -> Result end, @@ -960,15 +965,15 @@ fix_inout(out) -> fix_inout(inout) -> ?INOUT. -string_terminate([Value| _ ] = Values) when is_list(Value)-> - case (catch - lists:map(fun(Str) -> Str ++ [?STR_TERMINATOR] end, Values)) of - Result -> - Result - end; -string_terminate([Value| _ ] = Values) when is_binary(Value)-> - case (catch - lists:map(fun(B) -> <<B/binary,0:16>> end, Values)) of +string_terminate(Values) -> + case (catch lists:map(fun string_terminate_value/1, Values)) of Result -> Result end. + +string_terminate_value(String) when is_list(String) -> + String ++ [?STR_TERMINATOR]; +string_terminate_value(Binary) when is_binary(Binary) -> + <<Binary/binary,0:16>>; +string_terminate_value(null) -> + null. |