Is it possible to build SQL statements including 'XOR' condition with jQuery QueryBuilder
I want to get an SQL statement using the jQuery QueryBuilder plugin. Everything works exactly as I want when using the default conditions 'AND' and 'OR'. However, when I try to use the 'XOR' condition I get an error (see below).
This is my code for the QueryBuilder:
$(document).ready(function () {
var options = {
allow_empty: true,
filters: [
{
id: "name",
label: "Name",
type: "string",
default_value: "Noah",
size: 30,
unique: true,
},
],
/* XOR does not work with SQL */
conditions: ["AND", "OR", "XOR"],
};
$("#builder").queryBuilder(options);
$(".parse-json").on("click", function () {
var res = $("#builder").queryBuilder("getSQL", $(this).data("stmt"), false);
console.log(
res.sql +
(res.params ? "\n\n" + JSON.stringify(res.params, undefined, 2) : "")
);
});
});
Here is an example image of the QueryBuilder on my website: click here
As said before, when I choose the 'OR' condition I get the desired SQL statement.
name = ? OR name = ?
[
"Noah",
"James"
]
But when I choose the "XOR" condition I get an error message saying:
Uncaught Error: Unable to build SQL query with condition "XOR"
at Function.error (jquery-2.x-git.min.js:2)
Here you can find the file giving the error: jquery-2.x-git.min.js:2
I have searched the internet for over 2 hours but I could not find anything remotely pointing in that direction. Any help is greatly appreciated.
**One thing that is worth noting is that when extracting the rules as an object instead of an SQL statement, the "XOR" condition works. Here you can see the code used to get the statement as an object:
$(".parse-json").on("click", function () {
console.log(
JSON.stringify($("#builder").queryBuilder("getRules"), undefined, 2)
);
});
Solution 1:
No, it is not possible, the conditions
property only allows 'AND'
or 'OR'
as values, as stated in the documentation:
Name | type | default | description |
---|---|---|---|
[…] | |||
conditions | string[] | ['AND', 'OR'] | Array of available group conditions. Use the lang option to change the label. |
[…] |
Looks like there was an issue reported in 2018, but it was closed without any evident resolution: https://github.com/mistic100/jQuery-QueryBuilder/issues/645
I researched a bit more and it is not as clear-cut. Apparently, there are even tests in the repository which use 'XOR' and 'NAND' as conditions: https://github.com/mistic100/jQuery-QueryBuilder/blob/dev/tests/core.module.js. This is the opposite of what the documentation states.