Commit c4242b1b authored by nanahira's avatar nanahira

add regex

parent 2c1e2806
......@@ -9,6 +9,12 @@ type Where = string | any;
interface Card {
id: number;
name: string;
desc: string;
}
interface RegexCondition {
pattern: string;
field: 'name' | 'desc';
}
interface QueryInfo {
......@@ -16,6 +22,7 @@ interface QueryInfo {
lower: number;
noFilterAlias: boolean;
where?: Where[];
regexConditions?: RegexCondition[];
mayIncorrectStatements?: MayIncorrectStatement[];
}
......@@ -155,9 +162,15 @@ function getMedianCode(cards: Card[]) {
async function queryCards(queryInfo: QueryInfo) {
const whereClauseString = parseWhereClause(queryInfo.where);
const mayIncorrectClauseString = parseMayIncorrectStatements(queryInfo.mayIncorrectStatements);
const sql = `select datas.id,texts.name from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? ${queryInfo.noFilterAlias ? "" : "and (datas.alias = 0 or datas.id - datas.alias > 10)"} ${whereClauseString} ${mayIncorrectClauseString} order by datas.id asc`;
const sql = `select datas.id,texts.name,texts.desc from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and datas.id > ? and datas.id <= ? ${queryInfo.noFilterAlias ? "" : "and (datas.alias = 0 or datas.id - datas.alias > 10)"} ${whereClauseString} ${mayIncorrectClauseString} order by datas.id asc`;
console.error(`SQL: ${sql}`);
const cards: Card[] = await db.all(sql, [queryInfo.lower, queryInfo.upper]);
let cards: Card[] = await db.all(sql, [queryInfo.lower, queryInfo.upper]);
if (queryInfo.regexConditions) {
for (let regexCondition of queryInfo.regexConditions) {
const regexObj = new RegExp(regexCondition.pattern);
cards = cards.filter(card => (card[regexCondition.field] as string).match(regexObj));
}
}
return cards;
}
......@@ -173,7 +186,7 @@ async function main() {
const cards = await queryCards(queryInfo);
console.log(`Cards:`);
for (let card of cards) {
console.log(JSON.stringify(card));
console.log(`${card.id} ${card.name}`);
}
console.log(`Size: ${cards.length}`);
if (cards.length > 1) {
......
......@@ -28,6 +28,9 @@
upper: 100000000
lower: 0
noFilterAlias: false
regexConditions:
- field: name
pattern: '^雷龙$'
where:
- "type & {TYPE_MONSTER} > 0"
mayIncorrectStatements:
......@@ -51,6 +54,8 @@ mayIncorrectStatements:
* `where` 约束条件,使用 SQL 语句填写,相互之间是 AND 的关系。
* `regexConditions` 卡名或描述的正则过滤条件。`field` 可以是 `name` 或者 `desc`
### 模糊逻辑
`mayIncorrectStatements` 为模糊逻辑字段,非模糊逻辑规则可以删除该字段以不使用模糊逻辑查找。
......
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