@@ -210,56 +210,30 @@ defmodule Sqlite.Ecto.Query do
210
210
# busy error means another process is writing to the database; try again
211
211
{ :error , { :busy , _ } } -> do_query ( pid , sql , params , opts )
212
212
{ :error , msg } -> { :error , Sqlite.Ecto.Error . exception ( msg ) }
213
- { :ok , rows } when is_list ( rows ) -> query_result ( pid , sql , rows )
213
+ { :ok , rows } when is_list ( rows ) -> query_result ( pid , sql , rows , opts )
214
214
end
215
215
end
216
216
217
217
# If this is an INSERT, UPDATE, or DELETE, then return the number of changed
218
218
# rows. Otherwise (e.g. for SELECT) return the queried column values.
219
- defp query_result ( pid , << "INSERT " , _ :: binary >> , [ ] ) , do: changes_result ( pid )
220
- defp query_result ( pid , << "UPDATE " , _ :: binary >> , [ ] ) , do: changes_result ( pid )
221
- defp query_result ( pid , << "DELETE " , _ :: binary >> , [ ] ) , do: changes_result ( pid )
222
- defp query_result ( _pid , _sql , rows ) do
223
- rows = Enum . map ( rows , fn row ->
224
- row
225
- |> cast_any_datetimes
226
- |> Keyword . values
227
- |> Enum . map ( fn
228
- { :blob , binary } -> binary
229
- other -> other
230
- end )
231
- end )
232
- { :ok , % Result { rows: rows , num_rows: length ( rows ) } }
219
+ defp query_result ( pid , << "INSERT " , _ :: binary >> , [ ] , _opts ) , do: changes_result ( pid )
220
+ defp query_result ( pid , << "UPDATE " , _ :: binary >> , [ ] , _opts ) , do: changes_result ( pid )
221
+ defp query_result ( pid , << "DELETE " , _ :: binary >> , [ ] , _opts ) , do: changes_result ( pid )
222
+ defp query_result ( _pid , _sql , rows , opts ) do
223
+ { :ok , decode ( rows , Keyword . fetch ( opts , :decode ) ) }
233
224
end
234
225
235
- defp changes_result ( pid ) do
236
- { :ok , [ [ "changes()": count ] ] } = Sqlitex.Server . query ( pid , "SELECT changes()" )
237
- { :ok , % Result { rows: nil , num_rows: count } }
226
+ defp decode ( rows , { :ok , :manual } ) do
227
+ % Result { rows: rows , num_rows: length ( rows ) , decoder: :deferred }
238
228
end
239
-
240
- # HACK: We have to do a special conversion if the user is trying to cast to
241
- # a DATETIME type. Sqlitex cannot determine that the type of the cast is a
242
- # datetime value because datetime defaults to an integer type in SQLite.
243
- # Thus, we cast the value to a TEXT_DATETIME pseudo-type to preserve the
244
- # datetime string. Then when we get here, we convert the string to an Ecto
245
- # datetime tuple if it looks like a cast was attempted.
246
- defp cast_any_datetimes ( row ) do
247
- Enum . map row , fn { key , value } ->
248
- str = Atom . to_string ( key )
249
- if String . contains? ( str , "CAST (" ) && String . contains? ( str , "TEXT_DATE" ) do
250
- { key , string_to_datetime ( value ) }
251
- else
252
- { key , value }
253
- end
254
- end
229
+ defp decode ( rows , _ ) do # not specified or :auto
230
+ % Result { rows: rows , num_rows: length ( rows ) , decoder: :deferred }
231
+ |> Result . decode
255
232
end
256
233
257
- defp string_to_datetime ( << yr :: binary - size ( 4 ) , "-" , mo :: binary - size ( 2 ) , "-" , da :: binary - size ( 2 ) >> ) do
258
- { String . to_integer ( yr ) , String . to_integer ( mo ) , String . to_integer ( da ) }
259
- end
260
- defp string_to_datetime ( str ) do
261
- << yr :: binary - size ( 4 ) , "-" , mo :: binary - size ( 2 ) , "-" , da :: binary - size ( 2 ) , " " , hr :: binary - size ( 2 ) , ":" , mi :: binary - size ( 2 ) , ":" , se :: binary - size ( 2 ) , "." , fr :: binary - size ( 6 ) >> = str
262
- { { String . to_integer ( yr ) , String . to_integer ( mo ) , String . to_integer ( da ) } , { String . to_integer ( hr ) , String . to_integer ( mi ) , String . to_integer ( se ) , String . to_integer ( fr ) } }
234
+ defp changes_result ( pid ) do
235
+ { :ok , [ [ "changes()": count ] ] } = Sqlitex.Server . query ( pid , "SELECT changes()" )
236
+ { :ok , % Result { rows: nil , num_rows: count } }
263
237
end
264
238
265
239
# SQLite does not have a returning clause, but we append a pseudo one so
0 commit comments