@@ -27,16 +27,32 @@ let private requireNoObject (name : QualifiedObjectName WithSource) =
2727 | None -> ()
2828 }
2929
30+ let getRequiredSchema ( schemaName : Name option WithSource ) =
31+ stateful {
32+ let! model = State.get
33+ let source = schemaName.Source
34+ let schemaName =
35+ match schemaName.Value with
36+ | None -> model.DefaultSchema
37+ | Some name -> name
38+ let! schema = getSchema schemaName
39+ return
40+ match schema with
41+ | Some obj -> obj
42+ | None ->
43+ schemaName
44+ |> Error.noSuchSchema model.Schemas
45+ |> failAt source
46+ }
47+
3048let getRequiredObject objectTypeName ( name : QualifiedObjectName WithSource ) =
3149 stateful {
32- let! schema = getSchema name.Value.SchemaName
33- match schema with
34- | None -> return failAt name.Source <| Error.noSuchSchema name.Value.SchemaName
35- | Some schema ->
36- return
37- match schema.Objects |> Map.tryFind name.Value.ObjectName with
38- | None -> failAt name.Source <| Error.noSuchObject objectTypeName name.Value.ObjectName
39- | Some obj -> obj
50+ let! model = State.get
51+ let! schema = name.Map( fun n -> Some n.SchemaName) |> getRequiredSchema
52+ return
53+ match schema.Objects |> Map.tryFind name.Value.ObjectName with
54+ | None -> failAt name.Source <| Error.noSuchObject objectTypeName name.Value.ObjectName
55+ | Some obj -> obj
4056 }
4157
4258let getRequiredTable name =
@@ -75,27 +91,19 @@ let putSchema (schema : Schema) =
7591/// Create or update an object within an existing schema in the model.
7692let putObject ( name : QualifiedObjectName WithSource ) ( obj : SchemaObject ) =
7793 stateful {
78- let! schema = getSchema name.Value.SchemaName
79- match schema with
80- // shouldn't have called this with a bogus schema
81- | None ->
82- failAt name.Source <| Error.noSuchSchema name.Value.SchemaName
83- | Some schema ->
84- let newSchema = { schema with Objects = schema.Objects |> Map.add name.Value.ObjectName obj }
85- return ! putSchema newSchema
94+ let! model = State.get
95+ let! schema = name.Map( fun n -> Some n.SchemaName) |> getRequiredSchema
96+ let newSchema = { schema with Objects = schema.Objects |> Map.add name.Value.ObjectName obj }
97+ return ! putSchema newSchema
8698 }
8799
88100/// Remove an existing object from the model.
89101let removeObject ( name : QualifiedObjectName WithSource ) =
90102 stateful {
91- let! schema = getSchema name.Value.SchemaName
92- match schema with
93- // shouldn't have called this with a bogus schema
94- | None ->
95- failAt name.Source <| Error.noSuchSchema name.Value.SchemaName
96- | Some schema ->
97- let newSchema = { schema with Objects = schema.Objects |> Map.remove name.Value.ObjectName }
98- return ! putSchema newSchema
103+ let! model = State.get
104+ let! schema = name.Map( fun n -> Some n.SchemaName) |> getRequiredSchema
105+ let newSchema = { schema with Objects = schema.Objects |> Map.remove name.Value.ObjectName }
106+ return ! putSchema newSchema
99107 }
100108
101109/// Create a new table with a given name.
@@ -477,4 +485,4 @@ let changeColumnCollation tableName (columnName : Name WithSource) newCollation
477485 { col with Collation = Some newCollation }
478486 let table = { table with Columns = table.Columns |> Map.add columnName.Value newColumn }
479487 return ! putObject tableName ( SchemaTable table)
480- }
488+ }
0 commit comments