Skip to content

Commit 43c21f4

Browse files
author
Marcos Magueta
committed
add function call support
1 parent 5e1ce5e commit 43c21f4

File tree

2 files changed

+82
-34
lines changed

2 files changed

+82
-34
lines changed

src/SQLProvider.Runtime/Providers.MsSqlServer.Ssdt.fs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ module MSSqlServerSsdt =
112112
sb.AppendLine(s.FullName) |> ignore
113113
failwith (sb.ToString())
114114

115-
116115
/// Tries to parse a schema model from the given .dacpac file path.
117116
let parseDacpac = findDacPacFile >> extractModelXml >> parseXml
118117

@@ -367,38 +366,43 @@ type internal MSSqlServerProviderSsdt(tableNames: string, ssdtPath: string) =
367366
)
368367

369368
member __.GetSprocs(con) =
370-
ssdtSchema.Value.StoredProcs
371-
|> List.map (fun sp ->
372-
let inParams =
373-
sp.Parameters
374-
|> List.mapi (fun idx p ->
375-
{ Name = p.Name
376-
TypeMapping = MSSqlServerSsdt.tryFindMappingOrVariant (ssdtSchema.Value.UserDefinedDataTypes) p.DataType
377-
Direction = if p.IsOutput then ParameterDirection.InputOutput else ParameterDirection.Input
378-
Length = p.Length
379-
Ordinal = idx }
380-
)
381-
let outParams =
382-
sp.Parameters
383-
|> List.filter (fun p -> p.IsOutput)
384-
|> List.mapi (fun idx p ->
385-
{ Name = p.Name
386-
TypeMapping = MSSqlServerSsdt.tryFindMappingOrVariant (ssdtSchema.Value.UserDefinedDataTypes) p.DataType
387-
Direction = ParameterDirection.InputOutput
388-
Length = p.Length
389-
Ordinal = idx }
390-
)
391-
392-
// If no outParams, add a "ResultSet" property (see issue #706)
393-
let outParams =
394-
match outParams with
395-
| [] -> [ sprocReturnParam 0 ]
396-
| _ -> outParams
397-
398-
let spName = { ProcName = sp.Name; Owner = sp.Schema; PackageName = sp.Schema; }
399-
400-
Root("Procedures", Sproc({ Name = spName; Params = (fun con -> inParams); ReturnColumns = (fun con sparams -> outParams) }))
401-
)
369+
let convertExecutable type' elems =
370+
elems
371+
|> List.map (fun sp ->
372+
let inParams =
373+
sp.Parameters
374+
|> List.mapi (fun idx p ->
375+
{ Name = p.Name
376+
TypeMapping = MSSqlServerSsdt.tryFindMappingOrVariant (ssdtSchema.Value.UserDefinedDataTypes) p.DataType
377+
Direction = if p.IsOutput then ParameterDirection.InputOutput else ParameterDirection.Input
378+
Length = p.Length
379+
Ordinal = idx }
380+
)
381+
let outParams =
382+
sp.Parameters
383+
|> List.filter (fun p -> p.IsOutput)
384+
|> List.mapi (fun idx p ->
385+
{ Name = p.Name
386+
TypeMapping = MSSqlServerSsdt.tryFindMappingOrVariant (ssdtSchema.Value.UserDefinedDataTypes) p.DataType
387+
Direction = ParameterDirection.InputOutput
388+
Length = p.Length
389+
Ordinal = idx }
390+
)
391+
392+
// If no outParams, add a "ResultSet" property (see issue #706)
393+
let outParams =
394+
match outParams with
395+
| [] -> [ sprocReturnParam 0 ]
396+
| _ -> outParams
397+
398+
let spName = { ProcName = sp.Name; Owner = sp.Schema; PackageName = sp.Schema; }
399+
400+
Root(type', Sproc({ Name = spName; Params = (fun con -> inParams); ReturnColumns = (fun con sparams -> outParams) }))
401+
)
402+
let sprocs = convertExecutable "Procedures" ssdtSchema.Value.StoredProcs
403+
let funcs = convertExecutable "Functions" ssdtSchema.Value.Functions
404+
List.append sprocs funcs
405+
402406
member __.GetIndividualsQueryText(table,amount) = String.Empty // Not implemented for SSDT
403407
member __.GetIndividualQueryText(table,column) = String.Empty // Not implemented for SSDT
404408

src/SQLProvider.Runtime/Ssdt.DacpacParser.fs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type SsdtSchema = {
1111
Relationships: SsdtRelationship list
1212
Descriptions: SsdtDescriptionItem list
1313
UserDefinedDataTypes: SsdtUserDefinedDataType
14+
Functions: SsdtStoredProc list
1415
}
1516
and SsdtTable = {
1617
FullName: string
@@ -377,6 +378,38 @@ let parseXml(xml: string) =
377378
Name = parts.[1]
378379
Parameters = parameters |> Seq.toList }
379380

381+
let parseFunctions (funElement: XmlNode) =
382+
let fullName = funElement |> att "Name" |> RegexParsers.splitFullName |> fun n -> String.Join(".", n) |> fun n -> n.ToUpper()
383+
let parameters =
384+
let paramsNode = funElement |> nodes "x:Relationship" |> Seq.tryFind (fun r -> r |> att "Name" = "Parameters")
385+
match paramsNode with
386+
| Some e ->
387+
e
388+
|> nodes "x:Entry"
389+
|> Seq.map (fun entry ->
390+
let el = entry |> node "x:Element"
391+
let parameterName = el |> att "Name"
392+
let isOutput =
393+
match el |> nodes "x:Property" |> Seq.tryFind (fun p -> p |> att "Name" = "IsOutput") with
394+
| Some p when p |> att "Value" = "True" -> true
395+
| _ -> false
396+
let dataType =
397+
el
398+
|> node "x:Relationship/x:Entry/x:Element/x:Relationship/x:Entry/x:References"
399+
|> att "Name"
400+
{ FullName = parameterName
401+
Name = parameterName |> RegexParsers.splitFullName |> Array.last
402+
DataType = dataType |> removeBrackets
403+
Length = ValueNone
404+
IsOutput = isOutput })
405+
| None -> Seq.empty
406+
407+
let parts = fullName |> RegexParsers.splitFullName
408+
{ FullName = fullName
409+
Schema = parts.[0]
410+
Name = parts.[1]
411+
Parameters = parameters |> Seq.toList }
412+
380413
let parseDescription (extElement: XmlNode) =
381414
let fullName = extElement |> att "Name"
382415
let parts = fullName |> RegexParsers.splitFullName
@@ -412,6 +445,16 @@ let parseXml(xml: string) =
412445
|> Seq.map parseUserDefinedDataType
413446
|> Map.ofSeq
414447

448+
let functions =
449+
let filterPredicate elem =
450+
(att "Type" elem) = "SqlScalarFunction"
451+
|| (att "Type" elem) = "SqlInlineTableValuedFunction"
452+
model
453+
|> nodes "x:Element"
454+
|> Seq.filter filterPredicate
455+
|> Seq.map parseFunctions
456+
|> Seq.toList
457+
415458
let storedProcs =
416459
model
417460
|> nodes "x:Element"
@@ -492,4 +535,5 @@ let parseXml(xml: string) =
492535
TryGetTableByName = fun nm -> tablesAndViews |> (List.tryFind (fun t -> t.Name = nm) >> function Some x -> ValueSome x | None -> ValueNone)
493536
Relationships = relationships
494537
Descriptions = descriptions
495-
UserDefinedDataTypes = userDefinedDataTypes } : SsdtSchema
538+
UserDefinedDataTypes = userDefinedDataTypes
539+
Functions = functions } : SsdtSchema

0 commit comments

Comments
 (0)