|
3 | 3 | var BaseDialect = require('../base');
|
4 | 4 | var _ = require('underscore');
|
5 | 5 | var util = require('util');
|
| 6 | +var templateChecks = require('../../utils/templateChecks'); |
6 | 7 |
|
7 | 8 | var Dialect = module.exports = function(builder) {
|
8 | 9 | BaseDialect.call(this, builder);
|
| 10 | + |
| 11 | + this.blocks.set('limit', function(params) { |
| 12 | + return (params.offset) ? '' : 'TOP(' + builder._pushValue(params.limit) + ')'; |
| 13 | + }); |
| 14 | + |
| 15 | + this.blocks.set('offset', function(params) { |
| 16 | + var pre = (!params.sort) ? 'ORDER BY 1 ' : ''; |
| 17 | + if (params.limit) { |
| 18 | + return pre + 'OFFSET ' + params.offset + ' ROWS FETCH NEXT ' + params.limit + ' ROWS ONLY'; |
| 19 | + }else { |
| 20 | + return pre + 'OFFSET ' + params.offset + ' ROWS'; |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + this.templates.set('select', { |
| 25 | + pattern: '{with} {withRecursive} select {limit} {distinct} {fields} ' + |
| 26 | + 'from {from} {table} {query} {select} {expression} {alias} ' + |
| 27 | + '{join} {condition} {group} {having} {sort} {offset}', |
| 28 | + defaults: { |
| 29 | + fields: {} |
| 30 | + }, |
| 31 | + validate: function(type, params) { |
| 32 | + templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']); |
| 33 | + templateChecks.propType(type, params, 'with', 'object'); |
| 34 | + templateChecks.propType(type, params, 'withRecursive', 'object'); |
| 35 | + |
| 36 | + templateChecks.propType(type, params, 'distinct', 'boolean'); |
| 37 | + |
| 38 | + templateChecks.propType(type, params, 'fields', ['array', 'object']); |
| 39 | + |
| 40 | + templateChecks.propType(type, params, 'from', ['string', 'array', 'object']); |
| 41 | + |
| 42 | + templateChecks.atLeastOneOfProps(type, params, ['table', 'query', 'select', 'expression']); |
| 43 | + templateChecks.onlyOneOfProps(type, params, ['table', 'query', 'select', 'expression']); |
| 44 | + |
| 45 | + templateChecks.propType(type, params, 'table', 'string'); |
| 46 | + templateChecks.propType(type, params, 'query', 'object'); |
| 47 | + templateChecks.propType(type, params, 'select', 'object'); |
| 48 | + templateChecks.propType(type, params, 'expression', ['string', 'object']); |
| 49 | + |
| 50 | + templateChecks.propType(type, params, 'alias', ['string', 'object']); |
| 51 | + |
| 52 | + templateChecks.propType(type, params, 'join', ['array', 'object']); |
| 53 | + |
| 54 | + templateChecks.propType(type, params, 'condition', ['array', 'object']); |
| 55 | + templateChecks.propType(type, params, 'having', ['array', 'object']); |
| 56 | + |
| 57 | + templateChecks.propType(type, params, 'group', ['string', 'array']); |
| 58 | + |
| 59 | + templateChecks.propType(type, params, 'sort', ['string', 'array', 'object']); |
| 60 | + |
| 61 | + templateChecks.propType(type, params, 'offset', ['number', 'string']); |
| 62 | + templateChecks.propType(type, params, 'limit', ['number', 'string']); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
9 | 66 | };
|
10 | 67 |
|
11 | 68 | util.inherits(Dialect, BaseDialect);
|
|
0 commit comments