-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypes.fs
190 lines (169 loc) · 5.04 KB
/
Types.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
namespace PgGen
open System
type Db = { DName : string ; Owner : string ; Schemas : Schema list ; Comment : string option }
and Schema = { SName : string ; Tables : Table list ; Comment : string option ; Enums : PEnum list}
and ForeignRef = {
/// Schema with the referring table/column
FromSchema : string option
/// Table with the referring column
FromTable : string option
/// Cols that make up the reference (see Generate option though which overrides this)
FromCols : string list
/// Other table that we are referring to
ToTable : string
/// Schema of other table that we are referring to
ToSchema : string option
ToCols : string list
/// SHould we auto generate the referring column?
Generate : bool
IsNullable : bool
Name : string option
}
and FRefAttr =
| FName of string
| FComment of string
| FNullable
and Unique = {
Cols : string list
}
/// This is a reference to an existing enum (inside a table)
and EnumRef = {
EName : string
ESchema : string option
Generate : bool
IsNullable : bool
Name : string option
}
and EnumRefAttr =
| EName of string
| ERComment of string
| ENullable
and PKey = {
Cols : string list
}
and Table = {
TName : string
Comment : string option
Attributes : TableAttr list
Cols : Column list
FRefs : ForeignRef list
ERefs : EnumRef list
Uniques : Unique list
PKey : PKey option
} with
member x.FSharpName() =
x.TName.Split([|'_'|])
|> Array.map (fun (s:string) -> s.[0].ToString().ToUpper() + s.[1..]) |> String.concat ""
member x.PKeyTypeName() =
match x.PKey with
| Some _ -> $"{x.FSharpName()}PKey"
| None -> "int"
member x.FullCols() =
[ yield! x.Cols // regular columns
for fk in x.FRefs do // foreign references
if fk.Generate then
match fk.Name with
| None ->
yield { CName = $"id_{fk.ToTable}"; CType = ColumnType.Int32; Nullable = fk.IsNullable; Array = false ; Comment = None}
| Some
name -> yield { CName = name; CType = ColumnType.Int32; Nullable = fk.IsNullable; Array = false ; Comment = None}
else
failwithf $"Not implemented, non int/id foreign keys {fk}"
for e in x.ERefs do
if e.Generate then
match e.Name with
| None ->
yield { CName = e.EName; CType = ColumnType.Enum e.EName; Nullable = e.IsNullable; Array = false ; Comment = None}
| Some
name -> yield { CName = name; CType = ColumnType.Enum e.EName; Nullable = e.IsNullable; Array = false ; Comment = None}
else
failwithf $"Not implemented, non generated enum keys {e}"
]
and TableAttr =
| Comment of string
| TBD
and ColumnType =
| Id
| Id64
| String
| Bool
| Int32
| Timestamp
| Jsonb
| Float
| Guid
| Blob
| Decimal
| Enum of string
member x.Sql() =
match x with
| Id -> "int"
| Id64 -> "bigint"
| String -> "text"
| Bool -> "boolean"
| Int32 -> "int"
| Timestamp -> "timestamptz default now()"
| Jsonb -> "jsonb"
| Float -> "float"
| Decimal -> "decimal"
| Blob -> "bytea"
| Guid -> "uuid"
| Enum e -> e
member x.FSharpType() =
match x with
| Id -> "int"
| Id64 -> "int64"
| String -> "string"
| Bool -> "bool"
| Int32 -> "int"
| Timestamp -> "DateTime"
| Jsonb -> "string"
| Float -> "float"
| Blob -> "byte[]"
| Decimal -> "decimal"
| Guid -> "Guid"
| Enum e -> e
and Column = {
CName : string
CType : ColumnType
Nullable : bool
Array : bool
Comment : string option
} with
member x.FSharpName() =
x.CName.Split([|'_'|])
|> Array.map (fun (s:string) -> s.[0].ToString().ToUpper() + s.[1..]) |> String.concat ""
and PEnum = {
EName : string
EValues : string list
EComment : string option
}
type EAttr =
| Member of string
| EComment of string
type CUpdateStrategy =
| Never
| Now
type ColumnMatters =
| Nullable
| Array
| CComment of string
| CUpdate of CUpdateStrategy
/// Things in the table itself (cols et al)
type TableBodyItem =
| ColumnItem of Column
| Unique of Unique
| ForeignRef of ForeignRef
| EnumRef of EnumRef
| PKey of PKey
type SchemaAttr =
| TBD
| SComment of string
type SchemaBodyItem =
| TableItem of Table
| PEnum of PEnum
type DBAttr =
| Owner of string
| DComment of string
type DBBodyItem =
| SchemaItem of Schema