count.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { entityKind } from "../../entity.js";
  2. import { SQL, sql } from "../../sql/sql.js";
  3. class GelCountBuilder extends SQL {
  4. constructor(params) {
  5. super(GelCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
  6. this.params = params;
  7. this.mapWith(Number);
  8. this.session = params.session;
  9. this.sql = GelCountBuilder.buildCount(
  10. params.source,
  11. params.filters
  12. );
  13. }
  14. sql;
  15. static [entityKind] = "GelCountBuilder";
  16. [Symbol.toStringTag] = "GelCountBuilder";
  17. session;
  18. static buildEmbeddedCount(source, filters) {
  19. return sql`(select count(*) from ${source}${sql.raw(" where ").if(filters)}${filters})`;
  20. }
  21. static buildCount(source, filters) {
  22. return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters};`;
  23. }
  24. then(onfulfilled, onrejected) {
  25. return Promise.resolve(this.session.count(this.sql)).then(
  26. onfulfilled,
  27. onrejected
  28. );
  29. }
  30. catch(onRejected) {
  31. return this.then(void 0, onRejected);
  32. }
  33. finally(onFinally) {
  34. return this.then(
  35. (value) => {
  36. onFinally?.();
  37. return value;
  38. },
  39. (reason) => {
  40. onFinally?.();
  41. throw reason;
  42. }
  43. );
  44. }
  45. }
  46. export {
  47. GelCountBuilder
  48. };
  49. //# sourceMappingURL=count.js.map