Use new DateExpr and start working on stuff
This commit is contained in:
parent
eac7a415dc
commit
d4b5d0b7c2
1 changed files with 80 additions and 26 deletions
|
|
@ -4,21 +4,56 @@
|
||||||
module TaskMachine.Database
|
module TaskMachine.Database
|
||||||
( TaskRow(..)
|
( TaskRow(..)
|
||||||
, initializeNewDB
|
, initializeNewDB
|
||||||
|
, updateTasks
|
||||||
) where
|
) where
|
||||||
|
|
||||||
--import Control.Exception
|
import Control.Exception
|
||||||
|
import Data.Maybe
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time.Calendar
|
import Data.Time.Calendar
|
||||||
import qualified Database.SQLite.Simple as DB
|
import qualified Database.SQLite.Simple as DB
|
||||||
|
import qualified Database.SQLite.Simple.FromField as DB
|
||||||
|
import qualified Database.SQLite.Simple.Ok as DB
|
||||||
|
import qualified Database.SQLite.Simple.ToField as DB
|
||||||
|
|
||||||
import qualified TaskMachine.DateExpr as TM
|
import qualified TaskMachine.DateExpr as TM
|
||||||
|
|
||||||
|
data IntFormula = IntFormula
|
||||||
|
{ intFormulaText :: T.Text
|
||||||
|
, intFormulaExpr :: TM.IntExpr
|
||||||
|
}
|
||||||
|
|
||||||
|
instance DB.ToField IntFormula where
|
||||||
|
toField = DB.toField . intFormulaText
|
||||||
|
|
||||||
|
instance DB.FromField IntFormula where
|
||||||
|
fromField f = case DB.fromField f of
|
||||||
|
DB.Errors e -> DB.Errors e
|
||||||
|
DB.Ok text -> case TM.parseIntExpr (T.unpack text) of
|
||||||
|
Nothing -> DB.Errors [] -- TODO: Proper exception?
|
||||||
|
Just expr -> DB.Ok IntFormula{ intFormulaText = text, intFormulaExpr = expr }
|
||||||
|
|
||||||
|
data BoolFormula = BoolFormula
|
||||||
|
{ boolFormulaText :: T.Text
|
||||||
|
, boolFormulaExpr :: TM.BoolExpr
|
||||||
|
}
|
||||||
|
|
||||||
|
instance DB.ToField BoolFormula where
|
||||||
|
toField = DB.toField . boolFormulaText
|
||||||
|
|
||||||
|
instance DB.FromField BoolFormula where
|
||||||
|
fromField f = case DB.fromField f of
|
||||||
|
DB.Errors e -> DB.Errors e
|
||||||
|
DB.Ok text -> case TM.parseBoolExpr (T.unpack text) of
|
||||||
|
Nothing -> DB.Errors [] -- TODO: Proper exception?
|
||||||
|
Just expr -> DB.Ok BoolFormula{ boolFormulaText = text, boolFormulaExpr = expr }
|
||||||
|
|
||||||
data TaskRow = TaskRow
|
data TaskRow = TaskRow
|
||||||
{ rowID :: Integer
|
{ rowID :: Integer
|
||||||
, rowDeadline :: Maybe Day
|
, rowDeadline :: Maybe Day
|
||||||
, rowFormula :: Maybe TM.BoolExpr
|
, rowBoolFormula :: Maybe BoolFormula -- deadline formula
|
||||||
, rowNumberFormula :: Maybe TM.IntExpr
|
, rowIntFormula :: Maybe IntFormula -- info formula (e. g. age for birthdays)
|
||||||
, rowDescription :: T.Text
|
, rowDescription :: T.Text
|
||||||
, rowDetails :: T.Text
|
, rowDetails :: T.Text
|
||||||
, rowRepetitionsTotal :: Integer
|
, rowRepetitionsTotal :: Integer
|
||||||
|
|
@ -29,8 +64,8 @@ instance DB.ToRow TaskRow where
|
||||||
toRow TaskRow{..} = DB.toRow
|
toRow TaskRow{..} = DB.toRow
|
||||||
( rowID
|
( rowID
|
||||||
, rowDeadline
|
, rowDeadline
|
||||||
, rowFormula
|
, rowBoolFormula
|
||||||
, rowNumberFormula
|
, rowIntFormula
|
||||||
, rowDescription
|
, rowDescription
|
||||||
, rowDetails
|
, rowDetails
|
||||||
, rowRepetitionsTotal
|
, rowRepetitionsTotal
|
||||||
|
|
@ -42,41 +77,60 @@ instance DB.FromRow TaskRow where
|
||||||
(a,b,c,d,e,f,g,h) <- DB.fromRow
|
(a,b,c,d,e,f,g,h) <- DB.fromRow
|
||||||
let rowID = a
|
let rowID = a
|
||||||
rowDeadline = b
|
rowDeadline = b
|
||||||
rowFormula = c
|
rowBoolFormula = c
|
||||||
rowNumberFormula = d
|
rowIntFormula = d
|
||||||
rowDescription = e
|
rowDescription = e
|
||||||
rowDetails = f
|
rowDetails = f
|
||||||
rowRepetitionsTotal = g
|
rowRepetitionsTotal = g
|
||||||
rowRepetitionsDone = h
|
rowRepetitionsDone = h
|
||||||
return TaskRow{..}
|
return TaskRow{..}
|
||||||
|
|
||||||
-- TODO: Maybe put this in separate module and/or make less specific
|
-- TODO: Maybe put this in separate module and/or make less specific?
|
||||||
--allowErrorConstraint :: IO () -> IO ()
|
allowErrorConstraint :: IO () -> IO ()
|
||||||
--allowErrorConstraint = handleJust isErrorConstraint (const $ return ())
|
allowErrorConstraint = handleJust isErrorConstraint (const $ return ())
|
||||||
-- where
|
where
|
||||||
-- isErrorConstraint DB.SQLError{DB.sqlError=DB.ErrorConstraint} = Just ()
|
isErrorConstraint DB.SQLError{DB.sqlError=DB.ErrorConstraint} = Just ()
|
||||||
-- isErrorConstraint _ = Nothing
|
isErrorConstraint _ = Nothing
|
||||||
|
|
||||||
initializeNewDB :: DB.Connection -> IO ()
|
initializeNewDB :: DB.Connection -> IO ()
|
||||||
initializeNewDB c = do
|
initializeNewDB c = do
|
||||||
DB.execute_ c createTaskTable
|
DB.execute_ c createTaskTable
|
||||||
-- DB.execute_ c createVersionTable
|
DB.execute_ c createVersionTable
|
||||||
-- allowErrorConstraint $ DB.execute c fillVersionTable (DB.Only (1 :: Integer))
|
allowErrorConstraint $ DB.execute c fillVersionTable (DB.Only (1 :: Integer))
|
||||||
where
|
where
|
||||||
createTaskTable =
|
createTaskTable =
|
||||||
"CREATE TABLE IF NOT EXISTS tasks (\
|
"CREATE TABLE IF NOT EXISTS tasks (\
|
||||||
\ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
|
\ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
|
||||||
\ deadline TEXT,\
|
\ deadline TEXT,\
|
||||||
\ formula TEXT,\
|
\ boolFormula TEXT,\
|
||||||
\ numberFormula TEXT,\
|
\ intFormula TEXT,\
|
||||||
\ description TEXT NOT NULL,\
|
\ description TEXT NOT NULL,\
|
||||||
\ details TEXT NOT NULL DEFAULT \"\",\
|
\ details TEXT NOT NULL DEFAULT \"\",\
|
||||||
\ repetitions_total INTEGER NOT NULL DEFAULT 1,\
|
\ repetitions_total INTEGER NOT NULL DEFAULT 1,\
|
||||||
\ repetitions_done INTEGER NOT NULL DEFAULT 0\
|
\ repetitions_done INTEGER NOT NULL DEFAULT 0\
|
||||||
\)"
|
\)"
|
||||||
-- createVersionTable =
|
createVersionTable =
|
||||||
-- "CREATE TABLE IF NOT EXISTS version (\
|
"CREATE TABLE IF NOT EXISTS version (\
|
||||||
-- \ version_number INTEGER PRIMARY KEY\
|
\ version_number INTEGER PRIMARY KEY\
|
||||||
-- \)"
|
\)"
|
||||||
-- fillVersionTable =
|
fillVersionTable =
|
||||||
-- "INSERT INTO version (version_number) VALUES (?)"
|
"INSERT INTO version (version_number) VALUES (?)"
|
||||||
|
|
||||||
|
updateTask :: TaskRow -> Maybe TaskRow
|
||||||
|
updateTask t = undefined
|
||||||
|
|
||||||
|
updateTasks :: DB.Connection -> IO ()
|
||||||
|
updateTasks c = DB.withTransaction c $ do
|
||||||
|
tasks <- DB.query_ c selectTasksToUpdate
|
||||||
|
let toUpdate = catMaybes $ map updateTask tasks
|
||||||
|
params = map (\t -> (rowDeadline t, rowID t)) toUpdate
|
||||||
|
DB.executeMany c updateTaskRow params
|
||||||
|
where
|
||||||
|
selectTasksToUpdate =
|
||||||
|
"SELECT * FROM tasks\
|
||||||
|
\ WHERE boolFormula IS NOT NULL"
|
||||||
|
updateTaskRow =
|
||||||
|
"UPDATE tasks\
|
||||||
|
\ SET deadline = ?\
|
||||||
|
\ repetitions_done = 0\
|
||||||
|
\ WHERE id = ?"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue