-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin_out.cpp
More file actions
822 lines (755 loc) · 25.7 KB
/
Copy pathin_out.cpp
File metadata and controls
822 lines (755 loc) · 25.7 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/**
* @file: in_out.cpp
* @author: fhn
* @date: 4/21
* @description: offer the main I/O interfaces for interpreters, in which we would interact with file
* in other words, this is the executing module.
* @version: 1.0: interact with txt files.
v2.0: finished with vector structrue ordered map.
*/
//--------------------------------------------------------------------------------------------
#include "in_out.hpp"
#include <sstream>// in v1.0 used for piece together the cmd
//--------------------------------------------------------------------------------------------
extern bool broken;
namespace in_out{
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
using std::fstream;
using std::ios;
string cur_db; // used for indicating the current database, called in use sentence.
string cur_tb; // used for indicating the current table, if doesn't change, edit directly in cache.
vector<string> dbs; bool dbs_flag = false;
Table cache;
/**
* @author: fhn
* @date: 4/27
* @description: (virtual) output function
* @version:
*/
void ColInfo::PrintInfo() const
{
#ifdef _LOC_
std::clog << "ColInfo::PrintInfo() called. column info was put once" << endl;
#endif
cout << col_num_ << ' ' << 0 << endl; // the second 0 indicates the num of elem (when table is created)
for (auto it : col_info_)
cout << it.first << ' ' << it.second << endl;
}
/**
* @author: fhn
* @date: 4/27
* @description: input a string, output the what's the rank of it in col_info.
not found: -1.
* @version: v1.0
*/
int ColInfo::FindCol(const string & name)
{
vector<std::pair<string, int>>::iterator it = cache.col_info_.begin();
for (auto it = col_info_.begin(); it < col_info_.end(); it++)
{
if ((*it).first == name)
return it - col_info_.begin();
}
return -1;
}
/**
* @author: fhn
* @date: 4/25
* @description: print the table_info, mainly used in tagged the table store file.
* @version: 1.0 : simply output the info of table's map.
* v2.0: deal with much trouble with the output range(DON'T TRUST VECTOR SIZE AS THE RANGE!!!) and the tmp watch is very significant
*/
void Table::PrintInfo() const
{
#ifdef _LOC_
std::clog << "table print once" << endl;
#endif
cout << col_num_ << ' ' << elem_num_ << endl;
for (int i = 0; i < col_num_; i++)
{
cout << col_info_[i].first << ' ' << col_info_[i].second << ' ';
#ifdef _LOC_
std::clog << "elem num is " << elem_num_ << " and before deleting: "<< elem_info_[0].size() << endl;
#endif
// int tmp = elem_num() < elem_info_[0].size() ? elem_num() : elem_info_[0].size();
// after deleting, use the elem_info[0], if insert is haulting, use elem_num; vector size is quite dangerous, use elem_num is somehow
int tmp = elem_num();
#ifdef _TEST5_//the rang is very strange, so we spent lots of time on it.
std::clog << "print info: " << tmp << endl;
int dcnt = 0;
for (int i = 0; i < isDelete.size(); i++)
if (isDelete[i])
std::clog << "isDelete[" << i << "] is true; " << endl;
#endif
#ifdef _TEST5_//the rang is very strange, so we spent lots of time on it.
std::clog << "print info: " << tmp << endl;
#endif
for (int j = 0; j < tmp; j++)
if (!isDelete[j])
{
cout << elem_info_[i][j].first << ' ';
#ifdef _TEST5_
std::clog << "print info: " << tmp << endl;
#endif
}
// else if (j == 0) tmp++;
else tmp++;
cout << endl;
}
}
/**
* @author: fhn
* @date: 4/26
* @description: push a pair of elem into the vector of col_info_;
* mode for InitRead(); mode 1 default: col_num plus; mode 0 alter: num unchanged.
* @version: v1.0
*/
void ColInfo::AddCol(string s, int i, bool mode)
{
#ifdef _LOC_
std::clog << "a col was added" << endl;
#endif
col_info_.push_back(make_pair(s, i));
if (mode)
col_num_++;
}
/**
* @author: fhn
* @date: 4/26
* @description: read information about a table from a file
* @version: v1.0; without judgeing if an elem is illegal(because the inserted data is legal)
*/
void Table::InitRead()
{
// for (auto it: elem_info_)
// while (!it.empty())
// it.pop_back();
#ifdef _LOC_
std::clog << "the table was initialized" << endl;
#endif
cin >> col_num_ >> elem_num_;
Break();
string tmp_name; int tmp_length; string tmp_elem;
SetColNum(col_num_);
isDelete.resize(elem_num_ * 2);
for (int i = 0; i < col_num_; i++)
{
cin >> tmp_name >> tmp_length;
#ifdef _TEST5_
std::clog << cin.eof() << " " << cin.bad() << " " << cin.fail() << std::endl;
#endif
Break();
col_info_[i].first = tmp_name;
col_info_[i].second = tmp_length;
elem_info_[i].resize(elem_num());
for (int j = 0; j < elem_num_; j++)
{
cin >> tmp_elem;
Break();
elem_info_[i][j].first = tmp_elem;
elem_info_[i][j].second = j;
}
}
}
/**
* @author: fhn
* @date: 4/27
* @description: set table's colnum, override ColInfo::SetColNum(); more : dealing the elem_info_
* @version: v2.0
*/
void Table::SetColNum(const int col_num)
{
#ifdef _LOC_
std::clog << "column num was set as "<< col_num << endl;
#endif
ColInfo::SetColNum(col_num);
elem_info_.resize(col_num);
}
void Table::SetElemNum(const int elem_num)
{
elem_num_ = elem_num;
for (auto it: elem_info_)
it.reserve(elem_num*2), it.resize(elem_num);
}
/**
* @author: fhn
* @date: 4/27
* @description: push a pair with its index_num to
* @version: v2.0: add pair into the 2D vector
*/
void Table::AddElem(int col_no, int index_num, string elem)
{
#ifdef _LOC_
std::clog << "vector[" << col_no << "] was added with elem. now elem_num is " << index_num + 1 << endl;
#endif
isDelete.push_back(false);
elem_info_[col_no].push_back(make_pair(elem, index_num));
#ifdef _TEST5_
std::clog << "vector[" << col_no << "] was added with " << elem << ". now elem_num is " << index_num + 1 << endl;
#endif
}
void Table::EraseElem(const int& id)
{
#ifdef _TEST5_
std::clog << "the "<< id << "th elem was erased" << endl;
#endif
if (isDelete[id]) return;
elem_num_--;
isDelete[id] = true;
}
/**
* @author: fhn
* @date: 4/26
* @description: create database using the name received.
* if failed, error by DOS cmd.
* @version: 1.0
*/
void CreateDatabase(const string& name)
{
if (!dbs_flag)
{
freopen("dbs_info", "r", stdin);
string tmp;
while (cin >> tmp)
dbs.push_back(tmp);
freopen("CON", "r", stdin);
dbs_flag = true;
}
auto it = find(dbs.begin(), dbs.end(), name);
if (it != dbs.end())
{ cout << "#ERROR subdirectory " << name << " already exists." << endl << endl; return; }
else if (it == dbs.end())
{
string mkdir = "mkdir " + name;
//system(mkdir.c_str());
cout << "Query OK, 1 row affected ";
freopen("CON", "w", stdout);
dbs.push_back(name);
time_cnt::end();
cout << "\n";
}
freopen("dbs_info", "w", stdout);
for (auto it: dbs)
cout << it << ' ';
freopen("CON", "w", stdout);
}
/**
* @author: fhn
* @date: 4/27
* @description: create a table, and input basic info of it.
* @version: v2.0: using redirection technique.
*/
void CreateTable(const string& name, const ColInfo& column_info)
{
if (cur_db.size() == 0)
{ cout << "#ERROR No database selected" << endl << endl; return; }
string file_path = cur_db + "\\" + name;
if (exist(file_path))
{ cout << "#ERROR file already exists" << endl << endl; return;}
freopen(file_path.c_str(), "w", stdout);
column_info.PrintInfo();// if we want to remain the const of table_info, we should define the print() as const.
freopen("CON", "w", stdout);
cout << "Query OK, 0 rows affected ";
time_cnt::end();
cout << endl;
}
/**
* @author: fhn
* @date: 4/26
* @description: change the cur_db,
* @version: 1.0
*/
void Use(string name)
{
if (!dbs_flag)
{
freopen("dbs_info", "r", stdin);
string tmp;
while (cin >> tmp)
dbs.push_back(tmp);
freopen("CON", "r", stdin);
dbs_flag = true;
}
auto it = find(dbs.begin(), dbs.end(), name);
if (it != dbs.end())
{
cur_db = name; cur_tb = "";
cout << "Database changed" << endl;
freopen("dbs_info", "w", stdout);
for (auto it: dbs)
cout << it << ' ';
freopen("CON", "w", stdout);
}
else cout << "#ERROR No such subdirectory" << endl << endl;
}
//--------------------------------------------------------------------------------------------
//below are some I/O functions
bool trim(string & str)
{
int pos1 = str.find_first_of("\""), pos2 = str.find_first_of("\'");
if (pos1 == string::npos && pos2 == string::npos)
return false;
else if (pos1 != string::npos && pos2 != string::npos)
return false;
else if (pos1 != string::npos && pos1 == 0)
{
//erase the first and last "
str.erase(pos1, 1);
pos1 = str.find_first_of("\"");
if (pos1 != string::npos && pos1 == str.size()-1)
str.erase(pos1, 1);
else return false;
//to see if there other non-alnum chars
for (auto it : str)
if (!isalnum(it))
return false;
}
else if (pos2 != string ::npos && pos2 == 0)
{
//erase the first and last "
str.erase(pos2, 1);
pos2 = str.find_last_of("\'");
if (pos2 != string::npos && pos2 == str.size()-1)
str.erase(pos2, 1);
else return false;
//to see if there other non-alnum chars
for (auto it : str)
if (!isalnum(it))
return false;
}
return true;
}
/**
* @author: fhn
* @date: 5/7
* @description: if there is something wrong in the in_out::Insert(), if the quotation mark is wrong
* @version: v1.0
*/
void Table::ClearError(int row_num, int err_col)
{
for (int i = 0; i < err_col; i++)
elem_info_[i].erase(elem_info_[i].begin()+row_num);
}
#ifdef _TEST5_
void WATCH()
{
for (int i = 0; i < cache.elem_info_.size(); i++)
{
for(int j = 0; j < cache.elem_info_[i].size(); j++)
std::clog << cache.elem_info_[i][j].first << ' ';
cout << std::endl;
}
}
#endif
/**
* @author: fhn
* @date: 4/26
* @description: insert a row into a table.
* @version: v1.0
* v2.0: using redirection technique; rewrite the Table::SetColNum()<else there'll be an segfault.>
* to be improved: this function and the ones following needs to deal with unexisted file.
* v3.0: improve the file-exist problem;
* high robustness with string and its quotaion mark.
* need to remember: DO NOT TRY TO SPAN 'freopen()' SCOPE TOO LONG.
*/
void Insert(string table_name, vector<string> col_name)
{
if (!cur_db.size())
{ cout << "#ERROR no database used" << endl << endl; return;}
string file_path = cur_db + "\\" + table_name;//default
if (!exist(file_path))
{ cout << "#ERROR table " << table_name << " doesn't exists" << endl << endl; return; }
if (cur_tb == table_name) // to use cache to just insert into cache
{
cache.SetColNum(cache.col_num());
if (cache.col_num() != col_name.size())
{ cout << "#ERROR in insert: param num not fit" << endl << endl; cur_tb = ""; return;}
for (int n_col = 0; n_col < cache.col_num(); n_col++)
{
if (cache.col_len(n_col) && !trim(col_name[n_col]))
{
cout << "#ERROR insert syntax wrong: varchar to be inserted illegal" << endl << endl;
cur_tb = ""; cache.ClearError(cache.elem_num()+1, n_col);
return;
}//if returns in these two interfaces, we must clear the polluted data.
if (cache.col_len(n_col) && col_name[n_col].size() > cache.col_len(n_col))
{
cout << "#ERROR Elem to be inserted illegal: too long" << endl << endl;
cur_tb = ""; cache.ClearError(cache.elem_num()+1, n_col);
return;
}//if returns here, the data which has been inserted was deprecated. for the elem_num_ remain unchanged.
cache.AddElem(n_col, cache.elem_num(), col_name[n_col]);
}
}
else {
freopen(file_path.c_str(), "r", stdin);
int tmp_num;
cin >> tmp_num; cache.SetColNum (tmp_num);
cin >> tmp_num; cache.SetElemNum(tmp_num);
string tmp_name; int tmp_length; string tmp_elem;
for (int n_col = 0; n_col < cache.col_num(); n_col++)
{
cin >> tmp_name >> tmp_length;
cache.col_info_[n_col].first = tmp_name;//don't try to use add col: just here, for the cache needs to be cleared.
cache.col_info_[n_col].second = tmp_length;
for (int j = 0; j < cache.elem_num(); j++)
{
cin >> tmp_elem;
cache.SetElem(n_col, j, tmp_elem);
}
string c = col_name[n_col];
if (cache.col_len(n_col))// if this colomn's type is varchar
if (!trim(c))
{
freopen("CON", "r", stdin);
cout << "#ERROR insert syntax wrong: varchar to be inserted illegal" << endl << endl;
cache.ClearError(cache.elem_num()+1, n_col);
return;
}
if (tmp_length && col_name[n_col].size() > tmp_length)
{
freopen("CON", "r", stdin); cur_tb = "";
cout << "Elem to be inserted illegal: too long" << endl;
cache.ClearError(cache.elem_num()+1, n_col);
return;
}//if returns here, the data which has been inserted was deprecated. for the elem_num_ remain unchanged.
cache.elem_info_[n_col].resize(cache.elem_num()+1);
cache.elem_info_[n_col][cache.elem_num()].first = c;
}
cur_tb = table_name;// renew only when everything is right.
}
freopen("CON", "r", stdin);
cache.SetElemNum(cache.elem_num()+1);
cache.isDelete.resize(cache.elem_num_ * 2);
#ifdef _TEST5_
WATCH();
#endif
freopen(file_path.c_str(), "w", stdout);
cache.PrintInfo();
freopen("CON", "w", stdout);
cout << "Query OK, 1 row affected ";
time_cnt::end(); cout << endl;
}
/**
* @author: fhn
* @date: 4/27
* @description: execute Select sentence cmds.
in order to simplify the output of charts, we input all the tableinfo once a time.
* @version: v1.0
* v1.1: about rereading: if this is the first time, the read and the unknown elem_num is compatible
*/
void Select(string table_name, vector<string> item, Clause where)
{
if (!cur_db.size())
{ cout << "#ERROR 1046 (3D000): No database selected" << endl << endl; return; }
string file_path = cur_db + "\\" + table_name;
if (!exist(file_path))
{ cout << "#ERROR table " << table_name << " doesn't exists" << endl << endl; return; }
#ifdef _TEST4_
std::clog << "current table name is " << cur_tb << endl;
#endif
if (cur_tb != table_name)
{
freopen(file_path.c_str(), "r", stdin);
cache.InitRead();
freopen("CON", "r", stdin); //here we didn't get the stdin back, which caused a bad loops
if (broken)
{ cur_tb = ""; cout << "#ERROR Data broken" << endl << endl; return;}
cur_tb = table_name;//evething has been put in, then renew;(somehow different from the Insert()): possibly because we need to output once a time
}
#ifdef _LOC_
std::clog << "current table name is " << cur_tb << endl;
#endif
//select columns to be put
vector<int> item_col(cache.col_num()); int tmp, sel_cnt = 0;
#ifdef _LOC_
std::clog << item[0] << endl;
#endif
if (item[0] == "*")
for (int i = 0; i < cache.col_num(); i++) item_col[i] = i, sel_cnt++;
else for (int i = 0; i < item.size(); i++)
{
tmp = cache.FindCol(item[i]);
if (tmp != -1)
item_col[i] = tmp, sel_cnt++;
else
{ cout << "#ERROR Col not found" << endl << endl; return;}
}
if (sel_cnt == 0)
{
cout << "empty set" << endl;
return;
}
#ifdef _LOC_
if (item[0] == "*")
for (int i = 0; i < item_col.size(); i++)
cout << "item_col[" << i << "] = " << item_col[i]<< endl;
std::clog << sel_cnt << " col(s) selected" << endl;
#endif
vector<int> col_len(sel_cnt);
for (int i = 0; i < sel_cnt; i++)
{
if (!cache.col_info_[item_col[i]].second)
col_len[i] = 10;
else col_len[i] = (cache.col_info_[item_col[i]].first.size() > cache.col_info_[item_col[i]].second
? cache.col_info_[item_col[i]].first.size() : cache.col_info_[item_col[i]].second) + 1;
}
//select rows to be put
if (!where.op.size()) // no more rules
{
PrintHead(col_len, item_col);
for (int i = 0; i < cache.elem_num(); i++)
PrintLine(col_len, item_col, i);
PrintTail(col_len);
if (cache.elem_num())
cout << cache.elem_num() << " rows in set ";
else cout << "empty set ";
time_cnt::end();
cout << endl;
}
else if (where.op.size())// where branch
{
int col_base, hasnt = 1;
col_base = cache.FindCol(where.name);
if (col_base == -1) { cout << "#ERROR Invalid name in where clause." << endl << endl; return; } //assignment and judge......
if (cache.col_len(col_base) && !trim(where.value))
{ cout << "#ERROR in Select(): where varchar value wrong" << endl << endl; return; }
for (auto it : where.value)
if (!isalnum(it))
{ cout << "#ERROR Invalid value in where clause." << endl << endl; return; }
bool (*p)(const string&, const string&);//choose the right mode to act as the cmp function
if (where.op == "=")
p = equal_to;
else if (where.op == ">")
p = greater;
else if (where.op == ">=")
p = greater_equal;
else if (where.op == "<")
p = less;
else if (where.op == "<=")
p = less_equal;
else if (where.op == "!=")
p = not_equal_to;
else
{ cout << "#ERROR unknown where operator type when Select()" << endl << endl; return;}
int row_cnt = 0;
for (auto it : cache.elem_info_[col_base])
if (p(it.first, where.value))
{
if (hasnt)
PrintHead(col_len, item_col);
PrintLine(col_len, item_col, it.second);
row_cnt++;
hasnt = 0;
}
if (hasnt)
cout << "Empty set ";
else
{
PrintTail(col_len);
cout << row_cnt << "row(s) in set ";
}
time_cnt::end(); cout << endl;
}
else cout << "#ERROR select wrong in both modes" << endl << endl;
#ifdef _TEST5_
WATCH();
#endif
}
/**
* @author: fhn
* @date: 5/4
* @description: print the upper chart line of a table and the gauge outfit.
* @version:
*/
void PrintHead(vector<int> col_len, vector<int> item_col)
{
cout << " ┌";
for (int i = 0; i < col_len.size(); i++)
{
for (int j = 0; j < col_len[i]; j++) cout << "─";
if (i < col_len.size() - 1)
cout << "┬";
else cout << "┐";
}
cout << endl;
cout << " │";
for (int i = 0; i < col_len.size(); i++)
{
int tmp = col_len[i]-cache.col_info_[item_col[i]].first.size();
for (int j = 0; j < tmp/2; j++) cout << ' ';
if (tmp%2) cout << ' ';
cout << cache.col_info_[item_col[i]].first;
for (int j = 0; j < tmp/2; j++) cout << ' ';
cout << "│";
}
cout << endl;
}
/**
* @author: fhn
* @date: 5/4
* @description: in select(), print the body info of table. upper chart line and a row of info
* @version: v2.0 :
* v2.1: add the quotation mark of varchar
*/
void PrintLine(vector<int> col_len, vector<int> item_col, int row_index)
{
cout << " ├";
for (int i = 0; i < col_len.size(); i++)
{
for (int j = 0; j < col_len[i]; j++) cout << "─";
if (i < col_len.size() - 1)
cout << "┼";
else cout << "┤";
}
cout << endl;
cout << " │";
for (int i = 0; i < col_len.size(); i++)
{
int tmp = col_len[i]-(cache.GetElem(item_col[i], row_index)).first.size();
if (cache.col_len(item_col[i]))// if it's varchar
tmp -= 2;
for (int j = 0; j < tmp/2; j++) cout << ' ';
if (tmp%2) cout << ' ';
if (cache.col_len(item_col[i]))// if it's varchar
cout << '\"' << cache.GetElem(item_col[i], row_index).first << '\"';
else cout << cache.GetElem(item_col[i], row_index).first;
for (int j = 0; j < tmp/2; j++) cout << ' ';
cout << "│";
}
cout << endl;
}
void PrintTail(vector<int> col_len)
{
cout << " └";
for (int i = 0; i < col_len.size(); i++)
{
for (int j = 0; j < col_len[i]; j++) cout << "─";
if (i < col_len.size() - 1)
cout << "┴";
else cout << "┘";
}
cout << endl;
}
//--------------------------------------------------------------------------------------------
void Update(string table_name, string col_name, string newvalue, Clause where)
{
if (!cur_db.size())
{ cout << "#ERROR 1046 (3D000): No database selected" << endl << endl; return; }
string file_path = cur_db + "\\" + table_name;
if (!exist(file_path))
{ cout << "#ERROR in Update(): Table " << table_name << " doesn't exists" << endl << endl; return; }
if (cur_tb != table_name)
{
freopen(file_path.c_str(), "r", stdin);
cache.InitRead();
freopen("CON", "r", stdin);
if (broken)
{ cur_tb = ""; cout << "#ERROR Data broken" << endl << endl; return;}
cur_tb = table_name;
}
#ifdef _TEST4_
cout << "in update: " << "table_name: " << table_name << "cur_tb: " << cur_tb << endl;
#endif
int nc = cache.FindCol(col_name);
if (nc == -1)
{ cout << "#ERROR in Update(): col to update invalid" << endl << endl; return;}
if (cache.col_len(nc) && !trim(newvalue))
{ cout << "#ERROR in Update(): varchar newvalue wrong" << endl << endl; return; }
int bc = cache.FindCol(where.name);
if (bc == -1) { cout << "col to judge (update) invalid " << endl << endl; return; }
if (cache.col_len(bc) && !trim(where.value))
{ cout << "#ERROR in Update()'s where clause: varchar value wrong" << endl << endl; return; }
bool (*p)(const string&, const string&);
if (where.op == "=")
p = equal_to;
else if (where.op == ">")
p = greater;
else if (where.op == ">=")
p = greater_equal;
else if (where.op == "<")
p = less;
else if (where.op == "<=")
p = less_equal;
else if (where.op == "!=")
p = not_equal_to;
else
{ cout << "#ERROR unknown where operator type when Select()" << endl << endl; return; }
int row_cnt = 0;
for (auto it : cache.elem_info_[bc])
if (p(it.first, where.value))
if (cache.GetElem(nc, it.second).first != newvalue)
cache.SetElem(nc, it.second, newvalue), row_cnt++;
freopen(file_path.c_str(), "w", stdout);
cache.PrintInfo();
freopen("CON", "w", stdout);
cout << "Query OK, " << row_cnt << " rows affected ";
time_cnt::end();
//TimeCount();
cout << endl;
cout << "Rows matched: " << row_cnt << " Changed: 0 Warnings: 0 ";
cout << endl << endl;
}
/**
* @author: fhn
* @date: 5/6
* @description: executing the deleting action
* @version: v3.0
*/
void Delete(string table_name, Clause where)
{
if (!cur_db.size())
{ cout << "#ERROR 1046 (3D000): No database selected" << endl << endl; return; }
string file_path = cur_db + "\\" + table_name;
if (!exist(file_path))
{ cout << "#ERROR table " << table_name << " doesn't exists" << endl << endl; return; }
#ifdef _TEST4_
cout << "in delete: " << "table_name: " << table_name << " cur_tb: " << cur_tb << endl;
#endif
if (cur_tb != table_name)
{
freopen(file_path.c_str(), "r", stdin);
cache.InitRead();
freopen("CON", "r", stdin);
if (broken)
{ cur_tb = ""; cout << "#ERROR Data broken" << endl << endl; return;}
// cur_tb = table_name;//unnecessary daylight
}
int bc = cache.FindCol(where.name);
if (bc == -1) { cout << "#ERROR in Delete(): col to judge (delete) invalid " << endl << endl; return; }
if (cache.col_len(bc) && !trim(where.value))
{ cout << "#ERROR in Delete()'s where: varchar value wrong" << endl << endl; return; }
bool (*p)(const string&, const string&);
if (where.op == "=")
p = equal_to;
else if (where.op == ">")
p = greater;
else if (where.op == ">=")
p = greater_equal;
else if (where.op == "<")
p = less;
else if (where.op == "<=")
p = less_equal;
else if (where.op == "!=")
p = not_equal_to;
else
{ cout << "#ERROR unknown where operator type when Select()" << endl << endl; return;}
int row_cnt = 0;
for (auto it : cache.elem_info_[bc])
if (p(it.first,where.value))
cache.EraseElem(it.second), row_cnt++;
freopen(file_path.c_str(), "w", stdout);
cache.PrintInfo();
freopen("CON", "w", stdout);
cur_tb = "";//because the elem in cache is quite different from that in the file, we'd better do this.
for (auto it : cache.isDelete)
it = false;
cout << "Query OK, " << row_cnt << " rows affected ";
time_cnt::end(); cout << endl;
}
}