xardao 1.2.4 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@
5
5
  "version": "0.2.0",
6
6
  "configurations": [
7
7
  {
8
- "type": "pwa-node",
8
+ "type": "node",
9
9
  "request": "launch",
10
10
  "name": "Launch Program",
11
11
  "skipFiles": [
package/lib/common.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /********************************************************************************
2
2
  * rdao_common.js
3
- * Common elements in rdao implementation.
3
+ * Common elements in xardao implementation.
4
4
  *
5
5
  * Author : Stephane Potelle
6
6
  * Email : stephane.potelle@gmail.com
@@ -97,9 +97,7 @@ exports.sqlParam = function(cn,value) {
97
97
  return 'null'
98
98
  }
99
99
 
100
- // Base implementation of the "mergeParams" method.
101
- // Specific drivers may use their own version of it.
102
- exports.mergeParams = function(cn,sql, params) {
100
+ exports.mergeParams_old = function(cn,sql, params) {
103
101
  var keys = Object.keys(params);
104
102
  var osql = sql;
105
103
  for ( let i = 0; i< keys.length; i++) {
@@ -109,6 +107,34 @@ exports.mergeParams = function(cn,sql, params) {
109
107
  return osql;
110
108
  }
111
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 )
130
+ throw ( `Missing value for parameter ${fn[0]} `)
131
+ rt.push(cn.sqlParam(v))
132
+ isql = isql.substring(p+fn[0].length)
133
+ }
134
+ }
135
+ return rt.join('')
136
+ }
137
+
112
138
  // Base implementation of the "getRealSql" method.
113
139
  // Specific drivers may use their own version of it.
114
140
  exports.getRealSql = function(cn,queryObject) {
@@ -197,12 +197,12 @@ class CRUDAdapter {
197
197
  if (err) callback(err)
198
198
  else {
199
199
  p = self.normalizeParameters( p )
200
- self.conn.getDataTable(self.selectStatement(p), function(err, dt) {
200
+ self.conn.getObjects(self.selectStatement(p), function(err, dt) {
201
201
  if (err) callback(err)
202
202
  else {
203
- if (dt.rows.length>0) {
204
- self.content = dt.rows[0].toObject()
205
- self.prevContent = dt.rows[0].toObject() // Make sure we have a clone.
203
+ if (dt.rlength>0) {
204
+ self.content = dt[0]
205
+ self.prevContent = dt[0]
206
206
  callback(undefined, self.content)
207
207
  } else {
208
208
  self.clear()
@@ -1,6 +1,6 @@
1
1
  /********************************************************************************
2
2
  * driver_mariadb.js
3
- * MariaDB rdao driver
3
+ * MariaDB xardao driver
4
4
  *
5
5
  * Author : Stephane Potelle
6
6
  * Email : stephane.potelle@gmail.com
@@ -14,7 +14,6 @@ try {
14
14
  process.exit(1)
15
15
  }
16
16
 
17
- var data = require('./datatable.js');
18
17
  var common = require ('./common.js');
19
18
  var ESNext = require('./esnext');
20
19
 
@@ -32,7 +31,6 @@ class Connection {
32
31
  this.connUrlInfo=connUrlInfo
33
32
 
34
33
  this.open = ESNext(this.openCB,this)
35
- this.getDataTable = ESNext(this.getDataTableCB,this)
36
34
  this.getObjects = ESNext(this.getObjectsCB,this)
37
35
  this.getSingleObject = ESNext(this.getSingleObjectCB ,this)
38
36
  this.getList = ESNext(this.getListCB,this)
@@ -45,12 +43,10 @@ class Connection {
45
43
  this.beginTrans = ESNext(this.beginTransCB, this)
46
44
  this.commitTrans = ESNext(this.commitTransCB, this)
47
45
  this.rollbackTrans = ESNext(this.rollbackTransCB, this)
48
- this.forEachRow = ESNext(this.forEachRowCB, this)
49
46
  this.close = ESNext(this.closeCB,this)
50
47
  }
51
48
 
52
49
  open() {}
53
- getDataTable() {}
54
50
  getObjects() {}
55
51
  getSingleObject () {}
56
52
  getList() {}
@@ -67,8 +63,18 @@ class Connection {
67
63
  }
68
64
 
69
65
  openCB(dbInfo, callback) {
70
- if (dbInfo) this.db = new mysql.createConnection(dbInfo);
71
- else this.db = new mysql.createConnection({ host: this.connUrlInfo.hostname, user: this.connUrlInfo.username, password: this.connUrlInfo.password, database: this.connUrlInfo.database})
66
+ if ( typeof(this.connUrlInfo) == 'object' ) {
67
+ let conninfo = {
68
+ host: this.connUrlInfo.hostname,
69
+ user: this.connUrlInfo.username,
70
+ password: this.connUrlInfo.password,
71
+ database: this.connUrlInfo.database
72
+ }
73
+ this.db = new mysql.createConnection( conninfo )
74
+ }
75
+ else {
76
+ this.db = new mysql.createConnection(dbInfo)
77
+ }
72
78
  this.debug('open','Opening connection for MariaDB')
73
79
  this.db.connect( callback )
74
80
  }
@@ -125,80 +131,7 @@ class Connection {
125
131
  mergeParams(sql, params) { return common.mergeParams(this, sql, params) }
126
132
  getRealSql(queryObject) { return common.getRealSql(this, queryObject) }
127
133
 
128
- forEachRowCB(params, eachfunc, callback) {
129
- var ts = Date.now();
130
- var realSql = this.getRealSql(params);
131
- var self = this;
132
- this.debug("forEachRow",realSql);
133
-
134
- let query = this.db.query(realSql)
135
- query.on("error", function(err) {
136
- self.debug("forEachRow",'Error: '+err.code);
137
- then( function() {callback(err)}) ;
138
- })
139
- .on("fields", function(fields) {})
140
- .on("result", function(row) {
141
- self.db.pause()
142
- eachfunc (row, function() {
143
- self.db.resume()
144
- })
145
- })
146
- .on("end", function() {
147
- self.debug("forEachRow",'Completed in ' + (Date.now() -ts) + ' ms');
148
- callback()
149
- })
150
- }
151
-
152
-
153
- getDataTableCB(params, callback) {
154
- var ts = Date.now();
155
- var realSql = this.getRealSql(params);
156
- var self = this;
157
- var useSnakeCase = false
158
- var useCamelCase = false
159
- if ( params.options ) {
160
- useSnakeCase = params.options.useSnakeCase || false
161
- useCamelCase = params.options.useCamelCase || false
162
- }
163
-
164
- this.debug("getDataTable",realSql);
165
- this.db.query({sql: realSql, timeout: this.timeoutMilliSeconds }, function(err, rows, fields) {
166
- let dt = new data.DataTable;
167
-
168
- function finish() {
169
- self.debug("getDataTable",'Completed in ' + (Date.now() -ts) + ' ms');
170
- if (isFunction(callback)) then( function() { callback(undefined, dt) } );
171
- }
172
-
173
- if (err) {
174
- self.debug("getDataTable",'Error: '+err.code);
175
- then( function() {callback(err)}) ;
176
- } else {
177
- if (rows.length > 0 ) {
178
- let cols = Object.keys(rows[0])
179
- for (let c = 0; c< cols.length; c++) {
180
- if (useSnakeCase) dt.addColumn(common.toSnakeCase(cols[c]))
181
- else if (useCamelCase) dt.addColumn(common.toCamelCase(cols[c]))
182
- else dt.addColumn(cols[c])
183
- }
184
-
185
- let r = 0
186
- then( function populateTable() {
187
- if (r<rows.length) {
188
- dt.addRow( dt.newRow(rows[r]));
189
- r++
190
- then(populateTable)
191
- } else {
192
- finish()
193
- }
194
- } )
195
- } else {
196
- finish()
197
- }
198
- }
199
- });
200
- }
201
-
134
+
202
135
  getObjectsCB(params, callback) {
203
136
  var ts = Date.now();
204
137
  var realSql = this.getRealSql(params);
@@ -268,7 +201,7 @@ class Connection {
268
201
  var ts = Date.now();
269
202
  var realSql = this.getRealSql(params);
270
203
  var self = this;
271
- this.debug("getDataTable",realSql);
204
+ this.debug("getList",realSql);
272
205
  this.db.query({sql: realSql, timeout: this.timeoutMilliSeconds }, function(err,rows, fields) {
273
206
  if (err) {
274
207
  self.debug("getList",'Error: '+err.code);
@@ -314,7 +247,7 @@ class Connection {
314
247
  var ts = Date.now();
315
248
  var realSql = this.getRealSql(params);
316
249
  var self = this;
317
- this.debug("getDataTable",realSql);
250
+ this.debug("getKVList",realSql);
318
251
  this.db.query({sql: realSql, timeout: this.timeoutMilliSeconds }, function(err,rows, fields) {
319
252
  if (err) {
320
253
  self.debug("getKVList",'Error: '+err.code);
@@ -340,7 +273,7 @@ class Connection {
340
273
  var ts = Date.now();
341
274
  var realSql = this.getRealSql(params);
342
275
  var self = this;
343
- this.debug("getDataTable",realSql);
276
+ this.debug("getScalar",realSql);
344
277
  this.db.query({sql: realSql, timeout: this.timeoutMilliSeconds }, function(err,rows, fields) {
345
278
  if (err) {
346
279
  self.debug("getScalar",'Error: '+err.code);
@@ -1,6 +1,6 @@
1
1
  /********************************************************************************
2
2
  * driver_mssql.js
3
- * MSSQL rdao driver
3
+ * MSSQL xardao driver
4
4
  *
5
5
  * Author : Stephane Potelle
6
6
  * Email : stephane.potelle@gmail.com
@@ -14,7 +14,6 @@ try {
14
14
  process.exit(1)
15
15
  }
16
16
 
17
- var data = require('./datatable.js')
18
17
  var common = require ('./common.js')
19
18
  var ESNext = require('./esnext')
20
19
 
@@ -32,7 +31,6 @@ class Connection {
32
31
  this.connUrlInfo=connUrlInfo
33
32
 
34
33
  this.open = ESNext(this.openCB, this)
35
- this.getDataTable = ESNext(this.getDataTableCB, this)
36
34
  this.getObjects = ESNext(this.getObjectsCB, this)
37
35
  this.getSingleObject = ESNext(this.getSingleObjectCB , this)
38
36
  this.getList = ESNext(this.getListCB, this)
@@ -45,12 +43,10 @@ class Connection {
45
43
  this.beginTrans = ESNext(this.beginTransCB, this)
46
44
  this.commitTrans = ESNext(this.commitTransCB, this)
47
45
  this.rollbackTrans = ESNext(this.rollbackTransCB, this)
48
- this.forEachRow = ESNext(this.forEachRowCB, this)
49
46
  this.close = ESNext(this.closeCB, this)
50
47
  }
51
48
 
52
49
  open() {}
53
- getDataTable() {}
54
50
  getObjects() {}
55
51
  getSingleObject () {}
56
52
  getList() {}
@@ -151,85 +147,8 @@ class Connection {
151
147
  }
152
148
 
153
149
  mergeParams(sql, params) { return common.mergeParams(this, sql, params) }
154
- getRealSql(queryObject) { return common.getRealSql(this, queryObject) }
155
-
156
- forEachRowCB(params, eachfunc, callback) {
157
- var ts = Date.now()
158
- var realSql = this.getRealSql(params);
159
- var self = this
160
- let fields = []
161
150
 
162
- this.debug("getDataTable",realSql)
163
- let dt = new data.DataTable
164
- let headersRead = false
165
- let request = new mssql.Request(realSql, function(err, rows, fields) {
166
- if (err) {
167
- self.debug("getDataTable",'Error: '+err.code)
168
- then( function() {callback(err)})
169
- } else {
170
- self.debug("getDataTable",'Completed in ' + (Date.now() -ts) + ' ms')
171
- if (isFunction(callback)) then( function() { callback(undefined, dt) } )
172
- }
173
- })
174
- request.setTimeout( this.timeoutMilliSeconds )
175
- request.on('row', function(columns) {
176
- if (! headersRead ) {
177
- for ( let i=0; i< columns.length; i++) {
178
- fields.push(columns[i].metadata.colName)
179
- }
180
- headersRead = true
181
- }
182
- let dr = {}
183
- for ( let i=0; i< columns.length; i++) {
184
- dr[fields[i]]=columns[i].value
185
- }
186
- request.pause()
187
- eachfunc(dr, function() { request.resume()})
188
- })
189
- this.db.execSql(request)
190
- }
191
-
192
- getDataTableCB(params, callback) {
193
- var ts = Date.now()
194
- var realSql = this.getRealSql(params);
195
- var self = this
196
- var useSnakeCase = false
197
- var useCamelCase = false
198
- if ( params.options ) {
199
- useSnakeCase = params.options.useSnakeCase || false
200
- useCamelCase = params.options.useCamelCase || false
201
- }
202
-
203
- this.debug("getDataTable",realSql)
204
- let dt = new data.DataTable
205
- let headersRead = false
206
- let request = new mssql.Request(realSql, function(err, rows, fields) {
207
- if (err) {
208
- self.debug("getDataTable",'Error: '+err.code)
209
- then( function() {callback(err)})
210
- } else {
211
- self.debug("getDataTable",'Completed in ' + (Date.now() -ts) + ' ms')
212
- if (isFunction(callback)) then( function() { callback(undefined, dt) } )
213
- }
214
- })
215
- request.setTimeout( this.timeoutMilliSeconds )
216
- request.on('row', function(columns) {
217
- if (! headersRead ) {
218
- for ( let i=0; i< columns.length; i++) {
219
- if (useSnakeCase) dt.addColumn(common.toSnakeCase(columns[i].metadata.colName))
220
- else if (useCamelCase) dt.addColumn(common.toCamelCase(columns[i].metadata.colName))
221
- else dt.addColumn(columns[i].metadata.colName)
222
- }
223
- headersRead = true
224
- }
225
- let dr = dt.newRow()
226
- for ( let i=0; i< columns.length; i++) {
227
- dr.items[i]=columns[i].value
228
- }
229
- dt.addRow(dr)
230
- })
231
- this.db.execSql(request)
232
- }
151
+ getRealSql(queryObject) { return common.getRealSql(this, queryObject) }
233
152
 
234
153
  getObjectsCB(params, callback) {
235
154
  var ts = Date.now()
@@ -274,32 +193,28 @@ class Connection {
274
193
  this.db.execSql(request)
275
194
  }
276
195
 
277
- getSingleObjectCB (params, callback) {
196
+ getSingleObjectCB(params, callback) {
278
197
  var ts = Date.now()
279
198
  var realSql = this.getRealSql(params);
280
199
  var self = this
281
200
  var useSnakeCase = false
282
- var useCamelCase = false
201
+ var useCamelCase = false
283
202
  if ( params.options ) {
284
203
  useSnakeCase = params.options.useSnakeCase || false
285
204
  useCamelCase = params.options.useCamelCase || false
286
205
  }
287
206
 
288
- this.debug("getSingleObjectCB",realSql)
289
- let dt = new data.DataTable
290
- let headersRead = false
291
- let valueRead = false
207
+ this.debug("getObjects",realSql)
208
+ let ret = undefined
292
209
  let fields = []
293
- let retVal
210
+ let headersRead = false
294
211
  let request = new mssql.Request(realSql, function(err, rows, fields) {
295
212
  if (err) {
296
- self.debug("getObject",'Error: '+err.code)
213
+ self.debug("getObjects",'Error: '+err.code)
297
214
  then( function() {callback(err)})
298
215
  } else {
299
- self.debug("getObject",'Completed in ' + (Date.now() -ts) + ' ms')
300
- if (useSnakeCase) retVal = common.toSnakeCase(retVal)
301
- if (useCamelCase) retVal = common.toCamelCase(retVal)
302
- if (isFunction(callback)) then( function() { callback(undefined, retVal) } )
216
+ self.debug("getObjects",'Completed in ' + (Date.now() -ts) + ' ms')
217
+ if (isFunction(callback)) then( function() { callback(undefined, ret) } )
303
218
  }
304
219
  })
305
220
  request.setTimeout( this.timeoutMilliSeconds )
@@ -314,12 +229,14 @@ class Connection {
314
229
  for ( let i=0; i< columns.length; i++) {
315
230
  dr[fields[i]]=columns[i].value
316
231
  }
317
- retVal = dr
318
- valueRead = true
319
- })
320
- this.db.execSql(request)
321
- }
232
+ if (useSnakeCase) dr = common.toSnakeCase(dr)
233
+ if (useCamelCase) dr = common.toCamelCase(dr)
234
+
235
+ ret=dr
322
236
 
237
+ })
238
+ this.db.execSql(request)
239
+ }
323
240
 
324
241
  getListCB(params, callback) {
325
242
  var ts = Date.now()
@@ -389,32 +306,20 @@ class Connection {
389
306
  this.db.execSql(request)
390
307
  }
391
308
 
392
- getScalarCB(params, callback) {
393
- var ts = Date.now()
394
- var realSql = this.getRealSql(params);
395
- var self = this
396
- this.debug("getScalar",realSql)
397
- let dt = new data.DataTable
398
- let valueRead = false
399
- let retVal
400
- let request = new mssql.Request(realSql, function(err, rows, fields) {
401
- if (err) {
402
- self.debug("getScalar",'Error: '+err.code)
403
- then( function() {callback(err)})
404
- } else {
405
- self.debug("getScalar",'Completed in ' + (Date.now() -ts) + ' ms')
406
- if (isFunction(callback)) then( function() { callback(undefined, retVal) } )
407
- }
408
- })
409
- request.setTimeout( this.timeoutMilliSeconds )
410
- request.on('row', function(columns) {
411
- if (! valueRead ) {
412
- retVal = columns[0].value
413
- valueRead = true
309
+ getScalarCB ( params, callback ) {
310
+ this.getSingleObjectCB(params,
311
+ function (err, res) {
312
+ if ( err ) callback (err)
313
+ else if (res) {
314
+ let keys = Object.keys(res)
315
+ callback( undefined, res[keys[0]])
316
+ }
317
+ else {
318
+ callback ( undefined, undefined )
319
+ }
414
320
  }
415
- })
416
- this.db.execSql(request)
417
- }
321
+ )
322
+ }
418
323
 
419
324
  execCB(params, callback) {
420
325
  common.exec(this, params, callback)
@@ -1,6 +1,6 @@
1
1
  /********************************************************************************
2
2
  * driver_pgsql.js
3
- * PosgreSQL rdao driver
3
+ * PosgreSQL xardao driver
4
4
  *
5
5
  * Author : Stephane Potelle
6
6
  * Email : stephane.potelle@gmail.com
@@ -13,7 +13,6 @@ try {
13
13
  console.error("Please add module \"pg\" to your package.json dependencies")
14
14
  process.exit(1)
15
15
  }
16
- var data = require('./datatable.js');
17
16
  var common = require ('./common.js');
18
17
  var ESNext = require('./esnext');
19
18
 
@@ -21,7 +20,7 @@ isFunction = common.isFunction;
21
20
  then = common.then;
22
21
 
23
22
  class Connection {
24
- constructor() {
23
+ constructor(connUrlInfo) {
25
24
  this.debugMode = false;
26
25
  this.lastInsertId = null;
27
26
  this.lastStatementChanges = null;
@@ -29,9 +28,9 @@ class Connection {
29
28
  this.explicitReturnKey = true
30
29
  this.saveDatesAsUTC = true
31
30
  this.transactionLevel = 0
31
+ this.connUrlInfo=connUrlInfo
32
32
 
33
33
  this.open = ESNext(this.openCB, this)
34
- this.getDataTable = ESNext(this.getDataTableCB, this)
35
34
  this.getObjects = ESNext(this.getObjectsCB, this)
36
35
  this.getSingleObject = ESNext(this.getSingleObjectCB , this)
37
36
  this.getList = ESNext(this.getListCB, this)
@@ -44,12 +43,10 @@ class Connection {
44
43
  this.beginTrans = ESNext(this.beginTransCB, this)
45
44
  this.commitTrans = ESNext(this.commitTransCB, this)
46
45
  this.rollbackTrans = ESNext(this.rollbackTransCB, this)
47
- this.forEachRow = ESNext(this.forEachRowCB, this)
48
46
  this.close = ESNext(this.closeCB, this)
49
47
  }
50
48
 
51
49
  open() {}
52
- getDataTable() {}
53
50
  getObjects() {}
54
51
  getSingleObject () {}
55
52
  getList() {}
@@ -66,14 +63,17 @@ class Connection {
66
63
  }
67
64
 
68
65
  openCB(dbInfo, callback) {
69
- function postConnection(cn, sql, callback) {
70
- if (typeof (sql) == "string" || Array.isArray(sql) ) cn.execCB(sql, callback)
71
- else then( callback )
66
+ this.debug('open','Opening connection for PGSQL')
67
+ let connInfo
68
+ if (dbInfo) {
69
+ connInfo=dbInfo
70
+ } else {
71
+ connInfo= { host: this.connUrlInfo.hostname, user: this.connUrlInfo.username, password: this.connUrlInfo.password, database: this.connUrlInfo.database }
72
72
  }
73
73
 
74
- this.db = new pg.Client(dbInfo);
74
+ this.db = new pg.Client(connInfo);
75
75
  this.debug('open','Opening connection for PostgresQL')
76
- this.db.connect( function() { postConnection(this, dbInfo.initSql, callback) })
76
+ this.db.connect( callback )
77
77
  }
78
78
 
79
79
  batch() {
@@ -128,88 +128,6 @@ class Connection {
128
128
  mergeParams(sql, params) { return common.mergeParams(this, sql, params) }
129
129
  getRealSql(queryObject) { return common.getRealSql(this, queryObject) }
130
130
 
131
- forEachRowCB(params, eachfunc, callback) {
132
- var ts = Date.now();
133
- var realSql = this.getRealSql(params);
134
- var self = this;
135
- this.debug("forEachRow",realSql);
136
-
137
- this.db.query(realSql, function(err,result) {
138
-
139
-
140
- function finish() {
141
- self.debug("forEachRow",'Completed in ' + (Date.now() -ts) + ' ms');
142
- if (isFunction(callback)) then( callback)
143
- }
144
-
145
- if (err) {
146
- self.debug("forEachRow",'Error: '+err.code);
147
- then( function() {callback(err)}) ;
148
- } else {
149
- let r = 0
150
- then( function populateTable() {
151
- if (r < result.rows.length) {
152
- eachfunc( result.rows[r], function( err) {
153
- r++
154
- then(populateTable)
155
- } )
156
- } else {
157
- finish()
158
- }
159
- } )
160
- }
161
- })
162
- }
163
-
164
- getDataTableCB(params, callback) {
165
- var ts = Date.now();
166
- var realSql = this.getRealSql(params);
167
- var self = this;
168
- var useSnakeCase = false
169
- var useCamelCase = false
170
- if ( params.options ) {
171
- useSnakeCase = params.options.useSnakeCase || false
172
- useCamelCase = params.options.useCamelCase || false
173
- }
174
-
175
- this.debug("getDataTable",realSql);
176
- this.db.query(realSql, function(err, result) {
177
- let dt = new data.DataTable;
178
-
179
- function finish() {
180
- self.debug("getDataTable",'Completed in ' + (Date.now() -ts) + ' ms');
181
- if (isFunction(callback)) then( function() { callback(undefined, dt) } );
182
- }
183
-
184
- if (err) {
185
- self.debug("getDataTable",'Error: '+err.code);
186
- then( function() {callback(err)}) ;
187
- } else {
188
- let rows=result.rows;
189
- if (rows.length > 0 ) {
190
- let cols = Object.keys(rows[0])
191
- for (let c = 0; c< cols.length; c++) {
192
- if (useSnakeCase) dt.addColumn(common.toSnakeCase(cols[c]))
193
- else if (useCamelCase) dt.addColumn(common.toCamelCase(cols[c]))
194
- else dt.addColumn(cols[c])
195
- }
196
- let r = 0
197
- then( function populateTable() {
198
- if (r<rows.length) {
199
- dt.addRow( dt.newRow(rows[r]));
200
- r++
201
- then(populateTable)
202
- } else {
203
- finish()
204
- }
205
- } )
206
- } else {
207
- finish()
208
- }
209
- }
210
- });
211
- }
212
-
213
131
  getObjectsCB(params, callback) {
214
132
  var ts = Date.now();
215
133
  var realSql = this.getRealSql(params);
@@ -283,7 +201,7 @@ class Connection {
283
201
  var ts = Date.now();
284
202
  var realSql = this.getRealSql(params);
285
203
  var self = this;
286
- this.debug("getDataTable",realSql);
204
+ this.debug("getList",realSql);
287
205
  this.db.query(realSql, function(err,result) {
288
206
  if (err) {
289
207
  self.debug("getList",'Error: '+err.code);
@@ -358,7 +276,7 @@ class Connection {
358
276
  var ts = Date.now();
359
277
  var realSql = this.getRealSql(params);
360
278
  var self = this;
361
- this.debug("getDataTable",realSql);
279
+ this.debug("getScalar",realSql);
362
280
  this.db.query(realSql, function(err,result) {
363
281
  if (err) {
364
282
  self.debug("getScalar",'Error: '+err.code);
@@ -1,6 +1,6 @@
1
1
  /********************************************************************************
2
2
  * driver_sqlite3.js
3
- * Sqlite3 rdao driver
3
+ * Sqlite3 xardao driver
4
4
  *
5
5
  * Author : Stephane Potelle
6
6
  * Email : stephane.potelle@gmail.com
@@ -13,7 +13,7 @@ try {
13
13
  console.error("Please add module \"sqlite3\" to your package.json dependencies")
14
14
  process.exit(1)
15
15
  }
16
- var data = require('./datatable.js');
16
+
17
17
  var common = require ('./common.js');
18
18
  var ESNext = require('./esnext');
19
19
 
@@ -33,7 +33,6 @@ class Connection {
33
33
 
34
34
 
35
35
  this.open = ESNext(this.openCB, this)
36
- this.getDataTable = ESNext(this.getDataTableCB, this)
37
36
  this.getObjects = ESNext(this.getObjectsCB, this)
38
37
  this.getSingleObject = ESNext(this.getSingleObjectCB , this)
39
38
  this.getList = ESNext(this.getListCB, this)
@@ -46,12 +45,10 @@ class Connection {
46
45
  this.beginTrans = ESNext(this.beginTransCB, this)
47
46
  this.commitTrans = ESNext(this.commitTransCB, this)
48
47
  this.rollbackTrans = ESNext(this.rollbackTransCB, this)
49
- this.forEachRow = ESNext(this.forEachRowCB, this)
50
48
  this.close = ESNext(this.closeCB, this)
51
49
  }
52
50
 
53
51
  open() {}
54
- getDataTable() {}
55
52
  getObjects() {}
56
53
  getSingleObject () {}
57
54
  getList() {}
@@ -133,123 +130,7 @@ class Connection {
133
130
  mergeParams(sql, params) { return common.mergeParams(this, sql, params) }
134
131
  getRealSql(queryObject) { return common.getRealSql(this, queryObject) }
135
132
 
136
- forEachRowCB(params, eachfunc, callback) {
137
- var ts = Date.now();
138
- var realSql = this.getRealSql(params);
139
-
140
- var self = this;
141
- this.debug("forEachRow",realSql);
142
-
143
- this.db.configure("busyTimeout", this.timeoutMilliSeconds)
144
- this.db.all(realSql, [], function(err,rows) {
145
- function finish() {
146
- self.debug("forEachRow",'Completed in ' + (Date.now() -ts) + ' ms');
147
- if (isFunction(callback)) then( function() { callback() } );
148
- }
149
-
150
- if (err) {
151
- self.debug("forEachRow",'Error: '+err.code);
152
- then( function() {callback(err)}) ;
153
- } else {
154
- if (rows.length > 0 ) {
155
-
156
- let r = 0
157
- then( function populateTable() {
158
- if (r<rows.length) {
159
- eachfunc( rows[r], function() {
160
- r++
161
- populateTable})
162
- } else {
163
- finish()
164
- }
165
- } )
166
-
167
- } else {
168
- finish()
169
- }
170
- }
171
- })
172
- }
173
-
174
-
175
- forEachRowCBSync(params, eachfunc, callback) {
176
- var ts = Date.now();
177
- var realSql = this.getRealSql(params);
178
- var self = this;
179
- this.debug("forEachRow",realSql)
180
-
181
- this.db.configure("busyTimeout", this.timeoutMilliSeconds)
182
- this.db.each(realSql, [],
183
- function(err, row ) {
184
- if (!err) eachfunc(row, function(){})
185
- },
186
- function(err,rows){
187
-
188
- function finish() {
189
- self.debug("forEachRow",'Completed in ' + (Date.now() -ts) + ' ms');
190
- if (isFunction(callback)) then( function() { callback() } );
191
- }
192
-
193
- if (err) {
194
- self.debug("forEachRow",'Error: '+err.code);
195
- then( function() {callback(err)}) ;
196
- } else {
197
- finish()
198
- }
199
- })
200
- }
201
-
202
- getDataTableCB(params, callback) {
203
- var ts = Date.now();
204
- var realSql = this.getRealSql(params);
205
- var useSnakeCase = false
206
- var useCamelCase = false
207
- if ( params.options ) {
208
- useSnakeCase = params.options.useSnakeCase || false
209
- useCamelCase = params.options.useCamelCase || false
210
- }
211
- var self = this;
212
- this.debug("getDataTable",realSql);
213
-
214
- this.db.configure("busyTimeout", this.timeoutMilliSeconds)
215
- this.db.all(realSql, [], function(err,rows) {
216
- let dt = new data.DataTable;
217
-
218
- function finish() {
219
- self.debug("getDataTable",'Completed in ' + (Date.now() -ts) + ' ms');
220
- if (isFunction(callback)) then( function() { callback(undefined, dt) } );
221
- }
222
-
223
- if (err) {
224
- self.debug("getDataTable",'Error: '+err.code);
225
- then( function() {callback(err)}) ;
226
- } else {
227
- if (rows.length > 0 ) {
228
- let cols = Object.keys(rows[0])
229
- for (let c = 0; c< cols.length; c++) {
230
- if (useSnakeCase) dt.addColumn(common.toSnakeCase(cols[c]))
231
- else if (useCamelCase) dt.addColumn(common.toCamelCase(cols[c]))
232
- else dt.addColumn(cols[c])
233
- }
234
-
235
- let r = 0
236
- then( function populateTable() {
237
- if (r<rows.length) {
238
- dt.addRow( dt.newRow(rows[r]));
239
- r++
240
- then(populateTable)
241
- } else {
242
- finish()
243
- }
244
- } )
245
-
246
- } else {
247
- finish()
248
- }
249
- }
250
- });
251
- }
252
-
133
+
253
134
  getObjectsCB(params, callback) {
254
135
  var ts = Date.now();
255
136
  var realSql = this.getRealSql(params);
package/lib/xardao.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /********************************************************************************
2
- * datatable.js
2
+ * xardao.js
3
3
  * data structure for SQL database interactions
4
4
  *
5
5
  * Author : Stephane Potelle
@@ -9,7 +9,6 @@
9
9
  const modCRUDAdapter = require ('./crud-adapter')
10
10
  const URL=require('url').URL
11
11
 
12
- exports.DataTable = require ('./datatable').DataTable
13
12
  exports.CRUDAdapter = modCRUDAdapter.CRUDAdapter
14
13
 
15
14
  function parseConnUrl(url) {
@@ -81,7 +80,7 @@ function Connection(url) {
81
80
  return cn
82
81
  }
83
82
  else {
84
- throw new Error('Invalid RDAO driver name')
83
+ throw new Error('Invalid xardao driver name')
85
84
  }
86
85
  } catch(e) {
87
86
  console.log(e)
@@ -106,10 +105,6 @@ exports.express.usingDBConnection = function usingDBConnection(connSpec, page )
106
105
  }
107
106
  else throw "Invalid database specification"
108
107
 
109
- if (typeof(connInfo) == 'string') {
110
- connInfo = { driver:connInfo, dbInfo: undefined}
111
- }
112
-
113
108
  var db = Connection(connInfo.driver)
114
109
  db.debugMode = connInfo.debugMode || false
115
110
  db.openCB(connInfo.dbInfo,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xardao",
3
- "version": "1.2.4",
3
+ "version": "1.3.1",
4
4
  "description": "Common Relational Data Access library",
5
5
  "main": "./lib/xardao.js",
6
6
  "scripts": {
@@ -3,6 +3,5 @@
3
3
  {
4
4
  "path": "."
5
5
  }
6
- ],
7
- "settings": {}
6
+ ]
8
7
  }
package/lib/datatable.js DELETED
@@ -1,193 +0,0 @@
1
- /********************************************************************************
2
- * datatable.js
3
- * data structure for SQL database interactions
4
- *
5
- * Author : Stephane Potelle
6
- * Email : stephane.potelle@gmail.com
7
- ********************************************************************************/
8
-
9
- var ESNext = require('./esnext');
10
-
11
- class DataTable {
12
- constructor(name) {
13
- this.columns = [];
14
-
15
- this.rows = [];
16
- this.colmap = {};
17
- if (name != undefined ) this.tablename = name;
18
- else this.tablename="";
19
- this.JSONAsObjects = ESNext(this.JSONAsObjectsCB,this);
20
- this.JSONAsTable = ESNext(this.JSONAsTableCB,this);
21
-
22
- }
23
-
24
- // This function will re-create all internal associations
25
- repair() {
26
- this.refreshColmap;
27
- for ( let i=0; i< this.rows.length; i++ ) {
28
- this.rows[i].datatable = this;
29
- }
30
- }
31
-
32
- JSONAsObjects() {} // This is just the function prototype
33
- JSONAsTable() {} // This is just the function prototype
34
-
35
- JSON() {
36
- return JSON.stringify(this, (key,value) => {
37
- if ( this && key == 'datatable' ) return undefined;
38
- if ( this && key == 'colmap' ) return undefined;
39
- return value;
40
- });
41
- }
42
-
43
- JSONAsObjectsCB( pfirst, plast, callback ) {
44
- let o = []
45
- let first = pfirst || 0
46
- let last = plast || -1
47
- if ( plast < 0 ) plast = this.rows.length
48
- this.forRange(first, last, function(row) {
49
- o.push(JSON.stringify(row.toObject()))
50
- }
51
- , function() {
52
- let resp = '[' + o.join(',\n') + ']'
53
- callback(undefined, resp)
54
- })
55
- }
56
-
57
- JSONAsTableCB( pfirst, plast, callback ) {
58
- let me = this;
59
- let o = []
60
- let first = pfirst || 0
61
- let last = plast || -1
62
- if ( plast < 0 ) plast = this.rows.length
63
- this.forRange(first, last, function(row) {
64
- o.push(JSON.stringify(row.items))
65
- }
66
- , function() {
67
- let resp = '{"columns":'+ JSON.stringify(me.columns) + ',\n"rows": [' + o.join(',\n') + ']}'
68
- callback(undefined, resp)
69
- })
70
- }
71
-
72
- newRow(value) {
73
- var r = new DataRow(this);
74
- if ( value != undefined && typeof(value) == 'object') r.set(value);
75
- return r;
76
- }
77
-
78
- addRow(r) {
79
- this.rows.push(r)
80
- }
81
-
82
- delRow(n) {
83
- this.rows.splice(n,1);
84
- }
85
-
86
- addColumn(colName, defaultValue = null){
87
- if (this.getColumnIndex(colName) == undefined ) {
88
- this.colmap[colName]=this.columns.length;
89
- this.columns.push(colName)
90
- for (let i=0; i< this.rows.length; i++) this.rows[i].items.push(defaultValue);
91
- }
92
- }
93
-
94
- delColumn(colNameOrIndex) {
95
- var cindex = this.getColumnIndex(colNameOrIndex);
96
- if (cindex != undefined) {
97
- this.columns.splice(cindex,1);
98
- for (let i=0; i< this.rows.length; i++) this.rows[i].items.splice(cindex,1);;
99
- }
100
- }
101
-
102
- refreshColmap() {
103
- this.colmap = {};
104
- for (let i = 0; i< this.columns.length; i++ ) {
105
- this.colmap[this.columns[i]] = i;
106
- }
107
- }
108
-
109
- getColumnIndex(colNameOrIndex) {
110
- if (typeof(colNameOrIndex) == 'number') return colNameOrIndex;
111
- else if (typeof(colNameOrIndex) == 'string') return this.colmap[colNameOrIndex];
112
- else throw new Error('Invalid index');
113
- }
114
-
115
- forRange( first, last, block, next ) {
116
- var i = first ;
117
- var me=this;
118
-
119
- if ( i < 0 ) i = 0;
120
-
121
- function loop() {
122
- if ( i< me.rows.length && ( i<=last || last < 0 )) {
123
- block(me.rows[i]);
124
- i++;
125
- then(loop);
126
- }
127
- else then(next);
128
- }
129
-
130
- loop();
131
- }
132
-
133
- forEachRow(block, next) {
134
- this.forRange(0,-1, block, next);
135
- }
136
-
137
- toArraySync(){
138
- let a = []
139
- for (let r = 0; r<this.rows.length; r++) {
140
- a.push(this.rows[r].items)
141
- }
142
- return a
143
- }
144
-
145
- toArray(callback){
146
- let a = []
147
-
148
- forEachRow(function() { a.push(this.rows[r].items) }, function() { callback(a) })
149
- }
150
- }
151
-
152
- class DataRow {
153
- constructor(table) {
154
- this.items = [];
155
- this.datatable = table;
156
- for (let i = 0; i< this.datatable.columns.length; i++ ) {
157
- this.items.push(null);
158
- }
159
- }
160
-
161
- get(index) {
162
- return this.items[this.datatable.getColumnIndex(index)];
163
- }
164
-
165
- set(index, value) {
166
- if ((typeof(index) == 'number' || typeof(index) == 'string' ) && value != undefined )
167
- this.items[this.datatable.getColumnIndex(index)]=value;
168
- else if ( typeof(index) == 'object' ) {
169
-
170
- let obj = index;
171
- let keys = Object.keys(obj);
172
- for ( let i = 0 ; i < keys.length; i ++ ){
173
- let k = keys[i];
174
- let ix;
175
- if ( ( ix = this.datatable.getColumnIndex(k)) !=undefined ){
176
- this.items[ix] = obj[k];
177
- }
178
- }
179
- }
180
- }
181
-
182
- toObject() {
183
- let ret = {};
184
- for (let i=0 ; i < this.datatable.columns.length; i++) {
185
- ret[this.datatable.columns[i]] = this.items[i];
186
- }
187
- return ret;
188
- }
189
-
190
- }
191
-
192
- exports.DataRow = DataRow;
193
- exports.DataTable = DataTable;