xardao 1.4.3 → 1.5.0

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.
@@ -1,17 +1,17 @@
1
- {
2
- // Use IntelliSense to learn about possible attributes.
3
- // Hover to view descriptions of existing attributes.
4
- // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
- "version": "0.2.0",
6
- "configurations": [
7
- {
8
- "type": "pwa-node",
9
- "request": "launch",
10
- "name": "Launch Program",
11
- "skipFiles": [
12
- "<node_internals>/**"
13
- ],
14
- "program": "${file}"
15
- }
16
- ]
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "pwa-node",
9
+ "request": "launch",
10
+ "name": "Launch Program",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${file}"
15
+ }
16
+ ]
17
17
  }
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 Stephane Potelle
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Stephane Potelle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # xardao
2
- Xardao Asynchronous Relational Database Access Object
3
-
4
- See https://stefpo.github.io/node-xardao/
1
+ # xardao
2
+ Xardao Asynchronous Relational Database Access Object
3
+
4
+ See https://stefpo.github.io/node-xardao/
package/lib/common.js CHANGED
@@ -1,252 +1,252 @@
1
- /********************************************************************************
2
- * rdao_common.js
3
- * Common elements in xardao implementation.
4
- *
5
- * Author : Stephane Potelle
6
- * Email : stephane.potelle@gmail.com
7
- ********************************************************************************/
8
-
9
- exports.isFunction = function (f) {
10
- if (!f ) return false;
11
- return (typeof(f)=='function');
12
- }
13
-
14
- exports.then = function (f) {
15
- if (isFunction(f)) setImmediate(f);
16
- else throw new Error('"Parameter should be a function. Found '+ typeof(f));
17
- }
18
-
19
- exports.Batch = class {
20
- constructor(cn){
21
- this.conn = cn;
22
- this.statements=[];
23
- }
24
-
25
- add(statement) {
26
- this.statements.push(statement);
27
- return this;
28
- }
29
-
30
- exec(callback) {
31
- cn.exec(this.statements, callback);
32
- }
33
- }
34
-
35
- // Common implementation of the "exec" functionality.
36
- // Specific drivers may use their own version of it.
37
- exports.exec = function(cn,params, callback) {
38
- let t=typeof(params)
39
- if (Array.isArray(params)) cn.execMultiple(params, callback);
40
- else if ( t == 'string' || t == 'object') cn.execSingle(params, callback);
41
- else throw new Error('Invalid parameter: '+t);
42
- }
43
-
44
- // Common implementation of the "execMultiple" functionality.
45
- // Specific drivers may use their own version of it.
46
- exports.execMultiple = function(cn, queries, callback) {
47
- var i = 0;
48
- loop();
49
-
50
- function loop(err) {
51
- if ( err ) then(function() { callback(err) });
52
- else if ( i>= queries.length ) then(callback);
53
- else {
54
- let p = queries[i];
55
- i++;
56
- cn.execSingle(p, loop);
57
- }
58
- }
59
- }
60
-
61
- // Common implementation of the "exec" functionality.
62
- // Specific drivers may use their own version of it.
63
- exports.execCB = function(cn,params, callback) {
64
- let t=typeof(params)
65
- if (Array.isArray(params)) cn.execMultipleCB(params, callback);
66
- else if ( t == 'string' || t == 'object') cn.execSingleCB(params, callback);
67
- else throw new Error('Invalid parameter: '+t);
68
- }
69
-
70
- // Common implementation of the "execMultiple" functionality.
71
- // Specific drivers may use their own version of it.
72
- exports.execMultipleCB = function(cn, queries, callback) {
73
- var i = 0;
74
- loop();
75
-
76
- function loop(err) {
77
- if ( err ) then(function() { callback(err) });
78
- else if ( i>= queries.length ) then(callback);
79
- else {
80
- let p = queries[i];
81
- i++;
82
- cn.execSingleCB(p, loop);
83
- }
84
- }
85
- }
86
-
87
-
88
-
89
- // Base implementation of the "sqlParam" method.
90
- // Specific drivers may use their own version of it.
91
- exports.sqlParam = function(cn,value) {
92
- if ( value == undefined ) return 'null'
93
- if ( typeof(value)=='number') { return value}
94
- if ( typeof(value)=='boolean') { return value == null ? null : value ? 1 : 0 }
95
- if ( typeof(value)=='string') { return "'" + value.replace(/'/g,"''") +"'" }
96
- if ( typeof(value.getFullYear )=='function') { return "'" + cn.sqlDate(value) + "'"}
97
- return 'null'
98
- }
99
-
100
- exports.mergeParams_old = function(cn,sql, params) {
101
- var keys = Object.keys(params);
102
- var osql = sql;
103
- for ( let i = 0; i< keys.length; i++) {
104
- let k = keys[i];
105
- osql = osql.replace(new RegExp('@'+ k + '\\b','ig'), cn.sqlParam(params[k]));
106
- }
107
- return osql;
108
- }
109
-
110
- // Base implementation of the "mergeParams" method.
111
- // Specific drivers may use their own version of it.
112
- exports.mergeParams = function(cn,sql, params) {
113
- let rt = []
114
- let isql = sql
115
- let uparams = {}
116
- for ( let k of Object.keys(params)) {
117
- uparams[k.toLowerCase()] = params[k]
118
- }
119
-
120
- while (true) {
121
- let p = isql.search( /@\w+/)
122
- if (p == -1 ) {
123
- rt.push(isql)
124
- break
125
- } else {
126
- rt.push(isql.substring(0, p))
127
- let fn = /@(\w+)/i.exec(isql)
128
- let v = uparams[fn[1].toLowerCase()]
129
- if ( v === undefined ) rt.push(fn[0])
130
- else rt.push(cn.sqlParam(v))
131
- isql = isql.substring(p+fn[0].length)
132
- }
133
- }
134
- return rt.join('')
135
- }
136
-
137
- // Base implementation of the "getRealSql" method.
138
- // Specific drivers may use their own version of it.
139
- exports.getRealSql = function(cn,queryObject) {
140
- var realSql;
141
- if ( queryObject.params == undefined ) queryObject.params = {}
142
- if (typeof(queryObject)=="string") { realSql= queryObject; }
143
- else {
144
- if (queryObject.params) realSql = cn.mergeParams(queryObject.sql, queryObject.params);
145
- }
146
- return realSql;
147
- }
148
-
149
- // Base implementation of the "sqlDate" method.
150
- // Specific drivers may use their own version of it.
151
- exports.sqlDate = function(cn,d) {
152
- function fixedInt(n, dig) {
153
- var s = '' + n;
154
- if (s.length < dig ) s = '0'.repeat(dig-s.length) + s;
155
- return s;
156
- }
157
-
158
- if ( cn.saveDatesAsUTC && ! d.isUTC ){
159
- if ( d.isDateOnly ) {
160
- return fixedInt(d.getUTCFullYear(), 4) + "-" +
161
- fixedInt(d.getUTCMonth()+1, 2) + '-' +
162
- fixedInt(d.getUTCDate(), 2) +
163
- " 12:00:00"
164
- } else {
165
- return fixedInt(d.getUTCFullYear(), 4) + "-" +
166
- fixedInt(d.getUTCMonth()+1, 2) + '-' +
167
- fixedInt(d.getUTCDate(), 2) + ' ' +
168
- fixedInt(d.getUTCHours(), 2) + ':' +
169
- fixedInt(d.getUTCMinutes(), 2) + ':' +
170
- fixedInt(d.getUTCSeconds(), 2) ;
171
- }
172
- } else {
173
- if ( d.isDateOnly ) {
174
- return fixedInt(d.getFullYear(), 4) + "-" +
175
- fixedInt(d.getMonth()+1, 2) + '-' +
176
- fixedInt(d.getDate(), 2) +
177
- " 12:00:00"
178
- } else {
179
- return fixedInt(d.getFullYear(), 4) + "-" +
180
- fixedInt(d.getMonth()+1, 2) + '-' +
181
- fixedInt(d.getDate(), 2) + ' ' +
182
- fixedInt(d.getHours(), 2) + ':' +
183
- fixedInt(d.getMinutes(), 2) + ':' +
184
- fixedInt(d.getSeconds(), 2) ;
185
- }
186
- }
187
- }
188
-
189
- function strToCamelCase(fn, startUpperCase) {
190
- let a = Array.from (fn)
191
- let ra = []
192
- let us = startUpperCase || false
193
- for ( let c of a ) {
194
- if ( c == '_') {
195
- us = true
196
- } else {
197
- if (us) ra.push(c.toUpperCase())
198
- else ra.push(c.toLowerCase())
199
- us = false
200
- }
201
- }
202
- return ra.join('')
203
- }
204
-
205
- function strToSnakeCase(fn) {
206
- let a = Array.from (fn)
207
- let ra = []
208
- let us = true
209
- for ( let c of a ) {
210
- //if (c.toUpperCase() == c && c != '_' && ! us ) {
211
- if ( c >='A' && c <= 'Z' && ! us ) {
212
- ra.push('_')
213
- us = true
214
- } else us = false
215
-
216
- ra.push(c.toLowerCase())
217
- if ( c == '_' ) us = true
218
- }
219
- return ra.join('')
220
- }
221
-
222
- exports.toCamelCase = function toCamelCase(o) {
223
- if (o == undefined || o == null)
224
- return o
225
- else if (typeof(o) == "object") {
226
- return transformKeyCase(o, strToCamelCase)
227
- } else if (typeof(o) == "string") {
228
- return strToCamelCase(o)
229
- } else {
230
- throw Error("o Must be object or string")
231
- }
232
- }
233
-
234
- exports.toSnakeCase = function toSnakeCase(o) {
235
- if (o == undefined || o == null)
236
- return o
237
- else if (typeof(o) == "object") {
238
- return transformKeyCase(o, strToSnakeCase)
239
- } else if (typeof(o) == "string") {
240
- return strToSnakeCase(o)
241
- } else {
242
- throw Error("o Must be object or string")
243
- }
244
- }
245
-
246
- function transformKeyCase(o, transform) {
247
- let ret = {}
248
- for ( let k of Object.keys(o)) {
249
- ret[transform(k)] = o[k]
250
- }
251
- return ret
1
+ /********************************************************************************
2
+ * rdao_common.js
3
+ * Common elements in xardao implementation.
4
+ *
5
+ * Author : Stephane Potelle
6
+ * Email : stephane.potelle@gmail.com
7
+ ********************************************************************************/
8
+
9
+ exports.isFunction = function (f) {
10
+ if (!f ) return false;
11
+ return (typeof(f)=='function');
12
+ }
13
+
14
+ exports.then = function (f) {
15
+ if (isFunction(f)) setImmediate(f);
16
+ else throw new Error('"Parameter should be a function. Found '+ typeof(f));
17
+ }
18
+
19
+ exports.Batch = class {
20
+ constructor(cn){
21
+ this.conn = cn;
22
+ this.statements=[];
23
+ }
24
+
25
+ add(statement) {
26
+ this.statements.push(statement);
27
+ return this;
28
+ }
29
+
30
+ exec(callback) {
31
+ cn.exec(this.statements, callback);
32
+ }
33
+ }
34
+
35
+ // Common implementation of the "exec" functionality.
36
+ // Specific drivers may use their own version of it.
37
+ exports.exec = function(cn,params, callback) {
38
+ let t=typeof(params)
39
+ if (Array.isArray(params)) cn.execMultiple(params, callback);
40
+ else if ( t == 'string' || t == 'object') cn.execSingle(params, callback);
41
+ else throw new Error('Invalid parameter: '+t);
42
+ }
43
+
44
+ // Common implementation of the "execMultiple" functionality.
45
+ // Specific drivers may use their own version of it.
46
+ exports.execMultiple = function(cn, queries, callback) {
47
+ var i = 0;
48
+ loop();
49
+
50
+ function loop(err) {
51
+ if ( err ) then(function() { callback(err) });
52
+ else if ( i>= queries.length ) then(callback);
53
+ else {
54
+ let p = queries[i];
55
+ i++;
56
+ cn.execSingle(p, loop);
57
+ }
58
+ }
59
+ }
60
+
61
+ // Common implementation of the "exec" functionality.
62
+ // Specific drivers may use their own version of it.
63
+ exports.execCB = function(cn,params, callback) {
64
+ let t=typeof(params)
65
+ if (Array.isArray(params)) cn.execMultipleCB(params, callback);
66
+ else if ( t == 'string' || t == 'object') cn.execSingleCB(params, callback);
67
+ else throw new Error('Invalid parameter: '+t);
68
+ }
69
+
70
+ // Common implementation of the "execMultiple" functionality.
71
+ // Specific drivers may use their own version of it.
72
+ exports.execMultipleCB = function(cn, queries, callback) {
73
+ var i = 0;
74
+ loop();
75
+
76
+ function loop(err) {
77
+ if ( err ) then(function() { callback(err) });
78
+ else if ( i>= queries.length ) then(callback);
79
+ else {
80
+ let p = queries[i];
81
+ i++;
82
+ cn.execSingleCB(p, loop);
83
+ }
84
+ }
85
+ }
86
+
87
+
88
+
89
+ // Base implementation of the "sqlParam" method.
90
+ // Specific drivers may use their own version of it.
91
+ exports.sqlParam = function(cn,value) {
92
+ if ( value == undefined ) return 'null'
93
+ if ( typeof(value)=='number') { return value}
94
+ if ( typeof(value)=='boolean') { return value == null ? null : value ? 1 : 0 }
95
+ if ( typeof(value)=='string') { return "'" + value.replace(/'/g,"''") +"'" }
96
+ if ( typeof(value.getFullYear )=='function') { return "'" + cn.sqlDate(value) + "'"}
97
+ return 'null'
98
+ }
99
+
100
+ exports.mergeParams_old = function(cn,sql, params) {
101
+ var keys = Object.keys(params);
102
+ var osql = sql;
103
+ for ( let i = 0; i< keys.length; i++) {
104
+ let k = keys[i];
105
+ osql = osql.replace(new RegExp('@'+ k + '\\b','ig'), cn.sqlParam(params[k]));
106
+ }
107
+ return osql;
108
+ }
109
+
110
+ // Base implementation of the "mergeParams" method.
111
+ // Specific drivers may use their own version of it.
112
+ exports.mergeParams = function(cn,sql, params) {
113
+ let rt = []
114
+ let isql = sql
115
+ let uparams = {}
116
+ for ( let k of Object.keys(params)) {
117
+ uparams[k.toLowerCase()] = params[k]
118
+ }
119
+
120
+ while (true) {
121
+ let p = isql.search( /@\w+/)
122
+ if (p == -1 ) {
123
+ rt.push(isql)
124
+ break
125
+ } else {
126
+ rt.push(isql.substring(0, p))
127
+ let fn = /@(\w+)/i.exec(isql)
128
+ let v = uparams[fn[1].toLowerCase()]
129
+ if ( v === undefined ) rt.push(fn[0])
130
+ else rt.push(cn.sqlParam(v))
131
+ isql = isql.substring(p+fn[0].length)
132
+ }
133
+ }
134
+ return rt.join('')
135
+ }
136
+
137
+ // Base implementation of the "getRealSql" method.
138
+ // Specific drivers may use their own version of it.
139
+ exports.getRealSql = function(cn,queryObject) {
140
+ var realSql;
141
+ if ( queryObject.params == undefined ) queryObject.params = {}
142
+ if (typeof(queryObject)=="string") { realSql= queryObject; }
143
+ else {
144
+ if (queryObject.params) realSql = cn.mergeParams(queryObject.sql, queryObject.params);
145
+ }
146
+ return realSql;
147
+ }
148
+
149
+ // Base implementation of the "sqlDate" method.
150
+ // Specific drivers may use their own version of it.
151
+ exports.sqlDate = function(cn,d) {
152
+ function fixedInt(n, dig) {
153
+ var s = '' + n;
154
+ if (s.length < dig ) s = '0'.repeat(dig-s.length) + s;
155
+ return s;
156
+ }
157
+
158
+ if ( cn.saveDatesAsUTC && ! d.isUTC ){
159
+ if ( d.isDateOnly ) {
160
+ return fixedInt(d.getUTCFullYear(), 4) + "-" +
161
+ fixedInt(d.getUTCMonth()+1, 2) + '-' +
162
+ fixedInt(d.getUTCDate(), 2) +
163
+ " 12:00:00"
164
+ } else {
165
+ return fixedInt(d.getUTCFullYear(), 4) + "-" +
166
+ fixedInt(d.getUTCMonth()+1, 2) + '-' +
167
+ fixedInt(d.getUTCDate(), 2) + ' ' +
168
+ fixedInt(d.getUTCHours(), 2) + ':' +
169
+ fixedInt(d.getUTCMinutes(), 2) + ':' +
170
+ fixedInt(d.getUTCSeconds(), 2) ;
171
+ }
172
+ } else {
173
+ if ( d.isDateOnly ) {
174
+ return fixedInt(d.getFullYear(), 4) + "-" +
175
+ fixedInt(d.getMonth()+1, 2) + '-' +
176
+ fixedInt(d.getDate(), 2) +
177
+ " 12:00:00"
178
+ } else {
179
+ return fixedInt(d.getFullYear(), 4) + "-" +
180
+ fixedInt(d.getMonth()+1, 2) + '-' +
181
+ fixedInt(d.getDate(), 2) + ' ' +
182
+ fixedInt(d.getHours(), 2) + ':' +
183
+ fixedInt(d.getMinutes(), 2) + ':' +
184
+ fixedInt(d.getSeconds(), 2) ;
185
+ }
186
+ }
187
+ }
188
+
189
+ function strToCamelCase(fn, startUpperCase) {
190
+ let a = Array.from (fn)
191
+ let ra = []
192
+ let us = startUpperCase || false
193
+ for ( let c of a ) {
194
+ if ( c == '_') {
195
+ us = true
196
+ } else {
197
+ if (us) ra.push(c.toUpperCase())
198
+ else ra.push(c.toLowerCase())
199
+ us = false
200
+ }
201
+ }
202
+ return ra.join('')
203
+ }
204
+
205
+ function strToSnakeCase(fn) {
206
+ let a = Array.from (fn)
207
+ let ra = []
208
+ let us = true
209
+ for ( let c of a ) {
210
+ //if (c.toUpperCase() == c && c != '_' && ! us ) {
211
+ if ( c >='A' && c <= 'Z' && ! us ) {
212
+ ra.push('_')
213
+ us = true
214
+ } else us = false
215
+
216
+ ra.push(c.toLowerCase())
217
+ if ( c == '_' ) us = true
218
+ }
219
+ return ra.join('')
220
+ }
221
+
222
+ exports.toCamelCase = function toCamelCase(o) {
223
+ if (o == undefined || o == null)
224
+ return o
225
+ else if (typeof(o) == "object") {
226
+ return transformKeyCase(o, strToCamelCase)
227
+ } else if (typeof(o) == "string") {
228
+ return strToCamelCase(o)
229
+ } else {
230
+ throw Error("o Must be object or string")
231
+ }
232
+ }
233
+
234
+ exports.toSnakeCase = function toSnakeCase(o) {
235
+ if (o == undefined || o == null)
236
+ return o
237
+ else if (typeof(o) == "object") {
238
+ return transformKeyCase(o, strToSnakeCase)
239
+ } else if (typeof(o) == "string") {
240
+ return strToSnakeCase(o)
241
+ } else {
242
+ throw Error("o Must be object or string")
243
+ }
244
+ }
245
+
246
+ function transformKeyCase(o, transform) {
247
+ let ret = {}
248
+ for ( let k of Object.keys(o)) {
249
+ ret[transform(k)] = o[k]
250
+ }
251
+ return ret
252
252
  }