module Database.Relational.Query.Table (
Untyped, name', width', columns', (!),
Table, unType, name, shortName, width, columns, index, table, toMaybe,
TableDerivable (..)
) where
import Data.Array (Array, listArray, elems)
import qualified Data.Array as Array
import Database.Record (PersistableWidth)
import Database.Relational.Query.Component (ColumnSQL, columnSQL)
data Untyped = Untyped String Int (Array Int ColumnSQL) deriving Show
name' :: Untyped -> String
name' (Untyped n _ _) = n
width' :: Untyped -> Int
width' (Untyped _ w _) = w
columnArray :: Untyped -> Array Int ColumnSQL
columnArray (Untyped _ _ c) = c
columns' :: Untyped -> [ColumnSQL]
columns' = elems . columnArray
(!) :: Untyped
-> Int
-> ColumnSQL
t ! i = columnArray t Array.! i
newtype Table r = Table Untyped
unType :: Table t -> Untyped
unType (Table u) = u
name :: Table r -> String
name = name' . unType
shortName :: Table r -> String
shortName = tail . dropWhile (/= '.') . name
width :: Table r -> Int
width = width' . unType
columns :: Table r -> [ColumnSQL]
columns = columns' . unType
index :: Table r
-> Int
-> ColumnSQL
index = (!) . unType
toMaybe :: Table r -> Table (Maybe r)
toMaybe (Table t) = Table t
table :: String -> [String] -> Table r
table n f = Table $ Untyped n w fa where
w = length f
fa = listArray (0, w 1) $ map columnSQL f
class PersistableWidth r => TableDerivable r where
derivedTable :: Table r