-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagparser.js
85 lines (71 loc) · 2.84 KB
/
tagparser.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
// Temporary script. Don't use it.
import database from "./src/database";
const connection = database();
const sql = `select * from tb_jamquery`;
const map = {};
const nameMap = {};
const printMap = () => {
console.log(map);
}
const parseRow = (row) => {
const tags = [];
var name = row.name;
while (true) {
let startIndex = name.indexOf("[") + 1;
let endIndex = name.indexOf("]", startIndex);
if (endIndex === -1) {
break;
}
let tag = name.slice(startIndex, endIndex);
name = name.slice(endIndex + 1).trim();
tags.push(tag);
}
map[row.id] = tags;
nameMap[row.id] = name;
console.log(`Tags for "${name}"`);
console.log(`[${tags}]`);
};
connection.query(sql, (err, rows, fields) => {
rows.forEach(parseRow);
Object.keys(map).forEach(key => {
let tags = map[key];
let newName = nameMap[key];
if (tags.length > 0) {
tags.forEach(tag => {
connection.query(`INSERT IGNORE INTO tb_tag (name) VALUES (?)`,
[tag],
(err, results, fields) => {
if (err) {
console.error(`Error while insert tag ${tag}. abort.`);
process.exit(1);
}
if (results.insertId === 0) {
console.log(`tag ${tag} already exists. insertion ignored.`);
} else {
console.log(`tag ${tag} inserted as id ${results.insertId}`);
}
connection.query(`SELECT id FROM tb_tag WHERE name=?`, [tag], (err, rows, fields) => {
let tagId = rows[0].id;
connection.query(`INSERT IGNORE INTO tb_jamquery_tag_relation (jamquery_id, tag_id) VALUES (?, ?)`,
[key, tagId],
(err, results, fields) => {
if (err) {
console.error(`Error while insert relation jamquery:${key} - tag:${tagId}. abort.`);
process.exit(1);
}
console.log(`Sucessfully inserted relation jamquery:${key} - tag:${tagId}.`);
}
);
connection.query(`UPDATE tb_jamquery SET name=? WHERE id=?`,
[newName, key],
(err, results, fields) => {
console.log(results);
}
);
});
}
);
});
}
});
});