Commit c4242b1b authored by nanahira's avatar nanahira

add regex

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