]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/ConnectionString.hs
Provide a default connection string (for sqlite).
[dead/htsn-import.git] / src / ConnectionString.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE OverloadedStrings #-}
3
4 -- | Definition of and instances for the ConnectionString type.
5 --
6 module ConnectionString (
7 ConnectionString(..) )
8 where
9
10 -- DC is needed only for the DCT.Configured instance of String.
11 import qualified Data.Configurator as DC()
12 import qualified Data.Configurator.Types as DCT (
13 Configured,
14 Value( String ),
15 convert )
16 import Data.Data (Data)
17 import System.Console.CmdArgs.Default (Default(..))
18 import Data.Typeable (Typeable)
19
20 -- | A newtype around a string that allows us to give a more
21 -- appropriate default value for a connection string.
22 --
23 newtype ConnectionString =
24 ConnectionString { get_connection_string :: String }
25 deriving (Data, Show, Typeable)
26
27 instance Default ConnectionString where
28 -- | This default is appropriate for SQLite databases which require
29 -- no authentication and live entirely in a file (or in this case,
30 -- memory).
31 def = ConnectionString ":memory:"
32
33
34 instance DCT.Configured ConnectionString where
35 -- | This allows us to read a ConnectionString out of a Configurator
36 -- config file. By default Configurator wouldn't know what to do,
37 -- so we have to tell it that we expect a DCT.String, and if one
38 -- exists, to apply the ConnectionString constructor to it.
39 convert s@(DCT.String _) =
40 fmap ConnectionString (convert_string s)
41 where
42 convert_string :: DCT.Value -> Maybe String
43 convert_string = DCT.convert
44
45 -- If we read anything other than a DCT.String out of the file, fail.
46 convert _ = Nothing