Skip to content

Instantly share code, notes, and snippets.

@mopemope
Created August 29, 2014 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mopemope/7ba8cee4d72187d9b90a to your computer and use it in GitHub Desktop.
Save mopemope/7ba8cee4d72187d9b90a to your computer and use it in GitHub Desktop.
Test
module Main where
import Control.Monad
import Data.Monoid
import Data.UnixTime
import Data.Convertible.Base
import Database.HDBC
import Database.HDBC.Sqlite3
getHistories :: (Integral a) => a -> IO [(Integer, Integer)]
getHistories n = do
now <- getUnixTime
forM [1..n] $ \i -> do
return (toInteger (9+i), (convert (getTime now i)::Integer))
where
getDelta = (* (60 * 60 * (-1)))
getTime now i = utSeconds $ now `addUnixDiffTime` secondsToUnixDiffTime (getDelta i)
getPlaces :: (Show a, Num a, Enum a) => a -> [([Char], [Char], [Char])]
getPlaces n = [("http://www.mozilla.jp?search=" <> (show x),
"Mozilla Japan — 次世代ブラウザ Firefox とメールソフト Thunderbird " <> (show x),
"guid_" <> (show x)) | x <- [1..n]]
insertPlace :: (IConnection a) => a -> (String, String, String) -> IO Integer
insertPlace conn (url, title, guid) = do
stmt <- prepare conn "INSERT INTO moz_places (url, title, guid) VALUES (?, ?, ?)"
execute stmt [toSql url, toSql title, toSql guid]
insertHistory :: (IConnection a) => a -> (Integer, Integer) -> IO Integer
insertHistory conn (placesId, date) = do
stmt <- prepare conn "INSERT INTO moz_historyvisits (from_visit, place_id, visit_date, visit_type, session) VALUES (3, ?, ?, 1, 0)"
execute stmt [toSql placesId, toSql date]
insertPlaces :: (IConnection a) => a -> Integer -> IO ()
insertPlaces conn n = do
mapM_ (insertPlace conn) $ getPlaces n
commit conn
insertHistories :: (IConnection a) => a -> Integer -> IO ()
insertHistories conn n = do
xs <- getHistories n
mapM_ (insertHistory conn) $ xs
commit conn
main :: IO ()
main = do
conn <- connectSqlite3 "places.sqlite"
insertPlaces conn 10
insertHistories conn 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment