Skip to content

Commit 66c5d0b

Browse files
author
Snowflake107
committed
support UPDATE
1 parent 391b024 commit 66c5d0b

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

src/jsql/Database.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const handlers = {
55
select: require('../util/methods/select'),
66
insert: require('../util/methods/insert'),
77
drop: require('../util/methods/drop'),
8+
update: require('../util/methods/update'),
89
};
910

1011
class Database {

src/util/Util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
chunk: (arr, len) => {
3+
const nm = [];
4+
for (let i = 0; i < arr.length; i += len) nm.push(arr.slice(i, i + len));
5+
return nm;
6+
}
7+
};

src/util/methods/select.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { chunk } = require("../Util");
2+
13
module.exports = (ast, db) => {
24
if (ast.type.toLowerCase() !== 'select') throw new TypeError('invalid query type');
35
const table = ast.from[0].table;
@@ -7,16 +9,10 @@ module.exports = (ast, db) => {
79
if (!data) throw new Error(`Table \"${table}\" does not exist`);
810

911
const fn = chunk(
10-
data.data.map((m) => ({ id: m.key, data: m.data ?? null })),
12+
data.data.map((m) => ({ key: m.key, data: m.data ?? null })),
1113
2
1214
);
1315

1416
if (limit >= 0) return fn.slice(0, limit);
1517
return fn;
1618
};
17-
18-
function chunk(arr, len) {
19-
const nm = [];
20-
for (let i = 0; i < arr.length; i += len) nm.push(arr.slice(i, i + len));
21-
return nm;
22-
}

src/util/methods/update.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { chunk } = require("../Util");
2+
3+
module.exports = (ast, db) => {
4+
if (ast.type.toLowerCase() !== 'update') throw new TypeError('invalid query type');
5+
const table = ast.table[0].table;
6+
const data = db.db;
7+
if (!data[table]) throw new Error(`Table "${table}" does not exist`);
8+
9+
const set = { col: ast.set[0].column, val: ast.set[0].value.value };
10+
const point = { left: ast.where.left.value, op: ast.where.operator, right: ast.where.right.value, set };
11+
if (!data[table].keys.some(x => x.column === set.col)) throw new Error(`Column "${set.col}" does not exists in table "${table}"`);
12+
const bin = binOp(data[table], point);
13+
if (bin.index < 0) return;
14+
15+
const dataAt = data[table].data[bin.index + bin.idx];
16+
data[table].data.splice(bin.index + bin.idx, 1, {
17+
...dataAt,
18+
data: set.val
19+
});
20+
21+
db.write(data);
22+
}
23+
24+
function binOp(data, clause) {
25+
switch(clause.op) {
26+
case "=":
27+
const child = chunk(data.data, 2);
28+
const fnx = m => m.key === clause.left && m.data === clause.right;
29+
const fn = x => x.find(fnx);
30+
31+
try {
32+
return {
33+
data: child.find(fn),
34+
index: child.findIndex(fn),
35+
idx: child.find(fn).findIndex(x => x.key === clause.set.col)
36+
};
37+
} catch {
38+
throw new Error(`Could not verify operation "${clause.left} = ${clause.right}"`);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)