Include "enum" field in column details
This commit is contained in:
+32
-9
@@ -11,6 +11,7 @@ import Data.Maybe (mapMaybe)
|
||||
import Control.Applicative ( (<*>) )
|
||||
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import Data.List.Split (splitOn)
|
||||
|
||||
import qualified Data.Aeson as JSON
|
||||
|
||||
@@ -47,6 +48,7 @@ data Column = Column {
|
||||
, colMaxLen :: Maybe Int
|
||||
, colPrecision :: Maybe Int
|
||||
, colDefault :: Maybe String
|
||||
, colEnum :: Maybe [String]
|
||||
} deriving (Show)
|
||||
|
||||
instance JSON.ToJSON Column where
|
||||
@@ -59,7 +61,8 @@ instance JSON.ToJSON Column where
|
||||
, "updatable" .= colUpdatable c
|
||||
, "maxLen" .= colMaxLen c
|
||||
, "precision" .= colPrecision c
|
||||
, "default" .= colDefault c ]
|
||||
, "default" .= colDefault c
|
||||
, "enum" .= colEnum c ]
|
||||
|
||||
data TableOptions = TableOptions {
|
||||
tblOptcolumns :: [Column]
|
||||
@@ -91,17 +94,36 @@ tables s conn = do
|
||||
columns :: String -> String -> Connection -> IO [Column]
|
||||
columns s t conn = do
|
||||
r <- quickQuery conn
|
||||
"select table_schema, table_name, column_name, ordinal_position,\
|
||||
\ is_nullable, data_type, is_updatable,\
|
||||
\ character_maximum_length, numeric_precision,\
|
||||
\ column_default\
|
||||
\ from information_schema.columns\
|
||||
\ where table_schema = ?\
|
||||
\ and table_name = ?" [toSql s, toSql t]
|
||||
" select info.table_schema as schema, info.table_name as table_name, \
|
||||
\ info.column_name as name, info.ordinal_position as position, \
|
||||
\ info.is_nullable as nullable, info.data_type as col_type, \
|
||||
\ info.is_updatable as updatable, info.character_maximum_length as max_len, \
|
||||
\ info.numeric_precision as precision, info.column_default as default_value,\
|
||||
\ array_to_string(enum_info.vals, ',') as enum \
|
||||
\ from ( \
|
||||
\ select table_schema, table_name, column_name, ordinal_position, \
|
||||
\ is_nullable, data_type, is_updatable, \
|
||||
\ character_maximum_length, numeric_precision, \
|
||||
\ column_default, udt_name \
|
||||
\ from information_schema.columns \
|
||||
\ where table_schema = ? \
|
||||
\ and table_name = ? \
|
||||
\ ) as info \
|
||||
\ left outer join ( \
|
||||
\ select n.nspname as s, \
|
||||
\ t.typname as n, \
|
||||
\ array_agg(e.enumlabel) as vals \
|
||||
\ from pg_type t \
|
||||
\ join pg_enum e on t.oid = e.enumtypid \
|
||||
\ join pg_catalog.pg_namespace n ON n.oid = t.typnamespace \
|
||||
\ group by s, n \
|
||||
\ ) as enum_info \
|
||||
\ on (info.udt_name = enum_info.n) \
|
||||
\order by position" [toSql s, toSql t]
|
||||
return $ mapMaybe mkColumn r
|
||||
|
||||
where
|
||||
mkColumn [schema, table, name, pos, nullable, colT, updatable, maxlen, precision, defVal] =
|
||||
mkColumn [schema, table, name, pos, nullable, colT, updatable, maxlen, precision, defVal, enum] =
|
||||
Just $ Column (fromSql schema)
|
||||
(fromSql table)
|
||||
(fromSql name)
|
||||
@@ -112,6 +134,7 @@ columns s t conn = do
|
||||
(fromSql maxlen)
|
||||
(fromSql precision)
|
||||
(fromSql defVal)
|
||||
(splitOn "," <$> fromSql enum)
|
||||
mkColumn _ = Nothing
|
||||
|
||||
printTables :: String -> Connection -> IO BL.ByteString
|
||||
|
||||
Reference in New Issue
Block a user