1------------------------------------------------------------------------------
3-- Module: GenBuiltinTypes
4-- Copyright: (c) 2011-2012 Leon P Smith
6-- Maintainer: Leon P Smith <leon@melding-monads.com>
7-- Stability: experimental
9-- Queries a PostgreSQL for the Object IDs associated with built-in typenames,
10-- and generates Haskell boilerplate to handle this mapping. The subset
11-- of types handled this way is easily modifiable, but note that
12-- user-defined types do not have stable oids, and can differ between
13-- databases or dumps/reloads.
14------------------------------------------------------------------------------
16{-# LANGUAGE OverloadedStrings, QuasiQuotes, ViewPatterns #-}
17module GenBuiltinTypes where
19import Prelude hiding ((++))
22import Database.PostgreSQL.Simple
23import qualified Data.ByteString.Char8 as B
24import qualified Data.ByteString.Lazy as L
25import qualified Blaze.ByteString.Builder as Blaze
26import qualified Blaze.ByteString.Builder.ByteString as Blaze
27import qualified Blaze.ByteString.Builder.Char8 as Blaze
29import Data.List ( sort, intersperse )
30import qualified Data.Map as Map
34(++) :: Monoid a => a -> a -> a
38type Map = Map.Map B.ByteString Int
39builtins :: [(B.ByteString, B.ByteString)]
77timestamptz TimestampTZ
90instance IsString Blaze.Builder where
91 fromString = Blaze.fromByteString . fromString
94 conn <- connectPostgreSQL ""
95 xs <- query conn "SELECT typname::text, oid::int\
98 (Only (In (sort (map fst builtins))))
100 let oidmap = Map.fromList xs :: Map
101 L.writeFile "../src/Database/PostgreSQL/Simple/BuiltinTypes.hs"
102 (Blaze.toLazyByteString (renderFile oidmap builtins))
104padCons str = fromString (replicate (len - B.length str) ' ')
106 len = maximum (map (B.length . snd) builtins)
109 = case Map.lookup x oidmap of
110 Nothing -> error (B.unpack x)
111 Just (show -> n) -> fromString (replicate (4 - length n) ' ')
115 = case Map.lookup x oidmap of
116 Nothing -> error (B.unpack x)
117 Just (show -> n) -> fromString n
118 ++ fromString (replicate (4 - length n) ' ')
120bs = Blaze.fromByteString
122renderFile :: Map -> [(B.ByteString,B.ByteString)] -> Blaze.Builder
123renderFile oidmap builtins = ([longstring|
124{-# LANGUAGE DeriveDataTypeable, OverloadedStrings #-}
126------------------------------------------------------------------------------
128-- Module: Database.PostgreSQL.Simple.BuiltinTypes
129-- Copyright: (c) 2011-2012 Leon P Smith
131-- Maintainer: Leon P Smith <leon@melding-monads.com>
132-- Stability: experimental
134------------------------------------------------------------------------------
136-- Note that this file is generated by tools/GenBuiltinTypes.hs, and should
137-- not be edited directly
139module Database.PostgreSQL.Simple.BuiltinTypes
140 {-# DEPRECATED "Use TypeInfo instead" #-}
149import Data.ByteString (ByteString)
150import qualified Database.PostgreSQL.LibPQ as PQ
153 = |] ++ mconcat (intersperse "\n | " (map (Blaze.fromByteString . snd) builtins)) ++ [longstring|
155 deriving (Eq, Ord, Enum, Bounded, Read, Show, Typeable)
157builtin2oid :: BuiltinType -> PQ.Oid
158builtin2oid typ = PQ.Oid $ case typ of
159|] ++ mconcat [mconcat [ " ", bs y, padCons y, " -> "
160 , renderOid oidmap x, "\n" ]
161 | (x,y) <- builtins ] ++ [longstring|
163oid2builtin :: PQ.Oid -> Maybe BuiltinType
164oid2builtin (PQ.Oid x) = case x of
165|] ++ mconcat [mconcat [ " ", renderOid' oidmap x, " -> Just ", bs y, "\n" ]
166 | (x,y) <- builtins ] ++ [longstring|
169builtin2typname :: BuiltinType -> ByteString
170builtin2typname typ = case typ of
171|] ++ mconcat [mconcat [ " ", bs y, padCons y, " -> ", bs x, "\n"]
172 | (x,y) <- builtins] ++ [longstring|
174oid2typname :: PQ.Oid -> Maybe ByteString
175oid2typname (PQ.Oid x) = case x of
176|] ++ mconcat [mconcat [ " ", renderOid' oidmap x, " -> Just ", bs x, "\n" ]
177 | (x,y) <- builtins ]
179 ++ mconcat [mconcat [ "\n"
180 , bs x, " :: ByteString\n"
181 , bs x, " = \"", bs x, "\"\n"]
182 | (x,_y) <- builtins ])
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list