| 15 | } |
| 16 | |
| 17 | export class QueryModifier implements IQueryModifier { |
| 18 | protected pagination?: QueryPagination |
| 19 | |
| 20 | constructor( |
| 21 | protected table: QueryTable, |
| 22 | protected actionConfig: ActionConfig, |
| 23 | protected options?: { |
| 24 | actionOptions?: { returning?: boolean; cascade?: boolean; enumArrayColumns?: Array<string> } |
| 25 | filters?: Array<Filter> |
| 26 | sorts?: Array<Sort> |
| 27 | } |
| 28 | ) {} |
| 29 | |
| 30 | /** |
| 31 | * Limits the result to rows within the specified range, inclusive. |
| 32 | * |
| 33 | * @param from The starting index from which to limit the result, inclusive. |
| 34 | * @param to The last index to which to limit the result, inclusive. |
| 35 | */ |
| 36 | range(from: number, to: number) { |
| 37 | this.pagination = { offset: from, limit: to - from + 1 } |
| 38 | return this |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Return SQL string for query chains |
| 43 | */ |
| 44 | toSql( |
| 45 | options: { isCTE: boolean; isFinal: boolean } = { isCTE: false, isFinal: true } |
| 46 | ): SafeSqlFragment { |
| 47 | try { |
| 48 | const { actionOptions, filters, sorts } = this.options ?? {} |
| 49 | switch (this.actionConfig.action) { |
| 50 | case 'count': { |
| 51 | return countQuery(this.table, { filters }) |
| 52 | } |
| 53 | case 'delete': { |
| 54 | return deleteQuery(this.table, filters, { |
| 55 | returning: actionOptions?.returning, |
| 56 | enumArrayColumns: actionOptions?.enumArrayColumns, |
| 57 | }) |
| 58 | } |
| 59 | case 'insert': { |
| 60 | return insertQuery(this.table, this.actionConfig.actionValue, { |
| 61 | returning: actionOptions?.returning, |
| 62 | enumArrayColumns: actionOptions?.enumArrayColumns, |
| 63 | }) |
| 64 | } |
| 65 | case 'select': { |
| 66 | return selectQuery( |
| 67 | this.table, |
| 68 | this.actionConfig.actionValue, |
| 69 | { |
| 70 | filters, |
| 71 | pagination: this.pagination, |
| 72 | sorts, |
| 73 | }, |
| 74 | options.isFinal, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…