orange-orm 5.1.0 → 5.2.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.
@@ -0,0 +1,139 @@
1
+ const negotiateRawSqlFilter = require('../column/negotiateRawSqlFilter');
2
+ const newJoin = require('./joinSql');
3
+ const newWhere = require('./whereSql');
4
+ const getSessionSingleton = require('../getSessionSingleton');
5
+ const newParameterized = require('../query/newParameterized');
6
+ const newBoolean = require('../column/newBoolean');
7
+
8
+ const nullOperator = ' is ';
9
+ const notNullOperator = ' is not ';
10
+ const isShallow = true;
11
+
12
+ function newCount(newRelatedTable, relations, depth) {
13
+
14
+ function count(context, fn) {
15
+ let shallowFilter;
16
+
17
+ if (fn !== undefined) {
18
+ let relatedTable = newRelatedTable(relations, isShallow, depth + 1);
19
+ let arg = typeof fn === 'function' ? fn(relatedTable) : fn;
20
+ shallowFilter = negotiateRawSqlFilter(context, arg);
21
+ }
22
+
23
+ const subQuery = newCountSubQuery(context, relations, shallowFilter, depth);
24
+ return newCountFilter(subQuery);
25
+ }
26
+
27
+ return count;
28
+ }
29
+
30
+ function newCountSubQuery(context, relations, shallowFilter, depth) {
31
+ const quote = getSessionSingleton(context, 'quote');
32
+ const relationCount = relations.length;
33
+ const alias = 'x' + relationCount;
34
+ const table = relations[relationCount - 1].childTable;
35
+ const select = newParameterized(`SELECT COUNT(*) FROM ${quote(table._dbName)} ${quote(alias)}`);
36
+ const join = newJoin(context, relations, depth);
37
+ const where = newWhere(context, relations, shallowFilter, depth);
38
+
39
+ return select.append(join).append(where);
40
+ }
41
+
42
+ function newCountFilter(subQuery) {
43
+ let c = {};
44
+
45
+ c.equal = function(context, arg) {
46
+ return compare(context, '=', arg);
47
+ };
48
+
49
+ c.notEqual = function(context, arg) {
50
+ return compare(context, '<>', arg);
51
+ };
52
+
53
+ c.lessThan = function(context, arg) {
54
+ return compare(context, '<', arg);
55
+ };
56
+
57
+ c.lessThanOrEqual = function(context, arg) {
58
+ return compare(context, '<=', arg);
59
+ };
60
+
61
+ c.greaterThan = function(context, arg) {
62
+ return compare(context, '>', arg);
63
+ };
64
+
65
+ c.greaterThanOrEqual = function(context, arg) {
66
+ return compare(context, '>=', arg);
67
+ };
68
+
69
+ c.between = function(context, from, to) {
70
+ from = c.greaterThanOrEqual(context, from);
71
+ to = c.lessThanOrEqual(context, to);
72
+ return from.and(context, to);
73
+ };
74
+
75
+ c.in = function(context, values) {
76
+ if (values.length === 0)
77
+ return newBoolean(newParameterized('1=2'));
78
+
79
+ let sqlParts = new Array(values.length);
80
+ let params = [];
81
+ for (let i = 0; i < values.length; i++) {
82
+ const encoded = encodeArg(context, values[i]);
83
+ sqlParts[i] = encoded.sql();
84
+ params = params.concat(encoded.parameters);
85
+ }
86
+
87
+ const sql = toSubQuery().sql() + ' in (' + sqlParts.join(',') + ')';
88
+ const allParams = toSubQuery().parameters.concat(params);
89
+ return newBoolean(newParameterized(sql, allParams));
90
+ };
91
+
92
+ c.notIn = function(context, values) {
93
+ return c.in(context, values).not(context);
94
+ };
95
+
96
+ c.eq = c.equal;
97
+ c.EQ = c.eq;
98
+ c.ne = c.notEqual;
99
+ c.NE = c.ne;
100
+ c.gt = c.greaterThan;
101
+ c.GT = c.gt;
102
+ c.ge = c.greaterThanOrEqual;
103
+ c.GE = c.ge;
104
+ c.lt = c.lessThan;
105
+ c.LT = c.lt;
106
+ c.le = c.lessThanOrEqual;
107
+ c.LE = c.le;
108
+ c.IN = c.in;
109
+
110
+ return c;
111
+
112
+ function compare(context, operator, arg) {
113
+ let encoded = encodeArg(context, arg);
114
+ let operatorSql = ' ' + operator + ' ';
115
+ if (encoded.sql() === 'null') {
116
+ if (operator === '=')
117
+ operatorSql = nullOperator;
118
+ else if (operator === '<>')
119
+ operatorSql = notNullOperator;
120
+ }
121
+
122
+ let sql = toSubQuery().append(operatorSql).append(encoded);
123
+ return newBoolean(sql);
124
+ }
125
+
126
+ function encodeArg(context, arg) {
127
+ if (arg && typeof arg._toFilterArg === 'function')
128
+ return arg._toFilterArg(context);
129
+ if (arg == null)
130
+ return newParameterized('null');
131
+ return newParameterized('?', [arg]);
132
+ }
133
+
134
+ function toSubQuery() {
135
+ return subQuery.prepend('(').append(')');
136
+ }
137
+ }
138
+
139
+ module.exports = newCount;
@@ -0,0 +1,20 @@
1
+ var newJoin = require('./joinSql');
2
+ var newWhere = require('./whereSql');
3
+ var newParameterized = require('../query/newParameterized');
4
+ var quote = require('../quote');
5
+
6
+ function newFilterArg(context, column, relations, depth = 0) {
7
+ var relationCount = relations.length;
8
+ var alias = 'x' + relationCount;
9
+ var table = relations[relationCount - 1].childTable;
10
+
11
+ var quotedAlias = quote(context, alias);
12
+ var quotedColumn = quote(context, column._dbName);
13
+ var quotedTable = quote(context, table._dbName);
14
+ var select = newParameterized(`(SELECT ${quotedAlias}.${quotedColumn} FROM ${quotedTable} ${quotedAlias}`);
15
+ var join = newJoin(context, relations, depth);
16
+ var where = newWhere(context, relations, null, depth);
17
+ return select.append(join).append(where).append(')');
18
+ }
19
+
20
+ module.exports = newFilterArg;
@@ -2,6 +2,7 @@ var newSubFilter = require('./subFilter');
2
2
  var aggregateGroup = require('./columnAggregateGroup');
