From b45bc71a5896cc2c33af731898674e7b3ff04bc1 Mon Sep 17 00:00:00 2001 From: Agustin Cautin Date: Tue, 6 Aug 2019 09:31:05 +0200 Subject: [PATCH] Added formatting to eliminate extra zeros from odpi numbers. #604 --- src/dderlodpi.erl | 56 +++++++++++++++++++++++++++++++++++++++------ src/gen_adapter.erl | 28 +++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/dderlodpi.erl b/src/dderlodpi.erl index 490043d7..6a7dee38 100644 --- a/src/dderlodpi.erl +++ b/src/dderlodpi.erl @@ -644,6 +644,49 @@ cols_to_rec([#{ , type = Type , prec = FsPrec , readonly = ReadOnly} | cols_to_rec(Rest, NewFields)]; +cols_to_rec([#{ + name := AliasStr, + typeInfo := #{ + oracleTypeNum := 'DPI_ORACLE_TYPE_NUMBER', + precision := 63, + scale := -127 + }} | Rest], Fields +) -> + Alias = list_to_binary(AliasStr), + {Tag, ReadOnly, NewFields} = find_original_field(Alias, Fields), + [#stmtCol{ tag = Tag + , alias = Alias + , type = 'DPI_ORACLE_TYPE_NUMBER' + , len = 19 + , prec = dynamic + , readonly = ReadOnly} | cols_to_rec(Rest, NewFields)]; +cols_to_rec([#{ + name := AliasStr, + typeInfo := #{ + oracleTypeNum := 'DPI_ORACLE_TYPE_NUMBER', + scale := -127 + }} | Rest], Fields +) -> + Alias = list_to_binary(AliasStr), + {Tag, ReadOnly, NewFields} = find_original_field(Alias, Fields), + [#stmtCol{ tag = Tag + , alias = Alias + , type = 'DPI_ORACLE_TYPE_NUMBER' + , len = 38 + , prec = dynamic + , readonly = ReadOnly} | cols_to_rec(Rest, NewFields)]; +cols_to_rec([#{ + name := AliasStr, + typeInfo := #{ + oracleTypeNum := Type + }} | Rest], Fields +) when Type =:= 'DPI_ORACLE_TYPE_NATIVE_DOUBLE'; Type =:= 'DPI_ORACLE_TYPE_NATIVE_FLOAT' -> + Alias = list_to_binary(AliasStr), + {Tag, ReadOnly, NewFields} = find_original_field(Alias, Fields), + [#stmtCol{ tag = Tag + , alias = Alias + , type = Type + , readonly = ReadOnly} | cols_to_rec(Rest, NewFields)]; cols_to_rec([#{ name := AliasStr, typeInfo := #{ @@ -676,13 +719,12 @@ translate_datatype(Stmt, [R | RestRow], [#stmtCol{type = 'DPI_ORACLE_TYPE_TIMEST [dpi_to_dderlts(R) | translate_datatype(Stmt, RestRow, RestCols)]; translate_datatype(Stmt, [R | RestRow], [#stmtCol{type = 'DPI_ORACLE_TYPE_DATE'} | RestCols]) -> [dpi_to_dderltime(R) | translate_datatype(Stmt, RestRow, RestCols)]; -translate_datatype(Stmt, [Mantissa | RestRow], [#stmtCol{type = 'SQLT_NUM', len = Scale, prec = dynamic} | RestCols]) -> - %% Float / Real type or unlimited numbers. - Number = dderloci_utils:clean_dynamic_prec(imem_datatype:decimal_to_io(Mantissa, Scale)), - [Number | translate_datatype(Stmt, RestRow, RestCols)]; -translate_datatype(Stmt, [Mantissa | RestRow], [#stmtCol{type = 'SQLT_NUM', prec = Prec} | RestCols]) -> - Number = imem_datatype:decimal_to_io(Mantissa, Prec), - [Number | translate_datatype(Stmt, RestRow, RestCols)]; +translate_datatype(Stmt, [Number | RestRow], [#stmtCol{type = Type} | RestCols]) when + Type =:= 'DPI_ORACLE_TYPE_NUMBER'; + Type =:= 'DPI_ORACLE_TYPE_NATIVE_DOUBLE'; + Type =:= 'DPI_ORACLE_TYPE_NATIVE_FLOAT' -> + Result = dderloci_utils:clean_dynamic_prec(float_to_binary(Number, [{decimals,20}, compact])), + [Result | translate_datatype(Stmt, RestRow, RestCols)]; translate_datatype(Stmt, [{_Pointer, Size, Path, Name} | RestRow], [#stmtCol{type = 'SQLT_BFILEE'} | RestCols]) -> SizeBin = integer_to_binary(Size), [<> | translate_datatype(Stmt, RestRow, RestCols)]; diff --git a/src/gen_adapter.erl b/src/gen_adapter.erl index b44649bc..03c100da 100644 --- a/src/gen_adapter.erl +++ b/src/gen_adapter.erl @@ -756,6 +756,34 @@ build_column_json([C|Cols], JCols, Counter) -> 'SQLT_LVC' -> Type = <<"text">>; 'SQLT_CLOB'-> Type = <<"text">>; 'SQLT_VST' -> Type = <<"text">>; +%% Oracle odpi types: + 'DPI_ORACLE_TYPE_VARCHAR' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_NVARCHAR' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_CHAR' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_NCHAR' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_ROWID' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_RAW' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_NATIVE_FLOAT' -> Type = <<"numeric">>; + 'DPI_ORACLE_TYPE_NATIVE_DOUBLE' -> Type = <<"numeric">>; + 'DPI_ORACLE_TYPE_NATIVE_INT' -> Type = <<"numeric">>; + 'DPI_ORACLE_TYPE_NATIVE_UINT' -> Type = <<"numeric">>; + 'DPI_ORACLE_TYPE_NUMBER' -> Type = <<"numeric">>; + 'DPI_ORACLE_TYPE_DATE' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_TIMESTAMP' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_TIMESTAMP_TZ' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_TIMESTAMP_LTZ' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_INTERVAL_DS' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_INTERVAL_YM' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_CLOB' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_NCLOB' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_BLOB' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_BFILE' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_STMT' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_BOOLEAN' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_OBJECT' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_LONG_VARCHAR' -> Type = <<"text">>; + 'DPI_ORACLE_TYPE_LONG_RAW' -> Type = <<"text">>; +%% Unknown types: _ -> Type = <<"undefined">> end, JC = [{<<"id">>, Nm1},