Adds table related types and some example tables.

pull/1/head
Abhinav Sarkar 2015-12-10 13:58:14 +05:30
parent 7db306602f
commit 048b19d4d2
3 changed files with 62 additions and 2 deletions

View File

@ -17,6 +17,11 @@ library
hs-source-dirs: src hs-source-dirs: src
exposed-modules: Ringo exposed-modules: Ringo
build-depends: base >= 4.7 && < 5 build-depends: base >= 4.7 && < 5
, text
, containers
default-extensions: OverloadedStrings, RecordWildCards, ScopedTypeVariables,
BangPatterns, TupleSections, NamedFieldPuns, GeneralizedNewtypeDeriving,
DeriveDataTypeable
default-language: Haskell2010 default-language: Haskell2010
executable ringo executable ringo
@ -24,6 +29,8 @@ executable ringo
main-is: Main.hs main-is: Main.hs
build-depends: base build-depends: base
, ringo , ringo
, pretty-show
ghc-options: -Wall -fno-warn-unused-do-bind -funbox-strict-fields -fno-warn-orphans
default-language: Haskell2010 default-language: Haskell2010
test-suite ringo-test test-suite ringo-test

View File

@ -1,2 +1,4 @@
module Ringo module Ringo where
() where
import Ringo.Types
import Ringo.Tables

51
src/Ringo/Types.hs Normal file
View File

@ -0,0 +1,51 @@
module Ringo.Types where
import Data.Text (Text)
(&) :: a -> (a -> b) -> b
x & f = f x
type ColumnName = Text
type ColumnType = Text
type TableName = Text
data Column = Column
{ columnName :: ColumnName
, columnType :: ColumnType
, columnNullable :: Bool
, columnDefault :: Maybe Text
} deriving (Eq, Show)
data ColumnRef = ColumnRef ColumnName deriving (Eq, Show)
data TableContraint = PrimaryKey ColumnRef
| UniqueKey [ColumnRef]
| ForeignKey TableRef [(ColumnRef, ColumnRef)]
deriving (Eq, Show)
data Table = Table
{ tableName :: TableName
, tableColumns :: [Column]
, tableConstraints :: [TableContraint]
} deriving (Eq, Show)
data TableRef = TableRef TableName deriving (Eq, Show)
column :: ColumnName -> ColumnType -> Column
column cname ctype = Column cname ctype True Nothing
colNotNull :: Column -> Column
colNotNull c = c { columnNullable = False }
colDefault :: Text -> Column -> Column
colDefault cdefault c = c { columnDefault = Just cdefault }
primaryKey :: ColumnName -> TableContraint
primaryKey = PrimaryKey . ColumnRef
uniqueKey :: [ColumnName] -> TableContraint
uniqueKey = UniqueKey . map ColumnRef
foreignKey :: TableName -> [(ColumnName, ColumnName)] -> TableContraint
foreignKey tableName =
ForeignKey (TableRef tableName) . map (\(c1, c2) -> (ColumnRef c1, ColumnRef c2))