import { Alias } from './alias' ; export class Select { static get Builder ( ) { class Builder { private allColumns = false ; private limitValue : number | undefined = undefined ; private columnNames = new Array < string | Alias > ( ) ; private keyspace : string | undefined = undefined ; private table : string | undefined = undefined ; private whereColumns : string [ ] | undefined = undefined ; private columnsTtlNames = new Array < string > ( ) ; private columnsWriteTimeNames = new Array < string > ( ) ; public from ( keyspace , table ) { this . keyspace = keyspace ; this . table = table ; return this ; } public all ( ) { this . allColumns = true ; return this ; } param columns public columns ( columns ) { this . columnNames = columns ; return this ; } param columns public columnsTtl ( columns ) { this . columnsTtlNames = columns ; return this ; } param columns public columnsWriteTime ( columns ) { this . columnsWriteTimeNames = columns ; return this ; } public where ( columns ) { this . whereColumns = columns ; return this ; } public limit ( n ) { if ( n !== undefined ) { this . limitValue = n ; } return this ; } public build ( ) { const statementPieces = [ 'SELECT' ] ; if ( this . allColumns ) { statementPieces . push ( '*' ) ; } else { statementPieces . push ( [ ... this . columnNames . map ( ( name ) => { if ( name instanceof Alias ) { return `"${ name . columnName }" as "${ name . alias }"` ; } else { return `"${ name }"` ; } } ) , ... this . columnsTtlNames . map ( ( column ) => `TTL("${ column }")` ) , ... this . columnsWriteTimeNames . map ( ( column ) => `writetime("${ column }")` ) , ] . join ( ', ' ) ) ; } statementPieces . push ( `FROM "${ this . keyspace }"."${ this . table }"` ) ; if ( this . whereColumns !== undefined && this . whereColumns . length > 0 ) { if ( this . whereColumns . some ( ( column ) => ! column ) ) { throw new Error ( `All columns in WHERE clause must be valid strings. Received: ${ JSON . stringify ( this . whereColumns ) }` ) ; } statementPieces . push ( 'WHERE' , this . whereColumns . map ( ( column ) => `"${ column }"=?` ) . join ( ' AND ' ) ) ; } if ( this . limitValue !== undefined ) { statementPieces . push ( `LIMIT ${ this . limitValue }` ) ; } return statementPieces . join ( ' ' ) ; } } return Builder ; } }