diff --git a/src/Datalog/Database.elm b/src/Datalog/Database.elm index 350e23d..fd036a5 100644 --- a/src/Datalog/Database.elm +++ b/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(..) @@ -16,7 +16,7 @@ Resources: - - -@docs Database, empty, insert, mergeRelations, replaceRelation +@docs Database, empty, insert, register, mergeRelations, replaceRelation @docs Relation, read, rows @@ -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) = diff --git a/tests/Datalog/DatabaseTests.elm b/tests/Datalog/DatabaseTests.elm index c29f33c..3c002cb 100644 --- a/tests/Datalog/DatabaseTests.elm +++ b/tests/Datalog/DatabaseTests.elm @@ -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"