/
table-role.pm6
239 lines (189 loc) · 6.54 KB
/
table-role.pm6
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
use v6.c;
use BSON::Document;
use MongoDB;
use MongoDB::Client;
use MongoDB::Database;
use MongoDB::Collection;
#-------------------------------------------------------------------------------
package MongoDB {
# Array index and error codes
constant C-MANDATORY = 0;
constant C-TYPE = 1;
# error code
constant C-NOTINSCHEMA = -1;
#-----------------------------------------------------------------------------
role MdbTableRole {
# Schema is a document to describe a table. Its keys are the fields in the table
# The value of a field is an Array or BSON::Document. When an Array, it holds
# the following info
# - Mandatory field if True(Bool)
# - Type of field,
# -
# When BSON::Document is used, it means the structure is nested and is always
# mandatory.
#
# BSON::Document.new: (
# contact => BSON::Document.new((
# name => [ True, Str],
# surname => [True, Str]
# )),
# address => BSON::Document.new((
# street => [ True, Str],
# number => [ False, Int],
# ))
# )
has BSON::Document $!schema;
# Record to read in data or write to database
has BSON::Document $!record;
has Array $!failed-fields;
has MongoDB::Client $!client;
has MongoDB::Database $!db;
has MongoDB::Collection $!cl;
#---------------------------------------------------------------------------
method reset( *%fields ) {
$!record .= new;
}
#---------------------------------------------------------------------------
method set( *%fields ) {
# Define the record in the same order as noted in schema
$!record .= new unless $!record.defined;
for $!schema.keys -> $field-name {
if %fields{$field-name}:exists {
$!record{$field-name} = %fields{$field-name};
}
else {
# $!record{$field-name} = Nil unless $!record{$field-name}.defined;
}
}
# Add the rest of the fields not in schema. These are failed later
for %fields.keys -> $field-name {
if $!schema{$field-name}:!exists {
$!record{$field-name} = %fields{$field-name};
}
}
}
#---------------------------------------------------------------------------
method read ( --> BSON::Document ) {
}
#---------------------------------------------------------------------------
method read-next ( --> BSON::Document ) {
}
#---------------------------------------------------------------------------
method insert ( --> BSON::Document ) {
my BSON::Document $doc;
$!failed-fields = [];
self!check-record( $!schema, $!record);
if $!failed-fields.elems {
$doc = self!document-failures;
}
else {
$doc = $!db.run-command(
BSON::Document.new: (
insert => $!cl.name,
documents => [
$!record
]
)
);
}
$doc;
}
#---------------------------------------------------------------------------
method update ( --> BSON::Document ) {
}
#---------------------------------------------------------------------------
method delete ( --> BSON::Document ) {
}
#---------------------------------------------------------------------------
method !check-record (
BSON::Document:D $schema,
BSON::Document:D $record
) {
for $!schema.keys -> $field-name {
if $record{$field-name}:exists {
if $record{$field-name} ~~ BSON::Document {
self!check-record( $!schema{$field-name}, $record{$field-name});
}
elsif $record{$field-name} !~~ $!schema{$field-name}[C-TYPE] {
$!failed-fields.push: [
$field-name, # failed fieldname
C-TYPE, # failed on type
$record{$field-name}.WHAT, # has type
$!schema{$field-name}[C-TYPE] # should be type
];
}
}
else {
if $!schema{$field-name}[C-MANDATORY] {
$!failed-fields.push: [
$field-name, # failed fieldname
C-MANDATORY, # field is missing
];
}
}
}
for $record.keys -> $field-name {
say "Check $field-name: ", $schema{$field-name}:exists;
if $schema{$field-name}:!exists {
$!failed-fields.push: [
$field-name, # failed fieldname
C-NOTINSCHEMA, # field not in schema
];
}
}
}
#---------------------------------------------------------------------------
method !check-schema ( BSON::Document:D $schema --> Bool ) {
}
#---------------------------------------------------------------------------
method !document-failures ( --> BSON::Document ) {
my BSON::Document $error-doc .= new;
$error-doc<ok> = 0;
$error-doc<reason> = 'Missing fields or fields having wrong types';
$error-doc<fields> = BSON::Document.new;
for @$!failed-fields -> $field-spec {
if $field-spec[1] ~~ C-MANDATORY {
$error-doc<fields>{$field-spec[0]} = 'missing';
}
elsif $field-spec[1] ~~ C-TYPE {
$error-doc<fields>{$field-spec[0]} =
[~] 'type failure, is ', $field-spec[2].WHAT.perl, " but must be ",
$field-spec[3].WHAT.perl;
}
elsif $field-spec[1] ~~ C-NOTINSCHEMA {
$error-doc<fields>{$field-spec[0]} = 'not described in schema';
}
}
$error-doc;
}
}
#-----------------------------------------------------------------------------
#TODO See if class can be generated in $A below
class MdbTable does MdbTableRole {
#---------------------------------------------------------------------------
submethod BUILD (
Str:D :$uri,
Str:D :$db-name,
Str:D :$cl-name,
BSON::Document:D :$schema
) {
$!client = MongoDB::Client.new(:$uri);
$!db = $!client.database($db-name);
$!cl = $!db.collection($cl-name);
$!schema = $schema;
}
}
sub gen-table-class (
Str:D :$uri,
Str:D :$db-name,
Str:D :$cl-name,
BSON::Document:D :$schema
--> MongoDB::MdbTable
) is export {
my $name = "$db-name.tc()::$cl-name.tc()";
my $A := Metamodel::ClassHOW.new_type(:$name);
$A.^add_parent( MongoDB::MdbTable, :!hides);
$A.^compose;
$A.new( :$uri, :$db-name, :$cl-name, :$schema);
}
}