Commits

Luke Plant  committed 485101f

Finished implementing migration of 'categories' data

  • Participants
  • Parent commits 56e4b96

Comments (0)

Files changed (3)

File src/Category.hs

 module Category where
 {
 
-  data Category = Category { id :: Int,
-                             name :: String } deriving (Show, Eq)
+    data Category = Category { id :: Int,
+                               name :: String } deriving (Show, Eq)
 
-}
-
+}
 module DB where
 
-import Database.HDBC (handleSqlError)
+import Database.HDBC
 import Database.HDBC.Sqlite3 (connectSqlite3 {-, setBusyTimeout-})
 import qualified Settings 
+import List
 
 connect = connectSqlite3 Settings.sqlite_path
 
+doInsert conn table columns values = let stmnt = mkInsertStatement table columns
+                                     in run conn stmnt values
+
+mkInsertStatement table columns = let joinC = concat . intersperse ", "
+                                      colSql = joinC columns
+                                      valSql = joinC $ take (length columns) $ repeat "?"
+                                   in "INSERT INTO " ++ table ++ 
+                                      " (" ++ colSql ++ ")" ++
+                                      " VALUES " ++
+                                      " (" ++ valSql ++ ");"

File src/Migrate.hs

 import qualified Category as C
 import qualified Settings
+import qualified DB
+import Database.HDBC
 -- Migration script for the old data
 
 -- Read a table of newline/tab delimited data,
                     let mkCat row = C.Category { C.id = read (row !! 0),
                                                  C.name = row !! 1}
                     return $ map mkCat ds
+
+writeCategories cs = do cn <- DB.connect
+                        mapM_ (\c -> DB.doInsert cn "categories" ["id", "name"] [toSql $ C.id c, toSql $ C.name c]) cs
+                        commit cn
+
+main = do cats <- readCategories
+          writeCategories cats