<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="134687" data-post-id="134687">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Right, this is entirely possible in Elixir. Build a datastructure in Yecc that represents the input. Build a recursive function that builds an ecto query by walking the datastructure yecc returns. If this seems like something you aren’t sure how to do, break the problem down. Play around with the ecto functions so that you get familiar with how to use them. Play around with walking data structures with an accumulator so that you can do simple things like count how many parts to the input there are. Then combine.</p>
<p>We’re happy to help with these steps, but you need to break the problem down into pieces you can work on incrementally.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134687" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-134687" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134687"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="134691" data-post-id="134691">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mfrasca" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mfrasca/120/15848_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mfrasca
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>all very reasonable, but I fail to see what you mean <em>in practice</em>, and the elixir code is still far too opaque to me, to browse through ecto and figure out what happens.  for example: the various clauses defining <code>from</code>, they all end with an evaluation of an other <code>from</code>, except one which ends with a <code>quoted</code>.  it makes no sense to me.</p>
<p>to refine my vocabulary when speaking in Elixir/Ecto, I proposed a page to be included in the documentation, which did help me understand how migrations work, but I was hoping for reviews.  I received hints on how to write better SQL though.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134691" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-134691" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134691"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="134694" data-post-id="134694">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mfrasca" data-post="13" data-topic="23644">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mfrasca/48/15848_2.png" class="avatar"> mfrasca:</div>
<blockquote>
<p>all very reasonable, but I fail to see what you mean <em>in practice</em> , and the elixir code is still far too opaque to me, to browse through ecto and figure out what happens. for example: the various clauses defining <code>from</code> , they all end with an evaluation of an other <code>from</code> , except one which ends with a <code>quoted</code> . it makes no sense to me.</p>
</blockquote>
</aside>
<p>You shouldn’t need to care how ecto works internally. Unless you’re doing some very complex stuff you should be able to recursively build up an ecto query just using the public api of it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134694" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-134694" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134694"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="134695" data-post-id="134695">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mfrasca" data-post="13" data-topic="23644">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mfrasca/48/15848_2.png" class="avatar"> mfrasca:</div>
<blockquote>
<p>I fail to see what you mean <em>in practice</em></p>
</blockquote>
</aside>
<p>Sure, so let’s do some practice. What I don’t recommend is trying to look at the ecto functions and see how they work, because they’re pretty complicated internally. Technically they’re all macros, so it’s just a bit of a pain to figure out what is going on. There’s a reason for this (compile time protection from sql injection, yay!) but it’s definitely complicated.</p>
<p>Fortunatelly, <em>using</em> the ecto query macros isn’t that bad, but there’s no substitute for practice. I highly recommend working through the programming ecto book, or any of the other Elixir books that uses ecto (programming phoenix, the graphql book, etc). If you want to go for it just based on docs that’s fine too, just build some simple exercises for yourself. Here’s an example of a function from the graphql book that takes this kind of input:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{filter: %{category: "drinks", priced_below: 10.00}, order: :desc}
</code></pre>
<p>And then builds an ecto query that filters / orders an <code>Item</code> schema accordingly:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def list_items(args) do
  args
  |&gt; items_query
  |&gt; Repo.all
end

def items_query(args) do
  Enum.reduce(args, Item, fn
    {:order, order}, query -&gt;
      query |&gt; order_by({^order, :name})
    {:filter, filter}, query -&gt;
      query |&gt; filter_with(filter)
  end)
end

defp filter_with(query, filter) do
  Enum.reduce(filter, query, fn
    {:name, name}, query -&gt;
      from q in query, where: ilike(q.name, ^"%#{name}%")
    {:priced_above, price}, query -&gt;
      from q in query, where: q.price &gt;= ^price
    {:priced_below, price}, query -&gt;
      from q in query, where: q.price &lt;= ^price
    {:added_after, date}, query -&gt;
      from q in query, where: q.added_on &gt;= ^date
    {:added_before, date}, query -&gt;
      from q in query, where: q.added_on &lt;= ^date
    {:category, category_name}, query -&gt;
      from q in query,
        join: c in assoc(q, :category),
        where: ilike(c.name, ^"%#{category_name}%")
    {:tag, tag_name}, query -&gt;
      from q in query,
        join: t in assoc(q, :tags),
        where: ilike(t.name, ^"%#{tag_name}%")
  end)
