Created
May 15, 2020 04:07
-
-
Save jeovazero/d3560088513dd15cc0c8cecd6a9ae31c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
module Repository.Tools.Statements ( | |
addTool, | |
getTools, | |
removeToolById, | |
addTags | |
) where | |
import Database.PostgreSQL.Typed (pgQuery, pgSQL, PGConnection) | |
import Database.PostgreSQL.Typed.Array () | |
import Database.PostgreSQL.Typed.Query (PGSimpleQuery) | |
import Data.UUID (UUID) | |
import Data.ByteString.Char8 (ByteString) | |
import DB.Session (useTPGDatabase, settings) | |
useTPGDatabase settings | |
getTools :: PGConnection -> IO [(UUID, ByteString, Maybe ByteString, Maybe ByteString)] | |
getTools conn = | |
pgQuery conn [pgSQL| | |
SELECT tool_id, hello.tools.name, hello.tools.description, hello.tags.name | |
FROM hello.tools | |
LEFT JOIN hello.tags USING (tool_id) | |
|] | |
addTool :: PGConnection -> ByteString -> ByteString -> IO [UUID] | |
addTool conn name description = | |
pgQuery conn [pgSQL| | |
INSERT INTO hello.tools (name, description) | |
VALUES (${name}, ${description}) | |
RETURNING tool_id | |
|] | |
removeToolById :: PGConnection -> UUID -> IO [()] | |
removeToolById conn tool_id = | |
pgQuery conn [pgSQL| | |
DELETE FROM hello.tools | |
WHERE tool_id = ${tool_id} | |
|] | |
addTags :: PGConnection -> [UUID] -> [ByteString] -> IO [UUID] | |
addTags conn tools_ids names = | |
pgQuery conn [pgSQL| | |
INSERT INTO hello.tags (tool_id,name) | |
SELECT * FROM UNNEST(${tools_ids}::uuid[], ${names}::text[]) | |
returning tool_id | |
|] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment