Commit 245eba66 authored by nanahira's avatar nanahira

fix non string things

parent c558236d
import { open, Database } from "sqlite";
import sqlite3 from "sqlite3";
import fs from "fs";
import _ from "underscore";
import _, { where } from "underscore";
import YAML from "yaml";
interface Card {
......@@ -12,7 +12,7 @@ interface Card {
interface QueryInfo {
upper: number;
lower: number;
where: string[];
where: (string | number)[];
}
let db: Database;
......@@ -29,18 +29,20 @@ async function loadConstants() {
}
}
function replaceWhereClause(whereClause: string[]) {
const newClause = _.clone(whereClause);
if (!newClause.length) {
function replaceWhereClause(whereClause: (string | number)[]) {
const newClause: string[] = [];
if (!whereClause.length) {
return "";
}
for (let i = 0; i < newClause.length; ++i) {
let clause = newClause[i];
for (let clause of whereClause) {
if (typeof (clause) !== "string") {
continue;
}
for (let constantKey of Array.from(constantDict.keys())) {
const regex = new RegExp(`\{${constantKey}\}`, "g");
clause = clause.replace(regex, constantDict.get(constantKey).toString());
}
newClause[i] = `and ${clause}`;
newClause.push(`and (${clause})`);
}
return newClause.join(' ');
}
......@@ -64,7 +66,7 @@ function getMedianCode(cards: Card[]) {
async function queryCards(queryInfo: QueryInfo) {
const replacedWhere = replaceWhereClause(queryInfo.where);
const cards: Card[] = await db.all(`select datas.id,texts.name from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? ${replacedWhere} order by datas.id asc`, [queryInfo.lower, queryInfo.upper]);
const cards: Card[] = await db.all(`select datas.id,texts.name from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? and (datas.alias = 0 or datas.alias - datas.id > 10) ${replacedWhere} order by datas.id asc`, [queryInfo.lower, queryInfo.upper]);
return cards;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment