]> gitweb.michael.orlitzky.com - mailbox-count.git/blobdiff - src/Main.hs
Add SQLite support (default if a filename is given as the database).
[mailbox-count.git] / src / Main.hs
index d153716e3ad575f9368ce2bc779785721986068b..f00ebb39dd2eb6b258d39261ca4f2a807da67d93 100644 (file)
@@ -1,3 +1,5 @@
+{-# LANGUAGE DoAndIfThenElse #-}
+{-# LANGUAGE NoMonomorphismRestriction #-}
 module Main
 where
 
@@ -5,13 +7,16 @@ import Data.Maybe ( fromMaybe )
 import Data.Monoid ( (<>) )
 import Data.String.Utils ( join )
 import Database.HDBC.PostgreSQL ( connectPostgreSQL )
+import Database.HDBC.Sqlite3 ( connectSqlite3 )
 import System.Console.CmdArgs ( def )
+import System.Directory ( doesFileExist )
 
 import CommandLine ( get_args )
 import Configuration ( Configuration(..), merge_optional )
 import qualified OptionalConfiguration as OC ( from_rc )
 import Report ( report )
 
+
 -- | Construct a connection string (postgres-only, for now) from a
 --   'Configuration'. All of these are optional, at least for
 --   Postgres, and so we want to avoid appending e.g. \"host=\" to the
@@ -58,16 +63,16 @@ main = do
   -- log the errors.
   let cfg = (def :: Configuration) `merge_optional` opt_config
 
-  -- Check the optional config for missing required options.
-  --when (isNothing (OC.password opt_config)) $ do
-  --  report_error "No password supplied."
-  --  exitWith (ExitFailure exit_no_password)
+  -- If a database name was specified, and that name exists as a file
+  -- on the system, assume that the user wanted to use SQLite.
+  r <- case (database cfg) of
+      Nothing -> connectPostgreSQL (connection_string cfg) >>= report cfg
 
-  --when (isNothing (OC.username opt_config)) $ do
-  --  report_error "No username supplied."
-  --exitWith (ExitFailure exit_no_username)
+      Just dbname -> do
+        exists <- doesFileExist dbname
+        if exists
+        then connectSqlite3 dbname >>= report cfg
+        else connectPostgreSQL (connection_string cfg) >>= report cfg
 
-  conn <- connectPostgreSQL (connection_string cfg)
-  r <- report conn (detail cfg)
+  -- The DB connection is implicitly closed when it gets garbage collected.
   putStrLn r
---  disconnect conn