prowdsponsor / esqueleto

Bare bones, type-safe EDSL for SQL queries on persistent backends.
http://hackage.haskell.org/package/esqueleto
BSD 3-Clause "New" or "Revised" License
178 stars 51 forks source link

coalesce of sub-query generates syntax error #90

Closed aherrmann closed 9 years ago

aherrmann commented 9 years ago

Calling coalesce, or coalesceDefault with sub_select like here:

ret <- select $                                                     
       from $ \b -> do                                              
         let sub =                                                  
                 from $ \p -> do                                    
                 where_ (p ^. PersonId ==. b ^. BlogPostAuthorId)   
                 return $ p ^. PersonAge                            
         return $ coalesceDefault [sub_select sub] (val (42 :: Int))

causes the following run-time error (formatted for readability):

uncaught exception: IOException of type UserError
(user error (SQLite3 returned ErrorError while attempting to perform prepare
"SELECT COALESCE(SELECT \"Person\".\"age\"
 FROM \"Person\"
 WHERE \"Person\".\"id\" = \"BlogPost\".\"authorId\"
 , ?)
 FROM \"BlogPost\"
": near "SELECT": syntax error))

I.e. the sub-query is missing an extra pair of parentheses around it.

I found a fix for which I will create a PR shortly.