Repository navigation
Expand file tree
/
Copy pathexecutor.js
More file actions
107 lines (100 loc) · 2.9 KB
/
Copy pathexecutor.js
File metadata and controls
107 lines (100 loc) · 2.9 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
import { addTableMeta, dbMeta, clearMeta } from "./meta.js";
import { createTableFile } from "./table.js";
import selectShape from "./commands.js";
export function executor(command, query) {
structureQuerytoAst(command, query);
switch (command) {
case "select":
console.log("A select will be triggered");
break;
case "insert":
console.log("An insert operation will be triggerd");
break;
case "create":
console.log("A create operation will be performed");
create(query);
break;
case "drop":
clearMeta();
}
}
function extractTokens(startIndex, query) {
console.log(startIndex, query, "This is the startIndex");
let i = startIndex;
while (i <= query.length - 1) {
if (query[i].startsWith("(")) {
query[i] = query[i].substring(1, query[i].length);
i++;
} else if (query[i].endsWith(")")) {
query[i] = query[i].substring(0, query[i].length - 1);
console.log("im here");
console.log(query[i]);
i++;
} else if (query[i].endsWith(",")) {
query[i] = query[i].substring(0, query[i].length - 1);
console.log(", here");
console.log(query[i]);
i++;
} else {
i++;
}
}
return query.slice(3);
}
function structureQuerytoAst(command, query) {
//take the raw query and seperate data from commands
switch (command) {
case "create":
const ast = {};
//for create the elements before () will be the command and after that will be data
console.log("hereee", query.split(" "));
query = query.split(" ");
const tableName = query[2];
const cleanTokens = extractTokens(2, query);
console.log("Here are the cleaned tokens");
console.log(cleanTokens, tableName);
for (let i = 0; i < cleanTokens.length - 1; i += 2) {
console.log(cleanTokens[i], cleanTokens[i + 1]);
const key = cleanTokens[i];
const type = cleanTokens[i + 1];
ast[key] = type;
}
console.log(ast);
case "select":
select(query);
}
}
function create(query) {
console.log("A table is being created");
//implement write ahead logging
const tokenedQuery = tokenizer(query);
console.log(tokenedQuery);
const shouldCreateFlag = addTableMeta(tokenedQuery[2], tokenedQuery[3]);
if (shouldCreateFlag == true) {
createTableFile(tokenedQuery[2]);
}
}
function select(query, selectShape) {
console.log("We are in the query subfunction");
const queryList = tokenizer(query);
console.log(queryList);
console.log(selectShape);
}
function tokenizer(query) {
console.log("We are starting the tokenizer");
console.log(query);
let queryList = [];
let word = "";
for (let i = 0; i < query.length; i++) {
if (query[i] != " ") {
word = word + query[i];
console.log(word);
} else {
queryList.push(word);
word = "";
}
}
queryList.push(word);
console.log(queryList);
return queryList;
}