-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexe8.js
More file actions
123 lines (116 loc) · 4.09 KB
/
exe8.js
File metadata and controls
123 lines (116 loc) · 4.09 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Parser {
constructor() {
this.commands = new Map();
this.commands.set("createTable", /create table ([a-z]+) \((.+)\)/);
this.commands.set("insert", /insert into ([a-z]+) \((.+)\) values \((.+)\)/);
this.commands.set("select", /select (.+) from ([a-z]+)(?: where (.+))?/);
this.commands.set("delete", /delete from ([a-z]+)(?: where (.+))?/);
}
parse(statement) {
for (let [command, regexp] of this.commands) {
const parsedStatement = statement.match(regexp);
if (parsedStatement) {
return {
command,
parsedStatement
}
}
}
}
};
class DatabaseError {
constructor(statement, message) {
this.statement = statement;
this.message = message;
}
};
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(statemente) {
const result = this.parser.parse(statemente);
if (result) {
return this[result.command](result.parsedStatement);
}
throw new DatabaseError(statemente, `Syntax error: '${statemente}'`);
}
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);
}
};
try {
let database = new Database
database.execute("create table author (id number, name string, age number, city string, state string, country string)");
database.execute("insert into author (id, name, age) values (1, Douglas Crockford, 62)");
database.execute("insert into author (id, name, age) values (2, Linus Torvalds, 47)");
database.execute("insert into author (id, name, age) values (3, Martin Fowler, 54)");
database.execute("delete from author where id = 2");
database.execute("select name, age from author");
// console.log(JSON.stringify(database, undefined, " "));
} catch (e) {
console.log(e.message);
}