{-# LANGUAGE OverloadedStrings #-}

module PGExtras.Queries.Extensions (extensionsSQL, displayExtensions) where

import Database.PostgreSQL.Simple
import PGExtras.Helpers (maybeText)
import Control.Monad (forM_)
import qualified Data.Text as Text
import Data.List (intercalate)

extensionsSQL :: Query
extensionsSQL :: Query
extensionsSQL = "SELECT name, default_version, installed_version, comment FROM pg_available_extensions ORDER BY installed_version;"

displayExtensions :: [(Maybe Text.Text, Maybe Text.Text, Maybe Text.Text, Maybe Text.Text)] -> IO ()
displayExtensions :: [(Maybe Text, Maybe Text, Maybe Text, Maybe Text)] -> IO ()
displayExtensions rows :: [(Maybe Text, Maybe Text, Maybe Text, Maybe Text)]
rows = do
  String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
description
  String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate " | " [String]
tableHeaders
  [(Maybe Text, Maybe Text, Maybe Text, Maybe Text)]
-> ((Maybe Text, Maybe Text, Maybe Text, Maybe Text) -> IO ())
-> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Maybe Text, Maybe Text, Maybe Text, Maybe Text)]
rows (((Maybe Text, Maybe Text, Maybe Text, Maybe Text) -> IO ())
 -> IO ())
-> ((Maybe Text, Maybe Text, Maybe Text, Maybe Text) -> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \(arg1 :: Maybe Text
arg1, arg2 :: Maybe Text
arg2, arg3 :: Maybe Text
arg3, arg4 :: Maybe Text
arg4) ->
    String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe Text -> String
maybeText(Maybe Text
arg1) String -> String -> String
forall a. [a] -> [a] -> [a]
++ " | " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe Text -> String
maybeText(Maybe Text
arg2) String -> String -> String
forall a. [a] -> [a] -> [a]
++ " | " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe Text -> String
maybeText(Maybe Text
arg3) String -> String -> String
forall a. [a] -> [a] -> [a]
++ " | " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe Text -> String
maybeText(Maybe Text
arg4)

description :: [Char]
description :: String
description = "Available and installed extensions"

tableHeaders :: [[Char]]
tableHeaders :: [String]
tableHeaders = ["name", "default_version", "installed_version", "comment"]