|
1 | | -import { EntityConfig } from '@grandlinex/core'; |
| 1 | +import { |
| 2 | + CoreEntity, |
| 3 | + EntityConfig, |
| 4 | + isQInterfaceSearchAdvanced, |
| 5 | + isQInterfaceSearchAdvancedArr, |
| 6 | + QInterfaceSearch, |
| 7 | + QInterfaceSearchAdvanced, |
| 8 | +} from '@grandlinex/core'; |
2 | 9 | import { convertSpecialFields } from './converter.js'; |
3 | 10 |
|
4 | | -export default function buildSearchQ<E>( |
| 11 | +class ParamCounter { |
| 12 | + private count; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + this.count = 1; |
| 16 | + } |
| 17 | + |
| 18 | + next() { |
| 19 | + return `$${this.count++}`; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function aFilter<E extends CoreEntity>( |
| 24 | + key: string, |
| 25 | + s: QInterfaceSearchAdvanced<QInterfaceSearch<E>, keyof E>, |
| 26 | + count: ParamCounter, |
| 27 | +): string { |
| 28 | + switch (s.mode) { |
| 29 | + case 'equals': |
| 30 | + return `${key} = ${count.next()}`; |
| 31 | + case 'not': |
| 32 | + return `${key} != ${count.next()}`; |
| 33 | + case 'like': |
| 34 | + return `${key} like '%' || ${count.next()} || '%'`; |
| 35 | + case 'smallerThan': |
| 36 | + return `${key} < ${count.next()}`; |
| 37 | + case 'greaterThan': |
| 38 | + return `${key} > ${count.next()}`; |
| 39 | + default: |
| 40 | + throw new Error(`Unknown mode: ${s.mode}`); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export default function buildSearchQ<E extends CoreEntity>( |
5 | 45 | config: EntityConfig<E>, |
6 | | - search: { [P in keyof E]?: E[P] }, |
| 46 | + search: QInterfaceSearch<E>, |
7 | 47 | param: any[], |
8 | 48 | searchQ: string, |
9 | 49 | ) { |
10 | 50 | let temp = searchQ; |
11 | 51 | const keys: (keyof E)[] = Object.keys(search) as (keyof E)[]; |
12 | 52 | if (keys.length > 0) { |
13 | 53 | const filter: string[] = []; |
14 | | - let count = 1; |
| 54 | + const count = new ParamCounter(); |
15 | 55 | for (const key of keys) { |
16 | | - if (search[key] !== undefined) { |
17 | | - const meta = config.meta.get(key); |
18 | | - if (!meta) { |
19 | | - throw new Error('Missing meta'); |
20 | | - } |
21 | | - filter.push(`${String(key)} = $${count++}`); |
22 | | - convertSpecialFields(meta, search, key, param); |
| 56 | + const s: QInterfaceSearch<E>[keyof E] = search[key]; |
| 57 | + const meta = config.meta.get(key); |
| 58 | + if (!meta) { |
| 59 | + throw new Error('Missing meta'); |
| 60 | + } |
| 61 | + if (isQInterfaceSearchAdvanced(s)) { |
| 62 | + filter.push(aFilter(String(key), s, count)); |
| 63 | + convertSpecialFields(meta, s.value, param); |
| 64 | + } else if (isQInterfaceSearchAdvancedArr(s)) { |
| 65 | + filter.push( |
| 66 | + ...s.map((e) => { |
| 67 | + const ax = aFilter(String(key), e, count); |
| 68 | + convertSpecialFields(meta, e.value, param); |
| 69 | + return ax; |
| 70 | + }), |
| 71 | + ); |
| 72 | + } else { |
| 73 | + filter.push(`${String(key)} = ${count.next()}`); |
| 74 | + convertSpecialFields(meta, search[key], param); |
23 | 75 | } |
24 | 76 | } |
25 | 77 | if (filter.length > 0) { |
|
0 commit comments