query-core 0.1.27 → 0.1.29

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/README.md CHANGED
@@ -1 +1 @@
1
- # query
1
+ # query-core
package/lib/batch.js CHANGED
@@ -2,11 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var build_1 = require("./build");
4
4
  var SqlInserter = (function () {
5
- function SqlInserter(exec, table, attributes, param, map) {
5
+ function SqlInserter(exec, table, attributes, param, oneIfSuccess, map) {
6
6
  this.exec = exec;
7
7
  this.table = table;
8
8
  this.attributes = attributes;
9
9
  this.param = param;
10
+ this.oneIfSuccess = oneIfSuccess;
10
11
  this.map = map;
11
12
  this.write = this.write.bind(this);
12
13
  var x = build_1.version(attributes);
@@ -24,7 +25,12 @@ var SqlInserter = (function () {
24
25
  }
25
26
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
26
27
  if (stmt) {
27
- return this.exec(stmt.query, stmt.params);
28
+ if (this.oneIfSuccess) {
29
+ return this.exec(stmt.query, stmt.params).then(function (ct) { return ct > 0 ? 1 : 0; });
30
+ }
31
+ else {
32
+ return this.exec(stmt.query, stmt.params);
33
+ }
28
34
  }
29
35
  else {
30
36
  return Promise.resolve(0);
@@ -34,11 +40,12 @@ var SqlInserter = (function () {
34
40
  }());
35
41
  exports.SqlInserter = SqlInserter;
36
42
  var SqlUpdater = (function () {
37
- function SqlUpdater(exec, table, attributes, param, map) {
43
+ function SqlUpdater(exec, table, attributes, param, oneIfSuccess, map) {
38
44
  this.exec = exec;
39
45
  this.table = table;
40
46
  this.attributes = attributes;
41
47
  this.param = param;
48
+ this.oneIfSuccess = oneIfSuccess;
42
49
  this.map = map;
43
50
  this.write = this.write.bind(this);
44
51
  var x = build_1.version(attributes);
@@ -56,7 +63,12 @@ var SqlUpdater = (function () {
56
63
  }
57
64
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
58
65
  if (stmt) {
59
- return this.exec(stmt.query, stmt.params);
66
+ if (this.oneIfSuccess) {
67
+ return this.exec(stmt.query, stmt.params).then(function (ct) { return ct > 0 ? 1 : 0; });
68
+ }
69
+ else {
70
+ return this.exec(stmt.query, stmt.params);
71
+ }
60
72
  }
61
73
  else {
62
74
  return Promise.resolve(0);
@@ -66,11 +78,12 @@ var SqlUpdater = (function () {
66
78
  }());
67
79
  exports.SqlUpdater = SqlUpdater;
68
80
  var SqlBatchInserter = (function () {
69
- function SqlBatchInserter(exec, table, attributes, param, map) {
81
+ function SqlBatchInserter(exec, table, attributes, param, oneIfSuccess, map) {
70
82
  this.exec = exec;
71
83
  this.table = table;
72
84
  this.attributes = attributes;
73
85
  this.param = param;
86
+ this.oneIfSuccess = oneIfSuccess;
74
87
  this.map = map;
75
88
  this.write = this.write.bind(this);
76
89
  var x = build_1.version(attributes);
@@ -93,7 +106,12 @@ var SqlBatchInserter = (function () {
93
106
  }
94
107
  var stmt = build_1.buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
95
108
  if (stmt) {
96
- return this.exec(stmt.query, stmt.params);
109
+ if (this.oneIfSuccess) {
110
+ return this.exec(stmt.query, stmt.params).then(function (ct) { return objs.length; });
111
+ }
112
+ else {
113
+ return this.exec(stmt.query, stmt.params);
114
+ }
97
115
  }
98
116
  else {
99
117
  return Promise.resolve(0);
@@ -103,11 +121,12 @@ var SqlBatchInserter = (function () {
103
121
  }());
104
122
  exports.SqlBatchInserter = SqlBatchInserter;
105
123
  var SqlBatchUpdater = (function () {
106
- function SqlBatchUpdater(execBatch, table, attributes, param, notSkipInvalid, map) {
124
+ function SqlBatchUpdater(execBatch, table, attributes, param, oneIfSuccess, notSkipInvalid, map) {
107
125
  this.execBatch = execBatch;
108
126
  this.table = table;
109
127
  this.attributes = attributes;
110
128
  this.param = param;
129
+ this.oneIfSuccess = oneIfSuccess;
111
130
  this.notSkipInvalid = notSkipInvalid;
112
131
  this.map = map;
113
132
  this.write = this.write.bind(this);
@@ -131,7 +150,12 @@ var SqlBatchUpdater = (function () {
131
150
  }
132
151
  var stmts = build_1.buildToUpdateBatch(list, this.table, this.attributes, this.param, this.notSkipInvalid);
133
152
  if (stmts && stmts.length > 0) {
134
- return this.execBatch(stmts);
153
+ if (this.oneIfSuccess) {
154
+ return this.execBatch(stmts).then(function (ct) { return stmts.length; });
155
+ }
156
+ else {
157
+ return this.execBatch(stmts);
158
+ }
135
159
  }
136
160
  else {
137
161
  return Promise.resolve(0);
@@ -190,7 +214,7 @@ var StreamInserter = (function () {
190
214
  return this.exec(stmt.query, stmt.params).then(function (r) {
191
215
  _this.list = [];
192
216
  return total_1;
193
- });
217
+ }); F
194
218
  }
195
219
  else {
196
220
  return Promise.resolve(0);
package/lib/search.js CHANGED
@@ -1,57 +1,63 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function buildFromQuery(query, sql, params, limit, offset, mp, bools, provider, totalCol) {
4
- if (!limit || limit <= 0) {
5
- return query(sql, params, mp, bools).then(function (list) {
6
- var total = (list ? list.length : undefined);
7
- return { list: list, total: total };
8
- });
9
- }
10
- else {
11
- if (provider === exports.oracle) {
12
- if (!totalCol || totalCol.length === 0) {
13
- totalCol = 'total';
14
- }
15
- var sql2 = buildPagingQueryForOracle(sql, limit, offset, totalCol);
16
- return queryAndCount(query, sql2, params, totalCol, mp, bools);
4
+ if (!limit || limit <= 0) {
5
+ return query(sql, params, mp, bools).then(function (list) {
6
+ var total = (list ? list.length : undefined);
7
+ return { list: list, total: total };
8
+ });
17
9
  }
18
10
  else {
19
- var sql2 = buildPagingQuery(sql, limit, offset);
20
- var countQuery = buildCountQuery(sql);
21
- var resultPromise = query(sql2, params, mp, bools);
22
- var countPromise = query(countQuery, params).then(function (r) {
23
- if (!r || r.length === 0) {
24
- return 0;
11
+ if (provider === exports.oracle) {
12
+ if (!totalCol || totalCol.length === 0) {
13
+ totalCol = 'total';
14
+ }
15
+ var sql2 = buildPagingQueryForOracle(sql, limit, offset, totalCol);
16
+ return queryAndCount(query, sql2, params, totalCol, mp, bools);
25
17
  }
26
18
  else {
27
- var r0 = r[0];
28
- var keys = Object.keys(r0);
29
- return r0[keys[0]];
19
+ var sql2 = buildPagingQuery(sql, limit, offset);
20
+ var countQuery = buildCountQuery(sql);
21
+ var resultPromise = query(sql2, params, mp, bools);
22
+ var countPromise = query(countQuery, params).then(function (r) {
23
+ if (!r || r.length === 0) {
24
+ return 0;
25
+ }
26
+ else {
27
+ var r0 = r[0];
28
+ var keys = Object.keys(r0);
29
+ return r0[keys[0]];
30
+ }
31
+ });
32
+ return Promise.all([resultPromise, countPromise]).then(function (r) {
33
+ var list = r[0], total = r[1];
34
+ if (typeof total === 'string' && !isNaN(total)) {
35
+ var t = parseInt(total, 10);
36
+ return { list: list, total: t };
37
+ }
38
+ else {
39
+ return { list: list, total: total };
40
+ }
41
+ });
30
42
  }
31
- });
32
- return Promise.all([resultPromise, countPromise]).then(function (r) {
33
- var list = r[0], total = r[1];
34
- return { list: list, total: total };
35
- });
36
43
  }
37
- }
38
44
  }
39
45
  exports.buildFromQuery = buildFromQuery;
40
46
  function queryAndCount(query, sql, params, total, mp, bools) {
41
- if (!total || total.length === 0) {
42
- total = 'total';
43
- }
44
- return query(sql, params, mp, bools).then(function (list) {
45
- if (!list || list.length === 0) {
46
- return { list: [], total: 0 };
47
- }
48
- var t = list[0][total];
49
- for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
50
- var obj = list_1[_i];
51
- delete obj[total];
52
- }
53
- return { list: list, total: t };
54
- });
47
+ if (!total || total.length === 0) {
48
+ total = 'total';
49
+ }
50
+ return query(sql, params, mp, bools).then(function (list) {
51
+ if (!list || list.length === 0) {
52
+ return { list: [], total: 0 };
53
+ }
54
+ var t = list[0][total];
55
+ for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
56
+ var obj = list_1[_i];
57
+ delete obj[total];
58
+ }
59
+ return { list: list, total: t };
60
+ });
55
61
  }
56
62
  exports.queryAndCount = queryAndCount;
57
63
  exports.oracle = 'oracle';
@@ -60,70 +66,70 @@ var S = 'SELECT';
60
66
  var d = ' distinct ';
61
67
  var D = ' DISTINCT ';
62
68
  function buildPagingQuery(sql, limit, offset, provider) {
63
- if (limit === undefined || limit == null) {
64
- limit = 0;
65
- }
66
- if (offset === undefined || offset == null) {
67
- offset = 0;
68
- }
69
- if (provider !== exports.oracle) {
70
- return sql + " limit " + limit + " offset " + offset;
71
- }
72
- else {
73
- return buildPagingQueryForOracle(sql, limit, offset);
74
- }
69
+ if (limit === undefined || limit == null) {
70
+ limit = 0;
71
+ }
72
+ if (offset === undefined || offset == null) {
73
+ offset = 0;
74
+ }
75
+ if (provider !== exports.oracle) {
76
+ return sql + " limit " + limit + " offset " + offset;
77
+ }
78
+ else {
79
+ return buildPagingQueryForOracle(sql, limit, offset);
80
+ }
75
81
  }
76
82
  exports.buildPagingQuery = buildPagingQuery;
77
83
  function buildPagingQueryForOracle(sql, limit, offset, total) {
78
- if (!total || total.length === 0) {
79
- total = 'total';
80
- }
81
- if (!offset) {
82
- offset = 0;
83
- }
84
- var l = d.length;
85
- var i = sql.indexOf(d);
86
- if (i < 0) {
87
- i = sql.indexOf(D);
88
- }
89
- if (i < 0) {
90
- l = S.length;
91
- i = sql.indexOf(s);
92
- }
93
- if (i < 0) {
94
- i = sql.indexOf(S);
95
- }
96
- if (i >= 0) {
97
- return sql.substr(0, l) + " count(*) over() as " + total + ", " + sql.substr(l) + " offset " + offset + " rows fetch next " + limit + " rows only";
98
- }
99
- else {
100
- return sql + " offset " + offset + " rows fetch next " + limit + " rows only";
101
- }
84
+ if (!total || total.length === 0) {
85
+ total = 'total';
86
+ }
87
+ if (!offset) {
88
+ offset = 0;
89
+ }
90
+ var l = d.length;
91
+ var i = sql.indexOf(d);
92
+ if (i < 0) {
93
+ i = sql.indexOf(D);
94
+ }
95
+ if (i < 0) {
96
+ l = S.length;
97
+ i = sql.indexOf(s);
98
+ }
99
+ if (i < 0) {
100
+ i = sql.indexOf(S);
101
+ }
102
+ if (i >= 0) {
103
+ return sql.substr(0, l) + " count(*) over() as " + total + ", " + sql.substr(l) + " offset " + offset + " rows fetch next " + limit + " rows only";
104
+ }
105
+ else {
106
+ return sql + " offset " + offset + " rows fetch next " + limit + " rows only";
107
+ }
102
108
  }
103
109
  exports.buildPagingQueryForOracle = buildPagingQueryForOracle;
104
110
  function buildCountQuery(sql) {
105
- var sql2 = sql.trim();
106
- var i = sql2.indexOf('select ');
107
- if (i < 0) {
108
- return sql;
109
- }
110
- var j = sql2.indexOf(' from ', i + 6);
111
- if (j < 0) {
112
- return sql;
113
- }
114
- var k = sql2.indexOf(' order by ', j);
115
- var h = sql2.indexOf(' distinct ');
116
- if (h > 0) {
117
- var sql3 = 'select count(*) from (' + sql2.substring(i) + ') as main';
118
- return sql3;
119
- }
120
- if (k > 0) {
121
- var sql3 = 'select count(*) ' + sql2.substring(j, k);
122
- return sql3;
123
- }
124
- else {
125
- var sql3 = 'select count(*) ' + sql2.substring(j);
126
- return sql3;
127
- }
111
+ var sql2 = sql.trim();
112
+ var i = sql2.indexOf('select ');
113
+ if (i < 0) {
114
+ return sql;
115
+ }
116
+ var j = sql2.indexOf(' from ', i + 6);
117
+ if (j < 0) {
118
+ return sql;
119
+ }
120
+ var k = sql2.indexOf(' order by ', j);
121
+ var h = sql2.indexOf(' distinct ');
122
+ if (h > 0) {
123
+ var sql3 = 'select count(*) from (' + sql2.substring(i) + ') as main';
124
+ return sql3;
125
+ }
126
+ if (k > 0) {
127
+ var sql3 = 'select count(*) ' + sql2.substring(j, k);
128
+ return sql3;
129
+ }
130
+ else {
131
+ var sql3 = 'select count(*) ' + sql2.substring(j);
132
+ return sql3;
133
+ }
128
134
  }
129
135
  exports.buildCountQuery = buildCountQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/batch.ts CHANGED
@@ -3,7 +3,7 @@ import { Attributes, Statement } from './metadata';
3
3
 
4
4
  export class SqlInserter<T> {
5
5
  version?: string;
6
- constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public map?: (v: T) => T) {
6
+ constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public oneIfSuccess?: boolean, public map?: (v: T) => T) {
7
7
  this.write = this.write.bind(this);
8
8
  const x = version(attributes);
9
9
  if (x) {
@@ -20,7 +20,11 @@ export class SqlInserter<T> {
20
20
  }
21
21
  const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
22
22
  if (stmt) {
23
- return this.exec(stmt.query, stmt.params);
23
+ if (this.oneIfSuccess) {
24
+ return this.exec(stmt.query, stmt.params).then(ct => ct > 0 ? 1 : 0);
25
+ } else {
26
+ return this.exec(stmt.query, stmt.params);
27
+ }
24
28
  } else {
25
29
  return Promise.resolve(0);
26
30
  }
@@ -29,7 +33,7 @@ export class SqlInserter<T> {
29
33
  // tslint:disable-next-line:max-classes-per-file
30
34
  export class SqlUpdater<T> {
31
35
  version?: string;
32
- constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public map?: (v: T) => T) {
36
+ constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public oneIfSuccess?: boolean, public map?: (v: T) => T) {
33
37
  this.write = this.write.bind(this);
34
38
  const x = version(attributes);
35
39
  if (x) {
@@ -46,7 +50,11 @@ export class SqlUpdater<T> {
46
50
  }
47
51
  const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
48
52
  if (stmt) {
49
- return this.exec(stmt.query, stmt.params);
53
+ if (this.oneIfSuccess) {
54
+ return this.exec(stmt.query, stmt.params).then(ct => ct > 0 ? 1 : 0);
55
+ } else {
56
+ return this.exec(stmt.query, stmt.params);
57
+ }
50
58
  } else {
51
59
  return Promise.resolve(0);
52
60
  }
@@ -55,7 +63,7 @@ export class SqlUpdater<T> {
55
63
  // tslint:disable-next-line:max-classes-per-file
56
64
  export class SqlBatchInserter<T> {
57
65
  version?: string;
58
- constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: ((i: number) => string) | boolean, public map?: (v: T) => T) {
66
+ constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: ((i: number) => string) | boolean, public oneIfSuccess?: boolean, public map?: (v: T) => T) {
59
67
  this.write = this.write.bind(this);
60
68
  const x = version(attributes);
61
69
  if (x) {
@@ -76,7 +84,11 @@ export class SqlBatchInserter<T> {
76
84
  }
77
85
  const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
78
86
  if (stmt) {
79
- return this.exec(stmt.query, stmt.params);
87
+ if (this.oneIfSuccess) {
88
+ return this.exec(stmt.query, stmt.params).then(ct => objs.length);
89
+ } else {
90
+ return this.exec(stmt.query, stmt.params);
91
+ }
80
92
  } else {
81
93
  return Promise.resolve(0);
82
94
  }
@@ -85,7 +97,7 @@ export class SqlBatchInserter<T> {
85
97
  // tslint:disable-next-line:max-classes-per-file
86
98
  export class SqlBatchUpdater<T> {
87
99
  version?: string;
88
- constructor(public execBatch: (statements: Statement[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, protected notSkipInvalid?: boolean, public map?: (v: T) => T) {
100
+ constructor(public execBatch: (statements: Statement[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public oneIfSuccess?: boolean, protected notSkipInvalid?: boolean, public map?: (v: T) => T) {
89
101
  this.write = this.write.bind(this);
90
102
  const x = version(attributes);
91
103
  if (x) {
@@ -106,7 +118,11 @@ export class SqlBatchUpdater<T> {
106
118
  }
107
119
  const stmts = buildToUpdateBatch(list, this.table, this.attributes, this.param, this.notSkipInvalid);
108
120
  if (stmts && stmts.length > 0) {
109
- return this.execBatch(stmts);
121
+ if (this.oneIfSuccess) {
122
+ return this.execBatch(stmts).then(ct => stmts.length);
123
+ } else {
124
+ return this.execBatch(stmts);
125
+ }
110
126
  } else {
111
127
  return Promise.resolve(0);
112
128
  }
package/src/search.ts CHANGED
@@ -32,7 +32,12 @@ export function buildFromQuery<T>(query: <K>(sql: string, args?: any[], m?: Stri
32
32
  });
33
33
  return Promise.all([resultPromise, countPromise]).then(r => {
34
34
  const [list, total] = r;
35
- return {list, total};
35
+ if (typeof total === 'string' && !isNaN(total)) {
36
+ const t = parseInt(total, 10);
37
+ return {list, total: t};
38
+ } else {
39
+ return {list, total};
40
+ }
36
41
  });
37
42
  }
38
43
  }