haskell-opaleye icon indicating copy to clipboard operation
haskell-opaleye copied to clipboard

Feature request: query anonymization

Open zyla opened this issue 4 years ago • 3 comments

I'd like to anonymize SQL queries for logging purposes. This is commonly done by replacing literals with some placeholder, like ?. Re-parsing the generated query just to do that seems wasteful, so it seems that the best way to do that would be to modify Opaleye's query generator.

First, would such a feature be welcome?

Second, what would be the best way to do that? I see two options:

  1. Use a special SqlGenerator, overriding sqlLiteral, or
  2. Add a parameter to ppSqlExpr, which would influence how ConstSqlExpr is printed.

Both options require many changes in the code, to pass the "printing mode" parameter deep in the call stack, that's why I'm asking for advice on how to proceed.

zyla avatar Oct 05 '20 11:10 zyla

I'm happy to try to provide guidance. How do you imagine this being used? A new top-level function like showSql except that it replaces literals? SqlGenerators were the way that HaskellDB supported selectively replacing parts of the pretty printer. Opaleye hasn't kept up rigorously with that, although we could.

tomjaguarpaw avatar Oct 12 '20 11:10 tomjaguarpaw

A new top-level function like showSql except that it replaces literals?

Exactly that. Here is my work in progress. I've created a showSqlAnonymized function, which is like showSql except it outputs literals differently. The remaining thing to do is to provide analogous versions of arrange{Insert,Update,Delete}{,Returning}Sql.

zyla avatar Oct 12 '20 16:10 zyla

Ah nice, looks sensible.

tomjaguarpaw avatar Oct 27 '20 12:10 tomjaguarpaw