Merge pull request #55 from begriffs/defaults_in_options

Provide column default values in OPTIONS
This commit is contained in:
Joe Nelson
2014-10-07 12:02:30 -07:00
2 changed files with 15 additions and 7 deletions
+7 -3
View File
@@ -46,6 +46,7 @@ data Column = Column {
, colUpdatable :: Bool , colUpdatable :: Bool
, colMaxLen :: Maybe Int , colMaxLen :: Maybe Int
, colPrecision :: Maybe Int , colPrecision :: Maybe Int
, colDefault :: Maybe String
} deriving (Show) } deriving (Show)
instance JSON.ToJSON Column where instance JSON.ToJSON Column where
@@ -57,7 +58,8 @@ instance JSON.ToJSON Column where
, "type" .= colType c , "type" .= colType c
, "updatable" .= colUpdatable c , "updatable" .= colUpdatable c
, "maxLen" .= colMaxLen c , "maxLen" .= colMaxLen c
, "precision" .= colPrecision c ] , "precision" .= colPrecision c
, "default" .= colDefault c ]
data TableOptions = TableOptions { data TableOptions = TableOptions {
tblOptcolumns :: [Column] tblOptcolumns :: [Column]
@@ -91,14 +93,15 @@ columns s t conn = do
r <- quickQuery conn r <- quickQuery conn
"select table_schema, table_name, column_name, ordinal_position,\ "select table_schema, table_name, column_name, ordinal_position,\
\ is_nullable, data_type, is_updatable,\ \ is_nullable, data_type, is_updatable,\
\ character_maximum_length, numeric_precision\ \ character_maximum_length, numeric_precision,\
\ column_default\
\ from information_schema.columns\ \ from information_schema.columns\
\ where table_schema = ?\ \ where table_schema = ?\
\ and table_name = ?" [toSql s, toSql t] \ and table_name = ?" [toSql s, toSql t]
return $ mapMaybe mkColumn r return $ mapMaybe mkColumn r
where where
mkColumn [schema, table, name, pos, nullable, colT, updatable, maxlen, precision] = mkColumn [schema, table, name, pos, nullable, colT, updatable, maxlen, precision, defVal] =
Just $ Column (fromSql schema) Just $ Column (fromSql schema)
(fromSql table) (fromSql table)
(fromSql name) (fromSql name)
@@ -108,6 +111,7 @@ columns s t conn = do
(toBool (fromSql updatable)) (toBool (fromSql updatable))
(fromSql maxlen) (fromSql maxlen)
(fromSql precision) (fromSql precision)
(fromSql defVal)
mkColumn _ = Nothing mkColumn _ = Nothing
printTables :: String -> Connection -> IO BL.ByteString printTables :: String -> Connection -> IO BL.ByteString
+8 -4
View File
@@ -38,7 +38,8 @@ spec = around appWithFixture $ do
"type": "integer", "type": "integer",
"maxLen": null, "maxLen": null,
"nullable": false, "nullable": false,
"position": 1 "position": 1,
"default": "nextval('\"1\".auto_incrementing_pk_id_seq'::regclass)"
}, { }, {
"precision": null, "precision": null,
"updatable": true, "updatable": true,
@@ -47,7 +48,8 @@ spec = around appWithFixture $ do
"type": "character varying", "type": "character varying",
"maxLen": null, "maxLen": null,
"nullable": true, "nullable": true,
"position": 2 "position": 2,
"default": null
}, { }, {
"precision": null, "precision": null,
"updatable": true, "updatable": true,
@@ -56,7 +58,8 @@ spec = around appWithFixture $ do
"type": "character varying", "type": "character varying",
"maxLen": null, "maxLen": null,
"nullable": false, "nullable": false,
"position": 3 "position": 3,
"default": null
}, { }, {
"precision": null, "precision": null,
"updatable": true, "updatable": true,
@@ -65,7 +68,8 @@ spec = around appWithFixture $ do
"type": "timestamp with time zone", "type": "timestamp with time zone",
"maxLen": null, "maxLen": null,
"nullable": true, "nullable": true,
"position": 4 "position": 4,
"default": "now()"
} }
] ]
} }