3
3
  var aggregate = require('./columnAggregate');
4
4
  var childColumn = require('./childColumn');
5
+ var newFilterArg = require('./newFilterArg');
5
6
 
6
7
  function newRelatedColumn(column, relations, isShallow, depth) {
7
8
  var c = {};
@@ -25,9 +26,18 @@ function newRelatedColumn(column, relations, isShallow, depth) {
25
26
  c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', column, relations, false, ...rest]);
26
27
  c.count = (context, ...rest) => aggregate.apply(null, [context, 'count', column, relations, false, ...rest]);
27
28
  c.self = (context, ...rest) => childColumn.apply(null, [context, column, relations, ...rest]);
29
+ Object.defineProperty(c, '_toFilterArg', {
30
+ value: toFilterArg,
31
+ enumerable: false,
32
+ writable: false
33
+ });
28
34
 
29
35
  return c;
30
36
 
37
+ function toFilterArg(context) {
38
+ return newFilterArg(context, column, relations, depth);
39
+ }
40
+
31
41
  function wrapFilter(filter) {
32
42
  return runFilter;
33
43
 
@@ -46,4 +56,4 @@ function newRelatedColumn(column, relations, isShallow, depth) {
46
56
 
47
57
  }
48
58
 
49
- module.exports = newRelatedColumn;
59
+ module.exports = newRelatedColumn;
package/src/table.js CHANGED
@@ -79,6 +79,10 @@ function _new(tableName) {
79
79
  const args = [context, table, ...rest];
80
80
  return groupBy.apply(null, args);
81
81
  };
82
+ table.distinct = function(context, ...rest) {
83
+ const args = [context, table, ...rest, { distinct: true }];
84
+ return groupBy.apply(null, args);
85
+ };
82
86
 
83
87
  table.getMany.exclusive = function(context, ...rest) {
84
88
  const args = [context, table, ...rest];