Skip to content

Instantly share code, notes, and snippets.

@tobiassjosten
Created April 27, 2013 14:47
Show Gist options
  • Save tobiassjosten/5473378 to your computer and use it in GitHub Desktop.
Save tobiassjosten/5473378 to your computer and use it in GitHub Desktop.
-- http://sumedh.info/articles/importing-comments-disqus-xml-database.php
import Control.Monad
import Database.HDBC
import Database.HDBC.MySQL
main =
do conn <- connectMySQL defaultMySQLConnectInfo {
mysqlHost = "localhost",
mysqlDatabase = "tobiassjosten",
mysqlUser = "tobiassjosten",
mysqlPassword = "tobiassjosten",
mysqlUnixSocket = "/var/run/mysqld/mysqld.sock"
}
getArticles conn
articleQuery = "SELECT id,title,slug,created_at FROM article"
commentQuery id = "SELECT id,name,email,website,created_at,body FROM comment WHERE article_id = " ++ id
getArticles conn =
do articles <- quickQuery' conn articleQuery []
forM_ articles $ \article -> do comments <- getComments conn article
putStrLn $ putArticle article comments
getComments conn article = quickQuery' conn (commentQuery $ fromSql $ head article) []
putArticle :: [SqlValue] -> [[SqlValue]] -> String
putArticle article comments
| length comments == 0 = ""
| otherwise = "<item>" ++ articleMarkup article ++ commentsMarkup comments ++ "</item>"
articleMarkup :: [SqlValue] -> String
articleMarkup article = fromSql $ head article
commentsMarkup :: [[SqlValue]] -> String
commentsMarkup comments = foldr ((++) . commentMarkup) [] comments
commentMarkup :: [SqlValue] -> String
commentMarkup comment = "|" ++ (fromSql $ head comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment