query-core 0.1.31 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/search.ts CHANGED
@@ -1,126 +1,154 @@
1
- import {Attribute, StringMap} from './metadata';
1
+ import { Attribute, StringMap } from "./metadata"
2
2
 
3
3
  export interface SearchResult<T> {
4
- list: T[];
5
- total?: number;
4
+ list: T[]
5
+ total?: number
6
6
  }
7
- export function buildFromQuery<T>(query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<K[]>, sql: string, params?: any[], limit?: number, offset?: number, mp?: StringMap, bools?: Attribute[], provider?: string, totalCol?: string): Promise<SearchResult<T>> {
7
+ export function getOffset(limit: number, page: number, ifirstPageSize?: number): number {
8
+ if (ifirstPageSize && ifirstPageSize > 0) {
9
+ const offset = limit * (page - 2) + ifirstPageSize
10
+ return offset < 0 ? 0 : offset
11
+ } else {
12
+ const offset = limit * (page - 1)
13
+ return offset < 0 ? 0 : offset
14
+ }
15
+ }
16
+ export function buildFromQuery<T>(
17
+ query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<K[]>,
18
+ sql: string,
19
+ params?: any[],
20
+ limit?: number,
21
+ page?: number,
22
+ mp?: StringMap,
23
+ bools?: Attribute[],
24
+ provider?: string,
25
+ totalCol?: string,
26
+ ): Promise<SearchResult<T>> {
8
27
  if (!limit || limit <= 0) {
9
- return query<T>(sql, params, mp, bools).then(list => {
10
- const total = (list ? list.length : undefined);
11
- return {list, total};
12
- });
28
+ return query<T>(sql, params, mp, bools).then((list) => {
29
+ const total = list ? list.length : undefined
30
+ return { list, total }
31
+ })
13
32
  } else {
33
+ const ipage = !page || page <= 0 ? 1 : page
34
+ const offset = getOffset(limit, ipage)
14
35
  if (provider === oracle) {
15
36
  if (!totalCol || totalCol.length === 0) {
16
- totalCol = 'total';
37
+ totalCol = "total"
17
38
  }
18
- const sql2 = buildPagingQueryForOracle(sql, limit, offset, totalCol);
19
- return queryAndCount(query, sql2, params, totalCol, mp, bools);
39
+ const sql2 = buildPagingQueryForOracle(sql, limit, offset, totalCol)
40
+ return queryAndCount(query, sql2, params, totalCol, mp, bools)
20
41
  } else {
21
- const sql2 = buildPagingQuery(sql, limit, offset);
22
- const countQuery = buildCountQuery(sql);
23
- const resultPromise = query<T>(sql2, params, mp, bools);
24
- const countPromise = query<T>(countQuery, params).then(r => {
42
+ const sql2 = buildPagingQuery(sql, limit, offset)
43
+ const countQuery = buildCountQuery(sql)
44
+ const resultPromise = query<T>(sql2, params, mp, bools)
45
+ const countPromise = query<T>(countQuery, params).then((r) => {
25
46
  if (!r || r.length === 0) {
26
- return 0;
47
+ return 0
27
48
  } else {
28
- const r0 = r[0];
29
- const keys = Object.keys(r0 as any);
30
- return (r0 as any)[keys[0]] as number;
49
+ const r0 = r[0]
50
+ const keys = Object.keys(r0 as any)
51
+ return (r0 as any)[keys[0]] as number
31
52
  }
32
- });
33
- return Promise.all([resultPromise, countPromise]).then(r => {
34
- const [list, total] = r;
35
- if (typeof total === 'string' && !isNaN(total)) {
36
- const t = parseInt(total, 10);
37
- return {list, total: t};
53
+ })
54
+ return Promise.all([resultPromise, countPromise]).then((r) => {
55
+ const [list, total] = r
56
+ if (typeof total === "string" && !isNaN(total)) {
57
+ const t = parseInt(total, 10)
58
+ return { list, total: t }
38
59
  } else {
39
- return {list, total};
60
+ return { list, total }
40
61
  }
41
- });
62
+ })
42
63
  }
43
64
  }
44
65
  }
45
- export function queryAndCount<T>(query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<K[]>, sql: string, params: any[]|undefined, total: string, mp?: StringMap, bools?: Attribute[]): Promise<SearchResult<T>> {
66
+ export function queryAndCount<T>(
67
+ query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<K[]>,
68
+ sql: string,
69
+ params: any[] | undefined,
70
+ total: string,
71
+ mp?: StringMap,
72
+ bools?: Attribute[],
73
+ ): Promise<SearchResult<T>> {
46
74
  if (!total || total.length === 0) {
47
- total = 'total';
75
+ total = "total"
48
76
  }
49
- return query<T>(sql, params, mp, bools).then(list => {
77
+ return query<T>(sql, params, mp, bools).then((list) => {
50
78
  if (!list || list.length === 0) {
51
- return {list: [], total: 0};
79
+ return { list: [], total: 0 }
52
80
  }
53
- const t = (list[0] as any)[total] as number;
81
+ const t = (list[0] as any)[total] as number
54
82
  for (const obj of list) {
55
- delete (obj as any)[total];
83
+ delete (obj as any)[total]
56
84
  }
57
- return {list, total: t};
58
- });
85
+ return { list, total: t }
86
+ })
59
87
  }
60
- export const oracle = 'oracle';
61
- const s = 'select';
62
- const S = 'SELECT';
63
- const d = ' distinct ';
64
- const D = ' DISTINCT ';
88
+ export const oracle = "oracle"
89
+ const s = "select"
90
+ const S = "SELECT"
91
+ const d = " distinct "
92
+ const D = " DISTINCT "
65
93
  export function buildPagingQuery(sql: string, limit: number, offset?: number, provider?: string): string {
66
94
  if (limit === undefined || limit == null) {
67
- limit = 0;
95
+ limit = 0
68
96
  }
69
97
  if (offset === undefined || offset == null) {
70
- offset = 0;
98
+ offset = 0
71
99
  }
72
100
  if (provider !== oracle) {
73
- return `${sql} limit ${limit} offset ${offset}`;
101
+ return `${sql} limit ${limit} offset ${offset}`
74
102
  } else {
75
- return buildPagingQueryForOracle(sql, limit, offset);
103
+ return buildPagingQueryForOracle(sql, limit, offset)
76
104
  }
77
105
  }
78
106
  export function buildPagingQueryForOracle(sql: string, limit: number, offset?: number, total?: string) {
79
107
  if (!total || total.length === 0) {
80
- total = 'total';
108
+ total = "total"
81
109
  }
82
110
  if (!offset) {
83
- offset = 0;
111
+ offset = 0
84
112
  }
85
- let l = d.length;
86
- let i = sql.indexOf(d);
113
+ let l = d.length
114
+ let i = sql.indexOf(d)
87
115
  if (i < 0) {
88
- i = sql.indexOf(D);
116
+ i = sql.indexOf(D)
89
117
  }
90
118
  if (i < 0) {
91
- l = S.length;
92
- i = sql.indexOf(s);
119
+ l = S.length
120
+ i = sql.indexOf(s)
93
121
  }
94
122
  if (i < 0) {
95
- i = sql.indexOf(S);
123
+ i = sql.indexOf(S)
96
124
  }
97
125
  if (i >= 0) {
98
- return `${sql.substr(0, l)} count(*) over() as ${total}, ${sql.substr(l)} offset ${offset} rows fetch next ${limit} rows only`;
126
+ return `${sql.substring(0, l)} count(*) over() as ${total}, ${sql.substring(l)} offset ${offset} rows fetch next ${limit} rows only`
99
127
  } else {
100
- return `${sql} offset ${offset} rows fetch next ${limit} rows only`;
128
+ return `${sql} offset ${offset} rows fetch next ${limit} rows only`
101
129
  }
102
130
  }
103
131
  export function buildCountQuery(sql: string): string {
104
- const sql2 = sql.trim();
105
- const i = sql2.indexOf('select ');
132
+ const sql2 = sql.trim()
133
+ const i = sql2.indexOf("select ")
106
134
  if (i < 0) {
107
- return sql;
135
+ return sql
108
136
  }
109
- const j = sql2.indexOf(' from ', i + 6);
137
+ const j = sql2.indexOf(" from ", i + 6)
110
138
  if (j < 0) {
111
- return sql;
139
+ return sql
112
140
  }
113
- const k = sql2.indexOf(' order by ', j);
114
- const h = sql2.indexOf(' distinct ');
141
+ const k = sql2.indexOf(" order by ", j)
142
+ const h = sql2.indexOf(" distinct ")
115
143
  if (h > 0) {
116
- const sql3 = 'select count(*) from (' + sql2.substring(i) + ') as main';
117
- return sql3;
144
+ const sql3 = "select count(*) from (" + sql2.substring(i) + ") as main"
145
+ return sql3
118
146
  }
119
147
  if (k > 0) {
120
- const sql3 = 'select count(*) ' + sql2.substring(j, k);
121
- return sql3;
148
+ const sql3 = "select count(*) " + sql2.substring(j, k)
149
+ return sql3
122
150
  } else {
123
- const sql3 = 'select count(*) ' + sql2.substring(j);
124
- return sql3;
151
+ const sql3 = "select count(*) " + sql2.substring(j)
152
+ return sql3
125
153
  }
126
154
  }