module Villefort.Database where
import Control.Monad.IO.Class
import Database.HDBC.Sqlite3
import Database.HDBC
import Data.List.Split
import System.Environment.FindBin
import System.Environment
import Paths_Villefort
type Query = String
type Path = String
type Subject = String
getSubjects :: IO [Subject]
getSubjects = (\x-> (!! 0) <$> x) <$> makeQuery "select Subject from todo where state = 0 group by Subject"
path' :: IO FilePath
path' = do
args <- getArgs
if length args > 1 then
if args !! 0 == "--custom"
then return $ args !! 1
else getDataDir
else getDataDir
getDb :: IO Connection
getDb = (++ "/data/todo.db") <$> path' >>= \path -> connectSqlite3 path
convRow dat = Prelude.map (\x -> Prelude.map (\y -> fromSql y :: String ) x) dat
makeQuery query = do
conn <- getDb
taskRaw <- quickQuery' conn query []
disconnect conn
return (convRow taskRaw)
execQuery query params = getDb >>= \conn -> prepare conn query >>=
\stmt -> execute stmt ( map toSql params) >> commit conn >> disconnect conn
getNextId ::IO Integer
getNextId = do
f <-liftIO $ makeQuery "select id from todo order by id desc"
let rawid = head $ f
let id = (read (rawid !! 0) :: Integer) +1
return id