forked from fsprojects/Rezoom.SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSystem.fs
More file actions
277 lines (272 loc) · 10.7 KB
/
TypeSystem.fs
File metadata and controls
277 lines (272 loc) · 10.7 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
namespace Rezoom.SQL.Compiler
open System
open System.Data
open System.Collections.Generic
open Rezoom.SQL.Mapping
type CoreColumnType =
| BooleanType
| GuidType
| StringType
| IntegerType of IntegerSize
| FloatType of FloatSize
| DecimalType
| BinaryType
| DateTimeType
| DateTimeOffsetType
| DateTimeishTypeClass
| StringishTypeClass
| NumericTypeClass
| IntegralTypeClass
| FractionalTypeClass
| ScalarTypeClass
| AnyTypeClass
| ListType of CoreColumnType
| RawSQLType
member this.ParentType =
match this with
| IntegerType Integer16 -> IntegralTypeClass
| IntegerType Integer32 -> IntegerType Integer16
| IntegerType Integer64 -> IntegerType Integer32
| FloatType Float32 -> FractionalTypeClass
| FloatType Float64 -> FloatType Float32
| DecimalType -> FractionalTypeClass
| StringType
| BinaryType -> StringishTypeClass
| IntegralTypeClass
| FractionalTypeClass -> NumericTypeClass
| DateTimeType
| DateTimeOffsetType -> DateTimeishTypeClass
| BooleanType
| GuidType
| DateTimeishTypeClass
| NumericTypeClass
| StringishTypeClass -> ScalarTypeClass
| ScalarTypeClass
| RawSQLType
| AnyTypeClass -> AnyTypeClass
| ListType element ->
let elementParent = element.ParentType
if elementParent = element then AnyTypeClass
else ListType elementParent
member this.HasAncestor(candidate) =
if this = candidate then true else
let parent = this.ParentType
if parent = this then false
else parent.HasAncestor(candidate)
member left.Unify(right) =
if left.HasAncestor(right) then
Ok left
elif right.HasAncestor(left) then
Ok right
else
Error <| Error.cannotUnify left right
override this.ToString() =
match this with
| BooleanType -> "BOOL"
| GuidType -> "GUID"
| StringType -> "STRING"
| IntegerType Integer16 -> "INT16"
| IntegerType Integer32 -> "INT"
| IntegerType Integer64 -> "INT64"
| FloatType Float32 -> "FLOAT32"
| FloatType Float64 -> "FLOAT64"
| DecimalType -> "DECIMAL"
| BinaryType -> "BINARY"
| DateTimeType -> "DATETIME"
| DateTimeOffsetType -> "DATETIMEOFFSET"
| DateTimeishTypeClass -> "<datetimeish>"
| FractionalTypeClass -> "<fractional>"
| IntegralTypeClass -> "<integral>"
| NumericTypeClass -> "<numeric>"
| StringishTypeClass -> "<stringish>"
| ScalarTypeClass -> "<scalar>"
| AnyTypeClass -> "<any>"
| RawSQLType -> "<rawsql>"
| ListType t -> "[" + string t + "]"
member this.ApproximateTypeName() =
match this with
| BooleanType -> BooleanTypeName
| GuidType -> GuidTypeName
| StringishTypeClass
| ScalarTypeClass
| AnyTypeClass
| ListType _
| RawSQLType
| StringType -> StringTypeName(None)
| IntegerType Integer16 -> IntegerTypeName Integer16
| IntegerType Integer32 -> IntegerTypeName Integer32
| IntegralTypeClass
| IntegerType Integer64 -> IntegerTypeName Integer64
| FloatType Float32 -> FloatTypeName Float32
| FloatType Float64 -> FloatTypeName Float64
| FractionalTypeClass
| NumericTypeClass
| DecimalType -> DecimalTypeName
| BinaryType -> BinaryTypeName(None)
| DateTimeishTypeClass
| DateTimeType -> DateTimeTypeName
| DateTimeOffsetType -> DateTimeOffsetTypeName
static member OfTypeName(typeName : TypeName) =
match typeName with
| GuidTypeName -> GuidType
| StringTypeName _ -> StringType
| BinaryTypeName _ -> BinaryType
| IntegerTypeName sz -> IntegerType sz
| FloatTypeName sz -> FloatType sz
| DecimalTypeName -> DecimalType
| BooleanTypeName -> BooleanType
| DateTimeTypeName -> DateTimeType
| DateTimeOffsetTypeName -> DateTimeOffsetType
type ColumnType =
{ Type : CoreColumnType
Nullable : bool
}
static member OfTypeName(typeName : TypeName, nullable) =
{ Type = CoreColumnType.OfTypeName(typeName)
Nullable = nullable
}
member private ty.TypeInfo(useOptional) =
let nullify (clrType : Type) =
if ty.Nullable then
if useOptional then typedefof<_ option>.MakeGenericType(clrType)
elif clrType.IsValueType then typedefof<_ Nullable>.MakeGenericType(clrType)
else clrType
else
clrType
match ty.Type with
| IntegerType Integer16 -> DbType.Int16, nullify typeof<int16>
| IntegralTypeClass
| IntegerType Integer32 -> DbType.Int32, nullify typeof<int32>
| IntegerType Integer64 -> DbType.Int64, nullify typeof<int64>
| FloatType Float32 -> DbType.Single, nullify typeof<float32>
| FloatType Float64 -> DbType.Double, nullify typeof<double>
| BooleanType -> DbType.Boolean, nullify typeof<bool>
| FractionalTypeClass
| NumericTypeClass
| DecimalType -> DbType.Decimal, nullify typeof<decimal>
| DateTimeType -> DbType.DateTime, nullify typeof<DateTime>
| DateTimeishTypeClass
| DateTimeOffsetType -> DbType.DateTimeOffset, nullify typeof<DateTimeOffset>
| GuidType -> DbType.Guid, nullify typeof<Guid>
| StringType -> DbType.String, nullify typeof<string>
| BinaryType -> DbType.Binary, nullify typeof<byte array>
| StringishTypeClass
| ScalarTypeClass
| AnyTypeClass -> DbType.Object, nullify typeof<obj>
| ListType t ->
let dbType, clrType = { Type = t; Nullable = ty.Nullable }.TypeInfo(useOptional)
dbType, clrType.MakeArrayType()
| RawSQLType ->
// DbType part is not really used here
Unchecked.defaultof<DbType>, typeof<CommandFragment array>
member ty.CLRType(useOptional) = snd <| ty.TypeInfo(useOptional)
member ty.DbType = fst <| ty.TypeInfo(false)
override ty.ToString() =
string ty.Type + (if ty.Nullable then "?" else "")
type FunctionTermType =
{ TypeConstraint : CoreColumnType
TypeVariable : Name option
ForceNullable : bool
InfectNullable : bool
VarArg : FunctionTermVarArg option
}
override this.ToString() =
[ yield
match this.TypeVariable with
| None -> sprintf "%O" this.TypeConstraint
| Some name ->
match this.TypeConstraint with
| AnyTypeClass
| ScalarTypeClass -> sprintf "%O" name
| constr -> sprintf "%O %O" constr name
if this.ForceNullable then
yield "?"
if this.InfectNullable then
yield "^"
match this.VarArg with
| None -> ()
| Some varArg ->
let maxArgs = string (defaultArg (Option.map string varArg.MaxArgCount) "*")
yield "{" + string varArg.MinArgCount + ".." + maxArgs + "}"
] |> String.concat ""
and FunctionTermVarArg =
{ MinArgCount : int
MaxArgCount : int option
}
type AggregateType =
{ AllowWildcard : bool
AllowDistinct : bool
}
[<AbstractClass>]
type FunctionType
( name : Name
, parameters : FunctionTermType IReadOnlyList
, returns : FunctionTermType
, idem : bool
) =
do
let numVarArgs = parameters |> Seq.filter (fun p -> Option.isSome p.VarArg) |> Seq.truncate 2 |> Seq.length
if numVarArgs > 1 then bug <| sprintf "Can't have more than one vararg to a function (%O)" name
member __.FunctionName = name
member __.Parameters = parameters
member __.Returns = returns
member __.Idempotent = idem
/// Whether this function (of one argument) is erased when translated, i.e. `f(x)` becomes just `x`.
abstract member Erased : bool
default __.Erased = false
abstract member Aggregate : FunctionArguments<'t, 'e> -> AggregateType option
member __.TypeSignature =
"(" + (parameters |> Seq.map string |> String.concat ", ") + ") -> " + string returns
member __.MinimumParameters =
parameters |> Seq.sumBy (fun p -> match p.VarArg with | None -> 1 | Some v -> v.MinArgCount)
member internal this.ValidateArgs
( source : SourceInfo
, argList : 'a IReadOnlyList
, argSource : 'a -> SourceInfo
, validate : 'a -> FunctionTermType -> unit
) =
let mutable i = 0
for par in parameters do
match par.VarArg with
| None ->
if i >= argList.Count then
failAt source <| Error.insufficientArguments name argList.Count this.MinimumParameters
validate (argList.[i]) par
i <- i + 1
| Some varg ->
let start = i
// we can consume arguments until we get to this index
let indexOfLastVarArg = argList.Count - (parameters.Count - i)
let indexOfLastVarArg =
match varg.MaxArgCount with
| None -> indexOfLastVarArg
| Some maxCount -> min indexOfLastVarArg (i + maxCount - 1)
while i <= indexOfLastVarArg do
validate (argList.[i]) par
i <- i + 1
if i - start < varg.MinArgCount then
failAt source <| Error.insufficientArguments name i this.MinimumParameters
if i < argList.Count then
failAt (argSource argList.[i]) <| Error.excessiveArguments name argList.Count (i - 1)
override this.GetHashCode() =
let mutable h = hash name
for par in parameters do
h <- ((h <<< 5) + h) ^^^ hash par
h <- ((h <<< 5) + h) ^^^ hash returns
h <- ((h <<< 5) + h) ^^^ hash idem
h <- ((h <<< 5) + h) ^^^ hash this.Erased
h
member this.Equals(otherFunc : FunctionType) =
otherFunc.FunctionName = name
&& otherFunc.Returns = returns
&& otherFunc.Idempotent = idem
&& otherFunc.Erased = this.Erased
&& otherFunc.Parameters.Count = parameters.Count
&& (otherFunc.Parameters, parameters) ||> Seq.forall2 (=)
override this.Equals(other : obj) =
match other with
| :? FunctionType as otherFunc ->
this.Equals(otherFunc)
| _ -> false
interface IEquatable<FunctionType> with
member this.Equals(otherFunc) = this.Equals(otherFunc)