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
89instance IsString Blaze.Builder where
90 fromString = Blaze.fromByteString . fromString
93 conn <- connectPostgreSQL ""
94 xs <- query conn "SELECT typname::text, oid::int\
97 (Only (In (sort (map fst builtins))))
99 let oidmap = Map.fromList xs :: Map
100 L.writeFile "../src/Database/PostgreSQL/Simple/BuiltinTypes.hs"
101 (Blaze.toLazyByteString (renderFile oidmap builtins))
103padCons str = fromString (replicate (len - B.length str) ' ')
105 len = maximum (map (B.length . snd) builtins)
108 = case Map.lookup x oidmap of
109 Nothing -> error (B.unpack x)
110 Just (show -> n) -> fromString (replicate (4 - length n) ' ')
114 = case Map.lookup x oidmap of
115 Nothing -> error (B.unpack x)
116 Just (show -> n) -> fromString n
117 ++ fromString (replicate (4 - length n) ' ')
119bs = Blaze.fromByteString
121renderFile :: Map -> [(B.ByteString,B.ByteString)] -> Blaze.Builder
122renderFile oidmap builtins = ([longstring|
123{-# LANGUAGE DeriveDataTypeable, OverloadedStrings #-}
125------------------------------------------------------------------------------
127-- Module: Database.PostgreSQL.Simple.BuiltinTypes
128-- Copyright: (c) 2011-2012 Leon P Smith
130-- Maintainer: Leon P Smith <leon@melding-monads.com>
131-- Stability: experimental
133------------------------------------------------------------------------------
135-- Note that this file is generated by tools/GenBuiltinTypes.hs, and should
136-- not be edited directly
138module Database.PostgreSQL.Simple.BuiltinTypes
147import Data.ByteString (ByteString)
148import qualified Database.PostgreSQL.LibPQ as PQ
151 = |] ++ mconcat (intersperse "\n | " (map (Blaze.fromByteString . snd) builtins)) ++ [longstring|
153 deriving (Eq, Ord, Enum, Bounded, Read, Show, Typeable)
155builtin2oid :: BuiltinType -> PQ.Oid
156builtin2oid typ = PQ.Oid $ case typ of
157|] ++ mconcat [mconcat [ " ", bs y, padCons y, " -> "
158 , renderOid oidmap x, "\n" ]
159 | (x,y) <- builtins ] ++ [longstring|
161oid2builtin :: PQ.Oid -> Maybe BuiltinType
162oid2builtin (PQ.Oid x) = case x of
163|] ++ mconcat [mconcat [ " ", renderOid' oidmap x, " -> Just ", bs y, "\n" ]
164 | (x,y) <- builtins ] ++ [longstring|
167builtin2typname :: BuiltinType -> ByteString
168builtin2typname typ = case typ of
169|] ++ mconcat [mconcat [ " ", bs y, padCons y, " -> ", bs x, "\n"]
170 | (x,y) <- builtins] ++ [longstring|
172oid2typname :: PQ.Oid -> Maybe ByteString
173oid2typname (PQ.Oid x) = case x of
174|] ++ mconcat [mconcat [ " ", renderOid' oidmap x, " -> Just ", bs x, "\n" ]
175 | (x,y) <- builtins ]
177 ++ mconcat [mconcat [ "\n"
178 , bs x, " :: ByteString\n"
179 , bs x, " = \"", bs x, "\"\n"]
180 | (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