   end
 
   defp order_by(order_bys, sources) do
-    exprs = Enum.map_join(order_bys, ", ", fn %QueryExpr{expr: expr} ->
+    Enum.map_join(order_bys, ", ", fn %QueryExpr{expr: expr} ->
       Enum.map_join(expr, ", ", &ordering_term(&1, sources))
     end)
-
-    if exprs == "" do
-      []
-    else
-      ["ORDER BY", exprs]
-    end
-  end
+    |> order_by_clause
+  end
+
+  defp order_by_clause(""), do: []
+  defp order_by_clause(exprs), do: ["ORDER BY", exprs]
 
   defp ordering_term({:asc, expr}, sources), do: assemble(expr(expr, sources))
   defp ordering_term({:desc, expr}, sources) do


  defp json_library, do: Application.get_env(:ecto, :json_library)

  defp create_names(%{prefix: prefix, sources: sources}, stmt \\ :select) do
    create_names(prefix, sources, 0, tuple_size(sources), stmt) |> List.to_tuple()
  end
  defp create_names(prefix, sources, pos, limit, stmt) when pos < limit do
    {table, model} = elem(sources, pos)
    if stmt == :select do
      id = String.first(table) <> Integer.to_string(pos)
    else
      id = quote_id(table)
    end
    [{{prefix, table}, id, model} | create_names(prefix, sources, pos + 1, limit, stmt)]
  end
  defp create_names(_, _, pos, pos, _), do: []

  defp select(%SelectExpr{fields: fields}, distinct, sources) do
    fields = Enum.map_join(fields, ", ", fn (f) ->

    assemble [select, from, join, where, group_by, order_by, limit]
  end

  def update_all(%Ecto.Query{joins: [_ | _]}) do
    raise ArgumentError, "JOINS are not supported on UPDATE statements by SQLite"
  end
  def update_all(query) do
    sources = create_names(query, :update)
    {table, _name, _model} = elem(sources, 0)
    fields = update_fields(query.updates, sources)
