sql-tools 0.0.16 → 0.1.2

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.
Files changed (4) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +120 -120
  3. package/bin/sql-tools.js +43 -41
  4. package/package.json +6 -6
package/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015 Codenautas
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.
22
-
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Codenautas
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.
22
+
package/README.md CHANGED
@@ -1,121 +1,121 @@
1
- # sql-tools
2
- olap for sql non-olap engines / and other tools
3
- <!--multilang v0 en:README.md es:LEEME.md -->
4
-
5
- <!--lang:es--]
6
-
7
- OLAP y otras herramientas para motores de base de datos SQL que no tienen OLAP
8
-
9
- [!--lang:*-->
10
-
11
- ![designing](https://img.shields.io/badge/stability-desgining-red.svg)
12
- [![version](https://img.shields.io/npm/v/sql-tools.svg)](https://npmjs.org/package/sql-tools)
13
- [![downloads](https://img.shields.io/npm/dm/sql-tools.svg)](https://npmjs.org/package/sql-tools)
14
- [![build](https://img.shields.io/travis/codenautas/sql-tools/master.svg)](https://travis-ci.org/codenautas/sql-tools)
15
- [![coverage](https://img.shields.io/coveralls/codenautas/sql-tools/master.svg)](https://coveralls.io/r/codenautas/sql-tools)
16
- [![climate](https://img.shields.io/codeclimate/github/codenautas/sql-tools.svg)](https://codeclimate.com/github/codenautas/sql-tools)
17
- [![dependencies](https://img.shields.io/david/codenautas/sql-tools.svg)](https://david-dm.org/codenautas/sql-tools)
18
-
19
- <!--multilang buttons-->
20
-
21
- language: ![English](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-en.png)
22
- also available in:
23
- [![Spanish](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-es.png)](LEEME.md)
24
-
25
- <!--lang:en-->
26
-
27
- ## Install
28
-
29
- <!--lang:es--]
30
- ## Instalación
31
-
32
- [!--lang:*-->
33
-
34
- ```sh
35
- $ npm install sql-tools
36
- ```
37
-
38
- <!--lang:en-->
39
-
40
- ## Main goal
41
-
42
- Transfor a SQL sentence in a SQL with totals.
43
-
44
- <!--lang:es--]
45
-
46
- ## Objetivo principal
47
-
48
- Transformar una sentencia SQL en otra que obtenga también los totales agrupando por la variable especificada.
49
-
50
- [!--lang:en-->
51
-
52
- ## Definitions
53
-
54
- Uses a list of field definition with the form:
55
-
56
- attribute | mandatory | use
57
- ----------|-----------|-------------------------------------
58
- name | yes | name of the field in the database
59
- place | yes | 'data' if must be added, 'left' or 'top' if it must apear in the GROUP BY clausule
60
- aggLabel | for pivot | text to insert in the added rows
61
- aggExp | no | the expression when is different to SUM(x)
62
-
63
- <!--lang:es--]
64
-
65
- ## Definición
66
-
67
- Usa una lista de definición de campos con los siguientes atributos:
68
-
69
- atributo | obligatorio | uso
70
- ----------|-----------|-------------------------------------
71
- name | sí | nombre del campo
72
- place | sí | 'data' si el campo debe agregarse, 'left' o 'top' si debe pertenecer a la cláusula GROUP BY
73
- aggLabel | p/pivote | el texto que debe ponerse en las filas de datos agregados
74
- aggExp | no | la expresión de totalización cuando no es SUM(x)
75
-
76
- [!--lang:en-->
77
-
78
- ## Example
79
-
80
- <!--lang:es--]
81
-
82
- ## Ejemplo
83
-
84
- [!--lang:*-->
85
-
86
- ```js
87
- var olap = require('sql-tools').olap;
88
-
89
- var varsDef=[
90
- {name: "zone", place:"left", aggLabel:"=SUM="},
91
- {name: "kind", place:"left"},
92
- {name: "sales", place:"data"},
93
- {name: "calif", place:"data", aggExp:"min(calif)"}
94
- ];
95
-
96
- var sql_total=olap.cube("select * from sales", 'zone', varsDef);
97
-
98
- console.log(sql_total);
99
- /*
100
- WITH "olap cube" AS (
101
- select * from sales
102
- ) SELECT * FROM "olap cube"
103
- UNION SELECT '=SUM=', kind, SUM(sales), min(calif)
104
- FROM "olap cube"
105
- GROUP BY kind
106
- */
107
- ```
108
-
109
- <!--lang:en-->
110
-
111
- ## License
112
-
113
- <!--lang:es--]
114
-
115
- ## Licencia
116
-
117
- [!--lang:*-->
118
-
119
- [MIT](LICENSE)
120
-
1
+ # sql-tools
2
+ olap for sql non-olap engines / and other tools
3
+ <!--multilang v0 en:README.md es:LEEME.md -->
4
+
5
+ <!--lang:es--]
6
+
7
+ OLAP y otras herramientas para motores de base de datos SQL que no tienen OLAP
8
+
9
+ [!--lang:*-->
10
+
11
+ ![designing](https://img.shields.io/badge/stability-desgining-red.svg)
12
+ [![version](https://img.shields.io/npm/v/sql-tools.svg)](https://npmjs.org/package/sql-tools)
13
+ [![downloads](https://img.shields.io/npm/dm/sql-tools.svg)](https://npmjs.org/package/sql-tools)
14
+ [![build](https://img.shields.io/travis/codenautas/sql-tools/master.svg)](https://travis-ci.org/codenautas/sql-tools)
15
+ [![coverage](https://img.shields.io/coveralls/codenautas/sql-tools/master.svg)](https://coveralls.io/r/codenautas/sql-tools)
16
+ [![climate](https://img.shields.io/codeclimate/github/codenautas/sql-tools.svg)](https://codeclimate.com/github/codenautas/sql-tools)
17
+ [![dependencies](https://img.shields.io/david/codenautas/sql-tools.svg)](https://david-dm.org/codenautas/sql-tools)
18
+
19
+ <!--multilang buttons-->
20
+
21
+ language: ![English](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-en.png)
22
+ also available in:
23
+ [![Spanish](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-es.png)](LEEME.md)
24
+
25
+ <!--lang:en-->
26
+
27
+ ## Install
28
+
29
+ <!--lang:es--]
30
+ ## Instalación
31
+
32
+ [!--lang:*-->
33
+
34
+ ```sh
35
+ $ npm install sql-tools
36
+ ```
37
+
38
+ <!--lang:en-->
39
+
40
+ ## Main goal
41
+
42
+ Transfor a SQL sentence in a SQL with totals.
43
+
44
+ <!--lang:es--]
45
+
46
+ ## Objetivo principal
47
+
48
+ Transformar una sentencia SQL en otra que obtenga también los totales agrupando por la variable especificada.
49
+
50
+ [!--lang:en-->
51
+
52
+ ## Definitions
53
+
54
+ Uses a list of field definition with the form:
55
+
56
+ attribute | mandatory | use
57
+ ----------|-----------|-------------------------------------
58
+ name | yes | name of the field in the database
59
+ place | yes | 'data' if must be added, 'left' or 'top' if it must apear in the GROUP BY clausule
60
+ aggLabel | for pivot | text to insert in the added rows
61
+ aggExp | no | the expression when is different to SUM(x)
62
+
63
+ <!--lang:es--]
64
+
65
+ ## Definición
66
+
67
+ Usa una lista de definición de campos con los siguientes atributos:
68
+
69
+ atributo | obligatorio | uso
70
+ ----------|-----------|-------------------------------------
71
+ name | sí | nombre del campo
72
+ place | sí | 'data' si el campo debe agregarse, 'left' o 'top' si debe pertenecer a la cláusula GROUP BY
73
+ aggLabel | p/pivote | el texto que debe ponerse en las filas de datos agregados
74
+ aggExp | no | la expresión de totalización cuando no es SUM(x)
75
+
76
+ [!--lang:en-->
77
+
78
+ ## Example
79
+
80
+ <!--lang:es--]
81
+
82
+ ## Ejemplo
83
+
84
+ [!--lang:*-->
85
+
86
+ ```js
87
+ var olap = require('sql-tools').olap;
88
+
89
+ var varsDef=[
90
+ {name: "zone", place:"left", aggLabel:"=SUM="},
91
+ {name: "kind", place:"left"},
92
+ {name: "sales", place:"data"},
93
+ {name: "calif", place:"data", aggExp:"min(calif)"}
94
+ ];
95
+
96
+ var sql_total=olap.cube("select * from sales", 'zone', varsDef);
97
+
98
+ console.log(sql_total);
99
+ /*
100
+ WITH "olap cube" AS (
101
+ select * from sales
102
+ ) SELECT * FROM "olap cube"
103
+ UNION SELECT '=SUM=', kind, SUM(sales), min(calif)
104
+ FROM "olap cube"
105
+ GROUP BY kind
106
+ */
107
+ ```
108
+
109
+ <!--lang:en-->
110
+
111
+ ## License
112
+
113
+ <!--lang:es--]
114
+
115
+ ## Licencia
116
+
117
+ [!--lang:*-->
118
+
119
+ [MIT](LICENSE)
120
+
121
121
  .............................
package/bin/sql-tools.js CHANGED
@@ -29,13 +29,18 @@ SqlTools.defaults={
29
29
  };
30
30
 
31
31
  SqlTools.places={
32
- left:{groupby:true},
33
- top :{groupby:true},
34
- data:{aggregate:true}
32
+ left:{groupby:true},
33
+ top :{groupby:true},
34
+ data:{aggregate:true}
35
35
  };
36
36
 
37
37
  SqlTools.olap={};
38
38
 
39
+ /** @param {string[]} elements */
40
+ function join1wp(elements){
41
+ return (elements.length!=1?'(':'') + elements.join(', ') + (elements.length!=1?')':'');
42
+ }
43
+
39
44
  function coalesce(){
40
45
  var i=0;
41
46
  while(i<arguments.length-1 && arguments[i]==null) i++;
@@ -152,39 +157,32 @@ SqlTools.structuredData.sqlRead = function sqlRead(pk, structuredData, globalInf
152
157
 
153
158
  }
154
159
 
155
- SqlTools.structuredData.sqlsDeletes = function sqlsDeletes(data, structuredData, parentData, parentStructureData, queriesArray){
160
+ SqlTools.structuredData.sqlsDeletes = function sqlsDeletes(data, structuredData, queriesArray, parentPks){
156
161
  if(structuredData.childTables && structuredData.childTables.length){
157
162
  structuredData.childTables.forEach(function(childTable){
158
- if(parentStructureData){
159
- data.forEach(function(elem){
160
- queriesArray = SqlTools.structuredData.sqlsDeletes(elem[childTable.tableName], childTable, elem, structuredData, queriesArray);
161
- })
162
- }else{
163
- queriesArray = SqlTools.structuredData.sqlsDeletes(data[childTable.tableName], childTable, data, structuredData, queriesArray);
164
- }
165
- });
166
- }
167
- if(parentStructureData){
168
- var condition = [];
169
- data.forEach(function(elem){
170
- structuredData.pkFields.forEach(function(field){
171
- if(elem[field.fieldName] != null){
172
- condition.push(SqlTools.quoteIdent(field.fieldName) + ' <> ' + SqlTools.quoteLiteral(elem[field.fieldName]));
173
- }else{
174
- condition.push(
175
- SqlTools.quoteIdent(field.fieldName) + ' = ' + SqlTools.quoteLiteral(parentData[structuredData.fkFields.find(function(fkField){
176
- return fkField.source === field.fieldName
177
- }).target])
178
- );
163
+ var childPksIndex={};
164
+ Object.assign(childPksIndex, parentPks);
165
+ var conditionChild=childTable.fkFields.map(function(pair){
166
+ if(data[pair.target]!=null){
167
+ childPksIndex[pair.source]=data[pair.target];
179
168
  }
180
- })
181
- })
182
- parentStructureData.pkFields.forEach(function(field){
183
- if(parentData[structuredData.fkFields.find(function(elem){ return elem.target === field.fieldName}).source]){
184
- condition.push(SqlTools.quoteIdent(field.fieldName) + ' = ' + SqlTools.quoteLiteral(parentData[field.fieldName]));
185
- }
169
+ return pair.source + ' = ' + SqlTools.quoteLiteral(childPksIndex[pair.source])
170
+ });
171
+ (data[childTable.tableName]||[]).forEach(function(childData){
172
+ queriesArray = SqlTools.structuredData.sqlsDeletes(childData, childTable, queriesArray, childPksIndex);
173
+ });
174
+ var parentPk=childTable.pkFields.filter(function(field){
175
+ return !childPksIndex[field.fieldName];
176
+ }).map(function(field){ return field.fieldName; });
177
+ queriesArray.push(
178
+ "delete from " + SqlTools.quoteIdent(childTable.tableName) +
179
+ " where " + conditionChild.join(' and ') +
180
+ " and " + join1wp(parentPk) + `
181
+ not in (select ${parentPk.join(', ')} from jsonb_populate_recordset(null::${SqlTools.quoteIdent(childTable.tableName)},
182
+ ${SqlTools.quoteLiteral(JSON.stringify(data[childTable.tableName]))}::jsonb
183
+ ));`
184
+ );
186
185
  });
187
- queriesArray.push("delete from " + SqlTools.quoteIdent(structuredData.tableName) + " where " + condition.join(' and ') + ";");
188
186
  }
189
187
  return queriesArray
190
188
  }
@@ -197,15 +195,17 @@ SqlTools.structuredData.sqlsUpserts = function sqlsUpserts(data, structureData,
197
195
  var childStructuredData = structureData.childTables.find(function(table){
198
196
  return table.tableName==key
199
197
  });
198
+ var inheritedPksForChild={};
199
+ Object.assign(inheritedPksForChild, inheritedPks);
200
200
  if(childStructuredData){
201
201
  var childrenData = data[key];
202
202
  childrenData.forEach(function(childData){
203
203
  structureData.pkFields.forEach(function(pkField) {
204
- if (!inheritedPks[pkField.fieldName]){
205
- inheritedPks[pkField.fieldName] = data[pkField.fieldName]
204
+ if (data[pkField.fieldName] != null){
205
+ inheritedPksForChild[pkField.fieldName] = data[pkField.fieldName]
206
206
  }
207
207
  });
208
- childQueries=childQueries.concat(SqlTools.structuredData.sqlsUpserts(childData, childStructuredData, data, structureData, inheritedPks))
208
+ childQueries=childQueries.concat(SqlTools.structuredData.sqlsUpserts(childData, childStructuredData, data, structureData, inheritedPksForChild))
209
209
  })
210
210
  }else{
211
211
  fields.push(key);
@@ -236,18 +236,20 @@ SqlTools.structuredData.sqlsUpserts = function sqlsUpserts(data, structureData,
236
236
  for(var i in pkFields){
237
237
  where_expr.push(SqlTools.quoteIdent(structureData.tableName) + '.' + SqlTools.quoteIdent(pkFields[i]) + '=' + SqlTools.quoteLiteral(pkValues[i]));
238
238
  }
239
+ var doUpdate = ()=>`do update set ( ${SqlTools.quoteIdentArray(fieldsWithoutPk).join(',')} ) =
240
+ ${fieldsWithoutPk.length == 1?'row':''} ( ${SqlTools.quoteLiteralArray(valuesWithoutPk).join(',')} )
241
+ where ${where_expr.join(' and ')}`
239
242
  var query = `
240
- insert into ` + SqlTools.quoteIdent(structureData.tableName) + ` ( ` + fields.join(',') + `)
241
- values (` + SqlTools.quoteLiteralArray(values).join(',') + `)
242
- on conflict (` + SqlTools.quoteIdentArray(pkFields).join(',') + `)
243
- do update set (` + SqlTools.quoteIdentArray(fieldsWithoutPk).join(',') + `) = (` + SqlTools.quoteLiteralArray(valuesWithoutPk).join(',') + `)
244
- where ` + where_expr.join(' and ') + `;`
243
+ insert into ${SqlTools.quoteIdent(structureData.tableName)} ( ${fields.join(',')} )
244
+ values ( ${SqlTools.quoteLiteralArray(values).join(',')} )
245
+ on conflict ( ${SqlTools.quoteIdentArray(pkFields).join(',')} )
246
+ ${(fieldsWithoutPk.length == 0)?' do nothing':doUpdate()} ;`
245
247
  childQueries.unshift(query);
246
248
  return childQueries;
247
249
  }
248
250
 
249
251
  SqlTools.structuredData.sqlWrite = function sqlWrite(data, structuredData){
250
- return SqlTools.structuredData.sqlsDeletes(data, structuredData, null, null, [])
252
+ return SqlTools.structuredData.sqlsDeletes(data, structuredData,[], {})
251
253
  .concat(
252
254
  SqlTools.structuredData.sqlsUpserts(data, structuredData, null, null, [], {})
253
255
  );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "sql-tools",
3
3
  "description": "olap for sql non-olap engines / and other tools",
4
- "version": "0.0.16",
4
+ "version": "0.1.2",
5
5
  "author": "Codenautas <codenautas@googlegroups.com>",
6
6
  "license": "MIT",
7
7
  "repository": "codenautas/sql-tools",
@@ -16,17 +16,17 @@
16
16
  "devDependencies": {
17
17
  "expect.js": "~0.3.1",
18
18
  "istanbul": "~0.4.5",
19
- "mocha": "~5.2.0",
20
- "mini-tools": "~1.0.4",
21
- "pg-promise-strict": "~0.4.16"
19
+ "mocha": "~6.1.4",
20
+ "mini-tools": "~1.0.8",
21
+ "pg-promise-strict": "~1.0.10"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">= 0.8.0"
25
25
  },
26
26
  "scripts": {
27
27
  "test": "mocha --reporter spec --bail --check-leaks --globals json4allRegisteredTypes test/",
28
- "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
29
- "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
28
+ "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks --globals json4allRegisteredTypes test/",
29
+ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks --globals json4allRegisteredTypes test/",
30
30
  "start": "node example/server.js"
31
31
  },
32
32
  "files": [