Supporting expressions in `insert_all`

34 views
Skip to first unread message

Zach Daniel

unread,
Aug 28, 2023, 12:59:52 PM8/28/23
to elixir-ecto
I'm looking to figure out how I might go about supporting expressions while inserting, but not an entire query. What I'm imagining this would look like is something like this:


Repo.insert_all(Schema, [key: dynamic([], some_expression]))

It looks like the bones of this are there by virtue of being able to support individual queries as those keyword values, I'm just not too familiar with this part of Ecto's codebase. I started in on some implementations but my spidey sense was tingling, telling me that I was doing doing something wrong. If anyone could perhaps point me in the right direction, I would be grateful. From what I can tell, Ecto doesn't pass down dynamic expressions to adapters, it instead resolves/plans the things it sends down. So what I'm *thinking* I would need is to know what the normal way is to turn a DynamicExpr into a QueryExpr, and then handle that case in the `insert_each` code in `ecto_sql`

    defp insert_each(values, counter) do
      intersperse_reduce(values, ?,, counter, fn
        nil, counter ->
          {"DEFAULT", counter}
       
        {%Ecto.Query{} = query, params_counter}, counter ->
          {[?(, all(query), ?)], counter + params_counter}

       # adding a case here for %QueryExpr{}
          ...
      end)
    end

Thoughts? Any pointers?

Greg Rychlewski

unread,
Aug 28, 2023, 3:48:15 PM8/28/23
to elixir-ecto
Could you  give an example of the kind of query you want to support that isn't supported right now? I was writing a reply but realized I probably don't understand your problem too well. In general dynamic only makes sense as part of a query. And today you can supply dynamic non-query related values using interpolation.
Reply all
Reply to author
Forward
0 new messages