简体   繁体   English

是否可以使用 jQuery QueryBuilder 构建包含“XOR”条件的 SQL 语句

[英]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.我想使用 jQuery QueryBuilder 插件获得 SQL 语句。 Everything works exactly as I want when using the default conditions 'AND' and 'OR'.使用默认条件“AND”和“OR”时,一切都完全按照我的意愿工作。 However, when I try to use the 'XOR' condition I get an error (see below).但是,当我尝试使用“XOR”条件时,会出现错误(见下文)。

This is my code for the QueryBuilder:这是我的 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这是我网站上 QueryBuilder 的示例图像:单击此处

As said before, when I choose the 'OR' condition I get the desired SQL statement.如前所述,当我选择“或”条件时,我得到了所需的 SQL 语句。

name = ? OR name = ?

[
  "Noah",
  "James"
]

But when I choose the "XOR" condition I get an error message saying:但是当我选择“XOR”条件时,我收到一条错误消息:

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在这里你可以找到给出错误的文件: 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.我已经在互联网上搜索了 2 个多小时,但我找不到任何远程指向那个方向的东西。 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. **值得注意的一点是,当将规则提取为 object 而不是 SQL 语句时,“XOR”条件有效。 Here you can see the code used to get the statement as an object:在这里,您可以看到用于获取 object 语句的代码:

$(".parse-json").on("click", function () {
  console.log(
    JSON.stringify($("#builder").queryBuilder("getRules"), undefined, 2)
  );
});

No, it is not possible, the conditions property only allows 'AND' or 'OR' as values, as stated in the documentation :不,这是不可能的, conditions属性只允许'AND''OR'作为值,如文档中所述:

Name姓名 type类型 default默认 description描述
[…] […]
conditions条件 string[]细绳[] ['AND', 'OR'] ['与','或'] Array of available group conditions.可用组条件的数组。 Use the lang option to change the label.使用lang选项更改 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看起来在 2018 年报告了一个问题,但没有任何明显的解决方案就关闭了: 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 .显然,存储库中甚至有使用“XOR”和“NAND”作为条件的测试: https://github.com/mistic100/jQuery-QueryBuilder/blob/dev/tests/core.module.js This is the opposite of what the documentation states.这与文档所述相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM