add import script for existing game logs
This commit is contained in:
parent
eabe338971
commit
08b332ac6f
5 changed files with 142 additions and 2 deletions
|
|
@ -1730,6 +1730,9 @@ var GameSockets = {
|
|||
};
|
||||
|
||||
const log$1 = logger('Db.ts');
|
||||
// assume 32766 SQLITE_MAX_VARIABLE_NUMBER
|
||||
// @see https://sqlite.org/limits.html
|
||||
const SQLITE_MAX_VARIABLE_NUMBER = 32766;
|
||||
class Db {
|
||||
constructor(file, patchesDir) {
|
||||
this.file = file;
|
||||
|
|
@ -1855,6 +1858,9 @@ class Db {
|
|||
}
|
||||
return this.get(table, check)[idcol]; // get id manually
|
||||
}
|
||||
/**
|
||||
* Inserts data into table and returns the last insert id
|
||||
*/
|
||||
insert(table, data) {
|
||||
const keys = Object.keys(data);
|
||||
const values = keys.map(k => data[k]);
|
||||
|
|
@ -1863,6 +1869,41 @@ class Db {
|
|||
+ ' VALUES (' + keys.map(k => '?').join(',') + ')';
|
||||
return this.run(sql, values).lastInsertRowid;
|
||||
}
|
||||
/**
|
||||
* Inserts multiple datas into table. Returns the total number
|
||||
* of changes.
|
||||
*/
|
||||
insertMany(table, datas) {
|
||||
if (datas.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
const keys = Object.keys(datas[0]);
|
||||
const runChunk = (vars, values) => {
|
||||
const sql = `INSERT INTO ${table}
|
||||
(${keys.join(',')})
|
||||
VALUES ${vars.join(',')}`;
|
||||
return this.run(sql, values).changes;
|
||||
};
|
||||
let len = 0;
|
||||
let vars = [];
|
||||
let values = [];
|
||||
let changes = 0;
|
||||
for (const data of datas) {
|
||||
if (len + keys.length > SQLITE_MAX_VARIABLE_NUMBER) {
|
||||
changes += runChunk(vars, values);
|
||||
len = 0;
|
||||
vars = [];
|
||||
values = [];
|
||||
}
|
||||
len += keys.length;
|
||||
vars.push('(' + keys.map(_ => '?').join(',') + ')');
|
||||
values.push(...keys.map(k => data[k]));
|
||||
}
|
||||
if (len > 0) {
|
||||
changes += runChunk(vars, values);
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
update(table, data, whereRaw = {}) {
|
||||
const keys = Object.keys(data);
|
||||
if (keys.length === 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue