+19
-14
@@ -1,4 +1,4 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
|
||||||
|
|
||||||
module PgStructure where
|
module PgStructure where
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ import qualified Data.Aeson as JSON
|
|||||||
import Database.PostgreSQL.Simple
|
import Database.PostgreSQL.Simple
|
||||||
import Database.PostgreSQL.Simple.Types
|
import Database.PostgreSQL.Simple.Types
|
||||||
import Database.PostgreSQL.Simple.FromRow
|
import Database.PostgreSQL.Simple.FromRow
|
||||||
--import Database.PostgreSQL.Simple.Arrays (esc)
|
import Database.PostgreSQL.Simple.SqlQQ
|
||||||
|
|
||||||
import Data.Aeson ((.=))
|
import Data.Aeson ((.=))
|
||||||
|
|
||||||
@@ -67,18 +67,20 @@ instance JSON.ToJSON Column where
|
|||||||
|
|
||||||
tables :: String -> Connection -> IO [Table]
|
tables :: String -> Connection -> IO [Table]
|
||||||
tables s conn = query conn q $ Only s
|
tables s conn = query conn q $ Only s
|
||||||
where q = "select table_schema, table_name,\
|
where q = [sql|
|
||||||
\ is_insertable_into \
|
select table_schema, table_name,\
|
||||||
\ from information_schema.tables\
|
is_insertable_into \
|
||||||
\ where table_schema = ?"
|
from information_schema.tables\
|
||||||
|
where table_schema = ? |]
|
||||||
|
|
||||||
columns :: T.Text -> Connection -> IO [Column]
|
columns :: T.Text -> Connection -> IO [Column]
|
||||||
columns t conn = query conn q $ Only t
|
columns t conn = query conn q $ Only t
|
||||||
where q = "select table_schema, table_name, column_name, ordinal_position,\
|
where q = [sql|
|
||||||
\ is_nullable, data_type, is_updatable,\
|
select table_schema, table_name, column_name, ordinal_position,
|
||||||
\ character_maximum_length, numeric_precision\
|
is_nullable, data_type, is_updatable,
|
||||||
\ from information_schema.columns\
|
character_maximum_length, numeric_precision
|
||||||
\ where table_name = ?"
|
from information_schema.columns
|
||||||
|
where table_name = ? |]
|
||||||
|
|
||||||
namedColumnHash :: [Column] -> HashMap String Column
|
namedColumnHash :: [Column] -> HashMap String Column
|
||||||
namedColumnHash = fromList . (Prelude.zip =<< Prelude.map colName)
|
namedColumnHash = fromList . (Prelude.zip =<< Prelude.map colName)
|
||||||
@@ -90,6 +92,9 @@ printColumns :: T.Text -> Connection -> IO BL.ByteString
|
|||||||
printColumns table conn = JSON.encode . namedColumnHash <$> columns table conn
|
printColumns table conn = JSON.encode . namedColumnHash <$> columns table conn
|
||||||
|
|
||||||
selectAll :: T.Text -> Connection -> IO JSON.Value
|
selectAll :: T.Text -> Connection -> IO JSON.Value
|
||||||
selectAll table conn = fromOnly <$> Prelude.head <$> query conn sql (Only $ QualifiedIdentifier (Just "base") table)
|
selectAll table conn = fromOnly <$> Prelude.head <$> query conn q (Only safeName)
|
||||||
where sql = "select array_to_json(array_agg(row_to_json(t)))\
|
where
|
||||||
\ from (select * from ?) t;"
|
q = [sql|
|
||||||
|
select array_to_json(array_agg(row_to_json(t)))
|
||||||
|
from (select * from ?) t; |]
|
||||||
|
safeName = QualifiedIdentifier (Just "base") table
|
||||||
|
|||||||
Reference in New Issue
Block a user