This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckCommands.h
168 lines (159 loc) · 3.64 KB
/
CheckCommands.h
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
#pragma once
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include "Constants.h"
#include "Exception.h"
class CheckCommands {
public:
static Constants getCommandType(string line) {
for (size_t i = 0; i < line.size(); ++i) {
line[i] = toupper(line[i]);
}
if (line.substr(0, 13) == "CREATE TABLE ") {
return Constants::CREATE_TABLE;
}
if (line.substr(0, 11) == "DROP TABLE ") {
return Constants::DROP_TABLE;
}
if (line.substr(0, 8) == " VALUES ") {
return Constants::VALUES;
}
if (line.substr(0, 6) == " FROM ") {
return Constants::FROM;
}
if (line.substr(0, 7) == " WHERE ") {
return Constants::WHERE;
}
if (line.substr(0, 5) == " SET ") {
return Constants::SET;
}
if (line.substr(0, 14) == "DISPLAY TABLE ") {
return Constants::DISPLAY_TABLE;
}
if (line.substr(0, 12) == "INSERT INTO ") {
return Constants::INSERT_INTO;
}
if (line.substr(0, 12) == "DELETE FROM ") {
return Constants::DELETE_FROM;
}
if (line.substr(0, 7) == "SELECT ") {
return Constants::SELECT;
}
if (line.substr(0, 7) == "UPDATE ") {
return Constants::UPDATE;
}
return Constants::NONE;
}
static void parenthesesCheck(string line, int& i, int commasFound, string& fieldName, string& fieldType, string& defaultValue) {
while (line.at(i - 1) != ')' && i <= line.length()) {
while (line.at(i) != ',' && line.at(i) != ')') {
switch (commasFound) {
case 0:
{
fieldName += line.at(i);
break;
}
case 1:
{
fieldType += line.at(i);
break;
}
case 2:
{
defaultValue += line.at(i);
break;
}
}
i++;
}
if (line.at(i) != ',' && line.at(i) != ')') {
throw SQLException("The length is bigger than expected");
}
else {
commasFound++;
i++;
}
}
}
static void trimmer(string& line) {
for (int i = 0; i < line.length(); i++) {
if (line.at(i) == ' ') {
line = line.substr(0, i) + line.substr(i + 1);
}
}
}
static string getTableOrFieldName(string line) {
int i = 0;
string tableName = "";
while (i < line.length() && line.at(i) != ' ') {
tableName += line.at(i);
i++;
}
return tableName;
}
static void processBeforeCommand(string& line, string& tableOrFieldName, Constants command) {
removeCommandFromLine(line, command);
switch (command) {
case Constants::CREATE_TABLE:
case Constants::DELETE_FROM:
case Constants::DISPLAY_TABLE:
case Constants::FROM:
case Constants::UPDATE:
case Constants::SET:
case Constants::DROP_TABLE:
case Constants::INSERT_INTO:
case Constants::SELECT:
try {
tableOrFieldName = getTableOrFieldName(line);
line = line.substr(tableOrFieldName.size());
}
catch (SQLException e) {
cout << "-------------";
cout << endl << e.what() << endl;
}
break;
case Constants::WHERE:
case Constants::VALUES:
break;
}
}
static void removeCommandFromLine(string& line, Constants command) {
switch (command) {
case Constants::CREATE_TABLE:
line = line.substr(13);
break;
case Constants::DROP_TABLE:
line = line.substr(11);
break;
case Constants::INSERT_INTO:
line = line.substr(12);
break;
case Constants::UPDATE:
line = line.substr(7);
break;
case Constants::DISPLAY_TABLE:
line = line.substr(14);
break;
case Constants::DELETE_FROM:
line = line.substr(12);
break;
case Constants::SELECT:
line = line.substr(7);
break;
case Constants::VALUES:
line = line.substr(8);
break;
case Constants::SET:
line = line.substr(5);
break;
case Constants::FROM:
line = line.substr(6);
break;
case Constants::WHERE:
line = line.substr(7);
break;
}
}
};