It's not about the fragment syntax, I just saw fragment that have 20 question marks mixed with some case statements.
Moving this to a macro will make it even more convoluted.
dbvisor seems has a bit different purpose than fragments.
I was thinking about something like this - I'm not even sure if the fragment is valid:
defmodule InterpolatedFragment do
defmacro defrag(ast) do
string = Macro.to_string(ast)
pattern = ~r/\#\{([^\}]+)\}/
vars =
Regex.scan(pattern, string)
|> Enum.map(fn [_, var] -> var end)
|> Enum.join(", ")
new_string = Regex.replace(pattern, string, "?")
"fragment(#{new_string}, #{vars})"
|> Code.string_to_quoted!
end
end
defmodule YourModule do
import InterpolatedFragment
import Ecto.Query
def your_query(star, table) do
from q in defrag("select #{^star} from #{^table}")
end
def ecto_query(star, table) do
from q in fragment("select ? from ?", ^star, ^table)
end
end
star = "column"
table = "users"
YourModule.your_query(star, table)
#Ecto.Query<from f0 in fragment("select ? from ?", ^"column", ^"users")>