Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

WHERE clause not bracketed correctly #14

Closed
tomjaguarpaw opened this Issue Jun 23, 2013 · 1 comment

Comments

Projects
None yet
1 participant

In line 76 of Sql/Print.hs there is

intersperse (text "AND") (map ppSqlExpr es)

This leads to incorrect bracketing of the WHERE clause. For example I had the condition

  • job_table.person_id = 101 OR job_table.person_id = 102 (for restriction)
  • location.id = job_table.id_location (for joining)

and the generated SQL was

WHERE job_table.person_id = 101
      OR job_table.person_id = 102 AND location.id = job_table.id_location

which associates to

WHERE job_table.person_id = 101
      OR (job_table.person_id = 102 AND location.id = job_table.id_location)

Not what I wanted! I "fixed" it with this change

diff --git a/src/Database/HaskellDB/Sql/Print.hs b/src/Database/HaskellDB/Sql/Print.hs
index 9f9d6fc..522dcb8 100644
--- a/src/Database/HaskellDB/Sql/Print.hs
+++ b/src/Database/HaskellDB/Sql/Print.hs
@@ -71,7 +71,8 @@ ppTables ts = text "FROM" <+> commaV ppTable (zipWith tableAlias [1..] ts)
 ppWhere :: [SqlExpr] -> Doc
 ppWhere [] = empty
 ppWhere es = text "WHERE" 
-             <+> hsep (intersperse (text "AND") (map ppSqlExpr es))
+             <+> hsep (intersperse (text "AND")
+                       (map ((text "(" <>) . (<> text ")") . ppSqlExpr) es))

 ppGroupBy :: Mark -> Doc
 ppGroupBy All = error "Should not ever print GroupBy all."

but I don't know where the actual bug is considered to be. Perhaps ppWhere is being fed the wrong arguments.

Fixed by 34ee824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment