Skip to content

Instantly share code, notes, and snippets.

@filcab
Created April 30, 2011 21:14
Show Gist options
  • Save filcab/950001 to your computer and use it in GitHub Desktop.
Save filcab/950001 to your computer and use it in GitHub Desktop.
type RowState = !(Int, Int, String, TrackWords)
stN :: RowState -> Int
stN (n,_,_,_) = n
stRows :: RowState -> Int
stRows (_,rows,_,_) = rows
stTid :: RowState -> String
stTid (_,_,tid,_) = tid
stTw :: RowState -> TrackWords
stTw (_,_,_,tw) = tw
initialState :: RowState
initialState = (0,0,"",M.empty)
doRow :: Statement -> RowState -> IO RowState
doRow stmt st = do
tid <- getFieldValue stmt "track_id"
when (tid /= stTid st && stTid st /= "") $ writeWordFile tid (stTw st)
let (newStN,newTw) = if (tid /= stTid st)
then (stN st + 1,M.empty)
else (stN st,M.insert word count (stTw st))
word <- getFieldValue stmt "word"
count <- getFieldValue stmt "count"
return (newStN, stRows st + 1, tid, newTw)
@filcab
Copy link
Author

filcab commented Apr 30, 2011

-- main function
main = do
    argv <- getArgs
    when (length argv < 1) (putStrLn "Expecting 1 argument: BD location"
                                         >> exitFailure)
    db <- (liftM head) getArgs
    conn <- connectSQLite3 db        -- Connect
    putStrLn $ "Opened "++db
    s <- Hsql.query conn tracksQuery
    -- rows <- collectRows (\x -> return []) s
    putStrLn "Iterating rows..."
    finalState <- forEachRow doRow s initialState
    --putStrLn $ "Fetched " ++ (show $ length rows)
    Hsql.closeStatement s
    putStrLn $ "Closed connection to "++db
    Hsql.disconnect conn
    putStrLn $ "Written " ++ show finalState ++ " files."

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