From 32c7b87dd9886c3b09cdc032700d52f61e5661d0 Mon Sep 17 00:00:00 2001 From: Bikram Chatterjee Date: Thu, 13 Jun 2019 15:36:12 +0200 Subject: [PATCH] Partial common params support in creating connection (#59) * raise exception with full DPI error * add testcase catch_error_message * add testcase catch_error_message_conn * remove two debug prints in eunit * fix broken fixture * Added property utf8 when creating a new connection. * fixed MARCO args formatting * add a comma * temporary files and folders * read config from map - WIP --- .gitignore | 1 + c_src/dpiConn_nif.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- test/odpi_eunit.erl | 21 +++++++++++++++++++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 41a8ec4..75343e6 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ c_src/oci_api/session c_src/odpi priv/ ebin/ +\.*/ diff --git a/c_src/dpiConn_nif.c b/c_src/dpiConn_nif.c index 1233486..eeea38c 100644 --- a/c_src/dpiConn_nif.c +++ b/c_src/dpiConn_nif.c @@ -6,6 +6,9 @@ #include "dpiQueryInfo_nif.h" #include "stdio.h" +ERL_NIF_TERM ATOM_encoding = 0; +ERL_NIF_TERM ATOM_nencoding = 0; + ErlNifResourceType *dpiConn_type; void dpiConn_res_dtor(ErlNifEnv *env, void *resource) @@ -20,6 +23,7 @@ DPI_NIF_FUN(conn_create) dpiContext_res *contextRes; ErlNifBinary userName, password, connectString; + size_t commonParamsMapSize = 0; if (!enif_get_resource(env, argv[0], dpiContext_type, &contextRes)) return BADARG_EXCEPTION(0, "resource context"); if (!enif_inspect_binary(env, argv[1], &userName)) @@ -28,6 +32,43 @@ DPI_NIF_FUN(conn_create) return BADARG_EXCEPTION(2, "string/binary password"); if (!enif_inspect_binary(env, argv[3], &connectString)) return BADARG_EXCEPTION(3, "string/binary connectString"); + if (!enif_get_map_size(env, argv[4], &commonParamsMapSize)) + return BADARG_EXCEPTION(4, "map commonParams"); + + dpiCommonCreateParams commonParams; + RAISE_EXCEPTION_ON_DPI_ERROR( + contextRes->context, + dpiContext_initCommonCreateParams(contextRes->context, &commonParams), + NULL); + + if (commonParamsMapSize > 0) + { + // lazy create + if (!(ATOM_encoding | ATOM_nencoding)) + { + ATOM_encoding = enif_make_atom(env, "encoding"); + ATOM_nencoding = enif_make_atom(env, "nencoding"); + } + + ERL_NIF_TERM mapval; + char encodeStr[128]; + if (enif_get_map_value(env, argv[4], ATOM_encoding, &mapval)) { + if( + !enif_get_string( + env, mapval, encodeStr, sizeof(encodeStr), ERL_NIF_LATIN1) + ) return BADARG_EXCEPTION(4, "string\0 commonParams.encoding"); + commonParams.encoding = encodeStr; + } + + char nencodeStr[128]; + if (enif_get_map_value(env, argv[4], ATOM_nencoding, &mapval)) { + if( + !enif_get_string( + env, mapval, nencodeStr, sizeof(nencodeStr), ERL_NIF_LATIN1) + ) return BADARG_EXCEPTION(4, "string\0 commonParams.nencoding"); + commonParams.nencoding = nencodeStr; + } + } dpiConn_res *connRes = enif_alloc_resource(dpiConn_type, sizeof(dpiConn_res)); @@ -38,8 +79,8 @@ DPI_NIF_FUN(conn_create) contextRes->context, userName.data, userName.size, password.data, password.size, connectString.data, connectString.size, - NULL, // TODO implement commonParams - NULL, // TODO implement createParams + &commonParams, + NULL, // TODO implement connCreateParams &connRes->conn), connRes); diff --git a/test/odpi_eunit.erl b/test/odpi_eunit.erl index 8305d84..4e3fe3b 100644 --- a/test/odpi_eunit.erl +++ b/test/odpi_eunit.erl @@ -1088,8 +1088,25 @@ getTnsUserPass () -> start() -> {Tns, User, Password} = getTnsUserPass(), Context = dpi:context_create(3, 0), - Conn = dpi:conn_create(Context, User, Password, Tns, #{}, #{}), -[Context, Conn]. + try + Conn = dpi:conn_create( + Context, User, Password, Tns, + #{encoding => "AL32UTF8", nencoding => "AL32UTF8"}, + #{} + ), + [Context, Conn] + catch + error:{error, CSrc, Line, Details} -> + ?debugFmt( + "[~s:~p] ERROR ~p", [CSrc, Line, Details]), + throw(Details#{csrc => CSrc, line => Line}); + Class:Exception -> + ?debugFmt( + "Class ~p, Exception ~p, Context ~p", + [Class, Exception, Context] + ), + throw({Class, Exception}) + end. s() -> ?debugMsg("Performing setup."),