1

I've got a query like this in persistent:

findUserLocation userId =
  rawSql [str|SELECT name, ST_Transform(coordinates, 4326)
               FROM user 
                 JOIN user_location USING (user_id)
               WHERE user.user_id = ?|]
         [toPersistValue userId]

I'd like to rewrite this so it doesn't use raw strings, probably into an Esqueleto query, but I want to be sure the end result is equivalent.

Is there a way I can check the SQL my new query will produce without a database connection?
Is there something like SqlPersistM a -> [Text]? (I've searched, to no avail...)

Or do I have to connect to the DB to verify the query?

Kris Jenkins
  • 4,083
  • 30
  • 40
  • So your real question is: _Is there a way to see what SQL will be run from an esqueleto query?_ The SQL generated from your `rawSql` statement should be exactly what you have there. – ErikR Jul 11 '16 at 14:53
  • Related question: [How can I get esqueleto to generate an SQL string for me?](http://stackoverflow.com/questions/20435797/how-can-i-get-esqueleto-to-generate-an-sql-string-for-me) – ErikR Jul 11 '16 at 15:04
  • Well, since Esqueleto's just using Persistent under the hood, my question is "how do I turn any `SqlPersistM` into a string?" I know it's possible, because eventually the DB connection will require it. The question is, is that conversion user-accessible? – Kris Jenkins Jul 13 '16 at 13:19
  • SqlPersistM is too low-level - it is literally a fragment of code - it's not data. You can run that code against a dummy database, or a database driver with logging turned on, but otherwise there is no way to extract out a "query". – ErikR Jul 13 '16 at 14:02
  • If that's true, how does it manage to send a query string to the database when you run it? I mean, if you enable logging, you'll see a string, so it must be involved *somewhere*. – Kris Jenkins Jul 13 '16 at 16:23
  • Fundamentally a SqlPersistM is an IO-action. When you run it you need to provide a connection. The SqlPersistM action generates the SQL and gives it to the connection. If the connection logs it somewhere you'll see it in a log file. If you use a sqlite memory connection like in the SO answer I referenced above, perhaps you can see what the SQL is. – ErikR Jul 13 '16 at 17:11

0 Answers0