Skip to content

Commit 604e586

Browse files
authored
Merge pull request #47 from smoothdeveloper/better-error-message-table-name-qualified-with-schema
better exception message when sql contains qualified table name
2 parents 0dea2b3 + 16dd792 commit 604e586

5 files changed

Lines changed: 50 additions & 32 deletions

File tree

src/Rezoom.SQL.Compiler/Error.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ let columnAlreadyExists name =
4343
sprintf "SQ018: Column ``%O`` already exists" name
4444
let noSuchColumnToSet tbl col =
4545
sprintf "SQ019: No such column in table ``%O`` to set: ``%O``" tbl col
46-
let noSuchSchema schema =
47-
sprintf "SQ020: No such schema: ``%O``" schema
46+
let noSuchSchema existingSchemas schema =
47+
sprintf
48+
"SQ020: No such schema: ``%O``, existing schemas: %s."
49+
schema
50+
(existingSchemas |> Map.toArray |> Array.map (fst >> sprintf "``%O``") |> String.concat ", ")
4851
let ambiguousColumn name =
4952
sprintf "SQ021: Ambiguous column: ``%O``" name
5053
let ambiguousColumnBetween name tbl1 tbl2 =

src/Rezoom.SQL.Compiler/InferredTypes.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ and [<NoComparison>]
290290
| None ->
291291
let schema = this.Model.Schemas.[this.Model.DefaultSchema]
292292
this.ResolveObjectReferenceBySchema(schema, name.ObjectName, inferView)
293-
| Some schema ->
294-
let schema = this.Model.Schemas.[schema]
293+
| Some schemaName ->
294+
let schema = { Source = name.Source; Value = Some schemaName } |> ModelOps.getRequiredSchema |> State.runForOuputValue this.Model
295295
this.ResolveObjectReferenceBySchema(schema, name.ObjectName, inferView)
296296

297297
/// Resolve a column reference, which may be qualified with a table alias.

src/Rezoom.SQL.Compiler/ModelOps.fs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3048
let 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

4258
let getRequiredTable name =
@@ -75,27 +91,19 @@ let putSchema (schema : Schema) =
7591
/// Create or update an object within an existing schema in the model.
7692
let 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.
89101
let 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+
}

src/Rezoom.SQL.Test/Environment.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ let userModel1() = userModelByName "user-model-1"
1919

2020
let userModel2() = userModelByName "user-model-2"
2121

22-
let expectError (msg : string) (sql : string) =
22+
let expectErrorWithModel (mkMsg : Model -> string) (sql : string) =
2323
let userModel = userModel1()
2424
try
2525
ignore <| CommandEffect.OfSQL(userModel.Model, "anonymous", sql)
2626
failwith "Should've thrown an exception!"
2727
with
2828
| :? SourceException as exn ->
2929
printfn "\"%s\"" exn.Message
30-
Assert.AreEqual(msg, exn.Reason.Trim())
30+
Assert.AreEqual(mkMsg userModel.Model, exn.Reason.Trim())
31+
32+
let expectError (msg : string) (sql : string) = expectErrorWithModel (fun _ -> msg) sql
3133

3234
let dispenserParameterIndexer() =
3335
let dict = Dictionary()

src/Rezoom.SQL.Test/TestModelErrors.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ let ``no such column to index on creation`` () =
4545
[<Test>]
4646
let ``can't drop table referenced by others`` () =
4747
expectError (Error.tableIsReferencedByFKs "main.Users" ["main.UserGroupMaps"])
48-
"drop table Users"
48+
"drop table Users"
49+
50+
[<Test>]
51+
let ``non existing schema name`` () =
52+
expectErrorWithModel (fun model -> Error.noSuchSchema model.Schemas "a")
53+
"select * from a.b"

0 commit comments

Comments
 (0)