All files / src inline-table-of-column.ts

86.66% Statements 13/15
0% Branches 0/1
100% Functions 2/2
84.61% Lines 11/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2922x 22x 22x     22x               22x 4x     4x 4x 11x 4x             22x  
import id from './identifier.js'
import list from './list.js'
import row from './row.js'
import type S from './sanitised.js'
import type Sid from './sanitised-identifier.js'
import tsql from './template.js'
 
/**
 * @returns single column literal table from provided array of values.
 *
 * @throws {Error} if provided values array is empty.
 */
export const inlineTableOfColumn =
  (table: Sid | string, column: Sid | string, values: unknown[]): S => {
    Iif (values.length === 0) {
      throw new Error(`Can't generate literal table ${table} using column ${column} with an empty array of values.`)
    }
    const table_ = id(table)
    const column_ = id(column)
    const values_ = list(values.map(_ => row([ _ ])), undefined, ',\n')
    return tsql`
      (values
        ${values_}
      ) as ${table_} (${column_})
    `
  }
 
export default inlineTableOfColumn