24
24
% % OTHER DEALINGS IN THE SOFTWARE.
25
25
-module (emysql_util ).
26
26
-export ([field_names /1 , as_record /4 , as_record /3 , length_coded_binary /1 , length_coded_string /1 ,
27
- null_terminated_string /2 , asciz /1 , bxor_binary /2 , dualmap /3 , hash /1 ,
27
+ null_terminated_string /2 , asciz /1 , bxor_binary /2 , dualmap /3 , hash /1 , to_binary / 1 ,
28
28
rnd /3 , encode /1 , encode /2 , quote /1 ]).
29
- -compile (export_all ).
30
29
31
30
-include (" emysql.hrl" ).
32
31
@@ -160,97 +159,51 @@ encode(Val) ->
160
159
161
160
% % @doc Encode a value so that it can be included safely in a MySQL query.
162
161
% % @spec encode(term(), list | binary) -> string() | binary() | {error, Error}
163
- encode (Val , ReturnType ) when is_atom (Val ) ->
164
- encode (atom_to_list (Val ), ReturnType , latin1 ); % todo: latin1?
165
-
166
- encode (Val , ReturnType ) ->
167
- encode (Val , ReturnType , latin1 ).
168
-
169
- encode (null , list , _ ) ->
162
+ encode (null , list ) ->
170
163
" null" ;
171
-
172
- encode (undefined , list , _ ) ->
164
+ encode (undefined , list ) ->
173
165
" null" ;
174
-
175
- encode (null , binary , _ ) ->
166
+ encode (null , binary ) ->
176
167
<<" null" >>;
177
-
178
- encode (undefined , binary , _ ) ->
168
+ encode (undefined , binary ) ->
179
169
<<" null" >>;
180
-
181
- encode (Val , list , latin1 ) when is_binary (Val ) ->
170
+ encode (Val , list ) when is_binary (Val ) ->
182
171
quote (binary_to_list (Val ));
183
-
184
- encode (Val , list , Encoding ) when is_binary (Val ) ->
185
- quote (unicode :characters_to_list (Val , Encoding ));
186
-
187
- encode (Val , binary , Encoding ) when is_atom (Val ) ->
188
- encode (atom_to_list (Val ), binary , Encoding );
189
-
190
- encode (Val , binary , latin1 ) when is_list (Val ) ->
191
- list_to_binary (quote (Val ));
192
-
193
- encode (Val , binary , Encoding ) when is_list (Val ) ->
194
- unicode :characters_to_binary (quote (Val ), Encoding , Encoding );
195
-
196
-
197
- encode (Val , binary , latin1 ) when is_binary (Val ) ->
198
- % -% io:format("encode latin-1 in : ~s = ~w ~n", [Val, Val]),
199
- X = list_to_binary (quote (binary_to_list (Val ))),
200
- % -% io:format("encode latin-1 out: ~s = ~w ~n", [X, X]),
201
- X ;
202
-
203
- encode (Val , binary , Encoding ) when is_binary (Val ) ->
204
- case unicode :characters_to_list (Val ,Encoding ) of
205
- {error ,E1 ,E2 } -> exit ({invalid_utf8_binary , E1 , E2 });
206
- {incomplete ,E1 ,E2 } -> exit ({invalid_utf8_binary , E1 , E2 });
207
- List ->
208
- unicode :characters_to_binary (quote (List ),Encoding ,Encoding )
209
- end ;
210
-
211
- encode (Val , list , _ ) when is_list (Val ) ->
212
- % -% io:format("encode list in : ~s = ~w ~n", [Val, Val]),
213
- % -% io:format("encode list out: ~s = ~w ~n", [quote(Val), quote(Val)]),
172
+ encode (Val , binary ) when is_atom (Val ) ->
173
+ encode (atom_to_list (Val ), binary );
174
+ encode (Val , binary ) when is_list (Val ) ->
175
+ list_to_binary (quote (Val ));
176
+ encode (Val , binary ) when is_binary (Val ) ->
177
+ list_to_binary (quote (binary_to_list (Val )));
178
+ encode (Val , list ) when is_list (Val ) ->
214
179
quote (Val );
215
-
216
- encode (Val , list , _ ) when is_integer (Val ) ->
180
+ encode (Val , list ) when is_integer (Val ) ->
217
181
integer_to_list (Val );
218
-
219
- encode (Val , binary , _ ) when is_integer (Val ) ->
182
+ encode (Val , binary ) when is_integer (Val ) ->
220
183
list_to_binary (integer_to_list (Val ));
221
-
222
- encode (Val , list , _ ) when is_float (Val ) ->
184
+ encode (Val , list ) when is_float (Val ) ->
223
185
[Res ] = io_lib :format (" ~w " , [Val ]),
224
186
Res ;
225
-
226
- encode (Val , binary , _ ) when is_float (Val ) ->
187
+ encode (Val , binary ) when is_float (Val ) ->
227
188
iolist_to_binary (io_lib :format (" ~w " , [Val ]));
228
-
229
- encode ({datetime , Val }, ReturnType , Encoding ) ->
230
- encode (Val , ReturnType , Encoding );
231
-
232
- encode ({date , Val }, ReturnType , Encoding ) ->
233
- encode (Val , ReturnType , Encoding );
234
-
235
- encode ({time , Val }, ReturnType , Encoding ) ->
236
- encode (Val , ReturnType , Encoding );
237
-
238
- encode ({{Year , Month , Day }, {Hour , Minute , Second }}, list , _ ) ->
189
+ encode ({datetime , Val }, ReturnType ) ->
190
+ encode (Val , ReturnType );
191
+ encode ({date , Val }, ReturnType ) ->
192
+ encode (Val , ReturnType );
193
+ encode ({time , Val }, ReturnType ) ->
194
+ encode (Val , ReturnType );
195
+ encode ({{Year , Month , Day }, {Hour , Minute , Second }}, list ) ->
239
196
Res = io_lib :format (" '~4.4.0w -~2.2.0w -~2.2.0w ~2.2.0w :~2.2.0w :~2.2.0w '" ,
240
197
[Year , Month , Day , Hour , Minute , Second ]),
241
198
lists :flatten (Res );
242
-
243
- encode ({{_Year , _Month , _Day }, {_Hour , _Minute , _Second }}= Val , binary , E ) ->
244
- list_to_binary (encode (Val , list , E ));
245
-
246
- encode ({Time1 , Time2 , Time3 }, list , _ ) ->
199
+ encode ({{_Year , _Month , _Day }, {_Hour , _Minute , _Second }}= Val , binary ) ->
200
+ list_to_binary (encode (Val , list ));
201
+ encode ({Time1 , Time2 , Time3 }, list ) ->
247
202
Res = two_digits ([Time1 , Time2 , Time3 ]),
248
203
lists :flatten (Res );
249
-
250
- encode ({_Time1 , _Time2 , _Time3 }= Val , binary , E ) ->
251
- list_to_binary (encode (Val , list , E ));
252
-
253
- encode (Val , _ , _ ) ->
204
+ encode ({_Time1 , _Time2 , _Time3 }= Val , binary ) ->
205
+ list_to_binary (encode (Val , list ));
206
+ encode (Val , _ ) ->
254
207
{error , {unrecognized_value , Val }}.
255
208
256
209
% % @private
@@ -273,23 +226,6 @@ two_digits(Num) ->
273
226
quote (String ) when is_list (String ) ->
274
227
[39 | lists :reverse ([39 | quote_loop (String )])]. % % 39 is $'
275
228
276
- quote (String , _ ) when is_list (String ) ->
277
- quote (String );
278
-
279
- quote (Any , Pool ) when is_record (Pool ,pool ) ->
280
- quote (Any , Pool # pool .encoding );
281
-
282
- quote (Bin , latin1 ) when is_binary (Bin ) ->
283
- list_to_binary (quote (binary_to_list (Bin )));
284
-
285
- quote (Bin , Encoding ) when is_binary (Bin ) ->
286
- case unicode :characters_to_list (Bin ,Encoding ) of
287
- {error ,E1 ,E2 } -> exit ({invalid_utf8_binary , E1 , E2 });
288
- List ->
289
- unicode :characters_to_binary (quote (List ),Encoding ,Encoding )
290
- end .
291
- % note:quote is a codepoint-wise inspection (each is a number) that also works for Unicode.
292
-
293
229
% % @doc Make MySQL-safe backslash escapes before 10, 13, \, 26, 34, 39.
294
230
% % @spec quote_loop(list()) -> list()
295
231
% % @private
@@ -333,28 +269,5 @@ quote_loop([C | Rest], Acc) ->
333
269
% % can be used to determine if data can be interpreted as UTF-8.
334
270
% % Source: http://www.erlang.org/doc/apps/stdlib/unicode_usage.html
335
271
336
- anybin_to_list (Bin ) when is_binary (Bin ) ->
337
- case unicode :characters_to_binary (Bin ,utf8 ,utf8 ) of
338
- Bin -> unicode :characters_to_list (Bin );
339
- _ -> binary_to_list (Bin )
340
- end .
341
-
342
- any_to_binary (L ) when is_binary (L ) ->
343
- L ;
344
- any_to_binary (L ) when is_list (L ) ->
345
- case unicode :characters_to_binary (L ) of
346
- {error ,_ ,_ } -> list_to_binary (L );
347
- B -> case unicode :characters_to_list (B ,utf8 ) of
348
- L -> B ;
349
- _ -> list_to_binary (L )
350
- end
351
- end .
352
-
353
- to_binary (L ,_ ) when is_binary (L ) ->
354
- L ;
355
-
356
- to_binary (L ,latin1 ) when is_list (L ) ->
357
- list_to_binary (L );
358
-
359
- to_binary (L ,Encoding ) when is_list (L ) ->
360
- unicode :characters_to_binary (L ,Encoding ,Encoding ).
272
+ to_binary (L ) when is_binary (L ) -> L ;
273
+ to_binary (L ) when is_list (L ) -> list_to_binary (L ).
0 commit comments