Can I get Ecto to log raw SQL?

后端 未结 2 1863
死守一世寂寞
死守一世寂寞 2020-12-21 17:23

I am building an Ecto query like this:

from item in query,
where:  like(item.description, ^\"%#{text}%\")

I\'m concerned that this allows S

2条回答
  •  温柔的废话
    2020-12-21 17:49

    Yes, that is the exact SQL that is being executed by Ecto (it uses prepared queries through the db_connection package internally) and no SQL injection is possible in that code. This can be verified by turning on logging of all executed SQL queries by changing log_statement to all in postgresql.conf:

    ...
    log_statement = 'all'
    ...
    

    and then restarting PostgreSQL and running a query. For the following queries:

    Repo.get(Post, 1)
    Repo.get(Post, 2)
    

    this is logged:

    LOG:  execute ecto_818: SELECT p0."id", p0."title", p0."user_id", p0."inserted_at", p0."updated_at" FROM "posts" AS p0 WHERE (p0."id" = $1)
    DETAIL:  parameters: $1 = '1'
    LOG:  execute ecto_818: SELECT p0."id", p0."title", p0."user_id", p0."inserted_at", p0."updated_at" FROM "posts" AS p0 WHERE (p0."id" = $1)
    DETAIL:  parameters: $1 = '2'
    

提交回复
热议问题