forked from github-linguist/linguist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PgSchema.roc
223 lines (200 loc) · 6.31 KB
/
PgSchema.roc
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
# From https://github.com/agu-z/roc-pg/blob/main/sql-cli/src/Schema.roc
# License is UPL
interface Schema
exposes [
Schema,
ColumnId,
Table,
Column,
new,
getName,
getTables,
primaryColumn,
]
imports []
Nullable a : [Null, NotNull a]
Schema := {
name : Str,
tables : List Table,
references : Dict ColumnId ColumnId,
primaryColumns : Dict ColumnId { tableName : Str, columnName : Str },
}
ColumnId : (I32, I16)
Table : {
id : I32,
name : Str,
columns : List Column,
constraints : List Constraint,
}
Column : {
num : I16,
name : Str,
dataType : Str,
typeCategory : Str,
elemDataType : Nullable Str,
isNullable : Bool,
}
Constraint : {
type : Str,
columns : List I16,
foreignTable : I32, # pg sets this to 0 if not foreign key
foreignColumns : List I16,
}
new : Str, List Table -> Schema
new = \schemaName, tables ->
tablesLen : Nat
tablesLen = List.len tables
keys : {
primaryColumns : Dict ColumnId { tableName : Str, columnName : Str },
references : Dict ColumnId ColumnId,
}
keys =
stateTable, table <- List.walk tables {
primaryColumns: Dict.withCapacity tablesLen,
references: Dict.withCapacity (tablesLen * 4),
}
state, constraint <- List.walk table.constraints stateTable
when constraint.type is
"p" ->
newPrimaryColumns =
constraint.columns
|> List.map \colNum ->
names =
when List.findFirst table.columns \col -> col.num == colNum is
Ok { name } ->
{
tableName: table.name,
columnName: name,
}
Err NotFound ->
{
tableName: "\(Num.toStr table.id)",
columnName: "\(Num.toStr colNum)",
}
((table.id, colNum), names)
|> Dict.fromList
{ state & primaryColumns: Dict.insertAll state.primaryColumns newPrimaryColumns }
"f" ->
newReferences =
constraint.columns
|> List.map2 constraint.foreignColumns \colNum, foreignColumn ->
((table.id, colNum), (constraint.foreignTable, foreignColumn))
|> Dict.fromList
{ state & references: Dict.insertAll state.references newReferences }
_ ->
state
@Schema {
name: schemaName,
tables,
primaryColumns: keys.primaryColumns,
references: keys.references,
}
getTables : Schema -> List Table
getTables = \@Schema schema -> schema.tables
getName : Schema -> Str
getName = \@Schema schema -> schema.name
## Recursively find the final column referenced by another column.
##
## Returns itself if it's part of a primary key and no foreign key.
primaryColumn : Schema,
ColumnId
-> Result
{
id : ColumnId,
tableName : Str,
columnName : Str,
}
[KeyNotFound]
primaryColumn = \@Schema schema, column ->
when Dict.get schema.references column is
Ok refColumn ->
primaryColumn (@Schema schema) refColumn
Err KeyNotFound ->
Dict.get schema.primaryColumns column
|> Result.map \names -> {
id: column,
tableName: names.tableName,
columnName: names.columnName,
}
expect primaryColumn testSchema (1, 1) == Ok { id: (1, 1), tableName: "users", columnName: "id" }
expect primaryColumn testSchema (1, 2) == Err KeyNotFound
expect primaryColumn testSchema (2, 1) == Ok { id: (2, 1), tableName: "posts", columnName: "id" }
expect primaryColumn testSchema (2, 2) == Ok { id: (1, 1), tableName: "users", columnName: "id" }
expect primaryColumn testSchema (2, 3) == Err KeyNotFound
testSchema =
new "public" [
{
id: 1,
name: "users",
columns: [
{
num: 1,
name: "id",
dataType: "int4",
typeCategory: "N",
elemDataType: Null,
isNullable: Bool.false,
},
{
num: 2,
name: "name",
dataType: "text",
typeCategory: "S",
elemDataType: Null,
isNullable: Bool.false,
},
],
constraints: [
{
type: "p",
columns: [1],
foreignTable: 0,
foreignColumns: [],
},
],
},
{
id: 2,
name: "posts",
columns: [
{
num: 1,
name: "id",
dataType: "int4",
typeCategory: "N",
elemDataType: Null,
isNullable: Bool.false,
},
{
num: 2,
name: "user_id",
dataType: "int4",
typeCategory: "N",
elemDataType: Null,
isNullable: Bool.false,
},
{
num: 3,
name: "title",
dataType: "text",
typeCategory: "S",
elemDataType: Null,
isNullable: Bool.false,
},
],
constraints: [
{
type: "p",
columns: [1],
foreignTable: 0,
foreignColumns: [],
},
{
type: "f",
columns: [2],
foreignTable: 1,
foreignColumns: [1],
},
],
},
]