doix-db 1.0.2 → 1.0.4

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/index.js CHANGED
@@ -12,6 +12,8 @@ module.exports = {
12
12
  DbView: require ('./lib/model/DbView.js'),
13
13
  DbObjectMap: require ('./lib/model/DbObjectMap.js'),
14
14
  DbObject: require ('./lib/model/DbObject.js'),
15
+ DbProcedure: require ('./lib/model/DbProcedure.js'),
16
+ DbFunction: require ('./lib/model/DbFunction.js'),
15
17
  DbModel: require ('./lib/model/DbModel.js'),
16
18
 
17
19
  DbType: require ('./lib/model/types/DbType.js'),
package/lib/DbLang.js CHANGED
@@ -3,6 +3,9 @@ const stringEscape = require ('string-escape-map')
3
3
  const DbTable = require ('./model/DbTable.js')
4
4
  const DbView = require ('./model/DbView.js')
5
5
 
6
+ const DbProcedure = require ('./model/DbProcedure.js')
7
+ const DbFunction = require ('./model/DbFunction.js')
8
+
6
9
  const DbType = require ('./model/types/DbType.js')
7
10
  const DbTypeArithmetic = require ('./model/types/DbTypeArithmetic.js')
8
11
  const DbTypeArithmeticFixed = require ('./model/types/DbTypeArithmeticFixed.js')
@@ -73,9 +76,11 @@ class DbLang {
73
76
 
74
77
  if (typeof o !== 'object' || Array.isArray (o)) throw Error ('Not an object')
75
78
 
76
- if (!('columns' in o)) throw Error ('Cannot detect type for ' + JSON.stringify (o))
77
-
78
- return 'sql' in o ? DbView : DbTable
79
+ if ('columns' in o) return 'sql' in o ? DbView : DbTable
80
+
81
+ if ('body' in o) return 'returns' in o ? DbFunction : DbProcedure
82
+
83
+ throw Error ('Cannot detect type for ' + JSON.stringify (o))
79
84
 
80
85
  }
81
86
 
@@ -0,0 +1,5 @@
1
+ const DbRoutine = require ('./DbRoutine.js')
2
+
3
+ class DbFunction extends DbRoutine {}
4
+
5
+ module.exports = DbFunction
@@ -0,0 +1,5 @@
1
+ const DbRoutine = require ('./DbRoutine.js')
2
+
3
+ class DbProcedure extends DbRoutine {}
4
+
5
+ module.exports = DbProcedure
@@ -0,0 +1,41 @@
1
+ const DbObject = require ('./DbObject.js')
2
+ const DbRoutineParameter = require ('./DbRoutineParameter.js')
3
+
4
+ class DbRoutine extends DbObject {
5
+
6
+ constructor (o) {
7
+
8
+ for (const k of ['body']) if (!(k in o)) throw Error (`${k} is not defined`)
9
+
10
+ for (const k of ['parameters', 'options']) {
11
+
12
+ if (o [k] == null) {
13
+
14
+ o [k] = []
15
+
16
+ }
17
+ else if (!Array.isArray (o [k])) {
18
+
19
+ throw Error (`When defined, ${k} must be an array`)
20
+
21
+ }
22
+
23
+ }
24
+
25
+ o.parameters = o.parameters.map (o => new DbRoutineParameter (o))
26
+
27
+ super (o)
28
+
29
+ }
30
+
31
+ setLang (lang) {
32
+
33
+ super.setLang (lang)
34
+
35
+ for (const parameter of this.parameters) parameter.setLang (lang)
36
+
37
+ }
38
+
39
+ }
40
+
41
+ module.exports = DbRoutine
@@ -0,0 +1,86 @@
1
+ const CH_EQ = '='.charCodeAt (0)
2
+ const CH_SPACE = ' '.charCodeAt (0)
3
+ const MODES = ['OUT', 'INOUT', 'IN']
4
+
5
+ class DbRoutineParameter {
6
+
7
+ constructor (o) {
8
+
9
+ if (typeof o === 'string') {
10
+
11
+ if (o.length === 0) throw Error (`DbRoutineParameter DSL source must be a non empty string`)
12
+
13
+ this.parse (o)
14
+
15
+ }
16
+ else {
17
+
18
+ for (const k of ['name', 'type']) {
19
+
20
+ const v = o [k]
21
+
22
+ if (!v || typeof v !== 'string') throw Error (`DbRoutineParameter ${k} must be a non empty string`)
23
+
24
+ }
25
+
26
+ for (const k in o) this [k] = o [k]
27
+
28
+ if (!this.mode) this.mode = 'IN'
29
+
30
+ }
31
+
32
+ }
33
+
34
+ parse (src) {
35
+
36
+ const bkp = src
37
+
38
+ for (const mode of MODES) {
39
+
40
+ const {length} = mode; if (src.charCodeAt (length) !== CH_SPACE) continue
41
+
42
+ if (src.slice (0, length) !== mode) continue
43
+
44
+ this.mode = mode
45
+
46
+ src = src.slice (length).trim ()
47
+
48
+ break
49
+
50
+ }
51
+
52
+ if (!('mode' in this)) this.mode = 'IN'
53
+
54
+ {
55
+
56
+ const eq = src.indexOf ('='); if (eq !== -1) {
57
+
58
+ this.default = src.slice (eq + 1).trim ()
59
+
60
+ src = src.slice (0, eq).trim ()
61
+
62
+ }
63
+
64
+ }
65
+
66
+ {
67
+
68
+ const sp = src.indexOf (' '); if (sp === -1) throw Error (`Invalid parameter definition: "${bkp}"`)
69
+
70
+ this.name = src.slice (0, sp).trim ()
71
+
72
+ this.type = src.slice (sp).trim ()
73
+
74
+ }
75
+
76
+ }
77
+
78
+ setLang (lang) {
79
+
80
+ this.qName = lang.quoteName (this.name)
81
+
82
+ }
83
+
84
+ }
85
+
86
+ module.exports = DbRoutineParameter
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Shared database related code for doix",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -43,7 +43,7 @@
43
43
  "doix": "^1.0.0"
44
44
  },
45
45
  "devDependencies": {
46
- "jest": "^29.3.1"
46
+ "jest": "^29.6.1"
47
47
  },
48
48
  "dependencies": {
49
49
  "string-escape-map": "^1.0.0"