Skip to content

Instantly share code, notes, and snippets.

@jeovazero
Created May 15, 2020 04:07
Show Gist options
  • Save jeovazero/d3560088513dd15cc0c8cecd6a9ae31c to your computer and use it in GitHub Desktop.
Save jeovazero/d3560088513dd15cc0c8cecd6a9ae31c to your computer and use it in GitHub Desktop.
{-# 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