Paste: HDBC.Sqlite transactions?

Author: vegai
Mode: haskell
Date: Sun, 27 Sep 2009 11:24:40
Plain Text |
module Main where

import Database.HDBC.Sqlite3 (connectSqlite3)
import Codec.Compression.GZip (compress)
import Database.HDBC (execute, prepare, SqlValue(..), withTransaction, commit)
import System.Directory (getDirectoryContents)
import System.FilePath.Posix (takeExtension, joinPath)
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString 


dbfile = "/home/vegai/dbdump/xml.db"
xml_dir = "/home/vegai/dbdump/outgoing"
stmt = "INSERT INTO xmls VALUES ( ?, ? )"


writeRow db prepared_stmt file = writeRow' (takeExtension file)
    where writeRow' ".xml" = 
              do contents <-  BS.readFile expandedFile
                 let compressedContents = convert $ compress contents
                 execute prepared_stmt [ SqlString file, SqlByteString compressedContents ]
                 
                 return ()
          writeRow' _ = return ()

          expandedFile = joinPath [xml_dir, file]
          convert = Data.ByteString.concat . BS.toChunks

main = 
    do db <- connectSqlite3 dbfile
       files <- getDirectoryContents xml_dir
       prepared_stmt <- prepare db stmt


-- seems like the transactions don't work correctly. The sqlite journal
-- doesn't get bigger + the whole operation is *slower* with
-- the transaction
--       withTransaction db $ \db -> mapM_ (writeRow db prepared_stmt) files
       mapM_ (writeRow db prepared_stmt) files

       return ()

New Annotation

Summary:
Author:
Mode:
Body: