-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
1146 lines (998 loc) · 27.5 KB
/
server.cpp
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "server.h"
void Server::start() {
// 加载用户名-密码文件
loadAccountInfo();
string buffer = "";
//不输出error之外的信息
utils::logging::setLogLevel(utils::logging::LOG_LEVEL_ERROR);//静默 utils::logging::LOG_LEVEL_SILENT
while (true) {
// 打印提示信息
printPrompt();
// 获取用户输入
string input;
getline(cin, input);
size_t length = input.length();
if (!input.ends_with(";")){
// 存入缓存
buffer = buffer + input;
// 限制长度
if (buffer.size() > 1000) {
cerr << "Command is too long and must end with ; Please check your input." << endl;
buffer = "";
}
continue;
}
else {
// 存入缓存
input = buffer + input.substr(0, length-1);
// 清空缓存
buffer = "";
}
// 创建用户(数据库)
if (input.starts_with("CREATE USER")) {
dealCreateUser(input);
}
// 删除用户(数据库)
else if (input.starts_with("DROP USER")) {
dealDropUser(input);
}
// 显示当前存在的用户
else if (input == "SHOW USERS") {
dealShowUsers();
}
// 启动指定用户(数据库)
else if (input.starts_with("USE USER")) {
dealUseUser(input);
}
// 查看当前数据库中所有的表
else if (input == "SHOW TABLES") {
dealShowTables();
}
// 查看当前数据库中所有的视图
else if (input == "SHOW VIEWS") {
dealShowViews();
}
// 建表
else if (input.starts_with("CREATE TABLE")) {
dealCreateTable(input);
}
// 删除表
else if (input.starts_with("DROP TABLE")) {
dealDropTable(input);
}
// 打印表的属性
else if (input.starts_with("SHOW TABLE")) {
if (input.ends_with("COLUMNS")) {
dealShowColumns(input);
}
else if (input.ends_with("PRIMARYKEY")) {
dealShowPrimaryKey(input);
}
}
// 插入数据
else if (input.starts_with("INSERT INTO TABLE")) {
dealInsert(input);
}
// 查看数据
else if (input.starts_with("SELECT")) {
dealSelect(input);
}
// 删除数据
else if (input.starts_with("DELETE FROM TABLE")) {
dealDelete(input);
}
// 保存数据
else if (input.starts_with("SAVE TABLE")) {
dealSaveTable(input);
}
// 导入数据
else if (input.starts_with("IMPORT TABLE")) {
dealImportTable(input);
}
// 查看图片
else if (input.starts_with("SHOW PICTURE")) {
dealShowPicture(input);
}
// 查看流媒体
else if (input.starts_with("SHOW VIDEO")) {
dealShowVideo(input);
}
else if (input == "HELP") {
dealHelp();
}
// 退出数据库
else if (input == "EXIT") {
std::cout << "Bye!" << endl;
break;
}
// 非法输入
else {
cerr << "Unrecognized command. Please check your input." << endl;
}
}
}
void Server::printPrompt() {
if (database != nullptr) {
cout << database->owner << " > ";
}
else {
cout << "TinyDB > ";
}
}
// 处理新建用户请求
bool Server::dealCreateUser(const string& input) {
vector<string> words;
parseSentence(input, words);
if (words.size() == 5 && words[3] == "PASSWORD") {
string username = words[2];
if (accountInfo.count(username)) {
// 用户已存在
cerr << "Create user error: User already exist." << endl;
return false;
}
// 保存用户名密码信息
string password = sha256(words[4]);
accountInfo[username] = password;
saveAccountInfo();
cout << "User created successfully." << endl;
// 新建数据库路径
string path = "database\\" + username;
// 将 std::string 转换为宽字符字符串
int size_needed = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, NULL, 0);
wstring wideFolderPath(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, &wideFolderPath[0], size_needed);
// 使用CreateDirectory函数创建目录
if (CreateDirectory(wideFolderPath.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError()) {
cout << "User database created successfully." << endl;
}
else {
cerr << "Create user database error: Error creating folder." << endl;
return false;
}
}
else {
// SQL语句格式不正确
cerr << "Create user error: Invalid syntex." << endl;
return false;
}
return true;
}
// 处理删除用户请求
bool Server::dealDropUser(const string& input) {
vector<string> words;
parseSentence(input, words);
if (words.size() == 5 && words[3] == "PASSWORD") {
string username = words[2];
if (!accountInfo.count(username)) {
// 用户不存在
cerr << "Drop user error: User doesn't exist." << endl;
return false;
}
string password = sha256(words[4]);
if (password != accountInfo[username]) {
// 密码不正确
cerr << "Drop user error: Wrong password." << endl;
return false;
}
// 删除用户
accountInfo.erase(username);
saveAccountInfo();
cout << "Drop user successfully." << endl;
// 删除数据库
string path = "database\\" + username;
// 将 std::string 转换为宽字符字符串
int size_needed = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, NULL, 0);
wstring wideFolderPath(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, &wideFolderPath[0], size_needed);
SHFILEOPSTRUCT fileOp;
ZeroMemory(&fileOp, sizeof(SHFILEOPSTRUCT));
fileOp.wFunc = FO_DELETE;
fileOp.pFrom = wideFolderPath.c_str();
fileOp.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI | FOF_WANTNUKEWARNING;
if (SHFileOperation(&fileOp) == 0) {
cout << "User database removed successfully." << endl;
}
else {
cerr << "Drop user error: Error removing user database." << endl;
return false;
}
// 如果删除的是当前用户,释放database并指向nullptr
if (database != nullptr && username == database->owner) {
delete database;
database = nullptr;
}
}
else {
// SQL语句格式不正确
cerr << "Drop user error: Invalid syntex." << endl;
return false;
}
return true;
}
// 执行启用指定用户(数据库)
bool Server::dealUseUser(const string& input) {
vector<string> words;
parseSentence(input, words);
if (words.size() == 5 && words[3] == "PASSWORD") {
string username = words[2];
if (!accountInfo.count(username)) {
// 用户不存在
cerr << "Use user error: User doesn't exist." << endl;
return false;
}
if (database != nullptr && username == database->owner) {
// 要启用的用户就是当前用户
cerr << "Use user error: You are already using this user." << endl;
return false;
}
string password = sha256(words[4]);
if (password != accountInfo[username]) {
// 密码不正确
cerr << "Use user error: Wrong password." << endl;
return false;
}
// 切换数据库
delete database;
database = new Database(username);
cout << "Using user " << username << endl;
}
else {
// SQL语句格式不正确
cerr << "Use user error: Invalid syntex." << endl;
return false;
}
return true;
}
// 建表
bool Server::dealCreateTable(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Create table error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size < 6) {
cerr << "Create table error: Wrong syntex." << endl;
return false;
}
if (words[size - 2] == ("PRIMARYKEY")) {
string tablename = words[2]+".table";
if (database->tableNames.count(tablename)) {
// 表名和已有的表重复
cerr << "Create table error: Table name already exists." << endl;
return false;
}
// 解析主键
string primaryKey = words[size - 1];
// 解析用户想要定义的dataType和column,同时设置属性到存储下标的映射
vector<string> dataTypeString;
vector<string> column;
unordered_map<string, size_t> column2index;
for (int i = 3; i < size - 2; i++) {
size_t pos = words[i].find(':');
if (pos == string::npos) {
// 属性定义的格式不正确
cerr << "Create table error: Invalid column definition." << endl;
return false;
}
string col = words[i].substr(0, pos);
if (col.length() == 0) {
// 属性名称不能为空
cerr << "Create table error: Column value can't be NULL." << endl;
return false;
}
string dts = words[i].substr(pos+1, words[i].length()-1-pos);
if (dts != "INT" && dts != "STRING" && dts != "FLOAT") {
cerr << "Create table error: Invalid data type." << endl;
return false;
}
dataTypeString.push_back(dts);
column.push_back(col);
column2index[col] = i - 3;
}
// 规定:数据库表主键放在第一位
auto it = find(column.begin(), column.end(), primaryKey);
if (it == column.end()) {
// 没有找到定义主键的属性,语句错误
cerr << "Create table error: Wrong primary key name." << endl;
return false;
}
// 从迭代器转换为下标
size_t index = distance(column.begin(), it);
string tmpCol = column[0];
swap(column[index], column[0]);
swap(dataTypeString[index], dataTypeString[0]);
column2index[tmpCol] = index;
column2index[primaryKey] = 0;
// 创建表文件并写入表头信息
ofstream file("database\\" + database->owner + "\\" + tablename);
if (file.is_open()) {
for (const auto& dts : dataTypeString) {
file << dts << " ";
}
file << "\n";
for (const auto& col : column) {
file << col << " ";
}
file << "\n";
for (const auto& col : column) {
file << column2index[col] << " ";
}
file << "\n";
file.close();
cout << "Table created successfully" << endl;
database->tableNames.insert(tablename);
}
else {
cerr << "Create table error: Unable to open table file." << endl;
return false;
}
}
else {
// 必须指定主键(且必须为int类型)
cerr << "Create table error: Primary key must be assigned (int type only)." << endl;
return false;
}
return true;
}
// 删除表
bool Server::dealDropTable(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Drop table error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
if (words.size() == 3) {
string tablename = words[2];
if (!database->tableNames.count(tablename + ".table")) {
// 表名不存在
cerr << "Drop table error: Wrong table name." << endl;
return false;
}
if (database->openedTables.count(tablename)) {
// 如果还未关闭,保存
database->saveTable(tablename);
}
// 删除表(使用remove函数)
if (remove(("database\\" + database->owner + "\\" + tablename + ".table").c_str()) != 0) {
cerr << "Drop table error: Can't delete table." << endl;
return false;
}
else {
database->tableNames.erase(tablename + ".table");
cout << "Table successfully deleted" << endl;
return true;
}
}
else {
cerr << "Drop table error: Wrong syntex." << endl;
return false;
}
}
// 打印表的属性
bool Server::dealShowColumns(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Show columns error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
if (words.size() != 4) {
cerr << "Show columns error: Invalid syntex." << endl;
return false;
}
string tablename = words[2];
if (!database->loadTable(tablename)) {
cerr << "Show columns error: Failed loading table." << endl;
return false;
};
Table* table = database->openedTables[tablename + ".table"];
for (const auto& col : table->column) {
cout << col << " ";
}
cout << endl;
for (const auto& type : table->dataType) {
string typeString = "";
if (type == TYPE_INT) {
typeString = "INT";
}
else if (type == TYPE_FLOAT) {
typeString = "FLOAT";
}
else if (type == TYPE_STRING) {
typeString = "STRING";
}
cout << typeString << " ";
}
cout << endl;
// 释放空间
delete table;
// 从openedTables里删除该表
database->openedTables.erase(tablename + ".table");
return true;
}
// 打印表的主键
bool Server::dealShowPrimaryKey(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Show primary key error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
if (words.size() != 4) {
cerr << "Show primary key error: Invalid syntex." << endl;
return false;
}
string tablename = words[2];
if (!database->loadTable(tablename)) {
cerr << "Show primary key error: Failed loading table." << endl;
return false;
};
Table* table = database->openedTables[tablename + ".table"];
cout << table->column[0] << endl;
// 释放空间
delete table;
// 从openedTables里删除该表
database->openedTables.erase(tablename + ".table");
return true;
}
// 插入数据
bool Server::dealInsert(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Insert into table error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size < 5) {
cerr << "Insert into table error: Wrong syntex." << endl;
return false;
}
string tablename = words[3];
if (!database->loadTable(tablename)) {
cerr << "Insert into table error: Failed loading table." << endl;
return false;
}
Table* table = database->openedTables[tablename + ".table"];
if (table->numCols != size - 4) {
// 插入数量和表中属性数量不等
cerr << "Insert into table error: Wrong insert value amount." << endl;
return false;
}
vector<string> row(table->numCols, "");
for (int i = 4; i < size; i++) {
size_t pos = words[i].find(':');
if (pos == string::npos) {
// 属性赋值的格式不正确
cerr << "Insert into table error: Invalid assignment." << endl;
return false;
}
string col = words[i].substr(0, pos);
if (col.length() == 0) {
// 属性名称不能为空
cerr << "Insert into table error: Column value can't be NULL." << endl;
return false;
}
string value = words[i].substr(pos + 1, words[i].length() - 1 - pos);
if (value.length() == 0) {
// 属性值不能为空
cerr << "Insert into table error: Insert value can't be NULL." << endl;
return false;
}
/* TODO: 检查对应数据格式是否正确 */
row[table->column2index[col]] = value;
}
if (!table->insert(stoi(row[0]), row)) {
// 主键值重复
cerr << "Insert into table error: Duplicated primary key." << endl;
return false;
}
// 保存修改
if (database->saveTable(tablename)) {
cout << "Insert into table successfully." << endl;
}
return true;
}
bool Server::dealSelect(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Select error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size >= 5 && words[2] == "FROM" && words[3] == "TABLE") {
string target = words[1];
string tablename = words[4];
if (!database->loadTable(tablename)) {
cerr << "Select error: Failed loading table." << endl;
return false;
}
Table* table = database->openedTables[tablename + ".table"];
for (const auto& col : table->column) {
cout << col << " ";
}
cout << endl;
if (target == "*") {
// 查看表中的全部数据
vector<vector<string>> rows;
table->getRows(rows);
for (auto& row : rows) {
for (auto& item : row) {
cout << item << " ";
}
cout << endl;
}
}
else {
cerr << "Select error: Wrong select target." << endl;
return false;
}
// 释放空间
delete table;
// 从openedTables里删除该表
database->openedTables.erase(tablename + ".table");
}
else {
cerr << "Select error: Invalid syntex." << endl;
return false;
}
return true;
}
// 删除数据
bool Server::dealDelete(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Delete error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size == 6 && words[size - 2] == "WHERE") {
string tablename = words[3];
string condition = words[5];
if (!database->loadTable(tablename)) {
cerr << "Delete error: Failed loading table." << endl;
return false;
}
Table* table = database->openedTables[tablename + ".table"];
// 检查条件是否正确
size_t pkPos = condition.find(':');
if (pkPos == string::npos) {
// 属性赋值的格式不正确
cerr << "Delete error: Invalid condition." << endl;
return false;
}
string pkCol = condition.substr(0, pkPos);
if (pkCol.length() == 0) {
// key不能为空
cerr << "Delete error: Condition key can't be NULL." << endl;
return false;
}
if (pkCol != table->column[0]) {
// key必须是主键属性
cerr << "Delete error: Condition key must be primary key." << endl;
return false;
}
string pkValue = condition.substr(pkPos + 1, condition.length() - 1 - pkPos);
if (pkValue.length() == 0) {
// 主键值不能为空
cerr << "Delete error: Primary key value can't be NULL." << endl;
return false;
}
int pk = stoi(pkValue);
if (!table->get(pk)) {
// 主键值不存在
cerr << "Delete error: Primary key value doesn't exist." << endl;
return false;
}
// 删除
table->remove(pk);
// 保存修改
if (database->saveTable(tablename)) {
cout << "Delete from table successfully." << endl;
}
}
else {
cerr << "Delete error: Invalid syntex." << endl;
return false;
}
return true;
}
// 保存表信息到指定地址
bool Server::dealSaveTable(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Save table error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size == 7 && words[3] == "PATH" && words[5] == "AS") {
string tablename = words[2];
string path = words[4];
string format = words[6];
if (!filesystem::exists(path)) {
cerr << "Save table error: Save path doesn't exist." << endl;
return false;
}
if (format != "txt" && format != "csv") {
cerr << "Save table error: Table can only be saved as txt or csv." << endl;
return false;
}
if (!database->loadTable(tablename)) {
cerr << "Save table error: Failed loading table." << endl;
return false;
}
Table* table = database->openedTables[tablename + ".table"];
vector<vector<string>> rows;
table->getRows(rows);
// 保存到目标路径
if (!path.ends_with("\\")) {
path = path + "\\";
}
ofstream outputFile(path + tablename + "." + format);
char delimiter;
if (format == "txt")
delimiter = ' ';
else
delimiter = ',';
if (outputFile.is_open()) {
for (size_t i = 0; i < table->column.size(); i++) {
outputFile << table->column[i];
if (i < table->column.size() - 1) {
outputFile << delimiter;
}
}
outputFile << "\n";
for (const vector<string>& row : rows) {
for (size_t i = 0; i < row.size(); i++) {
outputFile << row[i];
if (i < row.size() - 1) {
outputFile << delimiter;
}
}
outputFile << "\n";
}
outputFile.close();
cout << "Table saved to path " << path + tablename + "." + format << " successfully." << endl;
// 释放空间
delete table;
// 从openedTables里删除该表
database->openedTables.erase(tablename + ".table");
}
else {
cerr << "Save table error: Unable to open table file." << endl;
// 释放空间
delete table;
// 从openedTables里删除该表
database->openedTables.erase(tablename + ".table");
return false;
}
}
else {
cerr << "Save table error: Invalid syntex." << endl;
return false;
}
return true;
}
// 导入表信息
bool Server::dealImportTable(const string& input){
if (database == nullptr) {
// 未选定数据库
cerr << "Import table error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size == 7 && words[3] == "PATH" && words[5] == "BY") {
string tablename = words[2];
string path = words[4];
string format = words[6];
if (!filesystem::exists(path)) {
cerr << "Import table error: Import path doesn't exist." << endl;
return false;
}
if (format != "txt" && format != "csv") {
cerr << "Import table error: Table can only be import by txt or csv." << endl;
return false;
}
// 按行将文本读取到lines中
vector<string> lines; // 存储每行的字符串
ifstream inputFile(path);
if (inputFile.is_open()) {
string line;
while (getline(inputFile, line)) {
lines.push_back(line); // 将每行添加到字符串向量中
}
inputFile.close(); // 关闭文件
}
else {
cerr << "Import table error: File can't be opened" << endl;
return false;
}
// 分隔符
char delimiter;
if (format == "txt")
delimiter = ' ';
else
delimiter = ',';
string fullPath = "database\\" + database->owner + "\\" + tablename + ".table";
if (!filesystem::exists(fullPath)) {
cerr << "Import table error: Target table you select to insert doesn't exist." << endl;
return false;
}
// 第一行为列名
string column;
vector<string> columns;
parseSentence(lines[0], columns, delimiter);
size_t num_col = columns.size();
//遍历每一行,从第二行开始
for (size_t i = 1; i < lines.size(); i++) {
//分割
vector<string> row;
parseSentence(lines[i], row, delimiter);
if (row.size() != num_col) {
cerr << "Import table error: Wrong number of columns" << endl;
return false;
}
//格式INSERT INTO TABLE t_nzx id:1 name:NZX;
string sql = "INSERT INTO TABLE " + tablename;
for (size_t j = 0; j < num_col; j++) {
sql += " " + columns[j] + ":" + row[j];
}
if (!dealInsert(sql)) {
cerr << "Import table error: Can't insert into table." << endl;
return false;
}
}
cout << "Import TABLE " + tablename + " from " + path + " format " + format + " sucessfully." << endl;
}
else {
cerr << "Import table error: Invalid syntex." << endl;
return false;
}
return true;
}
// 显示当前存在的用户
bool Server::dealShowUsers() {
for (const auto& account : accountInfo) {
cout << account.first << endl;
}
return true;
}
// 查看当前数据库中所有的表
bool Server::dealShowTables() {
if (database == nullptr) {
// 未选定数据库
cerr << "Show tables error: You have to select a database first." << endl;
return false;
}
for (const string& tableName : database->tableNames) {
// 去除后缀".table"
cout << tableName.substr(0, tableName.length() - 6) << endl;
}
return true;
}
// 查看当前数据库中所有的视图
bool Server::dealShowViews() {
if (database == nullptr) {
// 未选定数据库
cerr << "Show views error: You have to select a database first." << endl;
return false;
}
for (const string& viewName : database->viewNames) {
// 去除后缀".view"
cout << viewName.substr(0, viewName.length() - 5) << endl;
}
return true;
}
// 查看图片
bool Server::dealShowPicture(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Show picture error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size == 3) {
string filename = words[2];
string filePath = "database\\" + database->owner + "\\" + filename;
if (!filesystem::exists(filePath)) {
cerr << "Show picture error: File doesn't exist." << endl;
return false;
}
// 创建类对象 COpenCVWindowExt
COpenCVWindowExt img_window("img");
img_window.ImRead(filePath);
waitKey(0);
//按任意键关闭窗口
destroyAllWindows();
}
else {
cerr << "Show picture error: Invalid syntex." << endl;
return false;
}
return true;
}
// 查看流媒体
bool Server::dealShowVideo(const string& input) {
if (database == nullptr) {
// 未选定数据库
cerr << "Show video error: You have to select a database first." << endl;
return false;
}
vector<string> words;
parseSentence(input, words);
size_t size = words.size();
if (size == 3) {
string filename = words[2];
string filePath = "database\\" + database->owner + "\\" + filename;
// 打开视频
VideoCapture cap(filePath);
// 检查视频是否成功打开
if (!cap.isOpened()) {
cerr << "Show video error: Could not open or find the video." << endl;
return false;
}
// 读取和显示视频帧
Mat frame;
bool isPaused = false;
while (true) {
if (!isPaused)
{
cap.read(frame);
if (frame.empty()) {
break;
}
imshow("Video", frame);
}
// 按下空格暂停
auto key = waitKey(30);
if (key == 32)
isPaused = !isPaused;