Skip to content

Prefix string literals with N in mssql - #5039

Merged
mickhansen merged 3 commits into
sequelize:masterfrom
zapatek:zapatek
Dec 25, 2015
Merged

mickhansen merged 3 commits into
sequelize:masterfrom
zapatek:zapatek

Conversation

@zapatek

@zapatek zapatek commented Dec 14, 2015

Copy link
Copy Markdown
Contributor

Fixes #3752

@mickhansen

Copy link
Copy Markdown
Contributor

This probably fits better in data types, @janmeier?

@zapatek

zapatek commented Dec 15, 2015

Copy link
Copy Markdown
Contributor Author

@mickhansen, the enclosing quotes for string literals are put-in in the SqlString.escape function. Since N'...' can be thought as a special variant of enclosing quotes, I thought it would be the right function to modify.

Given my unfamiliarity with internals of sequelize, If you could provide a more specific suggestion as to where to look at, I am willing to put in the effort and get this fixed at the soonest. On the other hand, if this is good enough then I can also fix those test cases which fail after this fix (they fail because they do not anticipate the N prefix in the built mssql queries at the moment).

On a separate note, this has been a showstopper for us. Not getting it fixed basically means sequelize is not usable (with mssql) in majority of Asia.

@zapatek
zapatek force-pushed the zapatek branch 2 times, most recently from dafca02 to 3cec936 Compare December 15, 2015 03:59
@mickhansen

Copy link
Copy Markdown
Contributor

@janmeier will chime have to chime in here, but since it's mssql specific i think it's better suited being implemented on mssql specific data types: https://github.com/sequelize/sequelize/blob/master/lib/dialects/mssql/data-types.js

@zapatek

zapatek commented Dec 15, 2015

Copy link
Copy Markdown
Contributor Author

Thanks for the pointer, I'll look at it. Meanwhile, on the defence of the current approach, the modified SqlString.escape function has already some dialect specific code such as:

  if (dialect === 'postgres' || dialect === 'sqlite' || dialect === 'mssql') {
    // http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
    // http://stackoverflow.com/q/603572/130598
    val = val.replace(/'/g, "''");
  } else {
    val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
      switch (s) {

By the way, I have modified the test cases accordingly (anticipating the N prefix for mssql) and they all are passing now.

@mickhansen

Copy link
Copy Markdown
Contributor

@zapatek It indeed does, and we'd like to fix that going forward :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The ? should not be present in the final result, as evident from the existing test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, that question mark added in the fix is just a regular expression quantifier, meaning one or zero N characters are expected (to cover cases for mssql - one - and other dialects - zero - respectively)

@mickhansen mickhansen Dec 15, 2015 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh right, my bad, for a second i missed that you changed it to a regex.

@mickhansen

Copy link
Copy Markdown
Contributor

Okay looking at the tests you changed i'm not sure that a change to data types will be sufficient actually, but we'll have to wait for @janmeier to take a look

@janmeier

Copy link
Copy Markdown
Member

Nah, I think this is the best way to fix it for now - either that or we have to move some more of the logic for proper escaping etc. into data types as well - So I think this is fine for now

@janmeier

Copy link
Copy Markdown
Member

Could you add a changelog entry, then this should be good to go?

@zapatek

zapatek commented Dec 16, 2015

Copy link
Copy Markdown
Contributor Author

Done.

@zapatek

zapatek commented Dec 16, 2015

Copy link
Copy Markdown
Contributor Author

Seems like just missed the 3.15.0 release. Rebased the work branch for this fix to the tip of master to resolve the resultant conflict on changelog.md

@mickhansen

Copy link
Copy Markdown
Contributor

I'll cut another one once i merge a few pending PRs :)

@mickhansen

Copy link
Copy Markdown
Contributor

@zapatek Ah damn, looks like we need another rebase. Please do ping me when it's done and tests have run so i can get it merged in time.

@zapatek
zapatek force-pushed the zapatek branch 2 times, most recently from d11c88b to f9980fc Compare December 24, 2015 02:41
@zapatek

zapatek commented Dec 24, 2015

Copy link
Copy Markdown
Contributor Author

Amended the conflicting changelog.md but seems it wasn't in time for 3.15.1, so I put the entry under the 'Future' header. If you could merge now, it would surely make it into next release.. :)

@mickhansen

Copy link
Copy Markdown
Contributor

And needs a new rebase, how the hell does this keep happening to this PR :p

@zapatek
zapatek force-pushed the zapatek branch 2 times, most recently from e8ffbb3 to 14105e7 Compare December 24, 2015 18:36
@zapatek

zapatek commented Dec 24, 2015

Copy link
Copy Markdown
Contributor Author

Haha, this time the culprit was sql-string.js changed by the recent commit 4f4abdc. I have just resolved the merge conflict. Could you try to merge the pr once again?

mickhansen added a commit that referenced this pull request Dec 25, 2015
Prefix string literals with N in mssql
@mickhansen
mickhansen merged commit 2f7c717 into sequelize:master Dec 25, 2015
@brentjanderson

Copy link
Copy Markdown

This explains why my latin-encoded columns no longer work. I have a view that has the following structure:

Column_Name Type Length Nullable Collation
Column_A char 7 no SQL_Latin1_General_CP1_CI_AS
Column_B char 31 no SQL_Latin1_General_CP1_CI_AS

When I use a pretty standard Sequelize raw query, it generates the following with this new code:

SELECT [Column_A], [Column_B] 
FROM [xyzView] AS [xyzView] 
WHERE [xyzView].[Column_A] LIKE N'Value' OR [xyzView].[Column_B] LIKE N'Value'

Unfortunately, that code does not return any results. Dropping the Unicde prefix N from the strings in the WHERE clause does return results. I reviewed your changes, and it seems that all strings now are forced to unicode format via Sequelize. Did I miss an override? Should I get cracking on a pull request for such an option? Otherwise I may just pin my project at the older version v3.15.1.

@mickhansen

Copy link
Copy Markdown
Contributor

@brentjanderson I don't believe any override is availabe, please do provide a PR

@Shaffuan

Shaffuan commented Nov 5, 2018

Copy link
Copy Markdown

Why don't update this changes in sequelize latest version???

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.

5 participants