Skip to content

possibility to ignore values in escape and escapeId #21

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

Closed
wants to merge 4 commits into from
Closed

possibility to ignore values in escape and escapeId #21

wants to merge 4 commits into from

Conversation

SeregPie
Copy link
Contributor

Hello.

Nearly a year ago I created an issue.
mysqljs/mysql#1542
Now I take the task in my hand.
=)

SqlString.escape({counter: SqlString.escaped('(@var := @var + 1)')});
// => '`counter` = (@var := @var + 1)'

SqlString.escapeId(['id', SqlString.escaped('@col')]);
// => '`id`, @col'

@SeregPie SeregPie changed the title introducing a possibility to ignore values in escape and escapeId possibility to ignore values in escape and escapeId Aug 31, 2017
@dougwilson dougwilson self-assigned this Sep 1, 2017
@dougwilson
Copy link
Member

Hi @SeregPie I know you just made your PR, but I just got PR #9 landed. Do you think that (1) is this still useful to have, and if so (2) should it be implemented in terms of .toSqlString ?

@SeregPie
Copy link
Contributor Author

SeregPie commented Sep 1, 2017

I think toSqlString is just over complicated and does not feel intuitive for me. So i need to create a new object for every value I want to not be escaped.

Your example.

var CURRENT_TIMESTAMP = { toSqlString: function() { return 'CURRENT_TIMESTAMP()'; } };
var sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);

Could be just like this.

var sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [SqlString.escaped('CURRENT_TIMESTAMP()'), 42]);

And you can still use objects if you want, by using the standard toString function.

var CURRENT_TIMESTAMP = { toString: function() { return 'CURRENT_TIMESTAMP()'; } };
var sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [SqlString.escaped(CURRENT_TIMESTAMP), 42]);

@dougwilson
Copy link
Member

Why open a new PR without commenting that in the other PR first? At least right now, I can't merge this due tobmerge conflicts. Can you resolve them?

@SeregPie
Copy link
Contributor Author

SeregPie commented Sep 9, 2017

resolved

Copy link
Member

@dougwilson dougwilson left a comment

Choose a reason for hiding this comment

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

Please implement it in terms of the nee toSqlString and not using instanceof checks. The instanceof checks will not work if a dependency tree happens to have two copies of this module and the user makes the escaped objects from one and the other reads them, ending up with still being escaped.

@SeregPie SeregPie closed this Sep 11, 2017
@dougwilson
Copy link
Member

Hi @SeregPie I see you closed this PR. I'm going to land this tonight to get a new version, in which the implementation would be basically what you write here, except with the instanceof checks removed and the class changed to

function Escaped(val) {
  this.toSqlString = function toSqlString() {
    return String(val);
  };
}

Not sure why the closure instead of changing it, and I can commit this under you as the author, but don't want to do that unless you are OK with it. Probably if I don't hear anything in the next 10 hours or so, I'll just commit myself, otherwise let me know if you want to be the author.

@SeregPie
Copy link
Contributor Author

Sorry. Yes, you can commit it this way. Thank you.

dougwilson pushed a commit that referenced this pull request Oct 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants