-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.cs
More file actions
434 lines (406 loc) · 15.5 KB
/
Data.cs
File metadata and controls
434 lines (406 loc) · 15.5 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BackendDeveloperTest
{
class Data
{
public List<User> Users;
public List<Order> Orders;
public Data()
{
Users = new List<User>();
Orders = new List<Order>();
}
/// <summary>
/// main action of the query extarction
/// </summary>
/// <param name="fromList"></param>
/// <param name="commandList"></param>
/// <param name="selectList"></param>
/// <param name="D"></param>
/// <param name="bracket"></param>
/// <returns></returns>
public static command queryEngine(List<string> fromList, List<command> commandList, List<string> selectList, Data D, int bracket)
{
//build the tables of each numeric\ string commands ("string..." \ = \ < \ > \ <= \ >= )
foreach (command d in commandList)
{
if (d.commtype == 0)
{
Data.getTableForCommand(d, fromList, D);
}
}
int tableindecator = -1; // 0 - users , 1 -orders
//at this example we checking an simplefied query data extract -> single table check to get from data
if (fromList.ElementAt(0) == "Users") //taking care at *User* data rows
{
tableindecator = 0;
}
else
{
if (fromList.ElementAt(0) == "Orders") //taking care at *Order* data rows
{
tableindecator = 1;
}
}
//now we perform logic commands with 2\1 sets of commands (and the tables they got accordingly) => comm OR\AND comm
Stack<command> s1 = new Stack<command>();
Stack<command> s2 = new Stack<command>();
//organize the set of commands, which the first one waiting on the top of the stack s1
for (int i = commandList.Count-1; i>=0 ;i--)
{
s1.Push(commandList.ElementAt(i));
}
int bcare = bracket; // get the bracket layer number taking care of brackets ("bracketcare")
command temp1, temp2, temp3;
//special case : only one command
if (s1.Count == 1)
{
return s1.Pop();
}
//else : 3 or more commands
while (bcare>=0)
{
bool finishedlayer = false;
while (!finishedlayer)
{
while (s1.Count>0)
{
finishedlayer = true;
if (s1.Peek().priorityCount == bcare && s1.Count>2)// finding start of a bracket set of commands (minimum of 3 commands)
{
finishedlayer = false; // if detects a part of the layer, =false, and indicates to continue the next search
//start treating the current set of commands
temp1 = s1.Pop();
temp2 = s1.Pop();
temp3 = s1.Pop();
if (temp1.priorityCount == temp2.priorityCount && temp1.priorityCount == temp3.priorityCount && temp2.commtype == 1
&& (temp3.commtype == 0 || temp3.commtype == 3) && (temp1.commtype == 0 || temp1.commtype == 3))// check priority bracket and type of command
{
switch (tableindecator)
{
case 0: // User table
if (temp2.operand == "OR" || temp2.operand == "or")
{
temp2.quaryResUser = logicORListsUser(temp1.quaryResUser, temp3.quaryResUser);
}
else
{
temp2.quaryResUser = logicANDListUser(temp1.quaryResUser, temp3.quaryResUser);
}
break;
case 1: // Order table
if (temp2.operand == "OR" || temp2.operand == "or")
{
temp2.quaryResOrder = logicORListsOrder(temp1.quaryResOrder, temp3.quaryResOrder);
}
else
{
temp2.quaryResOrder = logicANDListOrder(temp1.quaryResOrder, temp3.quaryResOrder);
}
break;
}
temp2.commtype = 3;
if (temp2.priorityCount>0)
{
temp2.priorityCount--;
}
s2.Push(temp2);
}
}
else
{ // continue the search
s2.Push(s1.Pop());
}
}
if (s1.Count==0)
{
break;
}
}
bcare--; // layer done
while (s2.Count > 0) //move the elements from the end of the layer search back to s1
{
s1.Push(s2.Pop());
}
if (bcare < 0 && s1.Count>1)
{
bcare = 0; // need to go over layer 0 to perform all the commands
}
}
//get people by the set of commands
//pop command and return from s1
return s1.Pop();
}
/// <summary>
/// sets the 2 list<User\Order> the inside each command.
/// after we search data rows according to the command demand we
/// will make logic action between table rows.
/// </summary>
/// <param name="c"></param>
/// <param name="fromlist"></param>
/// <param name="D"></param>
private static void getTableForCommand(command c, List<string> fromlist, Data D)
{
//case : User table
if (fromlist.ElementAt(0) == "Users")
{
c.quaryResUser = new List<User>();
//string action
if (c.op1 == "Email" || c.op1 == "FullName")
{
foreach (User u in D.Users)
{
if (c.op1 == "Email" && u.Email == (string)c.op2)
{
c.quaryResUser.Add(u);
}
if (c.op1 == "FullName" && u.FullName == (string)c.op2)
{
c.quaryResUser.Add(u);
}
}
return;
}
//numeric action
if (c.op1 == "Age")
{
int act = -1;
if (c.operand == "<")
{
act = 0;
}
if (c.operand == ">")
{
act = 1;
}
if (c.operand == "<=")
{
act = 2;
}
if (c.operand == ">=")
{
act = 3;
}
if (c.operand == "=")
{
act = 4;
}
foreach (User u in D.Users)
{
switch (act)
{
case 0:
if (u.Age < Convert.ToInt32(c.op2))
{
c.quaryResUser.Add(u);
}
break;
case 1:
if (u.Age > Convert.ToInt32(c.op2))
{
c.quaryResUser.Add(u);
}
break;
case 2:
if (u.Age <= Convert.ToInt32(c.op2))
{
c.quaryResUser.Add(u);
}
break;
case 3:
if (u.Age >= Convert.ToInt32(c.op2))
{
c.quaryResUser.Add(u);
}
break;
case 4:
if (u.Age == Convert.ToInt32(c.op2))
{
c.quaryResUser.Add(u);
}
break;
}
}
return;
}
Console.WriteLine("wrong data variable to extarct from table!\n");
}
//case : Order table
else
{
c.quaryResOrder = new List<Order>();
if (fromlist.ElementAt(0) == "Orders")
{
//string action
if (c.op1 == "orderUserName")
{
foreach (Order o in D.Orders)
{
if (c.op1 == "orderUserName" && o.orderUserName == (string)c.op2)
{
c.quaryResOrder.Add(o);
}
}
return;
}
//numeric action
if (c.op1 == "totalcost")
{
int act = -1;
if (c.operand == "<")
{
act = 0;
}
if (c.operand == ">")
{
act = 1;
}
if (c.operand == "<=")
{
act = 2;
}
if (c.operand == ">=")
{
act = 3;
}
if (c.operand == "=")
{
act = 4;
}
foreach (Order o in D.Orders)
{
switch (act)
{
case 0:
if (o.totalcost < Convert.ToInt32(c.op2))
{
c.quaryResOrder.Add(o);
}
break;
case 1:
if (o.totalcost > Convert.ToInt32(c.op2))
{
c.quaryResOrder.Add(o);
}
break;
case 2:
if (o.totalcost <= Convert.ToInt32(c.op2))
{
c.quaryResOrder.Add(o);
}
break;
case 3:
if (o.totalcost >= Convert.ToInt32(c.op2))
{
c.quaryResOrder.Add(o);
}
break;
case 4:
if (o.totalcost == Convert.ToInt32(c.op2))
{
c.quaryResOrder.Add(o);
}
break;
}
}
return;
}
else
{
Console.WriteLine("table not found!");
}
}
}
}
/// <summary>
/// "AND" logic action on Users
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static List<User> logicANDListUser(List<User> a, List<User> b)
{
List<User> retlist = new List<User>();
foreach (User u in a)
{
if (b.Contains(u))
{
retlist.Add(u);
}
}
return retlist;
}
/// <summary>
/// "OR" logic action on Users
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static List<User> logicORListsUser(List<User> a, List<User> b)
{
List<User> retlist = new List<User>();
foreach (User u in a)
{
if (!retlist.Contains(u))
{
retlist.Add(u);
}
}
foreach (User u in b)
{
if (!retlist.Contains(u))
{
retlist.Add(u);
}
}
return retlist;
}
/// <summary>
/// "AND" logic action on Orders
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static List<Order> logicANDListOrder(List<Order> a, List<Order> b)
{
List<Order> retlist = new List<Order>();
foreach (Order o in a)
{
if (b.Contains(o))
{
retlist.Add(o);
}
}
return retlist;
}
/// <summary>
/// "OR" logic action on Orders
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static List<Order> logicORListsOrder(List<Order> a, List<Order> b)
{
List<Order> retlist = new List<Order>();
foreach (Order o in a)
{
if (!retlist.Contains(o))
{
retlist.Add(o);
}
}
foreach (Order o in b)
{
if (!retlist.Contains(o))
{
retlist.Add(o);
}
}
return retlist;
}
}
}