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