Add: drop the issue flag
[hdata.git] / Util.hs
blobcf090266cb27ff8b3abd919f603a29b4c836921b
1 module Util (
2 Operation (..),
3 createdb,
4 isHelp,
5 parseArg,
6 dbName,
7 opendb,
8 progName,
9 progVersion,
10 tableName,
11 usage
12 ) where
14 import Database.HDBC
15 import Database.HDBC.Sqlite3
16 import System.Environment
18 dbName = progName ++ ".db"
19 progName = "hdata"
20 progVersion = "0.0"
21 tableName = "mainTable"
23 data Operation = Add
24 | Bookmark
25 | Citation
26 | Help
27 | Modify
28 | Remove
29 | Search
30 | View
31 | Version
32 deriving (Show)
34 createdb :: Connection -> IO Connection
35 createdb conn = do run conn ("CREATE TABLE " ++ tableName ++ "(id INTEGER PRIMARY KEY,\
36 \ Path VARCHAR(1000),\
37 \ Title VARCHAR(1000),\
38 \ Authors VARCHAR(1000),\
39 \ Keywords VARCHAR(1000),\
40 \ Journal VARCHAR(1000),\
41 \ Volume VARCHAR(1000),\
42 \ Issue VARCHAR(1000),\
43 \ Date VARCHAR(1000),\
44 \ Pages VARCHAR(1000));") []
45 commit conn
46 return conn
48 isHelp :: String -> Bool
49 isHelp str = str `elem` ["-h","help","--help"]
51 opendb :: IO Connection
52 opendb = do
53 conn <- connectSqlite3 dbName
54 tables <- getTables conn
55 if not (tableName `elem` tables)
56 then do createdb conn
57 else do return conn
59 usage :: Operation -> String
60 usage op = show op ++ " not yet implemented"
64 parseArg :: String -> Either String Operation
65 parseArg op = case op of
66 "add" -> Right Add
67 "bookmark" -> Right Bookmark
68 "citation" -> Right Citation
69 "help" -> Right Help
70 "-h" -> Right Help
71 "--help" -> Right Help
72 "modify" -> Right Modify
73 "remove" -> Right Remove
74 "search" -> Right Search
75 "view" -> Right View
76 "version" -> Right Version
77 _ -> Left $ "Invalid argument: " ++ op