ExtractQueryParams - A simple elixir package for transforming keyword lists to SQL statements

Link to HexDocs Page


I was building a toy rest-api using plug-cowboy and wanted a way to turn query parameters such as name: "Bob, age: 15 into sql statements such as:

Depo.read("SELECT * FROM customers WHERE" <> "name = ? AND age = ?", "Bob", 15)

in a simple way


ExtractQueryParams.to_variables(name: "Bob", age: 15)
{"name= ? AND age = ?", ["Bob", 15]} #returns

The default logical operator is AND but you can also specify which operator you want:

ExtractQueryParams.to_variables([name: "Bob", age: 15], "OR")
{"name= ? OR age = ?", ["Bob", 15]} #returns

Feel free to leave any feedback or contribute if you want!


Are you intentionally wanting to avoid Ecto? With Ecto and a schema defined for Customer creating the query becomes a one-liner

filters = [name: "Bob", age: 15]
query = from c in Customer, where: ^filters

In cases you want to write raw sql queries with something like Depo and SQlite3 without using ecto

No. I don’t personally want to exchange Ecto for string concatenation. I’ll step out of the thread since I’m not part of your potential audience. Best wishes to you!