db-wrapper 0.0.7 → 0.0.9
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/LICENSE +201 -201
- package/README.md +24 -24
- package/bundles/db-wrapper.umd.js +718 -1027
- package/bundles/db-wrapper.umd.js.map +1 -1
- package/bundles/db-wrapper.umd.min.js +15 -1
- package/bundles/db-wrapper.umd.min.js.map +1 -1
- package/db-wrapper.d.ts +4 -4
- package/db-wrapper.metadata.json +1 -1
- package/esm2015/db-wrapper.js +5 -9
- package/esm2015/lib/db-wrapper.models.js +45 -206
- package/esm2015/lib/db-wrapper.module.js +21 -27
- package/esm2015/lib/db-wrapper.service.js +410 -779
- package/esm2015/lib/window.service.js +16 -24
- package/esm2015/public-api.js +7 -11
- package/esm5/db-wrapper.js +5 -9
- package/esm5/lib/db-wrapper.models.js +45 -206
- package/esm5/lib/db-wrapper.module.js +25 -36
- package/esm5/lib/db-wrapper.service.js +436 -895
- package/esm5/lib/window.service.js +23 -32
- package/esm5/public-api.js +7 -11
- package/fesm2015/db-wrapper.js +483 -851
- package/fesm2015/db-wrapper.js.map +1 -1
- package/fesm5/db-wrapper.js +518 -980
- package/fesm5/db-wrapper.js.map +1 -1
- package/lib/db-wrapper.models.d.ts +123 -119
- package/lib/db-wrapper.module.d.ts +5 -5
- package/lib/db-wrapper.service.d.ts +40 -37
- package/lib/window.service.d.ts +3 -3
- package/package.json +7 -7
- package/public-api.d.ts +3 -3
|
@@ -1,1063 +1,754 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
|
|
3
3
|
typeof define === 'function' && define.amd ? define('db-wrapper', ['exports', '@angular/core'], factory) :
|
|
4
|
-
(factory(
|
|
5
|
-
}(this, (function (exports,core) { 'use strict';
|
|
4
|
+
(global = global || self, factory(global['db-wrapper'] = {}, global.ng.core));
|
|
5
|
+
}(this, (function (exports, core) { 'use strict';
|
|
6
6
|
|
|
7
7
|
/*! *****************************************************************************
|
|
8
|
-
Copyright (c) Microsoft Corporation.
|
|
9
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
10
|
-
this file except in compliance with the License. You may obtain a copy of the
|
|
11
|
-
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
16
|
-
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
17
12
|
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
20
|
***************************************************************************** */
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
/* global Reflect, Promise */
|
|
22
|
+
|
|
23
|
+
var extendStatics = function(d, b) {
|
|
24
|
+
extendStatics = Object.setPrototypeOf ||
|
|
25
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
26
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
27
|
+
return extendStatics(d, b);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function __extends(d, b) {
|
|
31
|
+
extendStatics(d, b);
|
|
32
|
+
function __() { this.constructor = d; }
|
|
33
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
var __assign = function() {
|
|
37
|
+
__assign = Object.assign || function __assign(t) {
|
|
38
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
39
|
+
s = arguments[i];
|
|
40
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
25
41
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
return t;
|
|
43
|
+
};
|
|
44
|
+
return __assign.apply(this, arguments);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function __rest(s, e) {
|
|
48
|
+
var t = {};
|
|
49
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
50
|
+
t[p] = s[p];
|
|
51
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
52
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
53
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
54
|
+
t[p[i]] = s[p[i]];
|
|
31
55
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
56
|
+
return t;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function __decorate(decorators, target, key, desc) {
|
|
60
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
61
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
62
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
63
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function __param(paramIndex, decorator) {
|
|
67
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function __metadata(metadataKey, metadataValue) {
|
|
71
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
75
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
76
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
77
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
78
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
79
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
36
80
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
37
81
|
});
|
|
38
82
|
}
|
|
83
|
+
|
|
39
84
|
function __generator(thisArg, body) {
|
|
40
|
-
var _ = { label: 0, sent: function
|
|
41
|
-
|
|
42
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
|
|
85
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
86
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
43
87
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
44
88
|
function step(op) {
|
|
45
|
-
if (f)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
case 5:
|
|
62
|
-
_.label++;
|
|
63
|
-
y = op[1];
|
|
64
|
-
op = [0];
|
|
65
|
-
continue;
|
|
66
|
-
case 7:
|
|
67
|
-
op = _.ops.pop();
|
|
68
|
-
_.trys.pop();
|
|
69
|
-
continue;
|
|
70
|
-
default:
|
|
71
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
72
|
-
_ = 0;
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
|
76
|
-
_.label = op[1];
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
80
|
-
_.label = t[1];
|
|
81
|
-
t = op;
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
if (t && _.label < t[2]) {
|
|
85
|
-
_.label = t[2];
|
|
86
|
-
_.ops.push(op);
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
if (t[2])
|
|
90
|
-
_.ops.pop();
|
|
91
|
-
_.trys.pop();
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
op = body.call(thisArg, _);
|
|
95
|
-
}
|
|
96
|
-
catch (e) {
|
|
97
|
-
op = [6, e];
|
|
98
|
-
y = 0;
|
|
99
|
-
}
|
|
100
|
-
finally {
|
|
101
|
-
f = t = 0;
|
|
89
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
90
|
+
while (_) try {
|
|
91
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
92
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
93
|
+
switch (op[0]) {
|
|
94
|
+
case 0: case 1: t = op; break;
|
|
95
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
96
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
97
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
98
|
+
default:
|
|
99
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
100
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
101
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
102
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
103
|
+
if (t[2]) _.ops.pop();
|
|
104
|
+
_.trys.pop(); continue;
|
|
102
105
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return { value: op[0] ? op[1] : void 0, done: true };
|
|
106
|
+
op = body.call(thisArg, _);
|
|
107
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
108
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
106
109
|
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function __createBinding(o, m, k, k2) {
|
|
113
|
+
if (k2 === undefined) k2 = k;
|
|
114
|
+
o[k2] = m[k];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function __exportStar(m, exports) {
|
|
118
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function __values(o) {
|
|
122
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
123
|
+
if (m) return m.call(o);
|
|
124
|
+
if (o && typeof o.length === "number") return {
|
|
125
|
+
next: function () {
|
|
126
|
+
if (o && i >= o.length) o = void 0;
|
|
127
|
+
return { value: o && o[i++], done: !o };
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function __read(o, n) {
|
|
134
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
135
|
+
if (!m) return o;
|
|
136
|
+
var i = m.call(o), r, ar = [], e;
|
|
137
|
+
try {
|
|
138
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
115
139
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return _window();
|
|
121
|
-
},
|
|
122
|
-
enumerable: true,
|
|
123
|
-
configurable: true
|
|
124
|
-
});
|
|
125
|
-
WindowService.decorators = [
|
|
126
|
-
{ type: core.Injectable }
|
|
127
|
-
];
|
|
128
|
-
return WindowService;
|
|
129
|
-
}());
|
|
130
|
-
/**
|
|
131
|
-
* @return {?}
|
|
132
|
-
*/
|
|
133
|
-
function _window() {
|
|
134
|
-
// Return the global native browser window object
|
|
135
|
-
return typeof window !== 'undefined' ? window : undefined;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @fileoverview added by tsickle
|
|
140
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
141
|
-
*/
|
|
142
|
-
var DbWrapperService = /** @class */ (function () {
|
|
143
|
-
function DbWrapperService(dbConfig, dbSchema) {
|
|
144
|
-
var _this = this;
|
|
145
|
-
this._dbName = dbConfig.dbName;
|
|
146
|
-
this._location = dbConfig.location || 'default';
|
|
147
|
-
this._iosDatabaseLocation = dbConfig.iosDatabaseLocation || 'Library';
|
|
148
|
-
/** @type {?} */
|
|
149
|
-
var window = (new WindowService()).nativeWindow;
|
|
150
|
-
if (dbSchema) {
|
|
151
|
-
this.schema = dbSchema;
|
|
140
|
+
catch (error) { e = { error: error }; }
|
|
141
|
+
finally {
|
|
142
|
+
try {
|
|
143
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
152
144
|
}
|
|
153
|
-
|
|
154
|
-
* @param {?} resolve
|
|
155
|
-
* @param {?} reject
|
|
156
|
-
* @return {?}
|
|
157
|
-
*/function (resolve, reject) {
|
|
158
|
-
if (typeof window.cordova === 'undefined') {
|
|
159
|
-
setTimeout(( /**
|
|
160
|
-
* @return {?}
|
|
161
|
-
*/function () {
|
|
162
|
-
_this.init(dbSchema).then(( /**
|
|
163
|
-
* @return {?}
|
|
164
|
-
*/function () {
|
|
165
|
-
resolve();
|
|
166
|
-
}))
|
|
167
|
-
.catch(( /**
|
|
168
|
-
* @param {?} err
|
|
169
|
-
* @return {?}
|
|
170
|
-
*/function (err) {
|
|
171
|
-
reject(err);
|
|
172
|
-
}));
|
|
173
|
-
}), 500);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
document.addEventListener('deviceready', ( /**
|
|
177
|
-
* @return {?}
|
|
178
|
-
*/function () {
|
|
179
|
-
console.log('dbWrapper: device ready.');
|
|
180
|
-
_this.initDevice(dbSchema).then(( /**
|
|
181
|
-
* @return {?}
|
|
182
|
-
*/function () { return resolve(); })).catch(( /**
|
|
183
|
-
* @param {?} err
|
|
184
|
-
* @return {?}
|
|
185
|
-
*/function (err) { return reject(err); }));
|
|
186
|
-
}));
|
|
187
|
-
}
|
|
188
|
-
}));
|
|
145
|
+
finally { if (e) throw e.error; }
|
|
189
146
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
reject(err);
|
|
260
|
-
})).then(( /**
|
|
261
|
-
* @return {?}
|
|
262
|
-
*/function () {
|
|
263
|
-
resolve();
|
|
264
|
-
}));
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
resolve();
|
|
268
|
-
}
|
|
269
|
-
}));
|
|
270
|
-
};
|
|
271
|
-
/**
|
|
272
|
-
* @param {?} schema
|
|
273
|
-
* @return {?}
|
|
274
|
-
*/
|
|
275
|
-
DbWrapperService.prototype.generate = /**
|
|
276
|
-
* @param {?} schema
|
|
277
|
-
* @return {?}
|
|
278
|
-
*/
|
|
279
|
-
function (schema) {
|
|
280
|
-
var _this = this;
|
|
281
|
-
/** @type {?} */
|
|
282
|
-
var sentences = [];
|
|
283
|
-
this.schema = schema;
|
|
284
|
-
if (schema.tables) {
|
|
285
|
-
schema.tables.forEach(( /**
|
|
286
|
-
* @param {?} table
|
|
287
|
-
* @return {?}
|
|
288
|
-
*/function (table) {
|
|
289
|
-
sentences.push(_this.createTable(table));
|
|
290
|
-
}));
|
|
291
|
-
}
|
|
292
|
-
if (schema.views) {
|
|
293
|
-
schema.views.forEach(( /**
|
|
294
|
-
* @param {?} view
|
|
295
|
-
* @return {?}
|
|
296
|
-
*/function (view) {
|
|
297
|
-
sentences.push(_this.createView(view));
|
|
298
|
-
}));
|
|
299
|
-
}
|
|
300
|
-
if (sentences.length === 0) {
|
|
301
|
-
throw new Error('Esquema de la base de datos vacia.');
|
|
302
|
-
}
|
|
303
|
-
return Promise.all(sentences);
|
|
304
|
-
};
|
|
305
|
-
/**
|
|
306
|
-
* @param {?} table
|
|
307
|
-
* @return {?}
|
|
308
|
-
*/
|
|
309
|
-
DbWrapperService.prototype.createTable = /**
|
|
310
|
-
* @param {?} table
|
|
311
|
-
* @return {?}
|
|
312
|
-
*/
|
|
313
|
-
function (table) {
|
|
314
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
315
|
-
var sql, onConflictPrimary, sentences, newColumns, tableDDL;
|
|
316
|
-
var _this = this;
|
|
317
|
-
return __generator(this, function (_a) {
|
|
318
|
-
switch (_a.label) {
|
|
319
|
-
case 0:
|
|
320
|
-
sql = '';
|
|
321
|
-
sentences = [];
|
|
322
|
-
newColumns = [];
|
|
323
|
-
return [4 /*yield*/, this.tableExists(table.name)];
|
|
324
|
-
case 1:
|
|
325
|
-
tableDDL = _a.sent();
|
|
326
|
-
if (tableDDL) {
|
|
327
|
-
table.columns.forEach(( /**
|
|
328
|
-
* @param {?} col
|
|
329
|
-
* @return {?}
|
|
330
|
-
*/function (col) {
|
|
331
|
-
if (!_this.columnExists(( /** @type {?} */(tableDDL)), col.name)) {
|
|
332
|
-
newColumns.push(_this.addColumn(table.name, col));
|
|
333
|
-
}
|
|
334
|
-
}));
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
table.columns.forEach(( /**
|
|
338
|
-
* @param {?} col
|
|
339
|
-
* @return {?}
|
|
340
|
-
*/function (col) {
|
|
341
|
-
sentences.push(_this.defColumn(col));
|
|
342
|
-
}));
|
|
343
|
-
}
|
|
344
|
-
onConflictPrimary = 'on conflict fail';
|
|
345
|
-
if (Array.isArray(table.primary)) {
|
|
346
|
-
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary.join(',') + ') ' + onConflictPrimary);
|
|
347
|
-
}
|
|
348
|
-
if (typeof table.primary === 'string') {
|
|
349
|
-
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary + ') ' + onConflictPrimary);
|
|
350
|
-
}
|
|
351
|
-
if (table.uniques) {
|
|
352
|
-
table.uniques.forEach(( /**
|
|
353
|
-
* @param {?} uniq
|
|
354
|
-
* @return {?}
|
|
355
|
-
*/function (uniq) {
|
|
356
|
-
sentences.push(_this.defUnique(uniq));
|
|
357
|
-
}));
|
|
358
|
-
}
|
|
359
|
-
if (table.foreigns) {
|
|
360
|
-
table.foreigns.forEach(( /**
|
|
361
|
-
* @param {?} fore
|
|
362
|
-
* @return {?}
|
|
363
|
-
*/function (fore) {
|
|
364
|
-
sentences.push(_this.defForeign(fore));
|
|
365
|
-
}));
|
|
366
|
-
}
|
|
367
|
-
if (tableDDL && newColumns.length !== 0) {
|
|
368
|
-
return [2 /*return*/, Promise.all(newColumns)
|
|
369
|
-
.then(( /**
|
|
370
|
-
* @return {?}
|
|
371
|
-
*/function () { console.log("Table " + table.name + " altered."); }))
|
|
372
|
-
.catch(( /**
|
|
373
|
-
* @param {?} err
|
|
374
|
-
* @return {?}
|
|
375
|
-
*/function (err) {
|
|
376
|
-
console.error("Table " + table.name + ": " + err.message);
|
|
377
|
-
throw new Error("Table " + table.name + ": " + err.message);
|
|
378
|
-
}))];
|
|
379
|
-
}
|
|
380
|
-
else if (!tableDDL) {
|
|
381
|
-
sql = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + sentences.join(',') + ')';
|
|
382
|
-
return [2 /*return*/, this.query(sql)
|
|
383
|
-
.then(( /**
|
|
384
|
-
* @return {?}
|
|
385
|
-
*/function () { console.log("Table " + table.name + " created."); }))
|
|
386
|
-
.catch(( /**
|
|
387
|
-
* @param {?} err
|
|
388
|
-
* @return {?}
|
|
389
|
-
*/function (err) {
|
|
390
|
-
console.error("Table " + table.name + ": " + err.message);
|
|
391
|
-
throw new Error("Table " + table.name + ": " + err.message);
|
|
392
|
-
}))];
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
return [2 /*return*/, Promise.resolve().then(( /**
|
|
396
|
-
* @return {?}
|
|
397
|
-
*/function () { console.log("Table " + table.name + " initialized."); }))];
|
|
398
|
-
}
|
|
399
|
-
return [2 /*return*/];
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
});
|
|
403
|
-
};
|
|
404
|
-
/**
|
|
405
|
-
* @param {?} view
|
|
406
|
-
* @return {?}
|
|
407
|
-
*/
|
|
408
|
-
DbWrapperService.prototype.createView = /**
|
|
409
|
-
* @param {?} view
|
|
410
|
-
* @return {?}
|
|
411
|
-
*/
|
|
412
|
-
function (view) {
|
|
413
|
-
/** @type {?} */
|
|
414
|
-
var sql;
|
|
415
|
-
sql = 'CREATE VIEW IF NOT EXISTS ' + view.name
|
|
416
|
-
+ ' AS ' + view.sql;
|
|
417
|
-
return this.query(sql);
|
|
418
|
-
};
|
|
419
|
-
/**
|
|
420
|
-
* @param {?} tableName
|
|
421
|
-
* @return {?}
|
|
422
|
-
*/
|
|
423
|
-
DbWrapperService.prototype.getSchemaTable = /**
|
|
424
|
-
* @param {?} tableName
|
|
425
|
-
* @return {?}
|
|
426
|
-
*/
|
|
427
|
-
function (tableName) {
|
|
428
|
-
/** @type {?} */
|
|
429
|
-
var res;
|
|
430
|
-
this.schema.tables.forEach(( /**
|
|
431
|
-
* @param {?} table
|
|
432
|
-
* @return {?}
|
|
433
|
-
*/function (table) {
|
|
434
|
-
if (table.name === tableName) {
|
|
435
|
-
res = table;
|
|
436
|
-
}
|
|
437
|
-
}));
|
|
438
|
-
return res;
|
|
439
|
-
};
|
|
440
|
-
/**
|
|
441
|
-
* @param {?} table
|
|
442
|
-
* @return {?}
|
|
443
|
-
*/
|
|
444
|
-
DbWrapperService.prototype.getUniquesTable = /**
|
|
445
|
-
* @param {?} table
|
|
446
|
-
* @return {?}
|
|
447
|
-
*/
|
|
448
|
-
function (table) {
|
|
449
|
-
/** @type {?} */
|
|
450
|
-
var res;
|
|
451
|
-
table.uniques.forEach(( /**
|
|
452
|
-
* @param {?} uniq
|
|
453
|
-
* @return {?}
|
|
454
|
-
*/function (uniq) {
|
|
455
|
-
res[uniq.name] = uniq;
|
|
456
|
-
}));
|
|
457
|
-
return res;
|
|
458
|
-
};
|
|
459
|
-
/**
|
|
460
|
-
* @param {?} query
|
|
461
|
-
* @param {?=} bindings
|
|
462
|
-
* @return {?}
|
|
463
|
-
*/
|
|
464
|
-
DbWrapperService.prototype.query = /**
|
|
465
|
-
* @param {?} query
|
|
466
|
-
* @param {?=} bindings
|
|
467
|
-
* @return {?}
|
|
468
|
-
*/
|
|
469
|
-
function (query, bindings) {
|
|
470
|
-
var _this = this;
|
|
471
|
-
if (bindings === void 0) {
|
|
472
|
-
bindings = [];
|
|
473
|
-
}
|
|
474
|
-
return new Promise(( /**
|
|
475
|
-
* @param {?} resolve
|
|
476
|
-
* @param {?} reject
|
|
477
|
-
* @return {?}
|
|
478
|
-
*/function (resolve, reject) {
|
|
479
|
-
if (query.indexOf('select') === 0) {
|
|
480
|
-
_this.db.readTransaction(( /**
|
|
481
|
-
* @param {?} transaction
|
|
482
|
-
* @return {?}
|
|
483
|
-
*/function (transaction) {
|
|
484
|
-
transaction.executeSql(query, bindings, ( /**
|
|
485
|
-
* @param {?} transaction
|
|
486
|
-
* @param {?} result
|
|
487
|
-
* @return {?}
|
|
488
|
-
*/function (transaction, result) {
|
|
489
|
-
resolve(result);
|
|
490
|
-
}), ( /**
|
|
491
|
-
* @param {?} transaction
|
|
492
|
-
* @param {?} error
|
|
493
|
-
* @return {?}
|
|
494
|
-
*/function (transaction, error) {
|
|
495
|
-
reject(error);
|
|
496
|
-
}));
|
|
497
|
-
}), ( /**
|
|
498
|
-
* @param {?} err
|
|
499
|
-
* @return {?}
|
|
500
|
-
*/function (err) { return reject(err); }));
|
|
501
|
-
}
|
|
502
|
-
else {
|
|
503
|
-
_this.db.transaction(( /**
|
|
504
|
-
* @param {?} transaction
|
|
505
|
-
* @return {?}
|
|
506
|
-
*/function (transaction) {
|
|
507
|
-
transaction.executeSql(query, bindings, ( /**
|
|
508
|
-
* @param {?} transaction
|
|
509
|
-
* @param {?} result
|
|
510
|
-
* @return {?}
|
|
511
|
-
*/function (transaction, result) {
|
|
512
|
-
resolve(result);
|
|
513
|
-
}), ( /**
|
|
514
|
-
* @param {?} transaction
|
|
515
|
-
* @param {?} error
|
|
516
|
-
* @return {?}
|
|
517
|
-
*/function (transaction, error) {
|
|
518
|
-
reject(error);
|
|
519
|
-
}));
|
|
520
|
-
}), ( /**
|
|
521
|
-
* @param {?} err
|
|
522
|
-
* @return {?}
|
|
523
|
-
*/function (err) { return reject(err); }));
|
|
524
|
-
}
|
|
525
|
-
}));
|
|
526
|
-
};
|
|
527
|
-
/**
|
|
528
|
-
* @param {?} query
|
|
529
|
-
* @param {?=} bindings
|
|
530
|
-
* @return {?}
|
|
531
|
-
*/
|
|
532
|
-
DbWrapperService.prototype.Count = /**
|
|
533
|
-
* @param {?} query
|
|
534
|
-
* @param {?=} bindings
|
|
535
|
-
* @return {?}
|
|
536
|
-
*/
|
|
537
|
-
function (query, bindings) {
|
|
538
|
-
var _this = this;
|
|
539
|
-
if (bindings === void 0) {
|
|
540
|
-
bindings = [];
|
|
541
|
-
}
|
|
542
|
-
return this.query(query, bindings).then(( /**
|
|
543
|
-
* @param {?} rows
|
|
544
|
-
* @return {?}
|
|
545
|
-
*/function (rows) {
|
|
546
|
-
return _this.rowsCount(rows);
|
|
547
|
-
}));
|
|
548
|
-
};
|
|
549
|
-
/**
|
|
550
|
-
* @param {?} table
|
|
551
|
-
* @param {?} bindings
|
|
552
|
-
* @return {?}
|
|
553
|
-
*/
|
|
554
|
-
DbWrapperService.prototype.insert = /**
|
|
555
|
-
* @param {?} table
|
|
556
|
-
* @param {?} bindings
|
|
557
|
-
* @return {?}
|
|
558
|
-
*/
|
|
559
|
-
function (table, bindings) {
|
|
560
|
-
var _this = this;
|
|
561
|
-
/** @type {?} */
|
|
562
|
-
var sql = '';
|
|
563
|
-
/** @type {?} */
|
|
564
|
-
var sqlBinds = [];
|
|
565
|
-
/** @type {?} */
|
|
566
|
-
var sqlKeys = [];
|
|
567
|
-
/** @type {?} */
|
|
568
|
-
var sqlValues = [];
|
|
569
|
-
sql = sql + 'insert into ' + table + ' ';
|
|
570
|
-
for (var index in bindings) {
|
|
571
|
-
if (bindings.hasOwnProperty(index)) {
|
|
572
|
-
sqlKeys.push(index);
|
|
573
|
-
sqlValues.push(bindings[index]);
|
|
574
|
-
sqlBinds.push('?');
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
sql = sql + '(' + sqlKeys.join(',') + ') values (' + sqlBinds.join(',') + ')';
|
|
578
|
-
return new Promise(( /**
|
|
579
|
-
* @param {?} resolve
|
|
580
|
-
* @param {?} reject
|
|
581
|
-
* @return {?}
|
|
582
|
-
*/function (resolve, reject) {
|
|
583
|
-
_this.db.transaction(( /**
|
|
584
|
-
* @param {?} transaction
|
|
585
|
-
* @return {?}
|
|
586
|
-
*/function (transaction) {
|
|
587
|
-
transaction.executeSql(sql, sqlValues, ( /**
|
|
588
|
-
* @param {?} transaction
|
|
589
|
-
* @param {?} result
|
|
590
|
-
* @return {?}
|
|
591
|
-
*/function (transaction, result) {
|
|
592
|
-
resolve(result);
|
|
593
|
-
}), ( /**
|
|
594
|
-
* @param {?} transaction
|
|
595
|
-
* @param {?} error
|
|
596
|
-
* @return {?}
|
|
597
|
-
*/function (transaction, error) {
|
|
598
|
-
reject(error);
|
|
599
|
-
}));
|
|
600
|
-
}));
|
|
601
|
-
}));
|
|
602
|
-
};
|
|
603
|
-
/**
|
|
604
|
-
* @param {?} table
|
|
605
|
-
* @param {?} bindings
|
|
606
|
-
* @param {?} where
|
|
607
|
-
* @return {?}
|
|
608
|
-
*/
|
|
609
|
-
DbWrapperService.prototype.update = /**
|
|
610
|
-
* @param {?} table
|
|
611
|
-
* @param {?} bindings
|
|
612
|
-
* @param {?} where
|
|
613
|
-
* @return {?}
|
|
614
|
-
*/
|
|
615
|
-
function (table, bindings, where) {
|
|
616
|
-
var _this = this;
|
|
617
|
-
/** @type {?} */
|
|
618
|
-
var sql = '';
|
|
619
|
-
/** @type {?} */
|
|
620
|
-
var sqlBinds = [];
|
|
621
|
-
/** @type {?} */
|
|
622
|
-
var sqlKeys = [];
|
|
623
|
-
/** @type {?} */
|
|
624
|
-
var sqlValues = [];
|
|
625
|
-
sql = sql + 'update ' + table + ' set ';
|
|
626
|
-
for (var index in bindings) {
|
|
627
|
-
if (bindings.hasOwnProperty(index)) {
|
|
628
|
-
/** @type {?} */
|
|
629
|
-
var newObj = bindings[index];
|
|
630
|
-
if (typeof newObj === 'object' && newObj !== null) {
|
|
631
|
-
sqlValues.push(newObj.value);
|
|
632
|
-
if (newObj.ifnull) {
|
|
633
|
-
sqlBinds.push(index + ' = ifnull( ' + index + ',0) + ?');
|
|
634
|
-
}
|
|
635
|
-
else if (newObj.operator) {
|
|
636
|
-
sqlBinds.push(index + ' = ' + index + newObj.operator + ' ?');
|
|
637
|
-
}
|
|
638
|
-
else {
|
|
639
|
-
sqlBinds.push(index + ' = ?');
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
else {
|
|
643
|
-
sqlBinds.push(index + ' = ?');
|
|
644
|
-
sqlValues.push(bindings[index]);
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
for (var index in where) {
|
|
649
|
-
if (where.hasOwnProperty(index)) {
|
|
650
|
-
if (typeof where[index] === 'object' && where[index] !== null) {
|
|
651
|
-
sqlKeys.push(index + ' ' + where[index].operator + ' ?');
|
|
652
|
-
sqlValues.push(where[index].value);
|
|
653
|
-
}
|
|
654
|
-
else {
|
|
655
|
-
sqlKeys.push(index + ' = ?');
|
|
656
|
-
sqlValues.push(where[index]);
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
sql = sql + sqlBinds.join(',') + ' where ' + sqlKeys.join(' and ');
|
|
661
|
-
return new Promise(( /**
|
|
662
|
-
* @param {?} resolve
|
|
663
|
-
* @param {?} reject
|
|
664
|
-
* @return {?}
|
|
665
|
-
*/function (resolve, reject) {
|
|
666
|
-
_this.db.transaction(( /**
|
|
667
|
-
* @param {?} transaction
|
|
668
|
-
* @return {?}
|
|
669
|
-
*/function (transaction) {
|
|
670
|
-
transaction.executeSql(sql, sqlValues, ( /**
|
|
671
|
-
* @param {?} transaction
|
|
672
|
-
* @param {?} result
|
|
673
|
-
* @return {?}
|
|
674
|
-
*/function (transaction, result) {
|
|
675
|
-
resolve(result);
|
|
676
|
-
}), ( /**
|
|
677
|
-
* @param {?} transaction
|
|
678
|
-
* @param {?} error
|
|
679
|
-
* @return {?}
|
|
680
|
-
*/function (transaction, error) {
|
|
681
|
-
reject(error);
|
|
682
|
-
}));
|
|
683
|
-
}));
|
|
684
|
-
}));
|
|
685
|
-
};
|
|
686
|
-
/**
|
|
687
|
-
* @param {?} tables
|
|
688
|
-
* @return {?}
|
|
689
|
-
*/
|
|
690
|
-
DbWrapperService.prototype.dropTable = /**
|
|
691
|
-
* @param {?} tables
|
|
692
|
-
* @return {?}
|
|
693
|
-
*/
|
|
694
|
-
function (tables) {
|
|
695
|
-
var _this = this;
|
|
696
|
-
/** @type {?} */
|
|
697
|
-
var cont = 1;
|
|
698
|
-
cont = tables.length;
|
|
699
|
-
return new Promise(( /**
|
|
700
|
-
* @param {?} resolve
|
|
701
|
-
* @param {?} reject
|
|
702
|
-
* @return {?}
|
|
703
|
-
*/function (resolve, reject) {
|
|
704
|
-
tables.some(( /**
|
|
705
|
-
* @param {?} table
|
|
706
|
-
* @return {?}
|
|
707
|
-
*/function (table) {
|
|
708
|
-
if (cont !== 0) {
|
|
709
|
-
_this.query('DROP TABLE ' + table)
|
|
710
|
-
.then(( /**
|
|
711
|
-
* @return {?}
|
|
712
|
-
*/function () {
|
|
713
|
-
if (cont === 1) {
|
|
714
|
-
resolve();
|
|
715
|
-
}
|
|
716
|
-
else {
|
|
717
|
-
cont--;
|
|
718
|
-
}
|
|
719
|
-
}), ( /**
|
|
720
|
-
* @param {?} err
|
|
721
|
-
* @return {?}
|
|
722
|
-
*/function (err) {
|
|
723
|
-
cont = 0;
|
|
724
|
-
reject(err);
|
|
725
|
-
return true;
|
|
726
|
-
}));
|
|
727
|
-
return false;
|
|
728
|
-
}
|
|
729
|
-
}));
|
|
730
|
-
}));
|
|
731
|
-
};
|
|
732
|
-
/**
|
|
733
|
-
* @param {?} result
|
|
734
|
-
* @return {?}
|
|
735
|
-
*/
|
|
736
|
-
DbWrapperService.prototype.fetchAll = /**
|
|
737
|
-
* @param {?} result
|
|
738
|
-
* @return {?}
|
|
739
|
-
*/
|
|
740
|
-
function (result) {
|
|
741
|
-
/** @type {?} */
|
|
742
|
-
var output = [];
|
|
743
|
-
for (var i = 0; i < result.rows.length; i++) {
|
|
744
|
-
output.push(result.rows.item(i));
|
|
745
|
-
}
|
|
746
|
-
return output;
|
|
747
|
-
};
|
|
748
|
-
/**
|
|
749
|
-
* @param {?} result
|
|
750
|
-
* @return {?}
|
|
751
|
-
*/
|
|
752
|
-
DbWrapperService.prototype.fetch = /**
|
|
753
|
-
* @param {?} result
|
|
754
|
-
* @return {?}
|
|
755
|
-
*/
|
|
756
|
-
function (result) {
|
|
757
|
-
/** @type {?} */
|
|
758
|
-
var objeto = undefined;
|
|
759
|
-
/** @type {?} */
|
|
760
|
-
var newObj = undefined;
|
|
761
|
-
if (result.rows.length !== 0) {
|
|
762
|
-
objeto = result.rows.item(0);
|
|
763
|
-
newObj = {};
|
|
764
|
-
/** @type {?} */
|
|
765
|
-
var keys = Object.keys(objeto);
|
|
766
|
-
for (var key in keys) {
|
|
767
|
-
newObj[keys[key]] = objeto[keys[key]];
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
return newObj;
|
|
771
|
-
};
|
|
772
|
-
/**
|
|
773
|
-
* @param {?} result
|
|
774
|
-
* @return {?}
|
|
775
|
-
*/
|
|
776
|
-
DbWrapperService.prototype.rowsCount = /**
|
|
777
|
-
* @param {?} result
|
|
778
|
-
* @return {?}
|
|
779
|
-
*/
|
|
780
|
-
function (result) {
|
|
781
|
-
return result.rows.length;
|
|
782
|
-
};
|
|
783
|
-
/* Private */
|
|
784
|
-
/* Private */
|
|
785
|
-
/**
|
|
786
|
-
* @private
|
|
787
|
-
* @param {?} col
|
|
788
|
-
* @return {?}
|
|
789
|
-
*/
|
|
790
|
-
DbWrapperService.prototype.defColumn = /* Private */
|
|
791
|
-
/**
|
|
792
|
-
* @private
|
|
793
|
-
* @param {?} col
|
|
794
|
-
* @return {?}
|
|
795
|
-
*/
|
|
796
|
-
function (col) {
|
|
797
|
-
/** @type {?} */
|
|
798
|
-
var colSql;
|
|
799
|
-
colSql = col.name + ' ' + col.type;
|
|
800
|
-
if (!col.isNullable) {
|
|
801
|
-
colSql += ' NOT NULL ';
|
|
802
|
-
}
|
|
803
|
-
if (col.default) {
|
|
804
|
-
colSql += ' DEFAULT ' + col.default + ' ';
|
|
805
|
-
}
|
|
806
|
-
if (col.type === 'text' && col.length) {
|
|
807
|
-
colSql += " CONSTRAINT " + col.name + "_length CHECK ( ";
|
|
808
|
-
if (col.isNullable) {
|
|
809
|
-
colSql += col.name + " is null OR ";
|
|
810
|
-
}
|
|
811
|
-
colSql += "length(" + col.name + ") <= " + col.length + " )";
|
|
812
|
-
}
|
|
813
|
-
return colSql;
|
|
814
|
-
};
|
|
815
|
-
/**
|
|
816
|
-
* @private
|
|
817
|
-
* @param {?} unique
|
|
818
|
-
* @return {?}
|
|
819
|
-
*/
|
|
820
|
-
DbWrapperService.prototype.defUnique = /**
|
|
821
|
-
* @private
|
|
822
|
-
* @param {?} unique
|
|
823
|
-
* @return {?}
|
|
824
|
-
*/
|
|
825
|
-
function (unique) {
|
|
826
|
-
/** @type {?} */
|
|
827
|
-
var uniqSql;
|
|
828
|
-
// depende del entorno, producción o desarrollo
|
|
829
|
-
/** @type {?} */
|
|
830
|
-
var onConflictUnique = 'on conflict fail';
|
|
831
|
-
if (Array.isArray(unique.fields)) {
|
|
832
|
-
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields.join(',') + " ) " + onConflictUnique + " ";
|
|
833
|
-
}
|
|
834
|
-
if (typeof unique.fields === 'string') {
|
|
835
|
-
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields + " ) " + onConflictUnique + " ";
|
|
836
|
-
}
|
|
837
|
-
return uniqSql;
|
|
838
|
-
};
|
|
839
|
-
/**
|
|
840
|
-
* @private
|
|
841
|
-
* @param {?} fore
|
|
842
|
-
* @return {?}
|
|
843
|
-
*/
|
|
844
|
-
DbWrapperService.prototype.defForeign = /**
|
|
845
|
-
* @private
|
|
846
|
-
* @param {?} fore
|
|
847
|
-
* @return {?}
|
|
848
|
-
*/
|
|
849
|
-
function (fore) {
|
|
850
|
-
/** @type {?} */
|
|
851
|
-
var foreSql;
|
|
852
|
-
if (Array.isArray(fore.foreignKey)) {
|
|
853
|
-
foreSql = " FOREIGN KEY ( " + fore.foreignKey.join(',') + " ) ";
|
|
854
|
-
}
|
|
855
|
-
if (typeof fore.foreignKey === 'string') {
|
|
856
|
-
foreSql = " FOREIGN KEY ( " + fore.foreignKey + " ) ";
|
|
857
|
-
}
|
|
858
|
-
if (Array.isArray(fore.primaryKey)) {
|
|
859
|
-
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey.join(',') + " ) ";
|
|
860
|
-
}
|
|
861
|
-
if (typeof fore.primaryKey === 'string') {
|
|
862
|
-
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey + " ) ";
|
|
863
|
-
}
|
|
864
|
-
return foreSql;
|
|
865
|
-
};
|
|
866
|
-
/**
|
|
867
|
-
* @private
|
|
868
|
-
* @param {?} table
|
|
869
|
-
* @return {?}
|
|
870
|
-
*/
|
|
871
|
-
DbWrapperService.prototype.tableExists = /**
|
|
872
|
-
* @private
|
|
873
|
-
* @param {?} table
|
|
874
|
-
* @return {?}
|
|
875
|
-
*/
|
|
876
|
-
function (table) {
|
|
877
|
-
var _this = this;
|
|
878
|
-
return this.query('select sql from sqlite_master ' +
|
|
879
|
-
'where type=\'table\' and name=\'' + table + '\'')
|
|
880
|
-
.then(( /**
|
|
881
|
-
* @param {?} result
|
|
882
|
-
* @return {?}
|
|
883
|
-
*/function (result) {
|
|
884
|
-
if (_this.rowsCount(result) === 1) {
|
|
885
|
-
return ( /** @type {?} */((( /** @type {?} */(_this.fetch(result)))).sql));
|
|
886
|
-
}
|
|
887
|
-
else {
|
|
888
|
-
return false;
|
|
889
|
-
}
|
|
890
|
-
}));
|
|
891
|
-
};
|
|
892
|
-
/**
|
|
893
|
-
* @private
|
|
894
|
-
* @param {?} tableDDL
|
|
895
|
-
* @param {?} column
|
|
896
|
-
* @return {?}
|
|
897
|
-
*/
|
|
898
|
-
DbWrapperService.prototype.columnExists = /**
|
|
899
|
-
* @private
|
|
900
|
-
* @param {?} tableDDL
|
|
901
|
-
* @param {?} column
|
|
902
|
-
* @return {?}
|
|
903
|
-
*/
|
|
904
|
-
function (tableDDL, column) {
|
|
905
|
-
if (tableDDL.indexOf(column) === -1) {
|
|
906
|
-
return false;
|
|
907
|
-
}
|
|
908
|
-
else {
|
|
909
|
-
return true;
|
|
910
|
-
}
|
|
911
|
-
};
|
|
912
|
-
/**
|
|
913
|
-
* @private
|
|
914
|
-
* @param {?} table
|
|
915
|
-
* @param {?} col
|
|
916
|
-
* @return {?}
|
|
917
|
-
*/
|
|
918
|
-
DbWrapperService.prototype.addColumn = /**
|
|
919
|
-
* @private
|
|
920
|
-
* @param {?} table
|
|
921
|
-
* @param {?} col
|
|
922
|
-
* @return {?}
|
|
923
|
-
*/
|
|
924
|
-
function (table, col) {
|
|
925
|
-
/** @type {?} */
|
|
926
|
-
var colSql = this.defColumn(col);
|
|
927
|
-
/** @type {?} */
|
|
928
|
-
var query = 'alter TABLE ' + table + ' add COLUMN ' + colSql;
|
|
929
|
-
return this.query(query);
|
|
930
|
-
};
|
|
931
|
-
return DbWrapperService;
|
|
932
|
-
}());
|
|
933
|
-
/** @type {?} */
|
|
934
|
-
var DB_CONFIG = new core.InjectionToken('dbWrapperConfig');
|
|
935
|
-
/** @type {?} */
|
|
936
|
-
var DB_SCHEMA = new core.InjectionToken('dbWrapperSchema');
|
|
937
|
-
/**
|
|
938
|
-
* @return {?}
|
|
939
|
-
*/
|
|
940
|
-
function getDefaultDbConfig() {
|
|
941
|
-
return {
|
|
942
|
-
dbName: 'db.sqlite',
|
|
943
|
-
location: 'default',
|
|
944
|
-
iosDatabaseLocation: 'Library'
|
|
945
|
-
};
|
|
147
|
+
return ar;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function __spread() {
|
|
151
|
+
for (var ar = [], i = 0; i < arguments.length; i++)
|
|
152
|
+
ar = ar.concat(__read(arguments[i]));
|
|
153
|
+
return ar;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function __spreadArrays() {
|
|
157
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
158
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
159
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
160
|
+
r[k] = a[j];
|
|
161
|
+
return r;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
function __await(v) {
|
|
165
|
+
return this instanceof __await ? (this.v = v, this) : new __await(v);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function __asyncGenerator(thisArg, _arguments, generator) {
|
|
169
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
170
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
171
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
172
|
+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
|
173
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
174
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
175
|
+
function fulfill(value) { resume("next", value); }
|
|
176
|
+
function reject(value) { resume("throw", value); }
|
|
177
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function __asyncDelegator(o) {
|
|
181
|
+
var i, p;
|
|
182
|
+
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
|
|
183
|
+
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function __asyncValues(o) {
|
|
187
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
188
|
+
var m = o[Symbol.asyncIterator], i;
|
|
189
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
190
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
191
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function __makeTemplateObject(cooked, raw) {
|
|
195
|
+
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
|
|
196
|
+
return cooked;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function __importStar(mod) {
|
|
200
|
+
if (mod && mod.__esModule) return mod;
|
|
201
|
+
var result = {};
|
|
202
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
203
|
+
result.default = mod;
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function __importDefault(mod) {
|
|
208
|
+
return (mod && mod.__esModule) ? mod : { default: mod };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function __classPrivateFieldGet(receiver, privateMap) {
|
|
212
|
+
if (!privateMap.has(receiver)) {
|
|
213
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
214
|
+
}
|
|
215
|
+
return privateMap.get(receiver);
|
|
946
216
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
var config = !!dbConfig ? dbConfig : getDefaultDbConfig();
|
|
955
|
-
return new DbWrapperService(config, dbSchema);
|
|
217
|
+
|
|
218
|
+
function __classPrivateFieldSet(receiver, privateMap, value) {
|
|
219
|
+
if (!privateMap.has(receiver)) {
|
|
220
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
221
|
+
}
|
|
222
|
+
privateMap.set(receiver, value);
|
|
223
|
+
return value;
|
|
956
224
|
}
|
|
957
225
|
|
|
958
|
-
/**
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
* @param {?=} schema
|
|
973
|
-
* @return {?}
|
|
974
|
-
*/
|
|
975
|
-
function (dbConfig, schema) {
|
|
976
|
-
return {
|
|
977
|
-
ngModule: DbWrapperModule,
|
|
978
|
-
providers: [
|
|
979
|
-
{ provide: DB_CONFIG, useValue: dbConfig },
|
|
980
|
-
{ provide: DB_SCHEMA, useValue: schema },
|
|
981
|
-
{ provide: DbWrapperService, useFactory: provideDbWrapper, deps: [DB_CONFIG, DB_SCHEMA] }
|
|
982
|
-
]
|
|
983
|
-
};
|
|
984
|
-
};
|
|
985
|
-
DbWrapperModule.decorators = [
|
|
986
|
-
{ type: core.NgModule }
|
|
987
|
-
];
|
|
988
|
-
return DbWrapperModule;
|
|
226
|
+
var WindowService = /** @class */ (function () {
|
|
227
|
+
function WindowService() {
|
|
228
|
+
}
|
|
229
|
+
Object.defineProperty(WindowService.prototype, "nativeWindow", {
|
|
230
|
+
get: function () {
|
|
231
|
+
return _window();
|
|
232
|
+
},
|
|
233
|
+
enumerable: true,
|
|
234
|
+
configurable: true
|
|
235
|
+
});
|
|
236
|
+
WindowService = __decorate([
|
|
237
|
+
core.Injectable()
|
|
238
|
+
], WindowService);
|
|
239
|
+
return WindowService;
|
|
989
240
|
}());
|
|
241
|
+
function _window() {
|
|
242
|
+
// Return the global native browser window object
|
|
243
|
+
return typeof window !== 'undefined' ? window : undefined;
|
|
244
|
+
}
|
|
990
245
|
|
|
991
|
-
/**
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
246
|
+
var DbWrapperService = /** @class */ (function () {
|
|
247
|
+
function DbWrapperService(dbConfig, dbSchema) {
|
|
248
|
+
var _this = this;
|
|
249
|
+
this._dbName = dbConfig.dbName;
|
|
250
|
+
this._location = dbConfig.location || 'default';
|
|
251
|
+
this._iosDatabaseLocation = dbConfig.iosDatabaseLocation || 'Library';
|
|
252
|
+
this._androidDatabaseProvider = dbConfig.androidDatabaseProvider || 'default';
|
|
253
|
+
var window = (new WindowService()).nativeWindow;
|
|
254
|
+
if (dbSchema) {
|
|
255
|
+
this.schema = dbSchema;
|
|
256
|
+
}
|
|
257
|
+
this._readyPromise = new Promise(function (resolve, reject) {
|
|
258
|
+
if (typeof window.cordova === 'undefined') {
|
|
259
|
+
setTimeout(function () {
|
|
260
|
+
_this.init(dbSchema).then(function () {
|
|
261
|
+
resolve();
|
|
262
|
+
})
|
|
263
|
+
.catch(function (err) {
|
|
264
|
+
reject(err);
|
|
265
|
+
});
|
|
266
|
+
}, 500);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
document.addEventListener('deviceready', function () {
|
|
270
|
+
console.log('dbWrapper: device ready.');
|
|
271
|
+
_this.initDevice(dbSchema).then(function () { return resolve(); }).catch(function (err) { return reject(err); });
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
DbWrapperService.prototype.ready = function () {
|
|
277
|
+
return this._readyPromise;
|
|
278
|
+
};
|
|
279
|
+
DbWrapperService.prototype.initDevice = function (dbSchema) {
|
|
280
|
+
var _this = this;
|
|
281
|
+
var window = (new WindowService()).nativeWindow;
|
|
282
|
+
return new Promise(function (resolve, reject) {
|
|
283
|
+
_this.db = window.sqlitePlugin.openDatabase({
|
|
284
|
+
name: _this._dbName,
|
|
285
|
+
location: _this._location,
|
|
286
|
+
androidLockWorkaround: 1,
|
|
287
|
+
androidDatabaseProvider: _this._androidDatabaseProvider,
|
|
288
|
+
});
|
|
289
|
+
if (dbSchema) {
|
|
290
|
+
_this.generate(dbSchema).catch(function (err) {
|
|
291
|
+
reject(err);
|
|
292
|
+
}).then(function () { resolve(); });
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
resolve();
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
};
|
|
299
|
+
DbWrapperService.prototype.init = function (dbSchema) {
|
|
300
|
+
var _this = this;
|
|
301
|
+
var window = (new WindowService()).nativeWindow;
|
|
302
|
+
return new Promise(function (resolve, reject) {
|
|
303
|
+
_this.db = window.openDatabase(_this._dbName, '1.0', 'database', -1);
|
|
304
|
+
if (dbSchema) {
|
|
305
|
+
_this.generate(dbSchema).catch(function (err) {
|
|
306
|
+
reject(err);
|
|
307
|
+
}).then(function () {
|
|
308
|
+
resolve();
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
resolve();
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
DbWrapperService.prototype.generate = function (schema) {
|
|
317
|
+
var _this = this;
|
|
318
|
+
var sentences = [];
|
|
319
|
+
this.schema = schema;
|
|
320
|
+
if (schema.tables) {
|
|
321
|
+
schema.tables.forEach(function (table) {
|
|
322
|
+
sentences.push(_this.createTable(table));
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
if (schema.views) {
|
|
326
|
+
schema.views.forEach(function (view) {
|
|
327
|
+
sentences.push(_this.createView(view));
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
if (sentences.length === 0) {
|
|
331
|
+
throw new Error('Esquema de la base de datos vacia.');
|
|
332
|
+
}
|
|
333
|
+
return Promise.all(sentences);
|
|
334
|
+
};
|
|
335
|
+
DbWrapperService.prototype.createTable = function (table) {
|
|
336
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
337
|
+
var sql, onConflictPrimary, sentences, newColumns, tableDDL;
|
|
338
|
+
var _this = this;
|
|
339
|
+
return __generator(this, function (_a) {
|
|
340
|
+
switch (_a.label) {
|
|
341
|
+
case 0:
|
|
342
|
+
sql = '';
|
|
343
|
+
sentences = [], newColumns = [];
|
|
344
|
+
return [4 /*yield*/, this._tableExists(table.name)];
|
|
345
|
+
case 1:
|
|
346
|
+
tableDDL = _a.sent();
|
|
347
|
+
if (tableDDL) {
|
|
348
|
+
table.columns.forEach(function (col) {
|
|
349
|
+
if (!_this._columnExists(tableDDL, col.name)) {
|
|
350
|
+
newColumns.push(_this.addColumn(table.name, col));
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
table.columns.forEach(function (col) {
|
|
356
|
+
sentences.push(_this.defColumn(col));
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
onConflictPrimary = 'on conflict fail';
|
|
360
|
+
if (Array.isArray(table.primary)) {
|
|
361
|
+
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary.join(',') + ') ' + onConflictPrimary);
|
|
362
|
+
}
|
|
363
|
+
if (typeof table.primary === 'string') {
|
|
364
|
+
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary + ') ' + onConflictPrimary);
|
|
365
|
+
}
|
|
366
|
+
if (table.uniques) {
|
|
367
|
+
table.uniques.forEach(function (uniq) {
|
|
368
|
+
sentences.push(_this.defUnique(uniq));
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
if (table.foreigns) {
|
|
372
|
+
table.foreigns.forEach(function (fore) {
|
|
373
|
+
sentences.push(_this.defForeign(fore));
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
if (tableDDL && newColumns.length !== 0) {
|
|
377
|
+
return [2 /*return*/, Promise.all(newColumns)
|
|
378
|
+
.then(function () { console.log("Table " + table.name + " altered."); })
|
|
379
|
+
.catch(function (err) {
|
|
380
|
+
console.error("Table " + table.name + ": " + err.message);
|
|
381
|
+
throw new Error("Table " + table.name + ": " + err.message);
|
|
382
|
+
})];
|
|
383
|
+
}
|
|
384
|
+
else if (!tableDDL) {
|
|
385
|
+
sql = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + sentences.join(',') + ')';
|
|
386
|
+
return [2 /*return*/, this.query(sql)
|
|
387
|
+
.then(function () { console.log("Table " + table.name + " created."); })
|
|
388
|
+
.catch(function (err) {
|
|
389
|
+
console.error("Table " + table.name + ": " + err.message);
|
|
390
|
+
throw new Error("Table " + table.name + ": " + err.message);
|
|
391
|
+
})];
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
return [2 /*return*/, Promise.resolve().then(function () { console.log("Table " + table.name + " initialized."); })];
|
|
395
|
+
}
|
|
396
|
+
return [2 /*return*/];
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
};
|
|
401
|
+
DbWrapperService.prototype.createView = function (view) {
|
|
402
|
+
var sql;
|
|
403
|
+
sql = 'CREATE VIEW IF NOT EXISTS ' + view.name
|
|
404
|
+
+ ' AS ' + view.sql;
|
|
405
|
+
return this.query(sql);
|
|
406
|
+
};
|
|
407
|
+
DbWrapperService.prototype.getSchemaTable = function (tableName) {
|
|
408
|
+
var res;
|
|
409
|
+
this.schema.tables.forEach(function (table) {
|
|
410
|
+
if (table.name === tableName) {
|
|
411
|
+
res = table;
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
return res;
|
|
415
|
+
};
|
|
416
|
+
DbWrapperService.prototype.getUniquesTable = function (table) {
|
|
417
|
+
var res;
|
|
418
|
+
table.uniques.forEach(function (uniq) {
|
|
419
|
+
res[uniq.name] = uniq;
|
|
420
|
+
});
|
|
421
|
+
return res;
|
|
422
|
+
};
|
|
423
|
+
DbWrapperService.prototype.query = function (query, bindings) {
|
|
424
|
+
var _this = this;
|
|
425
|
+
if (bindings === void 0) { bindings = []; }
|
|
426
|
+
return new Promise(function (resolve, reject) {
|
|
427
|
+
if (query.indexOf('select') === 0) {
|
|
428
|
+
_this.db.readTransaction(function (transaction) {
|
|
429
|
+
transaction.executeSql(query, bindings, function (transaction, result) {
|
|
430
|
+
resolve(result);
|
|
431
|
+
}, function (transaction, error) {
|
|
432
|
+
reject(error);
|
|
433
|
+
});
|
|
434
|
+
}, function (err) { return reject(err); });
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
_this.db.transaction(function (transaction) {
|
|
438
|
+
transaction.executeSql(query, bindings, function (transaction, result) {
|
|
439
|
+
resolve(result);
|
|
440
|
+
}, function (transaction, error) {
|
|
441
|
+
reject(error);
|
|
442
|
+
});
|
|
443
|
+
}, function (err) { return reject(err); });
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
DbWrapperService.prototype.Count = function (query, bindings) {
|
|
448
|
+
var _this = this;
|
|
449
|
+
if (bindings === void 0) { bindings = []; }
|
|
450
|
+
return this.query(query, bindings).then(function (rows) {
|
|
451
|
+
return _this.rowsCount(rows);
|
|
452
|
+
});
|
|
453
|
+
};
|
|
454
|
+
DbWrapperService.prototype.insert = function (table, bindings) {
|
|
455
|
+
var _this = this;
|
|
456
|
+
var sql = '', sqlBinds = [], sqlKeys = [], sqlValues = [];
|
|
457
|
+
sql = sql + 'insert into ' + table + ' ';
|
|
458
|
+
for (var index in bindings) {
|
|
459
|
+
if (bindings.hasOwnProperty(index)) {
|
|
460
|
+
sqlKeys.push(index);
|
|
461
|
+
sqlValues.push(bindings[index]);
|
|
462
|
+
sqlBinds.push('?');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
sql = sql + '(' + sqlKeys.join(',') + ') values (' + sqlBinds.join(',') + ')';
|
|
466
|
+
return new Promise(function (resolve, reject) {
|
|
467
|
+
_this.db.transaction(function (transaction) {
|
|
468
|
+
transaction.executeSql(sql, sqlValues, function (transaction, result) {
|
|
469
|
+
resolve(result);
|
|
470
|
+
}, function (transaction, error) {
|
|
471
|
+
reject(error);
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
};
|
|
476
|
+
DbWrapperService.prototype.update = function (table, bindings, where) {
|
|
477
|
+
var _this = this;
|
|
478
|
+
var sql = '', sqlBinds = [], sqlKeys = [], sqlValues = [];
|
|
479
|
+
sql = sql + 'update ' + table + ' set ';
|
|
480
|
+
for (var index in bindings) {
|
|
481
|
+
if (bindings.hasOwnProperty(index)) {
|
|
482
|
+
var newObj = bindings[index];
|
|
483
|
+
if (typeof newObj === 'object' && newObj !== null) {
|
|
484
|
+
sqlValues.push(newObj.value);
|
|
485
|
+
if (newObj.ifnull) {
|
|
486
|
+
sqlBinds.push(index + ' = ifnull( ' + index + ',0) + ?');
|
|
487
|
+
}
|
|
488
|
+
else if (newObj.operator) {
|
|
489
|
+
sqlBinds.push(index + ' = ' + index + newObj.operator + ' ?');
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
sqlBinds.push(index + ' = ?');
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
sqlBinds.push(index + ' = ?');
|
|
497
|
+
sqlValues.push(bindings[index]);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
for (var index in where) {
|
|
502
|
+
if (where.hasOwnProperty(index)) {
|
|
503
|
+
if (typeof where[index] === 'object' && where[index] !== null) {
|
|
504
|
+
sqlKeys.push(index + ' ' + where[index].operator + ' ?');
|
|
505
|
+
sqlValues.push(where[index].value);
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
sqlKeys.push(index + ' = ?');
|
|
509
|
+
sqlValues.push(where[index]);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
sql = sql + sqlBinds.join(',') + ' where ' + sqlKeys.join(' and ');
|
|
514
|
+
return new Promise(function (resolve, reject) {
|
|
515
|
+
_this.db.transaction(function (transaction) {
|
|
516
|
+
transaction.executeSql(sql, sqlValues, function (transaction, result) {
|
|
517
|
+
resolve(result);
|
|
518
|
+
}, function (transaction, error) {
|
|
519
|
+
reject(error);
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
DbWrapperService.prototype.dropTable = function (tables) {
|
|
525
|
+
var _this = this;
|
|
526
|
+
var cont = 1;
|
|
527
|
+
cont = tables.length;
|
|
528
|
+
return new Promise(function (resolve, reject) {
|
|
529
|
+
tables.some(function (table) {
|
|
530
|
+
if (cont !== 0) {
|
|
531
|
+
_this.query('DROP TABLE ' + table)
|
|
532
|
+
.then(function () {
|
|
533
|
+
if (cont === 1) {
|
|
534
|
+
resolve();
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
cont--;
|
|
538
|
+
}
|
|
539
|
+
}, function (err) {
|
|
540
|
+
cont = 0;
|
|
541
|
+
reject(err);
|
|
542
|
+
return true;
|
|
543
|
+
});
|
|
544
|
+
return false;
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
};
|
|
549
|
+
DbWrapperService.prototype.fetchAll = function (result) {
|
|
550
|
+
var output = [];
|
|
551
|
+
for (var i = 0; i < result.rows.length; i++) {
|
|
552
|
+
output.push(result.rows.item(i));
|
|
553
|
+
}
|
|
554
|
+
return output;
|
|
555
|
+
};
|
|
556
|
+
DbWrapperService.prototype.fetch = function (result) {
|
|
557
|
+
var objeto = undefined, newObj = undefined;
|
|
558
|
+
if (result.rows.length !== 0) {
|
|
559
|
+
objeto = result.rows.item(0);
|
|
560
|
+
newObj = {};
|
|
561
|
+
var keys = Object.keys(objeto);
|
|
562
|
+
for (var key in keys) {
|
|
563
|
+
newObj[keys[key]] = objeto[keys[key]];
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return newObj;
|
|
567
|
+
};
|
|
568
|
+
DbWrapperService.prototype.rowsCount = function (result) {
|
|
569
|
+
return result.rows.length;
|
|
570
|
+
};
|
|
571
|
+
DbWrapperService.prototype.tableExists = function (table) {
|
|
572
|
+
var _this = this;
|
|
573
|
+
return this.query('select sql from sqlite_master ' +
|
|
574
|
+
'where type=\'table\' and name=\'' + table + '\'')
|
|
575
|
+
.then(function (result) { return (_this.rowsCount(result) === 1); });
|
|
576
|
+
};
|
|
577
|
+
DbWrapperService.prototype.columnExists = function (table, column) {
|
|
578
|
+
var _this = this;
|
|
579
|
+
return this._tableExists(table)
|
|
580
|
+
.then(function (tableDDL) {
|
|
581
|
+
if (tableDDL) {
|
|
582
|
+
return _this._columnExists(tableDDL, column);
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
/* Private */
|
|
590
|
+
DbWrapperService.prototype.defColumn = function (col) {
|
|
591
|
+
var colSql;
|
|
592
|
+
colSql = col.name + ' ' + col.type;
|
|
593
|
+
if (!col.isNullable) {
|
|
594
|
+
colSql += ' NOT NULL ';
|
|
595
|
+
}
|
|
596
|
+
if (col.default) {
|
|
597
|
+
colSql += ' DEFAULT ' + col.default + ' ';
|
|
598
|
+
}
|
|
599
|
+
if (col.type === 'text' && col.length) {
|
|
600
|
+
colSql += " CONSTRAINT " + col.name + "_length CHECK ( ";
|
|
601
|
+
if (col.isNullable) {
|
|
602
|
+
colSql += col.name + " is null OR ";
|
|
603
|
+
}
|
|
604
|
+
colSql += "length(" + col.name + ") <= " + col.length + " )";
|
|
605
|
+
}
|
|
606
|
+
return colSql;
|
|
607
|
+
};
|
|
608
|
+
DbWrapperService.prototype.defUnique = function (unique) {
|
|
609
|
+
var uniqSql;
|
|
610
|
+
// depende del entorno, producción o desarrollo
|
|
611
|
+
var onConflictUnique = 'on conflict replace';
|
|
612
|
+
if (Array.isArray(unique.fields)) {
|
|
613
|
+
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields.join(',') + " ) " + onConflictUnique + " ";
|
|
614
|
+
}
|
|
615
|
+
if (typeof unique.fields === 'string') {
|
|
616
|
+
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields + " ) " + onConflictUnique + " ";
|
|
617
|
+
}
|
|
618
|
+
return uniqSql;
|
|
619
|
+
};
|
|
620
|
+
DbWrapperService.prototype.defForeign = function (fore) {
|
|
621
|
+
var foreSql;
|
|
622
|
+
if (Array.isArray(fore.foreignKey)) {
|
|
623
|
+
foreSql = " FOREIGN KEY ( " + fore.foreignKey.join(',') + " ) ";
|
|
624
|
+
}
|
|
625
|
+
if (typeof fore.foreignKey === 'string') {
|
|
626
|
+
foreSql = " FOREIGN KEY ( " + fore.foreignKey + " ) ";
|
|
627
|
+
}
|
|
628
|
+
if (Array.isArray(fore.primaryKey)) {
|
|
629
|
+
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey.join(',') + " ) ";
|
|
630
|
+
}
|
|
631
|
+
if (typeof fore.primaryKey === 'string') {
|
|
632
|
+
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey + " ) ";
|
|
633
|
+
}
|
|
634
|
+
return foreSql;
|
|
635
|
+
};
|
|
636
|
+
DbWrapperService.prototype._tableExists = function (table) {
|
|
637
|
+
var _this = this;
|
|
638
|
+
return this.query('select sql from sqlite_master ' +
|
|
639
|
+
'where type=\'table\' and name=\'' + table + '\'')
|
|
640
|
+
.then(function (result) {
|
|
641
|
+
if (_this.rowsCount(result) === 1) {
|
|
642
|
+
return _this.fetch(result).sql;
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
};
|
|
649
|
+
DbWrapperService.prototype._columnExists = function (tableDDL, column) {
|
|
650
|
+
if (tableDDL.indexOf(column) === -1) {
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
return true;
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
DbWrapperService.prototype.addColumn = function (table, col) {
|
|
658
|
+
var colSql = this.defColumn(col);
|
|
659
|
+
var query = 'alter TABLE ' + table + ' add COLUMN ' + colSql;
|
|
660
|
+
return this.query(query);
|
|
661
|
+
};
|
|
662
|
+
return DbWrapperService;
|
|
663
|
+
}());
|
|
664
|
+
var DB_CONFIG = new core.InjectionToken('dbWrapperConfig');
|
|
665
|
+
var DB_SCHEMA = new core.InjectionToken('dbWrapperSchema');
|
|
666
|
+
function getDefaultDbConfig() {
|
|
667
|
+
return {
|
|
668
|
+
dbName: 'db.sqlite',
|
|
669
|
+
location: 'default',
|
|
670
|
+
iosDatabaseLocation: 'Library'
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
function provideDbWrapper(dbConfig, dbSchema) {
|
|
674
|
+
var config = !!dbConfig ? dbConfig : getDefaultDbConfig();
|
|
675
|
+
return new DbWrapperService(config, dbSchema);
|
|
676
|
+
}
|
|
1039
677
|
|
|
1040
|
-
/**
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
678
|
+
var DbWrapperModule = /** @class */ (function () {
|
|
679
|
+
function DbWrapperModule() {
|
|
680
|
+
}
|
|
681
|
+
DbWrapperModule_1 = DbWrapperModule;
|
|
682
|
+
DbWrapperModule.forRoot = function (dbConfig, schema) {
|
|
683
|
+
return {
|
|
684
|
+
ngModule: DbWrapperModule_1,
|
|
685
|
+
providers: [
|
|
686
|
+
{ provide: DB_CONFIG, useValue: dbConfig },
|
|
687
|
+
{ provide: DB_SCHEMA, useValue: schema },
|
|
688
|
+
{ provide: DbWrapperService, useFactory: provideDbWrapper, deps: [DB_CONFIG, DB_SCHEMA] }
|
|
689
|
+
]
|
|
690
|
+
};
|
|
691
|
+
};
|
|
692
|
+
var DbWrapperModule_1;
|
|
693
|
+
DbWrapperModule = DbWrapperModule_1 = __decorate([
|
|
694
|
+
core.NgModule()
|
|
695
|
+
], DbWrapperModule);
|
|
696
|
+
return DbWrapperModule;
|
|
697
|
+
}());
|
|
1044
698
|
|
|
1045
|
-
/**
|
|
1046
|
-
* @fileoverview added by tsickle
|
|
1047
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
1048
|
-
*/
|
|
1049
699
|
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
700
|
+
(function (DataType) {
|
|
701
|
+
DataType["Integer"] = "integer";
|
|
702
|
+
DataType["Int"] = "integer";
|
|
703
|
+
DataType["TinyInt"] = "integer";
|
|
704
|
+
DataType["SmallInt"] = "integer";
|
|
705
|
+
DataType["BigInt"] = "integer";
|
|
706
|
+
DataType["Text"] = "text";
|
|
707
|
+
DataType["Char"] = "text";
|
|
708
|
+
DataType["NChar"] = "text";
|
|
709
|
+
DataType["VarChar"] = "text";
|
|
710
|
+
DataType["NVarChar"] = "text";
|
|
711
|
+
DataType["CLOB"] = "text";
|
|
712
|
+
DataType["BLOB"] = "blob";
|
|
713
|
+
DataType["Real"] = "real";
|
|
714
|
+
DataType["Double"] = "real";
|
|
715
|
+
DataType["Decimal"] = "real";
|
|
716
|
+
DataType["Float"] = "real";
|
|
717
|
+
DataType["Boolean"] = "integer";
|
|
718
|
+
DataType["DateTime"] = "text";
|
|
719
|
+
DataType["Date"] = "text";
|
|
720
|
+
})(exports.DataType || (exports.DataType = {}));
|
|
721
|
+
|
|
722
|
+
(function (DefaultType) {
|
|
723
|
+
DefaultType["Integer"] = "0";
|
|
724
|
+
DefaultType["Int"] = "0";
|
|
725
|
+
DefaultType["TinyInt"] = "0";
|
|
726
|
+
DefaultType["SmallInt"] = "0";
|
|
727
|
+
DefaultType["BigInt"] = "0";
|
|
728
|
+
DefaultType["Text"] = "''";
|
|
729
|
+
DefaultType["Char"] = "''";
|
|
730
|
+
DefaultType["NChar"] = "''";
|
|
731
|
+
DefaultType["VarChar"] = "''";
|
|
732
|
+
DefaultType["NVarChar"] = "''";
|
|
733
|
+
DefaultType["CLOB"] = "''";
|
|
734
|
+
DefaultType["BLOB"] = "0";
|
|
735
|
+
DefaultType["Real"] = "0.0";
|
|
736
|
+
DefaultType["Double"] = "0.0";
|
|
737
|
+
DefaultType["Decimal"] = "0.0";
|
|
738
|
+
DefaultType["Float"] = "0.0";
|
|
739
|
+
DefaultType["Boolean"] = "0";
|
|
740
|
+
DefaultType["DateTime"] = "(datetime('now', 'localtime'))";
|
|
741
|
+
DefaultType["Date"] = "(date('now', 'localtime'))";
|
|
742
|
+
})(exports.DefaultType || (exports.DefaultType = {}));
|
|
743
|
+
|
|
1053
744
|
exports.DB_CONFIG = DB_CONFIG;
|
|
1054
745
|
exports.DB_SCHEMA = DB_SCHEMA;
|
|
1055
746
|
exports.DbWrapperModule = DbWrapperModule;
|
|
1056
|
-
exports.
|
|
1057
|
-
exports.
|
|
747
|
+
exports.DbWrapperService = DbWrapperService;
|
|
748
|
+
exports.getDefaultDbConfig = getDefaultDbConfig;
|
|
749
|
+
exports.provideDbWrapper = provideDbWrapper;
|
|
1058
750
|
|
|
1059
751
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1060
752
|
|
|
1061
753
|
})));
|
|
1062
|
-
|
|
1063
|
-
//# sourceMappingURL=db-wrapper.umd.js.map
|
|
754
|
+
//# sourceMappingURL=db-wrapper.umd.js.map
|