Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MSSQL support #26

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

add MSSQL support #26

wants to merge 9 commits into from

Conversation

iteufel
Copy link

@iteufel iteufel commented May 10, 2017

Added support for MSSQL.

Following should now work:

  • limit
  • offest
  • returning

@iteufel iteufel changed the title Added MSSQL support add MSSQL support May 10, 2017
@okv
Copy link
Contributor

okv commented May 11, 2017

Hi, @iteufel.
Looks like it may extend our limited mssql support, thanks for that.
I see some code style issues - tab indent used across the lib, gulp lint also outputs some errors.
We also usually update dependencies and bump version in separated commits/pull requests.
@artzhookov could you look at that?

Replaced spaces with tabs
Reverted package.json
@iteufel
Copy link
Author

iteufel commented May 11, 2017

Hi @okv,

  • gulp lint and gulp lintTests are now error free.
  • Reverted changes in package.json.
  • Replaced spaces with tabs

@okv
Copy link
Contributor

okv commented May 11, 2017

@iteufel, awesome.
Waiting for @artzhookov to check dialect changes...


var Dialect = module.exports = function(builder) {

builder._pushValue = function(value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't override builder._pushValue, you should override value block and do your specific stuff before parent value block call. See example in postgresql dialect: https://github.com/2do2go/json-sql/blob/master/lib/dialects/postgresql/blocks.js#L6


module.exports = function(dialect) {
dialect.blocks.set('limit', function(params) {
return (!isNaN(params.offset)) ? '' : 'top(' + dialect.builder._pushValue(params.limit) + ')';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe _.isUndefined check is better here?

return _.isUndefined(params.offset) ? 'top(' + dialect.builder._pushValue(params.limit) + ')' : '';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why you check offset value, but return limit value?

Copy link

@AsuraDiti AsuraDiti Jul 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSSQL has no LIMIT/OFFSET Statement like mysql.
There are 2 diffrent ways:
SELECT TOP X Fields... is the same as LIMIT.
But you cannot use OFFSET together with TOP. IN MSSQL 2012 and higher a new statement was added.
SELECT fields from ... ORDER BY A OFFSET Y ROWS FETCH NEXT X ROWS ONLY

That is why he checks for the params.offset. If it is used the top() statement will be removed and the offset/fetch statement will include the limit.

var str = pre + 'offset ' + dialect.builder._pushValue(params.offset);
str += ' rows fetch next ' + dialect.builder._pushValue(params.limit) + ' rows only';
return str;
}else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace is missed here


return dialect.buildTemplate('insertValues', {
fields: fields,
returning: params.returning || undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why returning is needed here?

});
});

dialect.blocks.add('insertValues:values', function(params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is seems that this block is not needed to override

str += ' rows fetch next ' + dialect.builder._pushValue(params.limit) + ' rows only';
return str;
}else {
return pre + 'OFFSET ' + dialect.builder._pushValue(params.offset) + ' rows';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all words should be in lower case, you use offset word above but here OFFSET

ojourmel added a commit to ojourmel/json-sql that referenced this pull request Aug 26, 2017
This commit refactors the `mssql` dialect as per the feedback
from @artzhookov on 2do2go#26.

 - Include all test files in gulp lint tasks
 - Move mssql `value` block override to blocks.js
 - Update mssql `limit` block to use `_.isUndefined`
 - Clean up minor mssql sql letter case and format
 - Set the `mssql` `identifier` `prefix` and `suffix`

This commit also introduces two changes to the `base` dialect.
 - Refactor the `base `insert:values` `block` function to clean up
   the logic
 - Refactor the `mssql` `inserted:values` `block` function to match
   the `base` function, and also include `returning` params, as it
   is needed. (mssql puts `output` keyword before `values` keyword)
 - Update `buildTemplate` to include an edge case where a `buildBlock`
   function does not return any value. This prevents unnecessary
   whitespace in the final query string.

These changes are not strictly necessary. The `mssql` `insert:values` `block`
could use the same structure as the original `base` `block`, and unnecessary
whitespace has no issues in terms of functionality, simply ascetics.

This commit refactors the `mssql` tests as per the changes made.
Test coverage and layout is not ideal, but functional.
ojourmel added a commit to ojourmel/json-sql that referenced this pull request Aug 26, 2017
This commit refactors the `mssql` dialect as per the feedback
from @artzhookov on 2do2go#26.

 - Include all test files in gulp lint tasks
 - Move mssql `value` block override to blocks.js
 - Update mssql `limit` block to use `_.isUndefined`
 - Clean up minor mssql sql letter case and format
 - Set the `mssql` `identifier` `prefix` and `suffix`

This commit also introduces two changes to the `base` dialect.
 - Refactor the `base `insert:values` `block` function to clean up
   the logic
 - Refactor the `mssql` `inserted:values` `block` function to match
   the `base` function, and also include `returning` params, as it
   is needed. (mssql puts `output` keyword before `values` keyword)
 - Update `buildTemplate` to include an edge case where a `buildBlock`
   function does not return any value. This prevents unnecessary
   whitespace in the final query string.

These changes are not strictly necessary. The `mssql` `insert:values` `block`
could use the same structure as the original `base` `block`, and unnecessary
whitespace has no issues in terms of functionality, simply ascetics.

This commit refactors the `mssql` tests as per the changes made.
Test coverage and layout is not ideal, but functional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants