Skip to content

Commit

Permalink
Merge pull request #25 from francesconi/master
Browse files Browse the repository at this point in the history
Lookup columns where the type is an enum array
  • Loading branch information
Zaid-Ajaj authored Feb 9, 2021
2 parents 2a6db5a + 5003c8b commit 959f00c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/NpgsqlFSharpAnalyzer.Core/InformationSchema.fs
Original file line number Diff line number Diff line change
Expand Up @@ -375,26 +375,29 @@ module InformationSchema =
| -1s -> ()
| attnum ->
let udtName = string row.["col_udt_name"]
let isArray = string row.["col_data_type"] = "ARRAY"
let dataType = if isArray then udtName.TrimStart('_') else udtName
let isUdt =
schemas.[schema.Name].Enums
|> Map.tryFind udtName
|> Map.tryFind dataType
|> Option.isSome

let clrType =
match string row.["col_data_type"] with
| "ARRAY" ->
let elemType = getTypeMapping(udtName.TrimStart('_') )
elemType.MakeArrayType()
if not isUdt then
let elemType = getTypeMapping(dataType)
elemType.MakeArrayType()
else typeof<string>.MakeArrayType()
| "USER-DEFINED" ->
if isUdt then typeof<string> else typeof<obj>
| dataType ->
getTypeMapping(dataType)

let column =
let isArray = string row.["col_data_type"] = "ARRAY"
{ ColumnAttributeNumber = attnum
Name = string row.["col_name"]
DataType = { Name = if isArray then udtName.TrimStart('_') else udtName
DataType = { Name = dataType
Schema = schema.Name
IsArray = isArray
ClrType = clrType }
Expand Down
29 changes: 28 additions & 1 deletion tests/NpgsqlFSharpAnalyzer.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ let tests =
Expect.isEmpty messages "No errors returned"
}

test "Semantic analysis: incorrect queries in executeTranscation are detected" {
test "Semantic analysis: incorrect queries in executeTransaction are detected" {
use db = createTestDatabase()

Sql.connect db.ConnectionString
Expand Down Expand Up @@ -430,6 +430,33 @@ let tests =
Expect.isFalse rolesColumn.Nullable "The column is not nullable"
}

test "SQL schema analysis with user defined arrays" {
use db = createTestDatabase ()

Sql.connect db.ConnectionString
|> Sql.executeTransaction [
"CREATE TYPE role AS ENUM ('admin')", []
"CREATE TABLE users (roles role[])", [] ]
|> raiseWhenFailed

let databaseMetadata =
InformationSchema.getDbSchemaLookups db.ConnectionString

let userColumns =
databaseMetadata.Schemas.["public"].Tables
|> Seq.tryFind (fun pair -> pair.Key.Name = "users")
|> Option.map (fun pair -> pair.Value)
|> Option.map List.ofSeq

match userColumns with
| None -> failwith "Expected to find columns for users table"
| Some columns ->
Expect.equal 1 (List.length columns) "There is one column"
let rolesColumn = columns |> List.find (fun column -> column.Name = "roles")
Expect.equal rolesColumn.DataType.Name "role" "The data type is role"
Expect.isTrue rolesColumn.DataType.IsArray "The data type is an array"
}

test "SQL query analysis" {
use db = createTestDatabase()

Expand Down

0 comments on commit 959f00c

Please sign in to comment.