sql-tools 0.1.0 → 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 +13 -6
  4. package/package.json +3 -3
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
@@ -36,6 +36,11 @@ SqlTools.places={
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++;
@@ -172,7 +177,7 @@ SqlTools.structuredData.sqlsDeletes = function sqlsDeletes(data, structuredData,
172
177
  queriesArray.push(
173
178
  "delete from " + SqlTools.quoteIdent(childTable.tableName) +
174
179
  " where " + conditionChild.join(' and ') +
175
- " and " + (parentPk.length!=1?'(':'') + parentPk.join(', ') + (parentPk.length!=1?')':'') + `
180
+ " and " + join1wp(parentPk) + `
176
181
  not in (select ${parentPk.join(', ')} from jsonb_populate_recordset(null::${SqlTools.quoteIdent(childTable.tableName)},
177
182
  ${SqlTools.quoteLiteral(JSON.stringify(data[childTable.tableName]))}::jsonb
178
183
  ));`
@@ -231,12 +236,14 @@ SqlTools.structuredData.sqlsUpserts = function sqlsUpserts(data, structureData,
231
236
  for(var i in pkFields){
232
237
  where_expr.push(SqlTools.quoteIdent(structureData.tableName) + '.' + SqlTools.quoteIdent(pkFields[i]) + '=' + SqlTools.quoteLiteral(pkValues[i]));
233
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 ')}`
234
242
  var query = `
235
- insert into ` + SqlTools.quoteIdent(structureData.tableName) + ` ( ` + fields.join(',') + `)
236
- values (` + SqlTools.quoteLiteralArray(values).join(',') + `)
237
- on conflict (` + SqlTools.quoteIdentArray(pkFields).join(',') + `)
238
- do update set (` + SqlTools.quoteIdentArray(fieldsWithoutPk).join(',') + `) = (` + SqlTools.quoteLiteralArray(valuesWithoutPk).join(',') + `)
239
- 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()} ;`
240
247
  childQueries.unshift(query);
241
248
  return childQueries;
242
249
  }
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.1.0",
4
+ "version": "0.1.2",
5
5
  "author": "Codenautas <codenautas@googlegroups.com>",
6
6
  "license": "MIT",
7
7
  "repository": "codenautas/sql-tools",
@@ -25,8 +25,8 @@
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": [