不要怂,就是干,撸起袖子干!

Commit 11ff7764 by Michael McCabe Committed by Sushant

feat(mssql): table hints (#8843)

1 parent 74d2b5f1
......@@ -431,3 +431,19 @@ Subtask.findAll({
order: sequelize.col('age')
})
```
## Table Hint
`tableHint` can be used to optionally pass a table hint when using mssql. The hint must be a value from `Sequelize.TableHints` and should only be used when absolutely necessary. Only a single table hint is currently supported per query.
Table hints override the default behavior of mssql query optimizer by specifing certain options. They only affect the table or view referenced in that clause.
```js
const TableHints = Sequelize.TableHints;
Project.findAll({
// adding the table hint NOLOCK
tableHint: TableHints.NOLOCK
// this will generate the SQL 'WITH (NOLOCK)'
})
```
......@@ -3,6 +3,7 @@
const _ = require('lodash');
const Utils = require('../../utils');
const DataTypes = require('../../data-types');
const TableHints = require('../../table-hints');
const AbstractQueryGenerator = require('../abstract/query-generator');
const randomBytes = require('crypto').randomBytes;
const semver = require('semver');
......@@ -815,6 +816,10 @@ const QueryGenerator = {
mainFragment += ' AS ' + mainTableAs;
}
if (options.tableHint && TableHints[options.tableHint]) {
mainFragment += ` WITH (${TableHints[options.tableHint]})`;
}
return mainFragment;
},
......
......@@ -12,6 +12,7 @@ const ModelManager = require('./model-manager');
const QueryInterface = require('./query-interface');
const Transaction = require('./transaction');
const QueryTypes = require('./query-types');
const TableHints = require('./table-hints');
const sequelizeErrors = require('./errors');
const Promise = require('./promise');
const Hooks = require('./hooks');
......@@ -1164,6 +1165,11 @@ Sequelize.prototype.Promise = Sequelize.Promise = Promise;
*/
Sequelize.prototype.QueryTypes = Sequelize.QueryTypes = QueryTypes;
/**
* Available table hints to be used for querying data in mssql for table hints
* @see {@link TableHints}
*/
Sequelize.prototype.TableHints = Sequelize.TableHints = TableHints;
/**
* Operators symbols to be used for querying data
......
'use strict';
/**
* An enum of table hints to be used in mssql for querying with table hints
*
* @property NOLOCK
* @property READUNCOMMITTED
* @property UPDLOCK
* @property REPEATABLEREAD
* @property SERIALIZABLE
* @property READCOMMITTED
* @property TABLOCK
* @property TABLOCKX
* @property PAGLOCK
* @property ROWLOCK
* @property NOWAIT
* @property READPAST
* @property XLOCK
* @property SNAPSHOT
* @property NOEXPAND
*/
const TableHints = module.exports = { // eslint-disable-line
NOLOCK: 'NOLOCK',
READUNCOMMITTED: 'READUNCOMMITTED',
UPDLOCK: 'UPDLOCK',
REPEATABLEREAD: 'REPEATABLEREAD',
SERIALIZABLE: 'SERIALIZABLE',
READCOMMITTED: 'READCOMMITTED',
TABLOCK: 'TABLOCK',
TABLOCKX: 'TABLOCKX',
PAGLOCK: 'PAGLOCK',
ROWLOCK: 'ROWLOCK',
NOWAIT: 'NOWAIT',
READPAST: 'READPAST',
XLOCK: 'XLOCK',
SNAPSHOT: 'SNAPSHOT',
NOEXPAND: 'NOEXPAND'
};
......@@ -4,6 +4,7 @@ const Support = require(__dirname + '/../../support');
const expectsql = Support.expectsql;
const current = Support.sequelize;
const Operators = require('../../../../lib/operators');
const TableHints = require('../../../../lib/table-hints');
const QueryGenerator = require('../../../../lib/dialects/mssql/query-generator');
const _ = require('lodash');
......@@ -63,6 +64,16 @@ if (current.dialect.name === 'mssql') {
mssql: 'SELECT id, name FROM myTable AS myOtherName'
});
// With tableHint - nolock
expectsql(modifiedGen.selectFromTableFragment({ tableHint: TableHints.NOLOCK }, { primaryKeyField: 'id' }, ['id', 'name'], 'myTable', 'myOtherName'), {
mssql: 'SELECT id, name FROM myTable AS myOtherName WITH (NOLOCK)'
});
// With tableHint - NOWAIT
expectsql(modifiedGen.selectFromTableFragment({ tableHint: TableHints.NOWAIT }, { primaryKeyField: 'id' }, ['id', 'name'], 'myTable', 'myOtherName'), {
mssql: 'SELECT id, name FROM myTable AS myOtherName WITH (NOWAIT)'
});
// With limit
expectsql(modifiedGen.selectFromTableFragment({ limit: 10 }, { primaryKeyField: 'id' }, ['id', 'name'], 'myTable', 'myOtherName'), {
mssql: 'SELECT id, name FROM myTable AS myOtherName'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!