Commit 245eba66 authored by nanahira's avatar nanahira

fix non string things

parent c558236d
import { open, Database } from "sqlite"; import { open, Database } from "sqlite";
import sqlite3 from "sqlite3"; import sqlite3 from "sqlite3";
import fs from "fs"; import fs from "fs";
import _ from "underscore"; import _, { where } from "underscore";
import YAML from "yaml"; import YAML from "yaml";
interface Card { interface Card {
...@@ -12,7 +12,7 @@ interface Card { ...@@ -12,7 +12,7 @@ interface Card {
interface QueryInfo { interface QueryInfo {
upper: number; upper: number;
lower: number; lower: number;
where: string[]; where: (string | number)[];
} }
let db: Database; let db: Database;
...@@ -29,18 +29,20 @@ async function loadConstants() { ...@@ -29,18 +29,20 @@ async function loadConstants() {
} }
} }
function replaceWhereClause(whereClause: string[]) { function replaceWhereClause(whereClause: (string | number)[]) {
const newClause = _.clone(whereClause); const newClause: string[] = [];
if (!newClause.length) { if (!whereClause.length) {
return ""; return "";
} }
for (let i = 0; i < newClause.length; ++i) { for (let clause of whereClause) {
let clause = newClause[i]; if (typeof (clause) !== "string") {
continue;
}
for (let constantKey of Array.from(constantDict.keys())) { for (let constantKey of Array.from(constantDict.keys())) {
const regex = new RegExp(`\{${constantKey}\}`, "g"); const regex = new RegExp(`\{${constantKey}\}`, "g");
clause = clause.replace(regex, constantDict.get(constantKey).toString()); clause = clause.replace(regex, constantDict.get(constantKey).toString());
} }
newClause[i] = `and ${clause}`; newClause.push(`and (${clause})`);
} }
return newClause.join(' '); return newClause.join(' ');
} }
...@@ -64,7 +66,7 @@ function getMedianCode(cards: Card[]) { ...@@ -64,7 +66,7 @@ function getMedianCode(cards: Card[]) {
async function queryCards(queryInfo: QueryInfo) { async function queryCards(queryInfo: QueryInfo) {
const replacedWhere = replaceWhereClause(queryInfo.where); 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; 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