conditions.cjs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var conditions_exports = {};
  20. __export(conditions_exports, {
  21. and: () => and,
  22. arrayContained: () => arrayContained,
  23. arrayContains: () => arrayContains,
  24. arrayOverlaps: () => arrayOverlaps,
  25. between: () => between,
  26. bindIfParam: () => bindIfParam,
  27. eq: () => eq,
  28. exists: () => exists,
  29. gt: () => gt,
  30. gte: () => gte,
  31. ilike: () => ilike,
  32. inArray: () => inArray,
  33. isNotNull: () => isNotNull,
  34. isNull: () => isNull,
  35. like: () => like,
  36. lt: () => lt,
  37. lte: () => lte,
  38. ne: () => ne,
  39. not: () => not,
  40. notBetween: () => notBetween,
  41. notExists: () => notExists,
  42. notIlike: () => notIlike,
  43. notInArray: () => notInArray,
  44. notLike: () => notLike,
  45. or: () => or
  46. });
  47. module.exports = __toCommonJS(conditions_exports);
  48. var import_column = require("../../column.cjs");
  49. var import_entity = require("../../entity.cjs");
  50. var import_table = require("../../table.cjs");
  51. var import_sql = require("../sql.cjs");
  52. function bindIfParam(value, column) {
  53. if ((0, import_sql.isDriverValueEncoder)(column) && !(0, import_sql.isSQLWrapper)(value) && !(0, import_entity.is)(value, import_sql.Param) && !(0, import_entity.is)(value, import_sql.Placeholder) && !(0, import_entity.is)(value, import_column.Column) && !(0, import_entity.is)(value, import_table.Table) && !(0, import_entity.is)(value, import_sql.View)) {
  54. return new import_sql.Param(value, column);
  55. }
  56. return value;
  57. }
  58. const eq = (left, right) => {
  59. return import_sql.sql`${left} = ${bindIfParam(right, left)}`;
  60. };
  61. const ne = (left, right) => {
  62. return import_sql.sql`${left} <> ${bindIfParam(right, left)}`;
  63. };
  64. function and(...unfilteredConditions) {
  65. const conditions = unfilteredConditions.filter(
  66. (c) => c !== void 0
  67. );
  68. if (conditions.length === 0) {
  69. return void 0;
  70. }
  71. if (conditions.length === 1) {
  72. return new import_sql.SQL(conditions);
  73. }
  74. return new import_sql.SQL([
  75. new import_sql.StringChunk("("),
  76. import_sql.sql.join(conditions, new import_sql.StringChunk(" and ")),
  77. new import_sql.StringChunk(")")
  78. ]);
  79. }
  80. function or(...unfilteredConditions) {
  81. const conditions = unfilteredConditions.filter(
  82. (c) => c !== void 0
  83. );
  84. if (conditions.length === 0) {
  85. return void 0;
  86. }
  87. if (conditions.length === 1) {
  88. return new import_sql.SQL(conditions);
  89. }
  90. return new import_sql.SQL([
  91. new import_sql.StringChunk("("),
  92. import_sql.sql.join(conditions, new import_sql.StringChunk(" or ")),
  93. new import_sql.StringChunk(")")
  94. ]);
  95. }
  96. function not(condition) {
  97. return import_sql.sql`not ${condition}`;
  98. }
  99. const gt = (left, right) => {
  100. return import_sql.sql`${left} > ${bindIfParam(right, left)}`;
  101. };
  102. const gte = (left, right) => {
  103. return import_sql.sql`${left} >= ${bindIfParam(right, left)}`;
  104. };
  105. const lt = (left, right) => {
  106. return import_sql.sql`${left} < ${bindIfParam(right, left)}`;
  107. };
  108. const lte = (left, right) => {
  109. return import_sql.sql`${left} <= ${bindIfParam(right, left)}`;
  110. };
  111. function inArray(column, values) {
  112. if (Array.isArray(values)) {
  113. if (values.length === 0) {
  114. return import_sql.sql`false`;
  115. }
  116. return import_sql.sql`${column} in ${values.map((v) => bindIfParam(v, column))}`;
  117. }
  118. return import_sql.sql`${column} in ${bindIfParam(values, column)}`;
  119. }
  120. function notInArray(column, values) {
  121. if (Array.isArray(values)) {
  122. if (values.length === 0) {
  123. return import_sql.sql`true`;
  124. }
  125. return import_sql.sql`${column} not in ${values.map((v) => bindIfParam(v, column))}`;
  126. }
  127. return import_sql.sql`${column} not in ${bindIfParam(values, column)}`;
  128. }
  129. function isNull(value) {
  130. return import_sql.sql`${value} is null`;
  131. }
  132. function isNotNull(value) {
  133. return import_sql.sql`${value} is not null`;
  134. }
  135. function exists(subquery) {
  136. return import_sql.sql`exists ${subquery}`;
  137. }
  138. function notExists(subquery) {
  139. return import_sql.sql`not exists ${subquery}`;
  140. }
  141. function between(column, min, max) {
  142. return import_sql.sql`${column} between ${bindIfParam(min, column)} and ${bindIfParam(
  143. max,
  144. column
  145. )}`;
  146. }
  147. function notBetween(column, min, max) {
  148. return import_sql.sql`${column} not between ${bindIfParam(
  149. min,
  150. column
  151. )} and ${bindIfParam(max, column)}`;
  152. }
  153. function like(column, value) {
  154. return import_sql.sql`${column} like ${value}`;
  155. }
  156. function notLike(column, value) {
  157. return import_sql.sql`${column} not like ${value}`;
  158. }
  159. function ilike(column, value) {
  160. return import_sql.sql`${column} ilike ${value}`;
  161. }
  162. function notIlike(column, value) {
  163. return import_sql.sql`${column} not ilike ${value}`;
  164. }
  165. function arrayContains(column, values) {
  166. if (Array.isArray(values)) {
  167. if (values.length === 0) {
  168. throw new Error("arrayContains requires at least one value");
  169. }
  170. const array = import_sql.sql`${bindIfParam(values, column)}`;
  171. return import_sql.sql`${column} @> ${array}`;
  172. }
  173. return import_sql.sql`${column} @> ${bindIfParam(values, column)}`;
  174. }
  175. function arrayContained(column, values) {
  176. if (Array.isArray(values)) {
  177. if (values.length === 0) {
  178. throw new Error("arrayContained requires at least one value");
  179. }
  180. const array = import_sql.sql`${bindIfParam(values, column)}`;
  181. return import_sql.sql`${column} <@ ${array}`;
  182. }
  183. return import_sql.sql`${column} <@ ${bindIfParam(values, column)}`;
  184. }
  185. function arrayOverlaps(column, values) {
  186. if (Array.isArray(values)) {
  187. if (values.length === 0) {
  188. throw new Error("arrayOverlaps requires at least one value");
  189. }
  190. const array = import_sql.sql`${bindIfParam(values, column)}`;
  191. return import_sql.sql`${column} && ${array}`;
  192. }
  193. return import_sql.sql`${column} && ${bindIfParam(values, column)}`;
  194. }
  195. // Annotate the CommonJS export names for ESM import in node:
  196. 0 && (module.exports = {
  197. and,
  198. arrayContained,
  199. arrayContains,
  200. arrayOverlaps,
  201. between,
  202. bindIfParam,
  203. eq,
  204. exists,
  205. gt,
  206. gte,
  207. ilike,
  208. inArray,
  209. isNotNull,
  210. isNull,
  211. like,
  212. lt,
  213. lte,
  214. ne,
  215. not,
  216. notBetween,
  217. notExists,
  218. notIlike,
  219. notInArray,
  220. notLike,
  221. or
  222. });
  223. //# sourceMappingURL=conditions.cjs.map