@@ -6,6 +6,7 @@ const chalk = require('chalk');
6
6
const lodash = require ( 'lodash' ) ;
7
7
8
8
const cfn = require ( './common-functions' ) ;
9
+ const docfn = require ( './doc-functions' ) ;
9
10
10
11
//simple where clause (Compare single property with single value)
11
12
function whereClause ( docname , prop , operator , val , callback ) {
@@ -15,15 +16,22 @@ function whereClause(docname, prop, operator, val, callback) {
15
16
if ( db !== null && fs . existsSync ( p ) ) {
16
17
if ( cache . length !== 0 && current_doc === docname ) {
17
18
//fast retrieval using cache
18
- output = cfn . whereComp ( prop , operator , val ) ;
19
- if ( output !== null ) {
20
- if ( callback )
21
- callback ( output )
22
- } else {
23
- msg = 'Operator not recognized !' ;
19
+ if ( ! docfn . propExists ( prop ) ) {
20
+ msg = 'Property does not exist in any record !' ;
24
21
if ( callback )
25
22
callback ( ( chalk . red . bold ( msg ) ) ) ;
26
23
}
24
+ else {
25
+ output = cfn . whereComp ( prop , operator , val ) ;
26
+ if ( output !== null ) {
27
+ if ( callback )
28
+ callback ( output )
29
+ } else {
30
+ msg = 'Operator not recognized !' ;
31
+ if ( callback )
32
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
33
+ }
34
+ }
27
35
} else {
28
36
//normal retrieval
29
37
if ( fs . statSync ( p ) . size !== 0 ) {
@@ -35,15 +43,22 @@ function whereClause(docname, prop, operator, val, callback) {
35
43
} else {
36
44
cache = msgpack . decode ( data ) ;
37
45
current_doc = docname ;
38
- output = cfn . whereComp ( prop , operator , val ) ;
39
- if ( output !== null ) {
40
- if ( callback )
41
- callback ( output )
42
- } else {
43
- msg = 'Operator not recognized !' ;
46
+ if ( ! docfn . propExists ( prop ) ) {
47
+ msg = 'Property does not exist in any record !' ;
44
48
if ( callback )
45
49
callback ( ( chalk . red . bold ( msg ) ) ) ;
46
50
}
51
+ else {
52
+ output = cfn . whereComp ( prop , operator , val ) ;
53
+ if ( output !== null ) {
54
+ if ( callback )
55
+ callback ( output )
56
+ } else {
57
+ msg = 'Operator not recognized !' ;
58
+ if ( callback )
59
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
60
+ }
61
+ }
47
62
}
48
63
} ) ;
49
64
} else {
@@ -69,27 +84,34 @@ function whereClause2(docname, prop1, operator1, val1, conjunction, prop2, opera
69
84
if ( db !== null && fs . existsSync ( p ) ) {
70
85
if ( cache . length !== 0 && current_doc === docname ) {
71
86
//fast retrieval using cache
72
- output1 = cfn . whereComp ( prop1 , operator1 , val1 ) ;
73
- output2 = cfn . whereComp ( prop2 , operator2 , val2 ) ;
74
- if ( output1 === null || output2 === null ) {
75
- msg = 'Operator not recognized !' ;
87
+ if ( ! docfn . propExists ( prop1 ) || ! docfn . propExists ( prop2 ) ) {
88
+ msg = 'One or more properties do not exist in any record !' ;
76
89
if ( callback )
77
90
callback ( ( chalk . red . bold ( msg ) ) ) ;
78
- } else {
79
- if ( conjunction === 'and' ) {
80
- let result = lodash . intersection ( output1 , output2 ) ;
81
- if ( callback )
82
- callback ( result )
83
- }
84
- else if ( conjunction === 'or' ) {
85
- let result = lodash . union ( output1 , output2 ) ;
86
- if ( callback )
87
- callback ( result )
88
- }
89
- else {
90
- msg = 'Conjunction not recognized !' ;
91
+ }
92
+ else {
93
+ output1 = cfn . whereComp ( prop1 , operator1 , val1 ) ;
94
+ output2 = cfn . whereComp ( prop2 , operator2 , val2 ) ;
95
+ if ( output1 === null || output2 === null ) {
96
+ msg = 'Operator not recognized !' ;
91
97
if ( callback )
92
98
callback ( ( chalk . red . bold ( msg ) ) ) ;
99
+ } else {
100
+ if ( conjunction === 'and' ) {
101
+ let result = lodash . intersection ( output1 , output2 ) ;
102
+ if ( callback )
103
+ callback ( result )
104
+ }
105
+ else if ( conjunction === 'or' ) {
106
+ let result = lodash . union ( output1 , output2 ) ;
107
+ if ( callback )
108
+ callback ( result )
109
+ }
110
+ else {
111
+ msg = 'Conjunction not recognized !' ;
112
+ if ( callback )
113
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
114
+ }
93
115
}
94
116
}
95
117
} else {
@@ -103,30 +125,100 @@ function whereClause2(docname, prop1, operator1, val1, conjunction, prop2, opera
103
125
} else {
104
126
cache = msgpack . decode ( data ) ;
105
127
current_doc = docname ;
106
- output1 = cfn . whereComp ( prop1 , operator1 , val1 ) ;
107
- output2 = cfn . whereComp ( prop2 , operator2 , val2 ) ;
108
- if ( output1 === null || output2 === null ) {
109
- msg = 'Operator not recognized !' ;
128
+ if ( ! docfn . propExists ( prop1 ) || ! docfn . propExists ( prop2 ) ) {
129
+ msg = 'One or more properties do not exist in any record !' ;
110
130
if ( callback )
111
131
callback ( ( chalk . red . bold ( msg ) ) ) ;
112
132
} else {
113
- if ( conjunction === 'and' ) {
114
- let result = lodash . intersection ( output1 , output2 ) ;
115
- if ( callback )
116
- callback ( result )
117
- }
118
- else if ( conjunction === 'or' ) {
119
- let result = lodash . union ( output1 , output2 ) ;
120
- if ( callback )
121
- callback ( result )
133
+ output1 = cfn . whereComp ( prop1 , operator1 , val1 ) ;
134
+ output2 = cfn . whereComp ( prop2 , operator2 , val2 ) ;
135
+ if ( output1 === null || output2 === null ) {
136
+ msg = 'Operator not recognized !' ;
137
+ if ( callback )
138
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
139
+ } else {
140
+ if ( conjunction === 'and' ) {
141
+ let result = lodash . intersection ( output1 , output2 ) ;
142
+ if ( callback )
143
+ callback ( result )
144
+ } else if ( conjunction === 'or' ) {
145
+ let result = lodash . union ( output1 , output2 ) ;
146
+ if ( callback )
147
+ callback ( result )
148
+ } else {
149
+ msg = 'Conjunction not recognized !' ;
150
+ if ( callback )
151
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
152
+ }
122
153
}
123
- else {
124
- msg = 'Conjunction not recognized !' ;
154
+ }
155
+ }
156
+ } ) ;
157
+ } else {
158
+ msg = 'Document is empty !' ;
159
+ if ( callback )
160
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
161
+ }
162
+ }
163
+ } else {
164
+ msg = 'No database selected or the document does not exist !' ;
165
+ if ( callback )
166
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
167
+ }
168
+ }
169
+ module . exports . whereClause2 = whereClause2 ;
170
+
171
+ // compare properties in a record
172
+ function propWhere ( docname , prop1 , operator , prop2 , callback ) {
173
+ let msg ;
174
+ let output ;
175
+ let p = `${ process . env . LAZLO_SOURCE } /${ db } /${ docname } .laz` ;
176
+ if ( db !== null && fs . existsSync ( p ) ) {
177
+ if ( cache . length !== 0 && current_doc === docname ) {
178
+ //fast retrieval using cache
179
+ if ( ! docfn . propExists ( prop1 ) || ! docfn . propExists ( prop2 ) ) {
180
+ msg = 'One or more properties do not exist in any record !' ;
181
+ if ( callback )
182
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
183
+ }
184
+ else {
185
+ output = cfn . propComp ( prop1 , operator , prop2 ) ;
186
+ if ( output !== null ) {
187
+ if ( callback )
188
+ callback ( output )
189
+ } else {
190
+ msg = 'Operator not recognized !' ;
191
+ if ( callback )
192
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
193
+ }
194
+ }
195
+ } else {
196
+ //normal retrieval
197
+ if ( fs . statSync ( p ) . size !== 0 ) {
198
+ fs . readFile ( p , ( err , data ) => {
199
+ if ( err ) {
200
+ msg = 'Data seems to be corrupted !' ;
201
+ if ( callback )
202
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
203
+ } else {
204
+ cache = msgpack . decode ( data ) ;
205
+ current_doc = docname ;
206
+ if ( ! docfn . propExists ( prop1 ) || ! docfn . propExists ( prop2 ) ) {
207
+ msg = 'One or more properties do not exist in any record !' ;
208
+ if ( callback )
209
+ callback ( ( chalk . red . bold ( msg ) ) ) ;
210
+ } else {
211
+ output = cfn . propComp ( prop1 , operator , prop2 ) ;
212
+ if ( output !== null ) {
213
+ if ( callback )
214
+ callback ( output )
215
+ } else {
216
+ msg = 'Operator not recognized !' ;
125
217
if ( callback )
126
218
callback ( ( chalk . red . bold ( msg ) ) ) ;
127
219
}
128
220
}
129
- }
221
+ }
130
222
} ) ;
131
223
} else {
132
224
msg = 'Document is empty !' ;
@@ -140,4 +232,4 @@ function whereClause2(docname, prop1, operator1, val1, conjunction, prop2, opera
140
232
callback ( ( chalk . red . bold ( msg ) ) ) ;
141
233
}
142
234
}
143
- module . exports . whereClause2 = whereClause2 ;
235
+ module . exports . propWhere = propWhere ;
0 commit comments