forked from Zaid-Ajaj/Npgsql.FSharp.Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSqlAnalysis.fs
1062 lines (923 loc) · 60.2 KB
/
SqlAnalysis.fs
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace Npgsql.FSharp.Analyzers.Core
open System
open FSharp.Compiler.Text
open F23.StringSimilarity
open NpgsqlFSharpParser
open InformationSchema
open Npgsql
open System.Data.SqlClient
module SqlAnalysis =
type ComparisonParameter = {
parameterName : string
column : string
table : string option
}
let rec trimParams = function
| Expr.Parameter(name) -> Expr.Parameter(name.TrimStart '@')
| Expr.Function(funcName, arguments) ->
let modifiedArguments = List.map trimParams arguments
Expr.Function(funcName, modifiedArguments)
| Expr.SelectQuery query ->
Expr.SelectQuery {
query with
Columns = List.map trimParams query.Columns
Where = Option.map trimParams query.Where
Limit = Option.map trimParams query.Limit
GroupBy = List.map trimParams query.GroupBy
Having = Option.map trimParams query.Having
Offset = Option.map trimParams query.Offset
}
| Expr.InsertQuery query ->
Expr.InsertQuery {
query with
Values = List.map trimParams query.Values
Returning = List.map trimParams query.Returning
ConflictResolution =
query.ConflictResolution
|> List.map (fun (column, expr) -> column, trimParams expr)
}
| Expr.DeleteQuery query ->
Expr.DeleteQuery {
query with
Where = Option.map trimParams query.Where
Returning = List.map trimParams query.Returning
}
| Expr.UpdateQuery query ->
Expr.UpdateQuery {
query with
Where = Option.map trimParams query.Where
Assignments = List.map trimParams query.Assignments
ConflictResolution = List.map trimParams query.ConflictResolution
Returning = List.map trimParams query.Returning
}
| Expr.And(left, right) -> Expr.And(trimParams left, trimParams right)
| Expr.Or(left, right) -> Expr.Or(trimParams left, trimParams right)
| Expr.Equals(left, right) -> Expr.Equals(trimParams left, trimParams right)
| Expr.Not(expr) -> Expr.Not(trimParams expr)
| Expr.In(left, right) -> Expr.In(trimParams left, trimParams right)
| Expr.LessThan(left, right) -> Expr.LessThan(trimParams left, trimParams right)
| Expr.LessThanOrEqual(left, right) -> Expr.LessThanOrEqual(trimParams left, trimParams right)
| Expr.GreaterThan(left, right) -> Expr.GreaterThan(trimParams left, trimParams right)
| Expr.GreaterThanOrEqual(left, right) -> Expr.GreaterThanOrEqual(trimParams left, trimParams right)
| expr -> expr
let parseQueryTrimmed query =
match Parser.parse query with
| Result.Error error -> Result.Error error
| Result.Ok expr -> Result.Ok (trimParams expr)
let columnParameterTable (parameterName: string) (columnName: string) =
if columnName.Contains "." then
let parts = columnName.Split '.'
{ parameterName = parameterName; column = parts.[1]; table = Some parts.[0] }
else
{ parameterName = parameterName; column = columnName; table = None }
let rec findComparisonParameters = function
| Expr.Equals(Expr.Ident columnName, Expr.Parameter parameterName) ->
[ columnParameterTable parameterName columnName ]
| Expr.Equals(Expr.Parameter parameterName, Expr.Ident columnName) ->
[ columnParameterTable parameterName columnName ]
| Expr.LessThan(Expr.Ident columnName, Expr.Parameter parameterName) ->
[ columnParameterTable parameterName columnName ]
| Expr.LessThan(Expr.Parameter parameterName, Expr.Ident columnName) ->
[ columnParameterTable parameterName columnName ]
| Expr.LessThanOrEqual(Expr.Ident columnName, Expr.Parameter parameterName) ->
[ columnParameterTable parameterName columnName ]
| Expr.LessThanOrEqual(Expr.Parameter parameterName, Expr.Ident columnName) ->
[ columnParameterTable parameterName columnName ]
| Expr.GreaterThan(Expr.Ident columnName, Expr.Parameter parameterName) ->
[ columnParameterTable parameterName columnName ]
| Expr.GreaterThan(Expr.Parameter parameterName, Expr.Ident columnName) ->
[ columnParameterTable parameterName columnName ]
| Expr.GreaterThanOrEqual(Expr.Ident columnName, Expr.Parameter parameterName) ->
[ columnParameterTable parameterName columnName ]
| Expr.GreaterThanOrEqual(Expr.Parameter parameterName, Expr.Ident columnName) ->
[ columnParameterTable parameterName columnName ]
| Expr.Not(expression) -> findComparisonParameters expression
| Expr.And(leftExpression, rightExpression) ->
[
yield! findComparisonParameters leftExpression
yield! findComparisonParameters rightExpression
]
| Expr.Or(leftExpression, rightExpression) ->
[
yield! findComparisonParameters leftExpression
yield! findComparisonParameters rightExpression
]
| expression -> [ ]
let containsNonInnerJoins (query: SelectExpr) =
query.Joins
|> List.exists (function
| JoinExpr.FullJoin _ -> true
| JoinExpr.LeftJoin _ -> true
| JoinExpr.RightJoin _ -> true
| JoinExpr.InnerJoin _ -> false
)
let determineParameterNullability (parameters: Parameter list) (schema: DbSchemaLookups) query =
match parseQueryTrimmed query with
| Result.Error error -> parameters
| Result.Ok (Expr.InsertQuery query) ->
let findParameter (expr, column) =
match expr with
| Expr.Parameter parameterName ->
Some (parameterName, column)
| _ ->
None
let insertParameters =
query.Columns
|> List.zip query.Values
|> List.choose findParameter
let columnNonNullable columnName =
schema.Columns
|> Seq.exists (fun column -> column.Value.Name = columnName && not column.Value.Nullable)
parameters
|> List.map (fun requiredParameter ->
insertParameters
|> List.tryFind (fun (parameter, column) -> parameter = requiredParameter.Name && columnNonNullable column)
|> function
| None -> requiredParameter
| Some _ -> { requiredParameter with IsNullable = false }
)
| Result.Ok (Expr.UpdateQuery query) ->
let columnNonNullable columnName (table: Option<string>) =
schema.Columns
|> Seq.exists (fun column ->
column.Value.Name = columnName
&& not column.Value.Nullable
&& table = Some column.Value.BaseTableName
)
let comparisonParameters =
match query.Where with
| None -> [ ]
| Some whereExpr ->
findComparisonParameters whereExpr
|> List.map (fun comparison ->
match comparison.table with
| None -> { comparison with table = Some query.Table }
| _ -> comparison
)
let updateAssignments =
query.Assignments
|> List.choose (function
| Expr.Equals(Expr.Ident columnName, Expr.Parameter parameter) ->
Some (parameter, columnName)
| _ ->
None
)
parameters
// determine nullability on comparison operators
|> List.map (fun requiredParameter ->
comparisonParameters
|> List.tryFind (fun comparison ->
comparison.parameterName = requiredParameter.Name
&& columnNonNullable comparison.column comparison.table)
|> function
| None -> requiredParameter
| Some _ -> { requiredParameter with IsNullable = false }
)
// determine nullability on update assignments
|> List.map (fun requiredParameter ->
updateAssignments
|> List.tryFind (fun (parameter, column) -> parameter = requiredParameter.Name && columnNonNullable column (Some query.Table))
|> function
| None -> requiredParameter
| Some _ -> { requiredParameter with IsNullable = false }
)
| Result.Ok (Expr.DeleteQuery query) ->
let columnNonNullable columnName (table: Option<string>) =
schema.Columns
|> Seq.exists (fun column ->
column.Value.Name = columnName
&& not column.Value.Nullable
&& table = Some column.Value.BaseTableName
)
let comparisonParameters =
match query.Where with
| None -> [ ]
| Some whereExpr ->
findComparisonParameters whereExpr
|> List.map (fun comparison ->
match comparison.table with
| None -> { comparison with table = Some query.Table }
| _ -> comparison
)
parameters
|> List.map (fun requiredParameter ->
comparisonParameters
|> List.tryFind (fun comparison ->
comparison.parameterName = requiredParameter.Name
&& columnNonNullable comparison.column comparison.table)
|> function
| None -> requiredParameter
| Some _ -> { requiredParameter with IsNullable = false }
)
| Result.Ok (Expr.SelectQuery query) ->
if containsNonInnerJoins query || query.From.IsNone then
// When query includes non-INNER JOINS, things get complicated
// Implement later...
parameters
else
let columnNonNullable columnName (table: Option<string>) =
schema.Columns
|> Seq.exists (fun column ->
column.Value.Name = columnName
&& not column.Value.Nullable
&& table = Some column.Value.BaseTableName
)
let comparisonParameters =
match query.Where with
| None -> [ ]
| Some whereExpr ->
findComparisonParameters whereExpr
|> List.map (fun comparison ->
match comparison.table, query.From with
| None, Some (Expr.Ident table) ->
{ comparison with table = Some table }
| None, Some (Expr.As(Expr.Ident table, alias)) ->
{ comparison with table = Some table }
| _ ->
comparison
)
parameters
|> List.map (fun requiredParameter ->
comparisonParameters
|> List.tryFind (fun comparison ->
comparison.parameterName = requiredParameter.Name
&& columnNonNullable comparison.column comparison.table)
|> function
| None -> requiredParameter
| Some _ -> { requiredParameter with IsNullable = false }
)
| Result.Ok expr ->
parameters
let missingInsertColumns (schema: DbSchemaLookups) (query:string) =
match Parser.parse query with
| Result.Error _ -> None
| Result.Ok expression ->
match expression with
| Expr.InsertQuery insertQuery ->
let missingInsertColumns =
schema.Columns.Values
|> Seq.filter (fun column -> column.BaseTableName = insertQuery.Table && not column.OptionalForInsert)
|> Seq.filter (fun column -> not (List.contains column.Name insertQuery.Columns))
if Seq.isEmpty missingInsertColumns then
None
else
let errorMessage =
missingInsertColumns
|> Seq.map (fun column -> sprintf "'%s'" column.Name)
|> Seq.toList
|> function
| [ ] -> "<empty>"
| [ first ] -> first
| [ first; second ] -> sprintf "%s and %s" first second
| columns ->
let lastColumn = List.last columns
let firstColumns =
columns
|> List.rev
|> List.skip 1
|> List.rev
|> String.concat ", "
sprintf "%s and %s" firstColumns lastColumn
|> fun columns -> sprintf "INSERT query is missing required columns %s when adding rows to the '%s' table" columns insertQuery.Table
Some errorMessage
| _ ->
None
// When casting columns during a select statement, if the original column is non-nullable
// then the computed column somehow becomes nullable when we ask the database for its type
// for example
// user_id int not null -> inferred non-nullable
// user_id::text -> inferred nullable
//
// This function resolves the correct nullability and infers non-nullable columns to be returned as such
let resolveColumnNullability (commandText: string) (schema: DbSchemaLookups) (columns: Column list) =
match Parser.parse commandText with
| Result.Ok (Expr.SelectQuery selectQuery) ->
let schemaColumns =
schema.Columns
|> Seq.map (fun pair -> pair.Value)
|> Seq.toList
columns
|> List.map (fun column ->
let originalColumnNameAndAlias =
selectQuery.Columns
|> List.tryPick (function
// find columns expressions of the shape
// {originalName}::{typeName} AS {alias}
| Expr.As(Expr.TypeCast(Expr.Ident originalName, Expr.Ident typeName), Expr.Ident alias) ->
Some (originalName, alias, false)
// 1::{typeName} AS {alias}
| Expr.As(Expr.TypeCast(Expr.Integer _, Expr.Ident typeName), Expr.Ident alias) ->
Some (column.Name, column.Name, true)
// {originalName}::{typeName}
| Expr.TypeCast(Expr.Ident originalName, Expr.Ident typeName) ->
Some (originalName, originalName, false)
// 1::{typeName}
| Expr.TypeCast(Expr.Integer _, Expr.Ident typeName) ->
Some (column.Name, column.Name, true)
// "text"::{typeName}
| Expr.TypeCast(Expr.StringLiteral _, Expr.Ident typeName) ->
Some (column.Name, column.Name, true)
// 1 as {alias}
| Expr.As(Expr.Integer _, Expr.Ident alias) ->
Some (alias, alias, true)
// "text" AS alias
| Expr.As(Expr.StringLiteral _, Expr.Ident alias) ->
Some (alias, alias, true)
| _ ->
None
)
match originalColumnNameAndAlias with
| None -> column
| Some (name, alias, isConst) when isConst-> { column with Nullable = false }
| Some (name, alias, isConst) ->
schemaColumns
|> List.tryFind (fun columnSchema -> columnSchema.Name = name && alias = column.Name)
|> function
| None -> column
| Some columnSchema ->
{ column with
Nullable = columnSchema.Nullable
DefaultConstraint = columnSchema.DefaultConstraint
PartOfPrimaryKey = columnSchema.PartOfPrimaryKey
BaseTableName = column.BaseTableName
BaseSchemaName = column.BaseSchemaName }
)
| _ ->
columns
let extractParametersAndOutputColumns(connectionString, commandText, dbSchemaLookups) =
try
let parameters, output, enums = InformationSchema.extractParametersAndOutputColumns(connectionString, commandText, false, dbSchemaLookups)
let parametersWithNullability = determineParameterNullability parameters dbSchemaLookups commandText
let potentiallyMissingColumns = missingInsertColumns dbSchemaLookups commandText
let rewrittenColumns = resolveColumnNullability commandText dbSchemaLookups output
Result.Ok (parametersWithNullability, rewrittenColumns, potentiallyMissingColumns)
with
| :? PostgresException as databaseError ->
// errors such as syntax errors are reported here
Result.Error (sprintf "%s: %s" databaseError.Severity databaseError.MessageText)
| :? SqlException as sqlexcpetion ->
Result.Error (sprintf "%s" (sqlexcpetion.Message.Replace("The batch could not be analyzed because of compile errors.",String.Empty)))
| error ->
// any other generic error
Result.Error (sprintf "%s\n%s" error.Message error.StackTrace)
let createWarning (message: string) (range: range) : Message =
{ Message = message;
Type = "SQL Analysis";
Code = "SQL0001";
Severity = Warning;
Range = range;
Fixes = [ ] }
let findQuery (operation: SqlOperation) =
operation.blocks
|> List.tryFind (function | SqlAnalyzerBlock.Query(query, range) -> true | _ -> false)
|> Option.map(function | SqlAnalyzerBlock.Query(query, range) -> (query, range) | _ -> failwith "should not happen")
let findTransactions (operation: SqlOperation) =
operation.blocks
|> List.tryFind (function | SqlAnalyzerBlock.Transaction queries -> true | _ -> false)
|> Option.map(function
| SqlAnalyzerBlock.Transaction queries -> queries
| _ -> failwith "should not happen"
)
let findParameters (operation: SqlOperation) =
operation.blocks
|> List.tryFind (function | SqlAnalyzerBlock.Parameters(parameters, range) -> true | _ -> false)
|> Option.map(function | SqlAnalyzerBlock.Parameters(parameters, range) -> (parameters, range) | _ -> failwith "should not happen")
let findColumnReadAttempts (operation: SqlOperation) =
operation.blocks
|> List.tryFind (function | SqlAnalyzerBlock.ReadingColumns(attempts) -> true | _ -> false)
|> Option.map(function | SqlAnalyzerBlock.ReadingColumns(attempts) -> attempts | _ -> failwith "should not happen")
let analyzeParameters parameters parametersRange (requiredParameters: InformationSchema.Parameter list) =
match parameters with
| None ->
if not (List.isEmpty requiredParameters) then
let missingParameters =
requiredParameters
|> List.map (fun p -> sprintf "%s:%s" p.Name p.DataType.Name)
|> String.concat ", "
|> sprintf "Missing parameters [%s]. Please use Sql.parameters to provide them."
[ createWarning missingParameters parametersRange ]
else
[ ]
| Some (queryParams, queryParamsRange) ->
if List.isEmpty requiredParameters then
[ createWarning "Provided parameters are redundant. Sql query is not parameterized" parametersRange ]
else
/// None of the required parameters have the name of this provided parameter
let isUnknown (parameter: UsedParameter) =
requiredParameters
|> List.forall (fun requiredParam -> parameter.name <> requiredParam.Name)
let isRedundant (parameter: UsedParameter) =
// every required parameter has a corresponding provided query parameter
let requirementSatisfied =
requiredParameters
|> List.forall (fun requiredParam -> queryParams |> List.exists (fun queryParam -> queryParam.name = requiredParam.Name))
requirementSatisfied && isUnknown parameter
[
for requiredParameter in requiredParameters do
if not (queryParams |> List.exists (fun providedParam -> providedParam.name = requiredParameter.Name))
then
let message = sprintf "Missing parameter '%s' of type %s" requiredParameter.Name requiredParameter.DataType.Name
yield createWarning message queryParamsRange
for providedParam in queryParams do
if isRedundant providedParam then
yield createWarning (sprintf "Provided parameter '%s' is redundant. The query does not require such parameter" providedParam.name) providedParam.range
else if isUnknown providedParam then
// parameters that haven't been provided yet
let remainingParameters =
requiredParameters
|> List.filter (fun requiredParam -> not (queryParams |> List.exists (fun queryParam -> queryParam.name = requiredParam.Name)))
let levenshtein = new NormalizedLevenshtein()
let closestAlternative =
remainingParameters
|> List.minBy (fun parameter -> levenshtein.Distance(parameter.Name, providedParam.name))
|> fun parameter -> parameter.Name
let expectedParameters =
remainingParameters
|> List.map (fun p -> sprintf "%s:%s" p.Name p.DataType.Name)
|> String.concat ", "
|> sprintf "Required parameters are [%s]."
let codeFixes =
remainingParameters
|> List.map (fun p ->
{ FromRange = providedParam.range
FromText = providedParam.name
ToText = sprintf "\"%s\"" p.Name })
let warning =
if String.IsNullOrWhiteSpace(providedParam.name)
then createWarning (sprintf "Empty parameter name was provided. Please provide one of %s" expectedParameters) providedParam.range
else if List.length codeFixes = 1
then createWarning (sprintf "Unexpected parameter '%s' is provided. Did you mean '%s'?" providedParam.name closestAlternative) providedParam.range
else createWarning (sprintf "Unexpected parameter '%s' is provided. Did you mean '%s'? %s" providedParam.name closestAlternative expectedParameters) providedParam.range
yield { warning with Fixes = codeFixes }
else
// do parameter type-checking
let matchingColumnType = requiredParameters |> List.tryFind(fun p -> p.Name = providedParam.name)
match matchingColumnType with
| None -> ()
| Some requiredParam ->
let typeMismatch (shouldUse: string list) =
let formattedSuggestions =
match shouldUse with
| [ ] -> "<empty>"
| [ first ] -> first
| [ first; second ] -> sprintf "%s or %s" first second
| _ ->
let lastSuggestion = List.last shouldUse
let firstSuggestions =
shouldUse
|> List.rev
|> List.skip 1
|> List.rev
|> String.concat ", "
sprintf "%s or %s" firstSuggestions lastSuggestion
let warning =
sprintf "Attempting to provide parameter '%s' of type '%s' using function %s. Please use %s instead."
providedParam.name
requiredParam.DataType.Name
providedParam.paramFunc
formattedSuggestions
let codeFixs : Fix list =
shouldUse
|> List.map (fun func ->
{
FromText = providedParam.name
FromRange = providedParam.paramFuncRange
ToText = func
})
{ createWarning warning providedParam.paramFuncRange with Fixes = codeFixs }
if providedParam.paramFunc = "Sql.parameter" then
// do not do anything when the input is a generic param
()
else if not requiredParam.DataType.IsArray then
match requiredParam.DataType.Name with
| ("bool" | "boolean" | "bit") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.bool" && providedParam.paramFunc <> "Sql.boolOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.bool"; "Sql.boolOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.bool"
then yield typeMismatch [ "Sql.bool" ]
| ("int" | "int32" | "integer" | "serial") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.int" && providedParam.paramFunc <> "Sql.intOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.int"; "Sql.intOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.int"
then yield typeMismatch [ "Sql.int" ]
| ("smallint" | "int16") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.int16" && providedParam.paramFunc <> "Sql.int16OrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.int16"; "Sql.int16OrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.int16"
then yield typeMismatch [ "Sql.int16" ]
| ("int64" | "bigint" |"bigserial") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.int64" && providedParam.paramFunc <> "Sql.int" && providedParam.paramFunc <> "Sql.int64OrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.int64"; "Sql.int"; "Sql.int64OrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.int64" && providedParam.paramFunc <> "Sql.int"
then yield typeMismatch [ "Sql.int64"; "Sql.int" ]
| ("numeric" | "decimal" | "money") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.decimal" && providedParam.paramFunc <> "Sql.decimalOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.decimal"; "Sql.decimalOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.decimal"
then yield typeMismatch [ "Sql.decimal" ]
| "double precision" ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.double" && providedParam.paramFunc <> "Sql.doubleOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.double"; "Sql.doubleOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.double"
then yield typeMismatch [ "Sql.double" ]
| "bytea" ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.bytea" && providedParam.paramFunc <> "Sql.byteaOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.bytea"; "Sql.byteaOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.bytea"
then yield typeMismatch [ "Sql.bytea" ]
| "uuid" ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.uuid" && providedParam.paramFunc <> "Sql.uuidOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.uuid"; "Sql.uuidOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.uuid"
then yield typeMismatch [ "Sql.uuid" ]
| "date" ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.date" && providedParam.paramFunc <> "Sql.dateOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.date"; "Sql.dateOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.date"
then yield typeMismatch [ "Sql.date" ]
| ("timestamp"|"timestamp without time zone") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.timestamp" && providedParam.paramFunc <> "Sql.timestampOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.timestamp"; "Sql.timestampOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.timestamp"
then yield typeMismatch [ "Sql.timestamp" ]
| ("timestamptz" | "timestamp with time zone") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.timestamptz" && providedParam.paramFunc <> "Sql.timestamptzOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.timestamptz"; "Sql.timestamptzOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.timestamptz"
then yield typeMismatch [ "Sql.timestamptz" ]
| "jsonb" ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.jsonb" && providedParam.paramFunc <> "Sql.jsonbOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.jsonb"; "Sql.jsonbOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.jsonb"
then yield typeMismatch [ "Sql.jsonb" ]
| ("text"|"json"|"xml"|"nvarchar"|"varchar") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.text" && providedParam.paramFunc <> "Sql.textOrNone" && providedParam.paramFunc <> "Sql.string" && providedParam.paramFunc <> "Sql.textOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.text"; "Sql.string"; "Sql.textOrNone"; "Sql.stringOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.text" && providedParam.paramFunc <> "Sql.string"
then yield typeMismatch [ "Sql.text"; "Sql.string" ]
| _ ->
()
else
// data type is array
match requiredParam.DataType.Name.Replace("[]", "") with
| ("int" | "int32" | "integer" | "serial" | "int64" | "bigint" |"bigserial") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.intArray" && providedParam.paramFunc <> "Sql.intArrayOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.intArray"; "Sql.intArrayOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.intArray"
then yield typeMismatch [ "Sql.intArray" ]
| "uuid" ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.uuidArray" && providedParam.paramFunc <> "Sql.uuidArrayOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.uuidArray"; "Sql.uuidArrayOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.uuidArray"
then yield typeMismatch [ "Sql.uuidArray" ]
| ("text"|"json"|"xml") ->
if requiredParam.IsNullable then
if providedParam.paramFunc <> "Sql.stringArray" && providedParam.paramFunc <> "Sql.stringArrayOrNone" && providedParam.paramFunc <> "Sql.dbnull"
then yield typeMismatch [ "Sql.stringArray"; "Sql.stringArrayOrNone"; "Sql.dbnull" ]
else
if providedParam.paramFunc <> "Sql.stringArray"
then yield typeMismatch [ "Sql.stringArray" ]
| _ ->
()
]
let findColumn (name: string) (availableColumns: InformationSchema.Column list) =
availableColumns
|> List.tryFind (fun column -> column.Name = name)
let formatColumns (availableColumns: InformationSchema.Column list) =
availableColumns
|> List.map (fun column ->
if column.DataType.IsArray
then sprintf "| -- %s of type %s[]" column.Name column.DataType.Name
else sprintf "| -- %s of type %s" column.Name column.DataType.Name
)
|> String.concat "\n"
let analyzeColumnReadingAttempts (columnReadAttempts: ColumnReadAttempt list) (availableColumns: InformationSchema.Column list) =
[
for attempt in columnReadAttempts do
match findColumn attempt.columnName availableColumns with
| None ->
if List.isEmpty availableColumns then
let warningMsg = sprintf "Attempting to read column named '%s' from a result set which doesn't return any columns. In case you are executing DELETE, INSERT or UPDATE queries, you might want to use Sql.executeNonQuery or Sql.executeNonQueryAsync to obtain the number of affected rows. You can also add a RETURNING clause in your query to make it return the rows which are updated, inserted or deleted from that query." attempt.columnName
yield createWarning warningMsg attempt.columnNameRange
else
let levenshtein = new NormalizedLevenshtein()
let closestAlternative =
availableColumns
|> List.minBy (fun column -> levenshtein.Distance(attempt.columnName, column.Name))
|> fun column -> column.Name
let warningMsg =
if String.IsNullOrWhiteSpace attempt.columnName then
sprintf "Attempting to read a column without specifying a name. Available columns returned from the result set are:\n%s" (formatColumns availableColumns)
else
sprintf "Attempting to read column named '%s' that was not returned by the result set. Did you mean to read '%s'?\nAvailable columns from the result set are:\n%s" attempt.columnName closestAlternative (formatColumns availableColumns)
let warning = createWarning warningMsg attempt.columnNameRange
let codeFixes =
availableColumns
|> List.map (fun column ->
{ FromRange = attempt.columnNameRange
FromText = attempt.columnName
ToText = sprintf "\"%s\"" column.Name })
yield { warning with Fixes = codeFixes }
| Some column ->
let typeMismatchMessage (shouldUse: string list) =
let formattedFunctions =
match shouldUse with
| [ ] -> "<empty>"
| [ first ] -> first
| [ first; second ] -> sprintf "%s or %s" first second
| _ ->
let lastSuggestion = List.last shouldUse
let firstSuggestions =
shouldUse
|> List.rev
|> List.skip 1
|> List.rev
|> String.concat ", "
sprintf "%s or %s" firstSuggestions lastSuggestion
String.concat String.Empty [
sprintf "Type mismatch: attempting to read column '%s' of " column.Name
if column.Nullable then "nullable " else "non-nullable "
if column.DataType.IsArray then "array " else ""
sprintf "type '%s' using %s. " column.DataType.Name attempt.funcName
sprintf "Please use %s instead" formattedFunctions
]
let typeMismatch (shouldUse: string list) =
let warningMessage = typeMismatchMessage shouldUse
let fixes =
shouldUse
|> List.map (fun func -> {
FromRange = attempt.funcCallRange
FromText = attempt.funcName
ToText = func
})
{ createWarning warningMessage attempt.funcCallRange with Fixes = fixes }
if attempt.funcName.StartsWith "Sql." then
match column.DataType.Name with
| ("bit"|"bool"|"boolean") when attempt.funcName <> "Sql.readBool" ->
yield typeMismatch [ "Sql.readBool" ]
| ("text"|"json"|"xml"|"jsonb") when attempt.funcName <> "Sql.readString" ->
yield typeMismatch [ "Sql.readString" ]
| ("character varying"|"character"|"char"|"nvarchar"|"varchar"|"citext") when attempt.funcName <> "Sql.readString" ->
yield typeMismatch [ "Sql.readString" ]
| ("int" | "int2" | "int4" | "smallint" | "integer") when attempt.funcName <> "Sql.readInt" ->
yield typeMismatch [ "Sql.readInt" ]
| ("int8" | "bigint") when attempt.funcName <> "Sql.readLong" ->
yield typeMismatch [ "Sql.readLong" ]
| ("real" | "float4" | "double precision" | "float8") when attempt.funcName <> "Sql.readNumber" ->
yield typeMismatch ["Sql.readNumber"]
| ("numeric" | "decimal" | "money") when attempt.funcName <> "Sql.readDecimal" || attempt.funcName <> "Sql.readMoney" ->
yield typeMismatch ["Sql.readDecimal"]
| "bytea" when attempt.funcName <> "Sql.readBytea" ->
yield typeMismatch ["Sql.readBytea"]
| "uuid" when attempt.funcName <> "Sql.readUuid" ->
yield typeMismatch [ "Sql.readUuid" ]
| "date" when attempt.funcName <> "Sql.readDate" ->
yield typeMismatch [ "Sql.readDate" ]
| ("timestamp"|"timestamp without time zone") when attempt.funcName <> "Sql.readTimestamp" ->
yield typeMismatch [ "Sql.readTimestamp" ]
| ("timestamptz"|"timestamp with time zone") when attempt.funcName <> "Sql.readTimestampTz" ->
yield typeMismatch [ "Sql.readTimestampTz" ]
| ("interval" | "time without time zone" | "time") when attempt.funcName <> "Sql.readTime" ->
yield typeMismatch [ "Sql.readTime" ]
| _ ->
()
else
let replace (name: string) =
match attempt.funcName.Split '.' with
| [| reader; funcName |] -> sprintf "%s.%s" reader name
| _ -> attempt.funcName
let using (name: string) = attempt.funcName.EndsWith (sprintf ".%s" name)
let notUsing = using >> not
if not column.DataType.IsArray then
match column.DataType.Name with
("bit"|"bool"|"boolean") ->
if column.Nullable && notUsing "boolOrNone"
then yield typeMismatch [ replace "boolOrNone" ]
else if notUsing "boolOrNone" && notUsing "bool"
then
if column.Nullable
then yield typeMismatch [ replace "boolOrNone" ]
else yield typeMismatch [ replace "bool" ]
else ()
| ("int8" | "tinyint") ->
if column.Nullable && List.forall notUsing [ "int8OrNone"; "int16OrNone"; "intOrNone"; "int64OrNone" ]
then yield typeMismatch [ replace "int8OrNone"; replace "int16OrNone"; replace "intOrNone"; replace "int64OrNone" ]
else if List.forall notUsing [ "int8OrNone"; "int16OrNone"; "intOrNone"; "int64OrNone"; "int8"; "int16"; "int"; "int64" ]
then
if column.Nullable
then yield typeMismatch [ replace "int8OrNone"; replace "int16OrNone"; replace "intOrNone"; replace "int64OrNone" ]
else yield typeMismatch [ replace "int8"; replace "int16"; replace "int"; replace "int64" ]
| ("int16"| "smallint") ->
if column.Nullable && List.forall notUsing [ "int16OrNone"; "intOrNone"; "int64OrNone" ]
then yield typeMismatch [ replace "int16OrNone"; replace "intOrNone"; replace "int64OrNone" ]
else if List.forall notUsing [ "int16OrNone"; "intOrNone"; "int64OrNone"; "int16"; "int"; "int64" ]
then
if column.Nullable
then yield typeMismatch [ replace "int16OrNone"; replace "intOrNone"; replace "int64OrNone" ]
else yield typeMismatch [ replace "int16"; replace "int"; replace "int64" ]
| ("int"|"integer"|"int32"|"serial"|"int4"|"int2") ->
if column.Nullable && List.forall notUsing [ "intOrNone"; "int64OrNone" ]
then yield typeMismatch [ replace "intOrNone"; replace "int64OrNone" ]
else if List.forall notUsing [ "intOrNone"; "int64OrNone"; "int"; "int64" ]
then
if column.Nullable
then yield typeMismatch [ replace "intOrNone"; replace "int64OrNone" ]
else yield typeMismatch [ replace "int"; replace "int64" ]
| ("int64"|"bigint"|"bigserial") ->
if column.Nullable && notUsing "int64OrNone"
then yield typeMismatch [ replace "int64OrNone" ]
else if notUsing "int64OrNone" && notUsing "int64"
then
if column.Nullable
then yield typeMismatch [ replace "int64OrNone" ]
else yield typeMismatch [ replace "int64" ]
else ()
| ("numeric"|"decimal"|"money") ->
if column.Nullable && notUsing "decimalOrNone"
then yield typeMismatch [ replace "decimalOrNone" ]
else if notUsing "decimalOrNone" && notUsing "decimal"
then
if column.Nullable
then yield typeMismatch [ replace "decimalOrNone" ]
else yield typeMismatch [ replace "decimal" ]
else ()
| "double precision" ->
if column.Nullable && notUsing "doubleOrNone"
then yield typeMismatch [ replace "doubleOrNone" ]
else if notUsing "doubleOrNone" && notUsing "double"
then
if column.Nullable
then yield typeMismatch [ replace "doubleOrNone" ]
else yield typeMismatch [ replace "double" ]
else ()
| ("text"|"json"|"xml"|"jsonb"|"varchar"|"nvarchar") ->
if column.Nullable && notUsing "textOrNone" && notUsing "stringOrNone"
then yield typeMismatch [ replace "textOrNone"; replace "stringOrNone" ]
else if notUsing "textOrNone" && notUsing "text" && notUsing "string" && notUsing "stringOrNone"
then
if column.Nullable
then yield typeMismatch [ replace "textOrNone"; replace "stringOrText" ]
else yield typeMismatch [ replace "text"; replace "string" ]
else ()
| "date" ->
if column.Nullable && notUsing "dateOrNone"
then yield typeMismatch [ replace "dateOrNone" ]
else if notUsing "dateOrNone" && notUsing "date"
then
if column.Nullable
then yield typeMismatch [ replace "dateOrNone" ]
else yield typeMismatch [ replace "date" ]
else ()
| ("timestamp"|"timestamp without time zone") ->
if column.Nullable && notUsing "timestampOrNone" && notUsing "dateTimeOrNone"
then yield typeMismatch [ replace "dateTimeOrNone"; replace "timestampOrNone" ]
else if notUsing "timestampOrNone" && notUsing "timestamp" && notUsing "dateTimeOrNone" && notUsing "dateTime"
then
if column.Nullable
then yield typeMismatch [ replace "dateTimeOrNone"; replace "timestampOrNone" ]
else yield typeMismatch [ replace "dateTime"; replace "timestamp" ]
else ()
| ("timestamptz"|"timestamp with time zone") ->
if column.Nullable && notUsing "timestamptzOrNone" && notUsing "dateTimeOrNone" && notUsing "datetimeOffsetOrNone"
then yield typeMismatch [ replace "datetimeOffsetOrNone"; replace "dateTimeOrNone"; replace "timestamptzOrNone" ]
//else if not column.Nullable && (using "timestamptzOrNone" || using "dateTimeOrNone")
//then yield typeMismatch [ replace "dateTime"; replace "timestamptz" ]
else if notUsing "timestamptzOrNone" && notUsing "timestamptz" && notUsing "dateTimeOrNone" && notUsing "dateTime" && notUsing "datetimeOffsetOrNone" && notUsing "datetimeOffset"
then
if column.Nullable
then yield typeMismatch [ replace "datetimeOffsetOrNone"; replace "dateTimeOrNone"; replace "timestamptzOrNone" ]
else yield typeMismatch [ replace "datetimeOffset"; replace "dateTime"; replace "timestamptz" ]
else ()
| "bytea" ->
if column.Nullable && notUsing "byteaOrNone"
then yield typeMismatch [ replace "byteaOrNone" ]
else if notUsing "byteaOrNone" && notUsing "bytea"
then
if column.Nullable
then yield typeMismatch [ replace "byteaOrNone" ]
else yield typeMismatch [ replace "bytea" ]
else ()
| _ ->
()
else
// type is an array
match column.DataType.Name with
| ("text"|"json"|"xml"|"jsonb"|"varchar") ->
if column.Nullable && notUsing "stringArrayOrNone"
then yield typeMismatch [ replace "stringArrayOrNone" ]
elif notUsing "stringArrayOrNone" && notUsing "stringArray"
then
if column.Nullable
then yield typeMismatch [ replace "stringArrayOrNone" ]
else yield typeMismatch [ replace "stringArray" ]
| "uuid" ->
if column.Nullable && notUsing "uuidArrayOrNone"
then yield typeMismatch [ replace "uuidArrayOrNone" ]
elif notUsing "uuidArrayOrNone" && notUsing "uuidArray"
then
if column.Nullable
then yield typeMismatch [ replace "uuidArrayOrNone" ]
else yield typeMismatch [ replace "uuidArray" ]
| ("int"|"integer"|"int32"|"serial"|"int16"|"int8"|"int4"|"int2") ->
if column.Nullable && notUsing "intArrayOrNone"
then yield typeMismatch [ replace "intArrayOrNone" ]
elif notUsing "intArrayOrNone" && notUsing "intArray"
then
if column.Nullable
then yield typeMismatch [ replace "intArrayOrNone" ]
else yield typeMismatch [ replace "intArray" ]
| _ ->
()
]
/// Tries to read the database schema from the connection string
let databaseSchema connectionString =
try Result.Ok (InformationSchema.getDbSchemaLookups(connectionString))
with | ex -> Result.Error ex.Message
/// Uses database schema that is retrieved once during initialization
/// and re-used when analyzing the rest of the Sql operation blocks