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

SQL to Caml parser is not happy about escaped quotes #89

Closed
petero-dk opened this issue Jan 10, 2018 · 15 comments
Closed

SQL to Caml parser is not happy about escaped quotes #89

petero-dk opened this issue Jan 10, 2018 · 15 comments

Comments

@petero-dk
Copy link

petero-dk commented Jan 10, 2018

Parent = "Support and deliver roll out of \"Sale Management\"" AND ID <> 400

When parsed becomes

<Eq><FieldRef Name="Parent" /><Value Type="Text">Support and deliver roll out of "Sale ManagementNull AND ID&lt;&gt;400</Value></Eq>

It seems to affect the second escaped "
If I switch around and use single ' they do have the same problem if the ' is the last item in the quotations.

Parent = 'transletsen af matrialeBy O\'Conner and O\''
<Eq><FieldRef Name="Parent" /><Value Type="Text">transletsen af matrialeBy O'Conner and O**Null**</Value></Eq>

Actually the problem is having the same escaped character as the quotation character in a where clause

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 10, 2018

Can you show me the full query that you try to parse ?

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 10, 2018

I'll assume it's :
"Parent = 'Support and deliver roll out of \"Sale Management\"' AND ID <> 400"

In that case you can proceed this way:

$SP().list("The List").get({
  whereCAML:true,
  where:$SP().parse("Parent = 'Support and deliver roll out of &quot;Sale Management&quot;' AND ID <> 400", false)
})

So you replace the quotes by &quot;, and you use $SP().parse() with false for escapeChar parameter.

@petero-dk
Copy link
Author

petero-dk commented Jan 10, 2018

I cannot do that. These are dynamic generated queries so I do not know which other chars that needs to be escaped. The weird thing is that it is only an issue when the escaped ' or " are followed by a non-escaped ' or " respectively.

When it is the last item in this loop:

var apos = letter;
var word = "", other="";
while ((letter = queryString.charAt(++i)) != apos && i < limitMax) {
if (letter == "\\") letter = queryString.charAt(++i);
word+=letter;
}

If you notice in my samples it inserts a null instead of the escaped " or '

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 10, 2018

What is your code exactly?

Because if I do this:

var parent = 'Support and deliver roll out of "Sale Management"';
var id = 400;
var where = $SP().parse("Parent = '"+parent+"' AND ID <> "+id);
console.log(where); // -> <And><Eq><FieldRef Name="Parent" /><Value Type="Text">Support and deliver roll out of "Sale Management"</Value></Eq><Neq><FieldRef Name="ID" /><Value Type="Number">400</Value></Neq></And>
$SP().list("The List").get({
  where:where
})
.then(function(data) {
  console.log(data.length); // -> return X
})

Then it does work...

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 11, 2018

So, is it OK now?

@petero-dk
Copy link
Author

Hi @Aymkdn

As I have tried to explain above, this is a narrow case, but not okay. The workaround you describe is also the one I have temporarily implemented already, however what I tried to describe is a bug in the code.

var parent = "Support and deliver roll out of \\'Sale Management\\'";
var id = 400;
var where = $SP().parse("Parent = '"+parent+"' AND ID <> "+id);
console.log(where); // -> <And><Eq><FieldRef Name="Parent" /><Value Type="Text">Support and deliver roll out of "Sale Management"</Value></Eq><Neq><FieldRef Name="ID" /><Value Type="Number">400</Value></Neq></And>
$SP().list("The List").get({
  where:where
})
.then(function(data) {
  console.log(data.length); // -> return X
})

and

var parent = 'Support and deliver roll out of \\"Sale Management\\"';
var id = 400;
var where = $SP().parse('Parent = "'+parent+'" AND ID <> '+id);
console.log(where); // -> <And><Eq><FieldRef Name="Parent" /><Value Type="Text">Support and deliver roll out of "Sale Management"</Value></Eq><Neq><FieldRef Name="ID" /><Value Type="Number">400</Value></Neq></And>
$SP().list("The List").get({
  where:where
})
.then(function(data) {
  console.log(data.length); // -> return X
})

will always fail, because of the escaped " or ' inside the " or ' in the query. Because the var parent is dynamic I can always end up in one of the edge cases, no matter which I choose.

Does that make sense? The output in my original post (after I have cleaned up a little) directly from my code. The Null in the output text is coming from the parse function

@petero-dk
Copy link
Author

petero-dk commented Jan 11, 2018

The output from the parse function will for both the var parent above be:

...<Value Type="Text">Support and deliver roll out of "Sale ManagementNull AND ID&lt;&gt;400</Value>...

Notice the Null and that the next part is joined into the Value tag

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 12, 2018

Actually the issue is due to \''. Example:

"Title = 'James O\'Connor and O\'' AND ID <> 0
_________________________________ ^

The '' will be converted in Null. I'll look at this to provide a fix for next release (v5.1)

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 12, 2018

If you want to test, replace ligne 694 by :

var queryString = q.replace(/(\s+)?(=|~=|<=|>=|<>|<|>| LIKE | IN )(\s+)?/g,"$2").replace(/([^\\?])""|([^\\?])''/g,"$1Null").replace(/==/g,"="); // remove unnecessary white space & replace '

And let me know please !

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 15, 2018

Did you get a chance to test it @petero-dk ?

@petero-dk
Copy link
Author

petero-dk commented Jan 15, 2018 via email

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 20, 2018

OK. Please let me know when you get back :-)

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 23, 2018

Sorry to hurry you, but it's been a while now and I'd like to close this issue.

If you don't reply soon, then I'll assume it works OK with this fix and it will be released for v5.1.

Thanks.

@petero-dk
Copy link
Author

Ill check today

@Aymkdn
Copy link
Owner

Aymkdn commented Jan 26, 2018

No news, so I'm closing it. Feel free to comment back if you want to.

Thanks for reporting back this issue!

@Aymkdn Aymkdn closed this as completed Jan 26, 2018
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

No branches or pull requests

2 participants