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
|
|
@ -5,6 +5,10 @@ import { logger } from '../common/Util'
|
|||
|
||||
const log = logger('Db.ts')
|
||||
|
||||
// assume 32766 SQLITE_MAX_VARIABLE_NUMBER
|
||||
// @see https://sqlite.org/limits.html
|
||||
const SQLITE_MAX_VARIABLE_NUMBER = 32766
|
||||
|
||||
/**
|
||||
* TODO: create a more specific type for OrderBy.
|
||||
* It looks like this (example):
|
||||
|
|
@ -186,6 +190,9 @@ class Db {
|
|||
return this.get(table, check)[idcol] // get id manually
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts data into table and returns the last insert id
|
||||
*/
|
||||
insert (table: string, data: Data): Integer.IntLike {
|
||||
const keys = Object.keys(data)
|
||||
const values = keys.map(k => data[k])
|
||||
|
|
@ -195,6 +202,45 @@ class Db {
|
|||
return this.run(sql, values).lastInsertRowid
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts multiple datas into table. Returns the total number
|
||||
* of changes.
|
||||
*/
|
||||
insertMany (table: string, datas: Data[]): number {
|
||||
if (datas.length === 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const keys = Object.keys(datas[0])
|
||||
|
||||
const runChunk = (vars: string[], values: any[]) => {
|
||||
const sql = `INSERT INTO ${table}
|
||||
(${keys.join(',')})
|
||||
VALUES ${vars.join(',')}`
|
||||
return this.run(sql, values).changes
|
||||
}
|
||||
|
||||
let len: number = 0
|
||||
let vars: string[] = []
|
||||
let values: any[] = []
|
||||
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: string, data: Data, whereRaw: WhereRaw = {}): void {
|
||||
const keys = Object.keys(data)
|
||||
if (keys.length === 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue