user974881
Pattern match postgex result - (MatchError) no match of right hand side value: %Postgrex.Result
I am trying to do pattern matching against a postgrex result, as such:
{:ok, %Postgrex.Result{rows: [[result]]}} = Postgrex.query!(:postgrex, "SELECT COUNT(1)
FROM pages
WHERE url = $1;", [url])
but I get
** (Mix) Could not start application dbhandler: DBHandler.Application.start(:normal, []) returned an error: shutdown: failed to start child: DBHandler
** (EXIT) an exception was raised:
** (MatchError) no match of right hand side value: %Postgrex.Result{command: :select, columns: ["count"], rows: [[1]], num_rows: 1, connection_id: 17022, messages: []}
(dbhandler 0.1.0) lib/dbhandler.ex:139: DBHandler.private_in_db?/1
(dbhandler 0.1.0) lib/dbhandler.ex:70: anonymous fn/1 in DBHandler.init/1
(elixir 1.17.3) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(dbhandler 0.1.0) lib/dbhandler.ex:69: DBHandler.init/1
(stdlib 4.3.1.6) gen_server.erl:851: :gen_server.init_it/2
(stdlib 4.3.1.6) gen_server.erl:814: :gen_server.init_it/6
(stdlib 4.3.1.6) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
What I might be doing wrong is that I didn’t put all the elements of the map, but I don’t THINK I have to do that.
So, is there a way to do this that works, or do I have to find a way other than pattern matching?
Marked As Solved
al2o3cr
Postgrex.query! raises a Postgrex.Error exception if anything goes wrong, so it returns an Postgrex.Result without an {:ok, ...} wrapper.
You could either remove the {:ok, ...} part on the left-hand side or switch the right-hand side to Postgrex.query (no !), depending on exactly what you want to happen if the query fails.
Also Liked
Schultzer
I see you are fiddling with hand written queries, you might be interested in GitHub - elixir-dbvisor/sql: Brings an extensible SQL parser and sigil to Elixir, confidently write SQL with automatic parameterized queries. so that your queries are automatically parametrized.
D4no0
Looks like an amazing alternative to ecto queries!
Schultzer
My goal is not to be an alternative, but a more fundamental building block for SQL based adapters https://groups.google.com/g/elixir-ecto/c/8MOkRFAdLZc
There is a potential for making Ecto.SQL more powerful and maintainable, by reusing code across adapters.
user974881
it does look quite interesting, I’ll try to use it, and I have currently a problem so this might fix it!
user974881
Well, I’m trying to do this:
query = ~SQL[from pages]
|> ~SQL[where url not in #{state.lock}]
|> ~SQL[where host not in #{state.politeness}]
|> ~SQL"select url"
Where state.lock and state.politeness are lists of strings.
But it just waits and does nothing when I use to_sql on it…







