1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(msmp_com_stmt_execute). |
17 |
|
|
18 |
|
|
19 |
|
-export([encode/1]). |
20 |
|
|
21 |
|
|
22 |
|
command() -> |
23 |
:-( |
<<16#17>>. |
24 |
|
|
25 |
|
|
26 |
|
encode(#{query_attributes := QueryAttributes}) -> |
27 |
:-( |
fun |
28 |
|
(Decoded) -> |
29 |
:-( |
(narcs_sequence:sequence( |
30 |
|
[narcs_bytes:tag(command()), |
31 |
|
narcs_combinator:v( |
32 |
|
statement_id, |
33 |
|
msmp_integer_fixed:encode(4)), |
34 |
|
narcs_combinator:v( |
35 |
|
flags, |
36 |
|
msmp_integer_fixed:encode(1)), |
37 |
|
narcs_combinator:v( |
38 |
|
iteration_count, |
39 |
|
msmp_integer_fixed:encode(4)), |
40 |
|
narcs_combinator:condition( |
41 |
|
QueryAttributes, |
42 |
|
narcs_combinator:v( |
43 |
|
parameters, |
44 |
|
narcs_combinator:map_result( |
45 |
|
fun |
46 |
|
(Parameters) -> |
47 |
:-( |
length(Parameters) |
48 |
|
end, |
49 |
|
msmp_integer_variable:encode()))), |
50 |
|
narcs_combinator:v( |
51 |
|
parameters, |
52 |
|
msmp_null_bitmap:encode()), |
53 |
|
narcs_combinator:v( |
54 |
|
new_params_bind_flag, |
55 |
|
msmp_integer_fixed:encode(1)), |
56 |
|
types(), |
57 |
|
narcs_combinator:condition( |
58 |
|
QueryAttributes, |
59 |
|
names()), |
60 |
|
values()]))(Decoded) |
61 |
|
end. |
62 |
|
|
63 |
|
|
64 |
|
types() -> |
65 |
:-( |
fun |
66 |
|
(#{types := Types, parameters := Parameters}) -> |
67 |
:-( |
lists:map( |
68 |
|
fun |
69 |
|
({#{type := Type, flags := #{unsigned := Unsigned}}, Value}) -> |
70 |
:-( |
[msmp_field:lookup(type(Type, Value)), |
71 |
|
case Unsigned of |
72 |
|
true -> |
73 |
:-( |
16#80; |
74 |
|
|
75 |
|
false -> |
76 |
:-( |
0 |
77 |
|
end] |
78 |
|
end, |
79 |
|
lists:zip(Types, Parameters)) |
80 |
|
end. |
81 |
|
|
82 |
|
|
83 |
|
names() -> |
84 |
:-( |
fun |
85 |
|
(#{types := Types}) -> |
86 |
:-( |
lists:map( |
87 |
|
fun |
88 |
|
(#{name := Name}) -> |
89 |
:-( |
(msmp_string_length_encoded:encode())(Name) |
90 |
|
end, |
91 |
|
Types) |
92 |
|
end. |
93 |
|
|
94 |
|
|
95 |
|
values() -> |
96 |
:-( |
fun |
97 |
|
(#{types := Types, parameters := Parameters}) -> |
98 |
:-( |
lists:filtermap( |
99 |
|
fun |
100 |
|
({_, null}) -> |
101 |
:-( |
false; |
102 |
|
|
103 |
|
({Definition, Parameter}) -> |
104 |
:-( |
{true, |
105 |
|
(msmp_binary:encode( |
106 |
|
type(Definition, Parameter))) |
107 |
|
(Parameter)} |
108 |
|
end, |
109 |
|
lists:zip(Types, Parameters)) |
110 |
|
end. |
111 |
|
|
112 |
|
|
113 |
|
type(#{type := null = Type} = Definition, Parameter) -> |
114 |
:-( |
Definition#{type := type(Type, Parameter)}; |
115 |
|
|
116 |
|
type(#{type := _} = Definition, _) -> |
117 |
:-( |
Definition; |
118 |
|
|
119 |
|
type(null, Parameter) when is_integer(Parameter) -> |
120 |
:-( |
longlong; |
121 |
|
|
122 |
|
type(null, Parameter) when is_float(Parameter) -> |
123 |
:-( |
double; |
124 |
|
|
125 |
|
type(null, Parameter) when is_binary(Parameter) -> |
126 |
:-( |
varchar; |
127 |
|
|
128 |
|
type(Type, _) when is_atom(Type) -> |
129 |
:-( |
Type. |