orange-orm 5.3.6 → 5.4.0-beta

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,74 @@
1
+ const getSessionSingleton = require('../../getSessionSingleton');
2
+
3
+ function selectLockSql(context, span, alias, exclusive) {
4
+ const lock = collectSelectLock(span, alias, exclusive);
5
+ if (!hasLock(lock))
6
+ return '';
7
+ const encode = getSessionSingleton(context, 'selectForUpdateSql');
8
+ if (!encode)
9
+ return '';
10
+ return encode(context, lock);
11
+ }
12
+
13
+ function tableHintSql(context, span, exclusive) {
14
+ const lock = spanToLock(span, exclusive);
15
+ if (!hasLock(lock))
16
+ return '';
17
+ const encode = getSessionSingleton(context, 'selectForUpdateSql');
18
+ if (!encode || !encode.tableHint)
19
+ return '';
20
+ return encode.tableHint(context, lock);
21
+ }
22
+
23
+ function collectSelectLock(span, alias, exclusive) {
24
+ const lock = {
25
+ aliases: [],
26
+ forUpdate: Boolean(exclusive),
27
+ skipLocked: false
28
+ };
29
+ collect(span, alias, lock);
30
+ if (exclusive && lock.aliases.indexOf(alias) === -1)
31
+ lock.aliases.unshift(alias);
32
+ return lock;
33
+ }
34
+
35
+ function collect(span, alias, lock) {
36
+ if (!span)
37
+ return;
38
+ if (span.forUpdate) {
39
+ lock.forUpdate = true;
40
+ lock.aliases.push(alias);
41
+ }
42
+ if (span.skipLocked)
43
+ lock.skipLocked = true;
44
+
45
+ if (!span.legs)
46
+ return;
47
+ const visitor = {};
48
+ visitor.visitJoin = visitJoinedLeg;
49
+ visitor.visitOne = visitJoinedLeg;
50
+ visitor.visitMany = function() {};
51
+
52
+ function visitJoinedLeg(leg) {
53
+ collect(leg.span, alias + leg.name, lock);
54
+ }
55
+
56
+ span.legs.forEach(leg => leg.accept(visitor));
57
+ }
58
+
59
+ function spanToLock(span, exclusive) {
60
+ return {
61
+ aliases: [],
62
+ forUpdate: Boolean(exclusive || span?.forUpdate),
63
+ skipLocked: Boolean(span?.skipLocked)
64
+ };
65
+ }
66
+
67
+ function hasLock(lock) {
68
+ return Boolean(lock.forUpdate || lock.skipLocked);
69
+ }
70
+
71
+ module.exports = {
72
+ selectLockSql,
73
+ tableHintSql
74
+ };
@@ -1,21 +1,27 @@
1
1
  var newPrimaryKeyFilter = require('./newPrimaryKeyFilter');
2
- var tryGetFirstFromDb = require('./tryGetFirstFromDb');
2
+ var getMany = require('./getMany');
3
3
  var extractStrategy = require('./tryGetFromDbById/extractStrategy');
4
4
 
5
5
  function tryGet(context) {
6
6
  var filter = newPrimaryKeyFilter.apply(null, arguments);
7
7
  var table = arguments[1];
8
8
  var strategy = extractStrategy.apply(null, arguments);
9
- return tryGetFirstFromDb(context, table, filter, strategy);
9
+ return getMany(context, table, filter, strategy).then(filterRows);
10
10
  }
11
11
 
12
12
  tryGet.exclusive = function tryGet(context) {
13
13
  var filter = newPrimaryKeyFilter.apply(null, arguments);
14
14
  var table = arguments[1];
15
15
  var strategy = extractStrategy.apply(null, arguments);
16
- return tryGetFirstFromDb.exclusive(context, table, filter, strategy);
16
+ return getMany.exclusive(context, table, filter, strategy).then(filterRows);
17
17
 
18
18
 
19
19
  };
20
20
 
21
+ function filterRows(rows) {
22
+ if (rows.length > 0)
23
+ return rows[0];
24
+ return null;
25
+ }
26
+
21
27
  module.exports = tryGet;
@@ -1,12 +1,14 @@
1
1
  var newColumnSql = require('./singleQuery/newShallowColumnSql');
2
2
  var newWhereSql = require('../../../table/query/singleQuery/newWhereSql');
3
3
  var newParameterized = require('../../../table/query/newParameterized');
4
+ var lockSql = require('../../../table/query/singleQuery/lockSql');
4
5
 
5
6
  function _new(context, table, filter, span, alias, subQueries, orderBy, limit, offset) {
6
7
  var columnSql = newColumnSql(context, table, alias, span);
7
8
  var whereSql = newWhereSql(context, table, filter, alias);
8
9
  if (limit)
9
10
  limit = limit + ' ';
11
+ const tableHint = lockSql.tableHintSql(context, span);
10
12
 
11
13
  let join = '';
12
14
  const set = new Set();
@@ -21,7 +23,7 @@ function _new(context, table, filter, span, alias, subQueries, orderBy, limit, o
21
23
  }
22
24
 
23
25
 
24
- return newParameterized('select ' + limit + columnSql).append(subQueries).append(' from ' + `[${table._dbName}] [${alias}]` + join).append(whereSql).append(orderBy + offset);
26
+ return newParameterized('select ' + limit + columnSql).append(subQueries).append(' from ' + `[${table._dbName}] [${alias}]` + tableHint + join).append(whereSql).append(orderBy + offset);
25
27
  }
26
28
 
27
- module.exports = _new;
29
+ module.exports = _new;
@@ -1,5 +1,18 @@
1
- const quote = require('../table/quote');
1
+ function selectForUpdateSql() {
2
+ return '';
3
+ }
2
4
 
3
- module.exports = function(alias) {
4
- return ' FOR UPDATE OF ' + quote(alias);
5
- };
5
+ selectForUpdateSql.tableHint = function(_context, lock) {
6
+ const hints = [];
7
+ if (lock.forUpdate)
8
+ hints.push('UPDLOCK');
9
+ if (lock.skipLocked) {
10
+ hints.push('READPAST');
11
+ hints.push('ROWLOCK');
12
+ }
13
+ if (hints.length === 0)
14
+ return '';
15
+ return ' WITH (' + hints.join(', ') + ')';
16
+ };
17
+
18
+ module.exports = selectForUpdateSql;