-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathSqlClientExtensions.fs
More file actions
693 lines (620 loc) · 30.9 KB
/
SqlClientExtensions.fs
File metadata and controls
693 lines (620 loc) · 30.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
[<AutoOpen>]
module FSharp.Data.SqlClient.Extensions
open System
open System.Data
open System.Collections.Generic
open System.IO
open System.Threading.Tasks
open System.Data.SqlClient
open FSharp.Data.SqlClient.Internals
open System.Diagnostics
type internal TypeInfoPerConnectionStringCache() =
let key = obj()
let dataTypeMappings = Dictionary<string, TypeInfo[]>()
let lock f = lock key f
member x.Clear(reason: string) =
lock (fun () ->
dataTypeMappings.Clear()
)
member x.ContainsConnectionString connectionString =
lock (fun () -> dataTypeMappings.ContainsKey connectionString)
member x.RegisterTypes(connectionString, types) =
lock (fun () ->
if dataTypeMappings.ContainsKey connectionString then
#if DEBUG
let existingTypes = dataTypeMappings.[connectionString]
if existingTypes = types then
System.Diagnostics.Debug.WriteLine(sprintf "types existing for connection %s, old types and new types are identical" connectionString)
else
System.Diagnostics.Debug.WriteLine(sprintf "types existing for connection %s, old types and new types are different" connectionString)
#endif
dataTypeMappings.[connectionString] <- types
else
dataTypeMappings.Add(connectionString, types)
)
member x.GetTypesForConnectionString connectionString =
lock (fun () ->
match dataTypeMappings.TryGetValue connectionString with
| true, types -> types
| false, _ ->
raise (new InvalidOperationException(sprintf "types for connection %s were not retrieved!" connectionString))
)
member x.DoIfConnectionStringNotRegistered connectionString ifAlreadyDone f =
lock (fun () ->
if x.ContainsConnectionString connectionString then
ifAlreadyDone ()
else
f ()
)
let internal sqlDataTypesCache = new TypeInfoPerConnectionStringCache()
let internal findTypeInfoBySqlEngineTypeId (connStr, system_type_id, user_type_id : int option) =
assert (sqlDataTypesCache.ContainsConnectionString connStr)
sqlDataTypesCache.GetTypesForConnectionString connStr
|> Array.filter(fun x ->
let result =
x.SqlEngineTypeId = system_type_id &&
(user_type_id.IsSome && x.UserTypeId = user_type_id.Value || user_type_id.IsNone && x.UserTypeId = int system_type_id)
result
)
|> Seq.exactlyOne
let internal findTypeInfoByProviderType(connStr, sqlDbType) =
assert (sqlDataTypesCache.ContainsConnectionString connStr)
sqlDataTypesCache.GetTypesForConnectionString connStr |> Array.find (fun x -> x.SqlDbType = sqlDbType)
type LiteralType = Microsoft.SqlServer.TransactSql.ScriptDom.LiteralType
type UnaryExpression = Microsoft.SqlServer.TransactSql.ScriptDom.UnaryExpression
let rec parseDefaultValue (definition: string) (expr: Microsoft.SqlServer.TransactSql.ScriptDom.ScalarExpression) =
match expr with
| :? Microsoft.SqlServer.TransactSql.ScriptDom.Literal as x ->
match x.LiteralType with
| LiteralType.Default | LiteralType.Null -> Some null
| LiteralType.Integer -> x.Value |> int |> box |> Some
| LiteralType.Money | LiteralType.Numeric -> x.Value |> decimal |> box |> Some
| LiteralType.Real -> x.Value |> float |> box |> Some
| LiteralType.String -> x.Value |> string |> box |> Some
| _ -> None
| :? UnaryExpression as x when x.UnaryExpressionType <> Microsoft.SqlServer.TransactSql.ScriptDom.UnaryExpressionType.BitwiseNot ->
let fragment = definition.Substring( x.StartOffset, x.FragmentLength)
match x.Expression with
| :? Microsoft.SqlServer.TransactSql.ScriptDom.Literal as x ->
match x.LiteralType with
| LiteralType.Integer -> fragment |> int |> box |> Some
| LiteralType.Money | LiteralType.Numeric -> fragment |> decimal |> box |> Some
| LiteralType.Real -> fragment |> float |> box |> Some
| _ -> None
| _ -> None
| _ -> None
type internal RoutineType = StoredProcedure | TableValuedFunction | ScalarValuedFunction
type internal Routine = {
Type: RoutineType
Schema: string
Name: string
Definition: string
Description: string option
BaseObject: string * string
} with
member this.TwoPartName = this.Schema, this.Name
member this.IsStoredProc = this.Type = StoredProcedure
member this.ToCommandText(parameters: Parameter list) =
let twoPartNameIdentifier = sprintf "%s.%s" <|| this.TwoPartName
match this.Type with
| StoredProcedure -> twoPartNameIdentifier
| TableValuedFunction ->
parameters |> List.map (fun p -> p.Name) |> String.concat ", " |> sprintf "SELECT * FROM %s(%s)" twoPartNameIdentifier
| ScalarValuedFunction ->
parameters |> List.map (fun p -> p.Name) |> String.concat ", " |> sprintf "SELECT %s(%s)" twoPartNameIdentifier
let internal providerTypes =
dict [
// exact numerics
"bigint", (SqlDbType.BigInt, "System.Int64", true)
"bit", (SqlDbType.Bit, "System.Boolean", true)
"decimal", (SqlDbType.Decimal, "System.Decimal", true)
"int", (SqlDbType.Int, "System.Int32", true)
"money", (SqlDbType.Money, "System.Decimal", true)
"numeric", (SqlDbType.Decimal, "System.Decimal", true)
"smallint", (SqlDbType.SmallInt, "System.Int16", true)
"smallmoney", (SqlDbType.SmallMoney, "System.Decimal", true)
"tinyint", (SqlDbType.TinyInt, "System.Byte", true)
// approximate numerics
"float", (SqlDbType.Float, "System.Double", true) // This is correct. SQL Server 'float' type maps to double
"real", (SqlDbType.Real, "System.Single", true)
// date and time
"date", (SqlDbType.Date, "System.DateTime", true)
"datetime", (SqlDbType.DateTime, "System.DateTime", true)
"datetime2", (SqlDbType.DateTime2, "System.DateTime", true)
"datetimeoffset", (SqlDbType.DateTimeOffset, "System.DateTimeOffset", true)
"smalldatetime", (SqlDbType.SmallDateTime, "System.DateTime", true)
"time", (SqlDbType.Time, "System.TimeSpan", true)
// character strings
"char", (SqlDbType.Char, "System.String", false)
"text", (SqlDbType.Text, "System.String", false)
"varchar", (SqlDbType.VarChar, "System.String", false)
// unicode character strings
"nchar", (SqlDbType.NChar, "System.String", false)
"ntext", (SqlDbType.NText, "System.String", false)
"nvarchar", (SqlDbType.NVarChar, "System.String", false)
"sysname", (SqlDbType.NVarChar, "System.String", false)
// binary
"binary", (SqlDbType.Binary, "System.Byte[]", false)
"image", (SqlDbType.Image, "System.Byte[]", false)
"varbinary", (SqlDbType.VarBinary, "System.Byte[]", false)
//spatial
"geography", (SqlDbType.Udt, "Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types", false)
"geometry", (SqlDbType.Udt, "Microsoft.SqlServer.Types.SqlGeometry, Microsoft.SqlServer.Types", false)
//other
"hierarchyid", (SqlDbType.Udt, "Microsoft.SqlServer.Types.SqlHierarchyId, Microsoft.SqlServer.Types", false)
"sql_variant", (SqlDbType.Variant, "System.Object", false)
"timestamp", (SqlDbType.Timestamp, "System.Byte[]", true) // note: rowversion is a synonym but SQL Server stores the data type as 'timestamp'
"uniqueidentifier", (SqlDbType.UniqueIdentifier, "System.Guid", true)
"xml", (SqlDbType.Xml, "System.String", false)
//TODO
//"cursor", typeof<TODO>
//"table", typeof<TODO>
]
type internal SqlTypeEntry = {
name : string
system_type_id : byte
user_type_id : int
is_table_type : bool
schema_name : string
is_user_defined : bool
precision : int16
scale : int16
}
type internal TableVariableEntry = {
name : string
system_type_id : byte
user_type_id : int
is_nullable : bool
max_length : int16
is_identity : bool
is_computed : bool
table_type_user_type_id : int
precision : byte
scale : byte
}
type SqlConnection with
//address an issue when regular Dispose on SqlConnection needed for async computation
//wipes out all properties like ConnectionString in addition to closing connection to db
member internal this.CheckVersion() =
assert (this.State = ConnectionState.Open)
let majorVersion = this.ServerVersion.Split('.').[0]
if int majorVersion < 11
then failwithf "Minimal supported major version is 11 (SQL Server 2012 and higher or Azure SQL Database). Currently used: %s" this.ServerVersion
member internal this.GetUserSchemas() =
use __ = this.UseLocally()
use cmd = this.CreateCommand(CommandText = "SELECT name FROM sys.schemas WHERE principal_id = 1")
cmd.ExecuteQuery(fun record -> record.GetString(0)) |> Seq.toList
member internal this.GetRoutines( schema, isSqlAzure) =
assert (this.State = ConnectionState.Open)
let descriptionSelector =
if isSqlAzure
then
"(SELECT NULL AS Value)"
else
"fn_listextendedproperty ('MS_Description', 'schema', BaseObjectSchema, ROUTINE_TYPE, BaseObjectName, default, default)"
let getRoutinesQuery =
sprintf "
WITH ExplicitRoutines AS
(
SELECT
ROUTINE_SCHEMA AS [Schema]
,ROUTINE_NAME AS Name
,ROUTINE_TYPE
,DATA_TYPE
,ROUTINE_SCHEMA AS BaseObjectSchema
,ROUTINE_NAME AS BaseObjectName
FROM
INFORMATION_SCHEMA.ROUTINES
),
Synonyms AS
(
SELECT
OBJECT_SCHEMA_NAME(object_id) AS [Schema]
,name AS Name
,ROUTINE_TYPE
,DATA_TYPE
,ROUTINE_SCHEMA AS BaseObjectSchema
,ROUTINE_NAME AS BaseObjectName
FROM
sys.synonyms
JOIN INFORMATION_SCHEMA.ROUTINES ON
OBJECT_ID(ROUTINES.ROUTINE_SCHEMA + '.' + ROUTINES.ROUTINE_NAME) = OBJECT_ID(base_object_name)
)
SELECT
XS.[Schema]
,XS.Name
,RoutineSubType =
CASE
WHEN XS.DATA_TYPE = 'TABLE' THEN 'TableValuedFunction'
WHEN XS.DATA_TYPE IS NULL THEN 'StoredProcedure'
ELSE 'ScalarValuedFunction'
END
,ISNULL( OBJECT_DEFINITION( OBJECT_ID( XS.BaseObjectSchema + '.' + XS.BaseObjectName)), '') AS [Definition]
,XS.BaseObjectSchema
,XS.BaseObjectName
,[Description] = XProp.Value
FROM
(
SELECT * FROM ExplicitRoutines
UNION ALL
SELECT * FROM Synonyms
) AS XS
OUTER APPLY %s AS XProp
WHERE
[Schema] = @schema
" descriptionSelector
use cmd = this.CreateCommand(CommandText = getRoutinesQuery)
cmd.Parameters.AddWithValue("@schema", schema) |> ignore
cmd.ExecuteQuery(fun x ->
let schema, name = unbox x.["Schema"], unbox x.["Name"]
let definition = unbox x.["Definition"]
let description = x.TryGetValue( "Description")
let routineType =
match string x.["RoutineSubType"] with
| "TableValuedFunction" -> TableValuedFunction
| "StoredProcedure" -> StoredProcedure
| "ScalarValuedFunction" -> ScalarValuedFunction
| unexpected -> failwithf "Unexpected database routine type: %s." unexpected
{
Type = routineType
Schema = schema
Name = name
Definition = definition
Description = description
BaseObject = unbox x.["BaseObjectSchema"], unbox x.["BaseObjectName"]
}
)
|> Seq.toArray
member internal this.GetParameters( routine: Routine, isSqlAzure, useReturnValue) =
assert (this.State = ConnectionState.Open)
let paramDefaults = Task.Factory.StartNew( fun() ->
let parser = Microsoft.SqlServer.TransactSql.ScriptDom.TSql140Parser( true)
let tsqlReader = new StringReader(routine.Definition)
let errors = ref Unchecked.defaultof<_>
let fragment = parser.Parse(tsqlReader, errors)
let result = Dictionary()
fragment.Accept {
new Microsoft.SqlServer.TransactSql.ScriptDom.TSqlFragmentVisitor() with
member __.Visit(node : Microsoft.SqlServer.TransactSql.ScriptDom.ProcedureParameter) =
base.Visit node
result.[node.VariableName.Value] <- parseDefaultValue routine.Definition node.Value
}
result
)
let descriptionSelector =
if isSqlAzure
then
"(SELECT NULL AS Value)"
else
let routineType = if routine.IsStoredProc then "PROCEDURE" else "FUNCTION"
sprintf "fn_listextendedproperty ('MS_Description', 'schema', OBJECT_SCHEMA_NAME(object_id), '%s', OBJECT_NAME(object_id), 'PARAMETER', p.name)" routineType
let query = sprintf "
SELECT
p.name
,system_type_id AS suggested_system_type_id
,user_type_id AS suggested_user_type_id
,is_output AS suggested_is_output
,CAST( IIF(is_output = 1, 0, 1) AS BIT) AS suggested_is_input
,max_length
,precision
,scale
,description = ISNULL( XProp.Value, '')
FROM sys.parameters AS p
OUTER APPLY %s AS XProp
WHERE
p.Name <> ''
AND OBJECT_ID('%s.%s') = object_id" descriptionSelector <|| routine.BaseObject
[
use cmd = this.CreateCommand(CommandText = query)
use cursor = cmd.ExecuteReader()
while cursor.Read() do
let name = string cursor.["name"]
let direction =
if unbox cursor.["suggested_is_output"]
then
ParameterDirection.Output
else
assert(unbox cursor.["suggested_is_input"])
ParameterDirection.Input
let system_type_id : int = unbox<byte> cursor.["suggested_system_type_id"] |> int
let user_type_id = cursor.TryGetValue "suggested_user_type_id"
let typeInfo = findTypeInfoBySqlEngineTypeId(this.ConnectionString, system_type_id, user_type_id)
let defaultValue = match paramDefaults.Result.TryGetValue(name) with | true, value -> value | false, _ -> None
let valueTypeWithNullDefault = typeInfo.IsValueType && defaultValue = Some(null)
yield {
Name = name
TypeInfo = typeInfo
Direction = direction
MaxLength = cursor.["max_length"] |> unbox<int16> |> int
Precision = unbox cursor.["precision"]
Scale = unbox cursor.["scale"]
DefaultValue = defaultValue
Optional = valueTypeWithNullDefault
Description = string cursor.["description"]
}
if routine.IsStoredProc && useReturnValue
then
yield {
Name = "@RETURN_VALUE"
TypeInfo = findTypeInfoByProviderType(this.ConnectionString, SqlDbType.Int)
Direction = ParameterDirection.ReturnValue
MaxLength = 4
Precision = 10uy
Scale = 0uy
DefaultValue = None
Optional = false
Description = ""
}
]
member internal this.GetTables( schema, isSqlAzure) =
assert (this.State = ConnectionState.Open)
let descriptionSelector =
if isSqlAzure
then
"(SELECT NULL AS Value)"
else
"fn_listextendedproperty ('MS_Description', 'schema', BaseObjectSchema, 'TABLE', BaseTableName, default, default)"
let getTablesQuery = sprintf "
WITH TableSynonyms AS
(
SELECT
Name
,SCHEMA_NAME(schema_id) AS [Schema]
,TABLE_NAME AS BaseTableName
,TABLE_SCHEMA AS BaseObjectSchema
FROM sys.synonyms
JOIN INFORMATION_SCHEMA.TABLES ON
OBJECT_ID(TABLES.TABLE_SCHEMA + '.' + TABLES.TABLE_NAME) = OBJECT_ID(base_object_name)
AND TABLE_TYPE = 'BASE TABLE'
),
Tables AS
(
SELECT
TABLE_NAME AS Name
,TABLE_SCHEMA AS [Schema]
,TABLE_NAME AS BaseTableName
,TABLE_SCHEMA AS BaseObjectSchema
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
)
SELECT
*
,DESCRIPTION = XProp.Value
FROM
(
SELECT * FROM TableSynonyms
UNION ALL
SELECT * FROM Tables
) AS _
OUTER APPLY %s AS XProp
WHERE
[Schema] = '%s'" descriptionSelector schema
use cmd = this.CreateCommand(CommandText = getTablesQuery)
cmd.ExecuteQuery(fun x ->
string x.["Name"],
string x.["BaseTableName"],
string x.["BaseObjectSchema"],
x.TryGetValue( "DESCRIPTION")
) |> Seq.toList
member internal this.GetFullQualityColumnInfo commandText =
assert (this.State = ConnectionState.Open)
use cmd = this.CreateCommand(CommandText = "sys.sp_describe_first_result_set", CommandType = CommandType.StoredProcedure)
cmd.Parameters.AddWithValue("@tsql", commandText) |> ignore
cmd.ExecuteQuery(fun cursor ->
let user_type_id = cursor.TryGetValue "user_type_id"
let system_type_id = cursor.["system_type_id"] |> unbox<int>
let precisionOrdinal = cursor.GetOrdinal("precision")
let scaleOrdinal = cursor.GetOrdinal("scale")
{
Column.Name = string cursor.["name"]
TypeInfo = findTypeInfoBySqlEngineTypeId (this.ConnectionString, system_type_id, user_type_id)
Nullable = unbox cursor.["is_nullable"]
MaxLength = cursor.["max_length"] |> unbox<int16> |> int
ReadOnly = not( cursor.GetValueOrDefault("is_updateable", true))
Identity = cursor.GetValueOrDefault("is_identity_column", false)
PartOfUniqueKey = cursor.GetValueOrDefault("is_part_of_unique_key", false)
DefaultConstraint = null
Description = null
Precision = int16 (cursor.GetByte precisionOrdinal)
Scale = int16 (cursor.GetByte scaleOrdinal)
}
)
|> Seq.toList
member internal this.FallbackToSETFMONLY(commandText, commandType, parameters: Parameter list) =
assert (this.State = ConnectionState.Open)
use cmd = this.CreateCommand(CommandText = commandText, CommandType = commandType)
for p in parameters do
cmd.Parameters.Add(p.Name, p.TypeInfo.SqlDbType) |> ignore
use reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)
match reader.GetSchemaTable() with
| null -> []
| columnSchema ->
[
for row in columnSchema.Rows do
yield {
Column.Name = unbox row.["ColumnName"]
TypeInfo =
let t = Enum.Parse(typeof<SqlDbType>, string row.["ProviderType"]) |> unbox
findTypeInfoByProviderType(this.ConnectionString, t)
Nullable = unbox row.["AllowDBNull"]
MaxLength = unbox row.["ColumnSize"]
ReadOnly = unbox row.["IsAutoIncrement"] || unbox row.["IsReadOnly"]
Identity = unbox row.["IsAutoIncrement"]
PartOfUniqueKey = false
DefaultConstraint = null
Description = null
Precision = unbox row.["NumericPrecision"]
Scale = unbox row.["NumericScale"]
}
]
member internal this.LoadDataTypesMap() =
sqlDataTypesCache.DoIfConnectionStringNotRegistered
this.ConnectionString
(fun () -> sqlDataTypesCache.GetTypesForConnectionString this.ConnectionString)
(fun () ->
assert (this.State = ConnectionState.Open)
let sqlEngineTypes, tableVariableTypes =
use cmd = this.CreateCommand(CommandText = """
select
t.name, t.system_type_id, t.user_type_id, t.is_table_type, s.name as schema_name, t.is_user_defined, t.[precision], t.scale
from
sys.types as t
join sys.schemas as s on t.schema_id = s.schema_id
select
c.name, c.system_type_id, c.user_type_id, c.is_nullable, c.max_length, c.is_identity, c.is_computed, tt.user_type_id table_type_user_type_id, c.[precision], c.scale
from sys.table_types as tt
inner join sys.columns as c on tt.type_table_object_id = c.object_id
order by
c.column_id
, tt.user_type_id
, c.user_type_id
"""
)
use reader = cmd.ExecuteReader()
[| while reader.Read() do
let system_type_id = unbox<byte> reader.["system_type_id"]
let user_type_id = unbox<int> reader.["user_type_id"]
yield
(system_type_id, user_type_id)
, { system_type_id = system_type_id
name = string reader.["name"]
user_type_id = user_type_id
is_table_type = unbox reader.["is_table_type"]
schema_name = string reader.["schema_name"]
is_user_defined = unbox reader.["is_user_defined"]
precision = int16 (reader.GetByte (reader.GetOrdinal "precision"))
scale = int16 (reader.GetByte (reader.GetOrdinal "scale")) }
|] |> dict
, [| reader.NextResult() |> ignore
while reader.Read() do
let table_type_user_type_id = unbox<int> reader.["table_type_user_type_id"]
yield
table_type_user_type_id
, {
table_type_user_type_id = table_type_user_type_id
name = string reader.["name"]
system_type_id = unbox<byte> reader.["system_type_id"]
is_nullable = unbox reader.["is_nullable"]
max_length = unbox<int16> reader.["max_length"]
is_identity = unbox reader.["is_identity"]
is_computed = unbox reader.["is_computed"]
user_type_id = unbox<int> reader.["user_type_id"]
precision = unbox<byte> reader.["precision"]
scale = unbox<byte> reader.["scale"]
}
|]
|> Array.groupBy fst
|> Array.map (fun (k,items) -> k, items |> Array.map snd)
|> dict
let getProvidedType name is_user_defined is_table_type system_type_id user_type_id =
match providerTypes.TryGetValue(name) with
| true, value -> Some value
| false, _ when is_user_defined && not is_table_type ->
let type_name = sqlEngineTypes.[system_type_id,user_type_id].name
match providerTypes.TryGetValue type_name with
| false, _ ->
match sqlEngineTypes.TryGetValue ((system_type_id,int system_type_id)) with
| false, _ -> None
| true, sqlType ->
let type_name = sqlType.name
Some providerTypes.[type_name]
| true, item -> Some item
| false, _ when is_table_type ->
Some (SqlDbType.Structured, null, false)
| _ -> failwith ("Unexpected type: " + name)
let getProvidedTypeForSqlTypeEntry (x:SqlTypeEntry) = getProvidedType x.name x.is_user_defined x.is_table_type x.system_type_id x.user_type_id
let rec makeColumn column =
let sqlTypeEntry =
{ sqlEngineTypes.[column.system_type_id, column.user_type_id] with
// important: retrieve the precision / scale from the table variable column entry itself
precision = int16 column.precision
scale = int16 column.scale }
{ Column.Name = column.name
TypeInfo = Option.get (makeTypeInfo sqlTypeEntry)
Nullable = column.is_nullable
MaxLength = int column.max_length
ReadOnly = column.is_identity || column.is_computed
Identity = column.is_identity
PartOfUniqueKey = false
DefaultConstraint = null
Description = null
Precision = sqlTypeEntry.precision
Scale = sqlTypeEntry.scale
}
and makeTypeInfo (entry: SqlTypeEntry) =
match getProvidedTypeForSqlTypeEntry entry with
| None -> None
| Some (sqldbtype, clrTypeFullName, isFixedLength) ->
let tableTypeColumns =
if entry.is_table_type then
tableVariableTypes.[entry.user_type_id] |> Array.map makeColumn
else
Array.empty
Some {
TypeName = entry.name
Schema = entry.schema_name
SqlEngineTypeId = int entry.system_type_id
UserTypeId = entry.user_type_id
SqlDbType = sqldbtype
IsFixedLength = isFixedLength
ClrTypeFullName = clrTypeFullName
UdttName = if entry.is_table_type then entry.name else ""
TableTypeColumns = tableTypeColumns
}
let typeInfosForTableTypes =
sqlEngineTypes.Values
|> Seq.choose (fun i ->
match makeTypeInfo i with
| Some typeInfo -> Some (i.user_type_id, (i, typeInfo))
| None ->
// fails when, for example, a SQLCLR type definition is on the DDL but the assembly is missing from the database.
// example: AdventureWorks2012 backup in the test folder tSQLt.Private
// ignoring such types
None
)
|> dict
let typeInfos = [|
for { name = name; system_type_id = system_type_id; user_type_id = user_type_id; is_table_type = is_table_type; schema_name = schema_name; is_user_defined = is_user_defined } in sqlEngineTypes.Values do
match getProvidedType name is_user_defined is_table_type system_type_id user_type_id with
| None -> ()
| Some (providerdbtype, clrTypeFullName, isFixedLength) ->
let columns =
if is_table_type then
let columns = tableVariableTypes.[user_type_id]
columns
|> Array.map (fun column ->
let sqlTypeInfo, typeInfo = typeInfosForTableTypes.[user_type_id]
// important: retrieve the precision / scale from the table variable column entry itself
let precision = int16 column.precision
let scale = int16 column.scale
{
Column.Name = column.name
TypeInfo = typeInfo
Nullable = column.is_nullable
MaxLength = int column.max_length
ReadOnly = column.is_identity || column.is_computed
Identity = column.is_identity
PartOfUniqueKey = false
DefaultConstraint = null
Description = null
Precision = precision
Scale = scale
})
else
Array.empty
yield
{
TypeName = name
Schema = schema_name
SqlEngineTypeId = int system_type_id
UserTypeId = user_type_id
SqlDbType = providerdbtype
IsFixedLength = isFixedLength
ClrTypeFullName = clrTypeFullName
UdttName = if is_table_type then name else ""
TableTypeColumns = columns
}
|]
sqlDataTypesCache.RegisterTypes(this.ConnectionString, typeInfos)
typeInfos
)