query.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const { AdvancedBase } = require("@heyputer/puter-js-common");
  20. const { WeakConstructorTrait } = require("../../traits/WeakConstructorTrait");
  21. class Predicate extends AdvancedBase {
  22. static TRAITS = [
  23. new WeakConstructorTrait(),
  24. ]
  25. }
  26. class Null extends Predicate {
  27. //
  28. }
  29. class And extends Predicate {
  30. //
  31. }
  32. class Or extends Predicate {
  33. async check (entity) {
  34. for ( const child of this.children ) {
  35. if ( await entity.check(child) ) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. }
  42. class Eq extends Predicate {
  43. async check (entity) {
  44. return (await entity.get(this.key)) == this.value;
  45. }
  46. }
  47. class IsNotNull extends Predicate {
  48. async check (entity) {
  49. return (await entity.get(this.key)) !== null;
  50. }
  51. }
  52. class Like extends Predicate {
  53. async check (entity) {
  54. // Convert SQL LIKE pattern to RegExp
  55. // TODO: Support escaping the pattern characters
  56. const regex = new RegExp(this.value.replaceAll('%', '.*').replaceAll('_', '.'), 'i');
  57. return regex.test(await entity.get(this.key));
  58. }
  59. }
  60. Predicate.prototype.and = function (other) {
  61. return new And({ children: [this, other] });
  62. }
  63. class PredicateUtil {
  64. static simplify (predicate) {
  65. if ( predicate instanceof And ) {
  66. const simplified = [];
  67. for ( const p of predicate.children ) {
  68. const s = PredicateUtil.simplify(p);
  69. if ( s instanceof And ) {
  70. simplified.push(...s.children);
  71. } else if ( ! (s instanceof Null) ) {
  72. simplified.push(s);
  73. }
  74. }
  75. if ( simplified.length === 0 ) {
  76. return new Null();
  77. }
  78. if ( simplified.length === 1 ) {
  79. return simplified[0];
  80. }
  81. return new And({ children: simplified });
  82. }
  83. if ( predicate instanceof Or ) {
  84. const simplified = [];
  85. for ( const p of predicate.children ) {
  86. const s = PredicateUtil.simplify(p);
  87. if ( s instanceof Or ) {
  88. simplified.push(...s.children);
  89. } else if ( ! (s instanceof Null) ) {
  90. simplified.push(s);
  91. }
  92. }
  93. if ( simplified.length === 0 ) {
  94. return new Null();
  95. }
  96. if ( simplified.length === 1 ) {
  97. return simplified[0];
  98. }
  99. return new Or({ children: simplified });
  100. }
  101. return predicate;
  102. }
  103. }
  104. module.exports = {
  105. Predicate,
  106. PredicateUtil,
  107. Null,
  108. And,
  109. Or,
  110. Eq,
  111. IsNotNull,
  112. Like,
  113. };