Commits

Luke Plant committed 96ecd42

Added support for 'update' sql statements

  • Participants
  • Parent commits dbb2d90

Comments (0)

Files changed (3)

File src/Blog/DB.hs

                , commit
                , doInsert
                , mkInsertStatement
+               , doUpdate
+               , mkUpdateStatement
                )
 where
 
     let sql = mkInsertStatement table columns
     in doSql conn sql values
 
+doUpdate conn table columns values whereClause whereClauseVals =
+    let sql = mkUpdateStatement table columns
+    in doSql conn (sql ++ " " ++ whereClause ++ ";") (values ++ whereClauseVals)
+
 mkInsertStatement table columns = let joinC = concat . intersperse ", "
                                       colSql = joinC columns
                                       valSql = joinC $ take (length columns) $ repeat "?"
                                       " (" ++ colSql ++ ")" ++
                                       " VALUES " ++
                                       " (" ++ valSql ++ ");"
+
+mkUpdateStatement table columns = let joinC = concat . intersperse ", "
+                                      valSql = joinC $ [c ++ "=?" | c <- columns]
+                                  in "UPDATE " ++ table ++
+                                     " SET " ++ valSql

File testsuite/Main.hs

+import qualified Tests.Blog.DB as DB
 import qualified Tests.Blog.DBUtils as DBUtils
 import qualified Tests.Blog.Formats as Formats
 import qualified Tests.Blog.Model as Model
                        , "Format tests" ~: Formats.tests
                        , "Model tests" ~: Model.tests
                        , "View tests" ~: View.tests
+                       , "DB tests" ~: DB.tests
                        ])

File testsuite/Tests/Blog/DB.hs

+module Tests.Blog.DB
+
+where
+
+import Blog.DB
+import Test.HUnit
+
+testMkUpdateStatement = "UPDATE foo SET bar=?, baz=?" ~=?
+                        mkUpdateStatement "foo" ["bar", "baz"]
+
+tests = test [ testMkUpdateStatement
+             ]