end
</code></pre>
<p>If we pass the example input into the <code>items_query</code> function we see that it returns an ecto query:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(2)&gt; query = PlateSlate.Menu.items_query(%{filter: %{category: "drinks", priced_below: 10.00}, order: :desc})
#Ecto.Query&lt;from i in PlateSlate.Menu.Item, join: c in assoc(i, :category),
 where: ilike(c.name, ^"%drinks%"), where: i.price &lt;= ^10.0,
 order_by: [desc: i.name]&gt;
</code></pre>
<p>This query can then be executed by our repo:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Repo.all(query)
</code></pre>
<p>I’m suggesting that you should build a function similar to my <code>items_query</code> function that recursively walks through your yecc output and matches on various sub parts, reducing on to the ecto query data structure.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134695" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-134695" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134695"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="134697" data-post-id="134697">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mfrasca" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mfrasca/120/15848_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mfrasca
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="LostKobrakai" data-post="14" data-topic="23644">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>to recursively build up an ecto query</p>
</blockquote>
</aside>
<p>imagine I have the value corresponding to <code>from(a in "accession")</code>, do you think you can help me with going from this value to the one corresponding to <code>from(a in "accession", where: a.code=="12345")</code>?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134697" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-134697" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134697"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="134699" data-post-id="134699">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mfrasca" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mfrasca/120/15848_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mfrasca
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>this would help me for simple queries, where all clauses are at the same level, “multiplied” by <code>and</code>.  it’s a start though, so thank you.<br>
I need to first represent the complete <code>where</code> clause, then to fire that in one step into the <code>query</code>, or I keep the above limitation.<br>
and you just answered the question which I was typing.</p>
<p>(my <code>where</code> clause <strong>include</strong> things like <code>count(plants.images)&gt;1</code>, where plants is a <code>has_many</code> association from accession, and images likewise from plant.  <code>count</code> is the obvious aggregation function.  and this is text typed by the user.)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134699" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-134699" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134699"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="134708" data-post-id="134708">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If you use named bindings this shouldn’t be a problem:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">base = from a in "accession", as: :accession
# later
binding = :accession
updated = from [{^binding, a}] in base, where: a.code == "12345"
</code></pre>
<p>If you cannot use named bindings you might need to keep track of the positions of bindings, which does allow you to do <code>[{a, ^position}]</code> to pull out the correct binding.</p>
<p>If you have complex <code>where</code> parts you might want to consider using <code>dynamic</code> to build up the condition and only attach the final condition to the query.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134708" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-134708" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134708"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="134711" data-post-id="134711">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mfrasca" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mfrasca/120/15848_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mfrasca
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="benwilson512" data-post="2" data-topic="23644" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p>The result of quote is not something you execute. Instead of building Elixir AST and then trying to get it compiled, you’d be much better off just dynamically constructing the ecto query data structure itself.</p>
</blockquote>
</aside>
<p>I think I finally get your point.  an AST (the value returned if I <code>quote</code> an expression) is a compile-time artifact, which I can compile, but not evaluate.</p>
<p>so instead of targeting the <code>quote</code>/AST, format, I should target the <code>Ecto.Query</code> format.  and since I cannot do this from yecc (or can I?  yecc is in Erlang), I need an intermediate representation.  yet, for each production in the grammar (have you seen my grammar?), (production which combines simpler elements), I need a combining function/clause.</p>
<p>I will take a pause and read again in a few hours, but I have the impression that all the examples/hints I’ve been reading, they contain hard coded names or hard coded patterns.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="134711" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-produce-executable-code-using-yecc/23644/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-134711" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="134711"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>