-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprequel.js
159 lines (127 loc) · 3.36 KB
/
prequel.js
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
import { commands } from "./commands.js";
import { EMPTYOBJECT } from "./commands.js";
const showDataBase = (database) => {
console.log("\nDATABASE:");
console.log("*".repeat(10));
console.table(database.tables);
console.log("*".repeat(10));
return database;
};
const dropTable = (database, tableName, tableLocation) => {
console.log(tableName, "table dropped");
const tables = database.tables.toSpliced(tableLocation, 1);
return { database, tables };
};
const internalCommands = {
SHOWDB: showDataBase,
DROP: dropTable,
};
const differringArgumentCommands = [
"CREATE",
"SHOWDB",
"DROP",
"SELECT",
"DELETE",
"TRUNCATE",
];
const validateCommands = (database, command, tableName) => {
if (command === "" || command === "EXIT") {
return false;
}
if (!(command in commands) && !(command in internalCommands)) {
console.log("Invalid Command");
return false;
}
const doesTableExist =
database.tables.findIndex(isMatchingTable(tableName)) !== -1;
if (command !== "CREATE" && command !== "SHOWDB" && !doesTableExist) {
console.log(tableName + " table does not exist");
return false;
}
return true;
};
const validateArguments = (command, commandArgs, condition) => {
if (
!differringArgumentCommands.includes(command) &&
commandArgs.length === 0
) {
console.log("Invalid Argument Length");
return false;
}
if (condition && condition.split(" ").length !== 3) {
console.log("Invalid condtion");
return false;
}
return true;
};
const validateQuery = (
database,
tableName,
commandArgs,
command,
condition
) => {
return (
validateCommands(database, command, tableName) &&
validateArguments(command, commandArgs, condition)
);
};
const isMatchingTable = (tableName) => {
return (element) => element.tableName === tableName;
};
const executeQuery = (database, tableName, commandArgs, command, condition) => {
const newDB = { ...database };
const tableLocation = newDB.tables.findIndex(isMatchingTable(tableName));
if (command in internalCommands) {
return internalCommands[command](newDB, tableName, tableLocation);
}
const originalTable = newDB.tables[tableLocation];
const isExistingTable = tableLocation !== -1;
const newTable = commands[command](
originalTable,
tableName,
commandArgs,
condition,
isExistingTable
);
if (!isExistingTable && newTable !== EMPTYOBJECT) {
newDB.tables.push(newTable);
return newDB;
}
if (newTable !== EMPTYOBJECT) {
newDB.tables[tableLocation] = newTable;
return newDB;
}
return database;
};
const splitQuery = (query) => {
const whereSplit = query.split("WHERE ");
const commandArgs = whereSplit[0].trimEnd().split(" ");
const [command, tableName] = commandArgs;
// const tableName = commandArgs[1];
const condition = whereSplit[1];
return [
tableName,
commandArgs.slice(2, commandArgs.length),
command,
condition,
];
};
const runMYPQL = (database, query) => {
if (query === "EXIT") {
return 0;
}
const queryInParts = splitQuery(query);
if (validateQuery(database, ...queryInParts)) {
const updatedDB = executeQuery(database, ...queryInParts);
return runMYPQL(updatedDB, prompt("MYPQL>"));
}
return runMYPQL(database, prompt("MYPQL>"));
};
const main = () => {
const myDB = {
tables: [],
};
runMYPQL(myDB, "");
};
main();