Skip to content

Commit 9632b94

Browse files
authored
Merge pull request #177 from erlangbureau/stage
clean up decode
2 parents 3cf0438 + ae3f982 commit 9632b94

File tree

3 files changed

+83
-95
lines changed

3 files changed

+83
-95
lines changed

src/jamdb_oracle_conn.erl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ sql_query(#oraclient{conn_state=connected} = State, {fetch, Cursor, RowFormat, L
104104
handle_resp({Cursor, RowFormat, [LastRow]}, State2);
105105
sql_query(#oraclient{conn_state=connected} = State, {Query, Bind, Batch, Fetch}) ->
106106
{ok, State2} = send_req(exec, State, {Query, Bind, Batch}),
107-
#oraclient{server=Ver, defcols=DefCol, params=RowFormat, type=Type} = State2,
108-
handle_resp(get_param(defcols, {DefCol, Ver, RowFormat, Type}),
107+
#oraclient{defcols=DefCol, params=RowFormat, type=Type} = State2,
108+
handle_resp(get_param(defcols, {DefCol, RowFormat, Type}),
109109
State2#oraclient{type=get_param(type, {Type, Fetch})});
110110
sql_query(#oraclient{conn_state=connected, timeouts={_Tout, ReadTout}} = State, {Query, Bind}) ->
111111
case lists:nth(1, string:tokens(string:to_upper(Query)," \t;")) of
@@ -276,7 +276,7 @@ handle_resp(Acc, #oraclient{socket=Socket, sdu=Length, timeouts=Touts} = State)
276276
end.
277277

278278
handle_resp(Data, Acc, #oraclient{type=Type, cursors=Cursors} = State) ->
279-
case ?DECODER:decode_token(Data, Acc) of
279+
case ?DECODER:decode_two_task(Data, Acc) of
280280
{0, _RowNumber, Cursor, {LCursor, RowFormat}, []} when Type =/= change, RowFormat =/= [] ->
281281
Type2 = if LCursor =:= Cursor -> Type; true -> cursor end,
282282
{ok, State2} = send_req(fetch, State, {Cursor, RowFormat}),
@@ -366,11 +366,9 @@ get_param(defcols, {Sum, Cursors}) when is_pid(Cursors) ->
366366
{Sum, proplists:get_value(Sum, Acc, {0,0,[]})};
367367
get_param(defcols, {_Sum, {LCursor, Cursor, _RowFormat}}) when LCursor =:= Cursor -> {LCursor, 0};
368368
get_param(defcols, {_Sum, {LCursor, Cursor, _RowFormat}}) -> {LCursor, Cursor};
369-
get_param(defcols, {{_Sum, {LCursor, Cursor, _RowFormat}}, Ver, RowFormat, Type}) when LCursor =:= 0; LCursor =/= Cursor ->
370-
{Ver, RowFormat, Type};
371-
get_param(defcols, {{_Sum, {_LCursor, _Cursor, RowFormat}}, Ver, _RowFormat, Type}) when Type =/= select ->
372-
{Ver, RowFormat, Type};
373-
get_param(defcols, {{_Sum, {LCursor, _Cursor, RowFormat}}, _Ver, _RowFormat, Type}) ->
369+
get_param(defcols, {{_Sum, {LCursor, Cursor, _RowFormat}}, RowFormat, Type}) when LCursor =:= 0; LCursor =/= Cursor ->
370+
{0, RowFormat, Type};
371+
get_param(defcols, {{_Sum, {LCursor, _Cursor, RowFormat}}, _RowFormat, Type}) ->
374372
{LCursor, RowFormat, Type};
375373
get_param(type, {true, false, [], Fetch}) -> {select, Fetch};
376374
get_param(type, {false, true, [], _Fetch}) -> {change, 0};

src/jamdb_oracle_tns_decoder.erl

Lines changed: 74 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
%% API
44
-export([decode_packet/2]).
5+
-export([decode_two_task/2]).
56
-export([decode_token/2]).
67
-export([decode_helper/3]).
78

@@ -31,7 +32,7 @@ decode_packet(<<PacketSize:16, _PacketFlags:16, Type, _Flags:8, 0:16, Rest/bits>
3132
decode_packet(_,_) ->
3233
{error, more}.
3334

34-
decode_token(<<Token, Data/binary>>, Acc) ->
35+
decode_two_task(<<Token, Data/binary>>, Acc) ->
3536
case Token of
3637
?TTI_DCB -> decode_token(dcb, Data, Acc);
3738
?TTI_IOV -> decode_token(iov, Data, Acc);
@@ -45,30 +46,8 @@ decode_token(<<Token, Data/binary>>, Acc) ->
4546
?TTI_FOB -> {error, fob}; %return
4647
_ ->
4748
{error, <<Token, (hd(binary:split(Data, <<0>>)))/binary>>}
48-
end;
49-
decode_token(net, {Data, EnvOpts}) ->
50-
Values = lists:map(fun(L) -> list_to_tuple(string:tokens(L, "=")) end,
51-
string:tokens(binary_to_list(hd(binary:split(Data, <<0>>))), "()")),
52-
Host = proplists:get_value("HOST", Values),
53-
Port = proplists:get_value("PORT", Values),
54-
{ok, [{host, Host}, {port, list_to_integer(Port)}]++EnvOpts};
55-
decode_token(rpa, Data) ->
56-
Num = decode_ub4(Data),
57-
Values = decode_keyval(decode_next(Data), Num, []),
58-
SessKey = get_value("AUTH_SESSKEY", Values),
59-
Salt = get_value("AUTH_VFR_DATA", Values),
60-
Type = get_value("AUTH_VFR_DATA", Values, 3),
61-
DerivedSalt = get_value("AUTH_PBKDF2_CSK_SALT", Values),
62-
Logon = #logon{type=Type, auth=SessKey, salt=Salt, der_salt=DerivedSalt},
63-
case get_value("AUTH_SVR_RESPONSE", Values) of
64-
undefined ->
65-
{?TTI_SESS, Logon};
66-
Resp ->
67-
Value = get_value("AUTH_VERSION_NO", Values),
68-
SessId = get_value("AUTH_SESSION_ID", Values),
69-
Ver = decode_version(Value),
70-
{?TTI_AUTH, Resp, Ver, SessId}
71-
end;
49+
end.
50+
7251
decode_token(oac, Data) ->
7352
DataType = decode_ub1(Data),
7453
A = decode_next(ub1,Data), %data type
@@ -87,17 +66,7 @@ decode_token(oac, Data) ->
8766
M = decode_next(ub1,L), %character set form
8867
N = decode_next(ub4,M), %mxlc
8968
{N, DataType, Length, Scale, Charset};
90-
decode_token(wrn, Data) ->
91-
A = decode_next(ub2,Data), %error number
92-
Length = decode_ub2(A),
93-
B = decode_next(ub2,A), %length of error message
94-
C = decode_next(ub2,B), %flags
95-
decode_next(ub1, C, Length).
96-
97-
decode_token(dcb, Data, {Ver, _RowFormat, Type}) when is_atom(Type) ->
98-
{A, RowFormat} = decode_token(dcb, decode_next(Data), Ver),
99-
decode_token(A, {0, RowFormat, []});
100-
decode_token(dcb, Data, Ver) ->
69+
decode_token(dcb, Data) ->
10170
A = decode_next(ub4,Data),
10271
Num = decode_ub4(A),
10372
B = decode_next(ub4,A),
@@ -106,21 +75,50 @@ decode_token(dcb, Data, Ver) ->
10675
0 -> B;
10776
_ -> decode_next(ub1,B)
10877
end,
109-
{RowFormat, D} = decode_token(uds, C, {Ver, [], Num}),
78+
{RowFormat, D} = decode_token(uds, C, {[], Num}),
11079
E = decode_next(dalc,D), %flag
11180
F = decode_next(ub4,E), %mdbz
11281
G = decode_next(ub4,F), %mnpr
11382
J = decode_next(ub4,G), %mxpr
11483
K = decode_next(ub4,J),
115-
L =
116-
case Ver of
117-
10 -> K;
118-
_ -> decode_next(dalc,K)
119-
end,
84+
L = decode_next(dalc,K),
12085
{L, RowFormat};
121-
decode_token(uds, Data, {_Ver, RowFormat, 0}) ->
86+
decode_token(net, {Data, EnvOpts}) ->
87+
Values = lists:map(fun(L) -> list_to_tuple(string:tokens(L, "=")) end,
88+
string:tokens(binary_to_list(hd(binary:split(Data, <<0>>))), "()")),
89+
Host = proplists:get_value("HOST", Values),
90+
Port = proplists:get_value("PORT", Values),
91+
{ok, [{host, Host}, {port, list_to_integer(Port)}]++EnvOpts};
92+
decode_token(rpa, Data) ->
93+
Num = decode_ub4(Data),
94+
Values = decode_keyval(decode_next(Data), Num, []),
95+
SessKey = get_value("AUTH_SESSKEY", Values),
96+
Salt = get_value("AUTH_VFR_DATA", Values),
97+
Type = get_value("AUTH_VFR_DATA", Values, 3),
98+
DerivedSalt = get_value("AUTH_PBKDF2_CSK_SALT", Values),
99+
Logon = #logon{type=Type, auth=SessKey, salt=Salt, der_salt=DerivedSalt},
100+
case get_value("AUTH_SVR_RESPONSE", Values) of
101+
undefined ->
102+
{?TTI_SESS, Logon};
103+
Resp ->
104+
Value = get_value("AUTH_VERSION_NO", Values),
105+
SessId = get_value("AUTH_SESSION_ID", Values),
106+
Ver = decode_version(Value),
107+
{?TTI_AUTH, Resp, Ver, SessId}
108+
end;
109+
decode_token(wrn, Data) ->
110+
A = decode_next(ub2,Data), %error number
111+
Length = decode_ub2(A),
112+
B = decode_next(ub2,A), %length of error message
113+
C = decode_next(ub2,B), %flags
114+
decode_next(ub1, C, Length).
115+
116+
decode_token(dcb, Data, {_Cursor, _RowFormat, Type}) when is_atom(Type) ->
117+
{A, RowFormat} = decode_token(dcb, decode_next(Data)),
118+
decode_two_task(A, {0, RowFormat, []});
119+
decode_token(uds, Data, {RowFormat, 0}) ->
122120
{lists:reverse(RowFormat), Data};
123-
decode_token(uds, Data, {Ver, RowFormat, Num}) ->
121+
decode_token(uds, Data, {RowFormat, Num}) ->
124122
{A, DataType, Length, Scale, Charset} = decode_token(oac, Data),
125123
B = decode_next(ub1,A), %nulls allowed
126124
C = decode_next(ub1,B), %v7 length of name
@@ -129,16 +127,12 @@ decode_token(uds, Data, {Ver, RowFormat, Num}) ->
129127
E = decode_next(dalc,D), %schema name
130128
F = decode_next(dalc,E), %type name
131129
G = decode_next(ub2,F), %column position
132-
J =
133-
case Ver of
134-
10 -> G;
135-
_ -> decode_next(ub4,G)
136-
end,
137-
decode_token(uds, J, {Ver, [#format{column_name=list_to_binary(Column),
130+
J = decode_next(ub4,G),
131+
decode_token(uds, J, {[#format{column_name=list_to_binary(Column),
138132
data_type=DataType,data_length=Length,data_scale=Scale,charset=Charset}|RowFormat], Num-1});
139133
decode_token(rxh, Data, {Cursor, RowFormat, Type}) when is_atom(Type) ->
140134
A = decode_next(rxh,Data),
141-
decode_token(A, {Cursor, RowFormat, [0], []});
135+
decode_two_task(A, {Cursor, RowFormat, [0], []});
142136
decode_token(rxh, Data, {Cursor, RowFormat, Rows}) ->
143137
A = decode_next(ub1,Data), %flag
144138
B = decode_next(ub2,A), %num requests
@@ -152,40 +146,40 @@ decode_token(rxh, Data, {Cursor, RowFormat, Rows}) ->
152146
_ -> decode_bvc(list_to_binary(Bitvec), RowFormat, [])
153147
end,
154148
F = decode_next(rxh,Data),
155-
decode_token(F, {Cursor, RowFormat, Bvc, Rows});
156-
decode_token(iov, Data, {Ver, RowFormat, Type}) when is_atom(Type) ->
149+
decode_two_task(F, {Cursor, RowFormat, Bvc, Rows});
150+
decode_token(iov, Data, {Cursor, RowFormat, Type}) when is_atom(Type) ->
157151
A = decode_next(ub1,Data),
158152
Num = decode_ub2(A),
159153
<<Mode:Num/binary, Rest/bits>> = decode_next(rxh,Data),
160154
case binary:matches(Mode,[<<16>>,<<48>>]) of
161-
[] -> decode_token(Rest, {0, [], []}); %proc_result
155+
[] -> decode_two_task(Rest, {0, [], []}); %proc_result
162156
_ ->
163157
Bind = lists:zip(binary_to_list(Mode), RowFormat),
164-
decode_token(Rest, {Ver, [decode_param(B) || B <- Bind], Type})
158+
decode_two_task(Rest, {Cursor, [decode_param(B) || B <- Bind], Type})
165159
end;
166-
decode_token(rxd, Data, {Ver, _RowFormat, fetch}) ->
167-
{A, CursorRowFormat} = decode_token(dcb, decode_next(ub1,Data), Ver),
160+
decode_token(rxd, Data, {_Cursor, _RowFormat, fetch}) ->
161+
{A, CursorRowFormat} = decode_token(dcb, decode_next(ub1,Data)),
168162
Cursor = decode_ub4(A),
169163
B = decode_next(ub4,A),
170164
{{Cursor, CursorRowFormat}, decode_next(ub2,B)};
171-
decode_token(rxd, Data, {Ver, _RowFormat, cursor}) ->
172-
{A, _CursorRowFormat} = decode_token(dcb, decode_next(ub1,Data), Ver),
165+
decode_token(rxd, Data, {_Cursor, _RowFormat, cursor}) ->
166+
{A, _CursorRowFormat} = decode_token(dcb, decode_next(ub1,Data)),
173167
{decode_ub4(A), decode_next(ub4,A)};
174-
decode_token(rxd, Data, {Ver, RowFormat, Type}) when is_atom(Type) ->
175-
case decode_data(Data, [], {[], RowFormat, Ver, Type}) of
168+
decode_token(rxd, Data, {_Cursor, RowFormat, Type}) when is_atom(Type) ->
169+
case decode_data(Data, [], {[], RowFormat, Type}) of
176170
{{Cursor, CursorRowFormat}, A} ->
177-
decode_token(A, {Cursor, CursorRowFormat, []}); %fetch cursor
178-
{Rows, A} -> decode_token(A, {0, RowFormat, Rows}) %proc_result
171+
decode_two_task(A, {Cursor, CursorRowFormat, []}); %fetch cursor
172+
{Rows, A} -> decode_two_task(A, {0, RowFormat, Rows}) %proc_result
179173
end;
180174
decode_token(rxd, Data, {Cursor, RowFormat, Bvc, Rows}) ->
181175
LastRow = last(Rows),
182176
{A, Row} = decode_rxd(Data, RowFormat, 1, Bvc, LastRow, []),
183-
decode_token(A, {Cursor, RowFormat, Bvc, Rows++[Row]});
177+
decode_two_task(A, {Cursor, RowFormat, Bvc, Rows++[Row]});
184178
decode_token(bvc, Data, {Cursor, RowFormat, _Bvc, Rows}) ->
185179
A = decode_next(ub2,Data),
186180
{Bvc, Num} = decode_bvc(A, RowFormat, []),
187181
B = decode_next(ub1, A, Num),
188-
decode_token(B, {Cursor, RowFormat, Bvc, Rows});
182+
decode_two_task(B, {Cursor, RowFormat, Bvc, Rows});
189183
decode_token(lob, Data, _Loc) ->
190184
try decode_chr(Data) of
191185
Value -> {ok, Value}
@@ -194,7 +188,7 @@ decode_token(lob, Data, _Loc) ->
194188
end;
195189
decode_token(rpa, Data, []) ->
196190
{ok, [decode_ub4(Data)]};
197-
decode_token(rpa, Data, {_Ver, RowFormat, Type}) when is_atom(Type) ->
191+
decode_token(rpa, Data, {_Cursor, RowFormat, Type}) when is_atom(Type) ->
198192
decode_token(rpa, Data, {0, RowFormat, []});
199193
decode_token(rpa, Data, {0, RowFormat, Rows}) ->
200194
Cursor =
@@ -207,15 +201,15 @@ decode_token(rpa, Data, {0, RowFormat, Rows}) ->
207201
decode_ub4(C)
208202
end,
209203
D = decode_next(rpa,Data),
210-
decode_token(D, {Cursor, RowFormat, Rows});
204+
decode_two_task(D, {Cursor, RowFormat, Rows});
211205
decode_token(rpa, Data, {Cursor, RowFormat, _Bvc, Rows}) ->
212206
decode_token(rpa, Data, {Cursor, RowFormat, Rows});
213207
decode_token(rpa, Data, {Cursor, RowFormat, Rows}) ->
214208
A = decode_next(rpa,Data),
215-
decode_token(A, {Cursor, RowFormat, Rows});
209+
decode_two_task(A, {Cursor, RowFormat, Rows});
216210
decode_token(oer, Data, []) ->
217211
decode_token(oer, Data, {0, [], []});
218-
decode_token(oer, Data, {_Ver, RowFormat, Type}) when is_atom(Type) ->
212+
decode_token(oer, Data, {_Cursor, RowFormat, Type}) when is_atom(Type) ->
219213
decode_token(oer, Data, {0, RowFormat, []});
220214
decode_token(oer, Data, {Cursor, RowFormat, _Bvc, Rows}) ->
221215
decode_token(oer, Data, {Cursor, RowFormat, Rows});
@@ -369,22 +363,22 @@ decode_bvc(Data, Acc, Num, I) when is_list(Acc) ->
369363
Bvc = decode_bvc(decode_ub1(Data), {}, 0, I),
370364
decode_bvc(decode_next(ub1,Data), Acc++tuple_to_list(Bvc), Num-1, I+1).
371365

372-
decode_data(Data, Values, {[], [], _Ver, _Type}) ->
366+
decode_data(Data, Values, {[], [], _Type}) ->
373367
{lists:reverse(Values), Data};
374-
decode_data(Data, _Values, {DefCol, [], _Ver, _Type}) ->
368+
decode_data(Data, _Values, {DefCol, [], _Type}) ->
375369
{DefCol, Data};
376-
decode_data(Data, Values, {DefCol, [#format{param=in}|RestRowFormat], Ver, Type}) ->
377-
decode_data(Data, Values, {DefCol, RestRowFormat, Ver, Type});
378-
decode_data(Data, Values, {_DefCol, [#format{data_type=?TNS_TYPE_REFCURSOR}|RestRowFormat], Ver, Type}) ->
379-
{DefCol, Bin} = decode_token(rxd, Data, {Ver, [], fetch}),
380-
decode_data(Bin, Values, {DefCol, RestRowFormat, Ver, Type});
381-
decode_data(Data, Values, {DefCol, [ValueFormat|RestRowFormat], Ver, Type=return}) ->
370+
decode_data(Data, Values, {DefCol, [#format{param=in}|RestRowFormat], Type}) ->
371+
decode_data(Data, Values, {DefCol, RestRowFormat, Type});
372+
decode_data(Data, Values, {_DefCol, [#format{data_type=?TNS_TYPE_REFCURSOR}|RestRowFormat], Type}) ->
373+
{DefCol, Bin} = decode_token(rxd, Data, {0, [], fetch}),
374+
decode_data(Bin, Values, {DefCol, RestRowFormat, Type});
375+
decode_data(Data, Values, {DefCol, [ValueFormat|RestRowFormat], Type=return}) ->
382376
Num = decode_ub4(Data),
383377
{Value, RestData} = decode_data(decode_next(ub4,Data), ValueFormat, [], Num, Type),
384-
decode_data(RestData, [Value|Values], {DefCol, RestRowFormat, Ver, Type});
385-
decode_data(Data, Values, {DefCol, [ValueFormat|RestRowFormat], Ver, Type=block}) ->
378+
decode_data(RestData, [Value|Values], {DefCol, RestRowFormat, Type});
379+
decode_data(Data, Values, {DefCol, [ValueFormat|RestRowFormat], Type=block}) ->
386380
{Value, RestData} = decode_data(Data, ValueFormat),
387-
decode_data(decode_next(ub2,RestData), [Value|Values], {DefCol, RestRowFormat, Ver, Type}).
381+
decode_data(decode_next(ub2,RestData), [Value|Values], {DefCol, RestRowFormat, Type}).
388382

389383
decode_data(Data, _ValueFormat, Values, 0, _Type) ->
390384
{lists:reverse(Values), Data};

src/jamdb_oracle_tns_encoder.erl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ encode_record(dty, #oraclient{charset=Charset}) ->
167167
encode_record(pro, _EnvOpts) ->
168168
<<
169169
?TTI_PRO,
170-
6,5,4,3,2,1,0,
171-
98,101,97,109,0
170+
6,0,98,101,97,109,0
172171
>>;
173172
encode_record(spfp, #oraclient{seq=Tseq}) ->
174173
<<
@@ -202,7 +201,7 @@ encode_record(fetch, #oraclient{fetch=Fetch,req=Cursor,seq=Tseq}) ->
202201
(encode_sb4(Cursor))/binary, %cursor
203202
(encode_sb4(Fetch))/binary %rows to fetch
204203
>>;
205-
encode_record(exec, #oraclient{type=Type,auto=Auto,charset=Charset,fetch=Fetch,server=Ver,req={Cursor,Query,Bind,Batch,Def},seq=Tseq}) ->
204+
encode_record(exec, #oraclient{type=Type,auto=Auto,charset=Charset,fetch=Fetch,req={Cursor,Query,Bind,Batch,Def},seq=Tseq}) ->
206205
QueryData = encode_str(Query),
207206
QueryLen = if Cursor =/= 0 -> 0; true -> byte_size(QueryData) end,
208207
BindLen = length(Bind),
@@ -242,10 +241,7 @@ encode_record(exec, #oraclient{type=Type,auto=Auto,charset=Charset,fetch=Fetch,s
242241
(encode_sb4(DefLen))/binary, %defcols count
243242
0, %registration
244243
0,1,
245-
(case Ver of
246-
10 -> <<>>;
247-
_ -> <<0,0,0,0,0>>
248-
end)/binary,
244+
0,0,0,0,0,
249245
(if QueryLen =/= 0 -> QueryData; true -> <<>> end)/binary,
250246
(encode_array(All8))/binary,
251247
(case {BindLen, DefLen, QueryLen} of

0 commit comments

Comments
 (0)