count.js 1.4 KB

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