Skip to content

Commit

Permalink
implement register for the database
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed May 13, 2021
1 parent bea41c4 commit 9f97b6d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Datalog/Database.elm
@@ -1,5 +1,5 @@
module Datalog.Database exposing
( Database, empty, insert, mergeRelations, replaceRelation
( Database, empty, insert, register, mergeRelations, replaceRelation
, Relation, read, rows
, Schema, FieldType(..)
, Constant(..), Problem(..)
Expand All @@ -16,7 +16,7 @@ Resources:
- <https://cs.uwaterloo.ca/~tozsu/courses/CS338/lectures/5%20Rel%20Algebra.pdf>
- <https://www.cs.ubc.ca/~laks/cpsc304/Unit05-FormalLanguages.pdf>
@docs Database, empty, insert, mergeRelations, replaceRelation
@docs Database, empty, insert, register, mergeRelations, replaceRelation
@docs Relation, read, rows
Expand Down Expand Up @@ -157,6 +157,32 @@ insert relationName row (Database db) =
)


register : String -> List FieldType -> Database -> Result Problem Database
register relationName schema (Database db) =
case Dict.get relationName db of
Nothing ->
db
|> Dict.insert relationName
(Relation
(Array.fromList schema)
(Set.empty rowSorter)
)
|> Database
|> Ok

Just (Relation existingSchema rows_) ->
if existingSchema == Array.fromList schema then
Ok (Database db)

else
Err
(SchemaMismatch
{ wanted = existingSchema
, got = Array.fromList schema
}
)


{-| -}
mergeRelations : String -> Relation -> Database -> Result Problem Database
mergeRelations relationName (Relation schema newRows) (Database db) =
Expand Down
60 changes: 60 additions & 0 deletions tests/Datalog/DatabaseTests.elm
Expand Up @@ -147,6 +147,66 @@ readTests =
]


registerTests : Test
registerTests =
describe "register"
[ test "cannot read a relation which has not been pre-registered and doesn't exist" <|
\_ ->
empty
|> read "test"
|> Expect.equal (Err (RelationNotFound "test"))
, test "can read a relation which has been pre-registered but doesn't exist" <|
\_ ->
empty
|> register "test" [ StringType ]
|> Result.andThen (read "test")
|> Result.map rows
|> Expect.equal (Ok [])
, test "can read the actual rows from a field that has been pre-registered and then filled" <|
\_ ->
empty
|> register "test" [ StringType ]
|> Result.andThen (insert "test" [ String "test" ])
|> Result.andThen (read "test")
|> Result.map rows
|> Expect.equal (Ok [ Array.fromList [ String "test" ] ])
, test "can read the actual rows from a field that has been filled and then pre-registered" <|
\_ ->
empty
|> insert "test" [ String "test" ]
|> Result.andThen (register "test" [ StringType ])
|> Result.andThen (read "test")
|> Result.map rows
|> Expect.equal (Ok [ Array.fromList [ String "test" ] ])
, test "cannot register with the wrong schema after inserting" <|
\_ ->
empty
|> insert "test" [ String "test" ]
|> Result.andThen (register "test" [ IntType ])
|> Expect.equal
(Err
(SchemaMismatch
{ wanted = Array.fromList [ StringType ]
, got = Array.fromList [ IntType ]
}
)
)
, test "cannot insert with the wrong schema after registering" <|
\_ ->
empty
|> register "test" [ IntType ]
|> Result.andThen (insert "test" [ String "test" ])
|> Expect.equal
(Err
(SchemaMismatch
{ wanted = Array.fromList [ IntType ]
, got = Array.fromList [ StringType ]
}
)
)
]


queryTests : Test
queryTests =
describe "query"
Expand Down

0 comments on commit 9f97b6d

Please sign in to comment.