-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.mjs
89 lines (84 loc) · 2.86 KB
/
database.mjs
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
import { Parser } from "./parser.mjs";
import { DatabaseError } from "./databaseError.mjs";
export class Database {
constructor() {
this.tables = {};
this.parser = new Parser();
}
createTable(parsedStatement) {
const tableName = parsedStatement[1];
const commandColumns = parsedStatement[2];
const columns = commandColumns.split(', ');
this.tables = {
[tableName]: {
columns: {},
data: []
}
};
for (const column of columns) {
const item = column.split(" ");
const col = item[0];
const tipo = item[1];
this.tables[tableName].columns[col] = tipo;
}
return this;
}
execute(statement) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result = this.parser.parse(statement);
if (result) {
resolve(this[result.command](result.parsedStatement));
}
reject(new DatabaseError(statement, `Syntax error: '${statement}'`));
}, 1000);
})
}
insert(parsedStatement) {
const tableName = parsedStatement[1];
const columns = parsedStatement[2].split(', ');
const values = parsedStatement[3].split(', ');
let row = {};
for (let i = 0; i < values.length; i++) {
const column = columns[i];
const value = values[i];
row[column] = value;
}
this.tables[tableName].data.push(row);
}
select(parsedStatement) {
const columns = parsedStatement[1].split(", ");
const tableName = parsedStatement[2];
let rows = this.tables[tableName].data;
const whereClause = parsedStatement[3] || null;
if (whereClause !== null) {
const whereCondition = whereClause.split(" ");
const [columnWhere, condition, valueWhere] = whereCondition;
rows = rows.filter(function (row) {
return (row[columnWhere] === valueWhere);
})
}
rows = rows.map(function (row) {
let selectedRows = {};
columns.forEach(function (column) {
selectedRows[column] = row[column];
})
return selectedRows;
});
console.log(rows);
}
delete(parsedStatement) {
let [, tableName, whereClause] = parsedStatement;
let rows = [];
if (whereClause) {
whereClause = whereClause.split(" ");
const [whereColumn, condition, whereValue] = whereClause;
rows = this.tables[tableName].data;
rows = rows.filter(function (row) {
return (row[whereColumn] !== whereValue);
})
}
this.tables[tableName].data = rows;
console.log(rows);
}
};