Skip to content

Commit

Permalink
Reformat all SQL queries with uppercase keywords and use sql QuasiQuoter
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Mar 14, 2024
1 parent 8b35b65 commit 6565730
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 406 deletions.
2 changes: 1 addition & 1 deletion archive/tasklite-server/source/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ getTasks conf tags = do
getTags :: Config -> Servant.Handler [Tag]
getTags conf =
liftIO $ execWithConn conf $ \connection ->
SQLite.query_ connection "select * from tags" :: IO [Tag]
SQLite.query_ connection "SELECT * FROM tags" :: IO [Tag]


-- `serve` comes from servant and hands you a WAI Application,
Expand Down
7 changes: 6 additions & 1 deletion tasklite-app/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ main = do
tasks <-
query_
connection
"select * from tasks_view order by ulid asc limit 10"
[sql|
SELECT *
FROM tasks_view
ORDER BY ulid ASC
LIMIT 10
|]

void $
run
Expand Down
10 changes: 4 additions & 6 deletions tasklite-core/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ commandParser conf =

-- TODO: Replace with tasks and tags commands
<> command "query" (toParserInfo (QueryTasks <$> strArgument
(metavar "QUERY" <> help "The SQL query after the \"where\" clause"))
"Run \"select * from tasks where QUERY\" on the database")
(metavar "QUERY" <> help "The SQL query after the \"WHERE\" clause"))
"Run \"SELECT * FROM tasks WHERE QUERY\" on the database")

-- <> command "metadata" (toParserInfo (pure $ ListNoTag)
-- "List all tasks with metadata")
Expand All @@ -772,11 +772,11 @@ commandParser conf =

-- <> command "tasks" (toParserInfo (QueryTasks <$> strArgument
-- (metavar "QUERY" <> help "The SQL query after the \"where\" clause"))
-- "Run \"select * from tasks where QUERY\" on the database")
-- "Run \"SELECT * FROM tasks WHERE QUERY\" on the database")

-- <> command "tags" (toParserInfo (QueryTasks <$> strArgument
-- (metavar "QUERY" <> help "The SQL query after the \"where\" clause"))
-- "Run \"select * from tasks where QUERY\" on the database")
-- "Run \"SELECT * FROM tasks WHERE QUERY\" on the database")

-- <> command "newest" "Show the newest task"
-- <> command "oldest" "Show the oldest task"
Expand Down Expand Up @@ -1099,8 +1099,6 @@ executeCLiCommand conf now connection = do

cliArgs <- execParser (parserInfo conf)

-- [[sqliteVersion]] <- SQLite.query_ connection "select sqlite_version()"

if runHelpCommand cliArgs
then pure $ extendHelp $ parserHelp defaultPrefs $ cliArgsParser conf
else case cliCommand cliArgs of
Expand Down
39 changes: 29 additions & 10 deletions tasklite-core/source/FullTask.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE QuasiQuotes #-}

{-|
Data type to represent tasks from the `tasks_view`
-}
Expand Down Expand Up @@ -32,15 +34,18 @@ import Data.Csv as Csv (
)
import Data.Text as T (Text, dropEnd, intercalate, split, splitOn)
import Data.Yaml as Yaml (encode)
import Database.SQLite.Simple (FromRow, SQLData (SQLNull, SQLText), field)
import Database.SQLite.Simple as Sql (
import Database.SQLite.Simple (
FromRow (..),
Query,
ResultError (ConversionFailed),
SQLData (SQLNull, SQLText),
field,
)
import Database.SQLite.Simple.FromField (FromField (..), fieldData, returnError)
import Database.SQLite.Simple.FromRow (fieldWith)
import Database.SQLite.Simple.Internal (Field (Field))
import Database.SQLite.Simple.Ok (Ok (Errors, Ok))
import Database.SQLite.Simple.QQ (sql)
import GHC.Exception (errorCallException)
import Prettyprinter (Pretty (pretty))

Expand Down Expand Up @@ -202,15 +207,29 @@ emptyFullTask =
}


selectQuery :: Text
selectQuery :: Query
selectQuery =
"\
\select\n\
\ tasks_view.ulid as ulid, body, modified_utc, awake_utc, ready_utc,\n\
\ waiting_utc, review_utc, due_utc, closed_utc, state,\n\
\ group_ulid, repetition_duration, recurrence_duration,\n\
\ tags, notes, priority, user, metadata\n\
\"
[sql|
SELECT
tasks_view.ulid AS ulid,
body,
modified_utc,
awake_utc,
ready_utc,
waiting_utc,
review_utc,
due_utc,
closed_utc,
state,
group_ulid,
repetition_duration,
recurrence_duration,
tags,
notes,
priority,
user,
metadata
|]


cpTimesAndState :: FullTask -> Task
Expand Down
6 changes: 3 additions & 3 deletions tasklite-core/source/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -626,15 +626,15 @@ ingestFile config connection filePath = do
dumpCsv :: Config -> IO (Doc AnsiStyle)
dumpCsv conf = do
execWithConn conf $ \connection -> do
rows :: [FullTask] <- query_ connection "select * from tasks_view"
rows :: [FullTask] <- query_ connection "SELECT * FROM tasks_view"
pure $ pretty $ TL.decodeUtf8 $ Csv.encodeDefaultOrderedByName rows


dumpNdjson :: Config -> IO (Doc AnsiStyle)
dumpNdjson conf = do
-- TODO: Use Task instead of FullTask to fix broken notes export
execWithConn conf $ \connection -> do
tasks :: [FullTask] <- query_ connection "select * from tasks_view"
tasks :: [FullTask] <- query_ connection "SELECT * FROM tasks_view"
pure $
vsep $
fmap (pretty . TL.decodeUtf8 . Aeson.encode) tasks
Expand All @@ -644,7 +644,7 @@ dumpJson :: Config -> IO (Doc AnsiStyle)
dumpJson conf = do
-- TODO: Use Task instead of FullTask to fix broken notes export
execWithConn conf $ \connection -> do
tasks :: [FullTask] <- query_ connection "select * from tasks_view"
tasks :: [FullTask] <- query_ connection "SELECT * FROM tasks_view"
pure $ pretty $ fmap (TL.decodeUtf8 . Aeson.encode) tasks


Expand Down
Loading

0 comments on commit 6565730

Please sign in to comment.