This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert.js
117 lines (109 loc) · 4.16 KB
/
convert.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
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
const localconfig = require('./localconfig');
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
database: localconfig.database.name,
user: localconfig.database.username,
port: localconfig.database.port,
password: localconfig.database.password,
connectionLimit: 5
});
const query = function (params) {
let values = '';
for (let i = 0; i < params.length; i++) {
values += `( '${params[i].join("',\n '")}' ),`
}
values = values.substring(0, values.length - 1);
values = values.replace(/'null'/g, "null");
values = values.replace(/\\/g, "");
return `INSERT INTO data (map_id,
name,
wikidata,
commons,
website,
image,
link_en,
link_de,
link_fr,
link_it,
link_tot_count,
lat,
lon,
id_history)
VALUES ${values}`
}
const seed = `
select *
from histories h
where diff = true
and error = false
and mapId = 3
and not exists (
select 1
from data
where id_history = h.id
)
limit 1
`
pool.getConnection()
.then(conn => {
main(conn);
}).catch(err => {
console.log(err);
process.exit(1);
});
main = function (conn) {
conn.query(seed)
.then((rows) => {
if (rows.length === 0) {
// done
conn.end();
pool.end();
process.exit(0);
}
let allParams = [];
for (let i = 0; i < rows.length; i++) {
let json = JSON.parse(rows[i].json);
for (let j = 0; j < json.data.length; j++) {
let queryParam = [];
queryParam.push(rows[i].mapId);
queryParam.push(json.data[j].properties.name);
queryParam.push(json.data[j].properties.wikidata);
queryParam.push(json.data[j].properties.commons || 'null');
queryParam.push(json.data[j].properties.website || 'null');
queryParam.push(json.data[j].properties.image || 'null');
queryParam.push(json.data[j].properties.lang[json.data[j].properties.lang.findIndex(element => element.includes("https://en.wikipedia.org/wiki/"))] || 'null');
queryParam.push(json.data[j].properties.lang[json.data[j].properties.lang.findIndex(element => element.includes("https://fr.wikipedia.org/wiki/"))] || 'null');
queryParam.push(json.data[j].properties.lang[json.data[j].properties.lang.findIndex(element => element.includes("https://de.wikipedia.org/wiki/"))] || 'null');
queryParam.push(json.data[j].properties.lang[json.data[j].properties.lang.findIndex(element => element.includes("https://it.wikipedia.org/wiki/"))] || 'null');
queryParam.push(json.data[j].properties.lang.length);
queryParam.push(json.data[j].geometry.coordinates[1]);
queryParam.push(json.data[j].geometry.coordinates[0]);
queryParam.push(rows[i].id);
for (let k = 0; k < queryParam.length; k++) {
if (typeof queryParam[k] === 'string')
queryParam[k] = queryParam[k].replace(/'/g, "''");
}
allParams.push(queryParam);
}
}
//console.log(query(allParams))
if (allParams.length === 0) {
return "no params";
} else {
return conn.query(query(allParams));
}
})
.then((res) => {
console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }
main(conn);
//conn.end();
//pool.end();
})
.catch(err => {
//handle error
console.log(err);
conn.end();
pool.end();
})
}