| 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_text). |
| 17 |
|
|
| 18 |
|
-feature(maybe_expr, enable). |
| 19 |
|
|
| 20 |
|
-export([decode/1]). |
| 21 |
|
-export([encode/1]). |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
decode(#{type := Type}) |
| 25 |
|
when Type == long; |
| 26 |
|
Type == longlong; |
| 27 |
|
Type == short; |
| 28 |
|
Type == year; |
| 29 |
|
Type == tiny -> |
| 30 |
:-( |
decoder(fun erlang:binary_to_integer/1); |
| 31 |
|
|
| 32 |
|
decode(#{type := Type}) |
| 33 |
|
when Type == float; |
| 34 |
|
Type == double; |
| 35 |
|
Type == decimal; |
| 36 |
|
Type == newdecimal -> |
| 37 |
:-( |
decoder(fun erlang:binary_to_float/1); |
| 38 |
|
|
| 39 |
|
decode(#{type := Type}) |
| 40 |
|
when Type == string; |
| 41 |
|
Type == var_string; |
| 42 |
|
Type == varchar; |
| 43 |
|
Type == long_blob; |
| 44 |
|
Type == blob -> |
| 45 |
:-( |
fun |
| 46 |
|
(Encoded) -> |
| 47 |
:-( |
{<<>>, Encoded} |
| 48 |
|
end. |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
decoder(Decoder) -> |
| 52 |
:-( |
fun |
| 53 |
|
(null) -> |
| 54 |
:-( |
{<<>>, null}; |
| 55 |
|
|
| 56 |
|
(Encoded) -> |
| 57 |
:-( |
{<<>>, Decoder(Encoded)} |
| 58 |
|
end. |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
encode(#{type := Type}) |
| 62 |
|
when Type == long; |
| 63 |
|
Type == longlong; |
| 64 |
|
Type == short; |
| 65 |
|
Type == year; |
| 66 |
|
Type == tiny -> |
| 67 |
:-( |
fun erlang:integer_to_binary/1; |
| 68 |
|
|
| 69 |
|
encode(#{type := Type}) |
| 70 |
|
when Type == float; |
| 71 |
|
Type == double; |
| 72 |
|
Type == decimal; |
| 73 |
|
Type == newdecimal -> |
| 74 |
:-( |
fun |
| 75 |
|
(Decoded) -> |
| 76 |
:-( |
float_to_binary(Decoded, [short]) |
| 77 |
|
end; |
| 78 |
|
|
| 79 |
|
encode(#{type := Type}) |
| 80 |
|
when Type == string; |
| 81 |
|
Type == var_string; |
| 82 |
|
Type == varchar; |
| 83 |
|
Type == long_blob; |
| 84 |
|
Type == blob -> |
| 85 |
:-( |
fun |
| 86 |
|
(Encoded) -> |
| 87 |
:-( |
Encoded |
| 88 |
|
end. |