module Database.Relational.Table (
Table, untype, name, shortName, width, columns, index, table, toMaybe, recordWidth,
unType,
toSubQuery,
TableDerivable (..)
) where
import Data.Array (listArray)
import Database.Record.Persistable
(PersistableWidth, PersistableRecordWidth, unsafePersistableRecordWidth)
import Database.Relational.Internal.UntypedTable (Untyped (Untyped), name', width', columns', (!))
import Database.Relational.Internal.String (StringSQL, stringSQL, )
import Database.Relational.SqlSyntax (SubQuery)
import qualified Database.Relational.SqlSyntax as Syntax
newtype Table r = Table Untyped
untype :: Table t -> Untyped
untype (Table u) = u
unType :: Table t -> Untyped
unType = untype
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 -> [StringSQL]
columns = columns' . untype
index :: Table r
-> Int
-> StringSQL
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 stringSQL f
toSubQuery :: Table r
-> SubQuery
toSubQuery = Syntax.Table . untype
class PersistableWidth r => TableDerivable r where
derivedTable :: Table r
recordWidth :: Table r -> PersistableRecordWidth r
recordWidth = unsafePersistableRecordWidth . width