import { getWhereClause } from '@/services/datastores/cassandra/lib/utils/schema-utils' ; import { IKeyQuery , IRowDetails , ITableColumn , ITableSchema , } from '@/services/datastores/cassandra/typings/cassandra' ; /**
 * Generates a CQL INSERT statement from the given input.
 */ export function buildInsertStatement ( keyspace , table , row ) { const fieldNames = Object . keys ( row ) . map ( ( field ) => `"${ field }"` ) ; return `
    INSERT INTO "${ keyspace }"."${ table }"
    (${ fieldNames . join ( ',' ) })
    VALUES (${ fieldNames . map ( ( ) => '?' ) . join ( ',' ) })` . trim ( ) ; } /**
 * Generates a CQL UPDATE statement from the given input.
 */ export function updateStatement ( keyspace , table , schema , primaryKey , updateFields ) { const whereClauses = getWhereClause ( schema , primaryKey ) ; const columnMap = schema . columns . reduce ( ( prev , curr ) => prev . set ( curr . name , curr ) , new Map < string , ITableColumn > ( ) ) ; const setStatements = new Array < string > ( ) ; Object . entries ( updateFields ) . forEach ( ( [ fieldName , details ] ) => { const column = columnMap . get ( fieldName ) ; if ( column && column . type === 'counter' ) { if ( typeof details . value === 'string' && details . value . indexOf ( fieldName ) >= 0 ) { setStatements . push ( `${ fieldName }=${ details . value }` ) ; } } else { setStatements . push ( `${ fieldName }=?` ) ; } } ) ; return `
    UPDATE "${ keyspace }"."${ table }"
    SET ${ setStatements . join ( ', ' ) }
    WHERE ${ whereClauses . join ( ' AND ' ) }` . trim ( ) ; } /**
 * Generates a CQL DELETE statement from the given input.
 */ export function deleteStatement ( keyspace , table , schema , primaryKey ) { const whereClauses = getWhereClause ( schema , primaryKey ) ; return `
    DELETE FROM "${ keyspace }"."${ table }"
    WHERE ${ whereClauses . join ( ' AND ' ) }` . trim ( ) ; } /**
 * Since difference C* drivers allow case-sensitive keyspaces/tables,
 * quotes must be applied to the keyspace and table names.
 *
 * e.g.
 * ```
 * const result = makeQueryCaseSensitive('select * from keyspaceA.tableB', 'KeySpaceA', 'TABLEb')
 * // select * from "KeySpaceA"."TABLEb"
 * ```
 */ export function makeQueryCaseSensitive ( query , keyspace ? , table ? ) { return keyspace && table ? query . replace ( /["]?([a-zA-Z0-9_-]*?)["]?\.["]?([a-zA-Z0-9_-]*)["]?/ , `"${ keyspace }"."${ table }"` ) : query ; } param query param limit returns export function limitSelectQuery ( query , limit ) { if ( ! isSelectQuery ( query ) ) { return query ; } const newLimit = `LIMIT ${ limit }` ; const matches = query . match ( new RegExp ( /^.*\s(limit\s(\d+))\s*$/ , 'i' ) ) ; if ( matches ) { const [ , limitString , limitValue ] = matches ; if ( parseInt ( limitValue , 10 ) <= limit ) { return query ; } return query . replace ( limitString , newLimit ) ; } else { return `${ query } ${ newLimit }` ; } } /**
 * Helper function for identifying SELECT queries.
 */ export function isSelectQuery ( query ) { return ! ! query . match ( new RegExp ( /^\s*select\s+.*$/ , 'i' ) ) ; } export function getColumnsFromSelect ( query ) { if ( ! isSelectQuery ( query ) ) { return [ ] ; } const matches = query . match ( new RegExp ( /^\s*select\s+(.+)\s+from/ , 'i' ) ) ; if ( matches ) { const [ , columns ] = matches ; return columns . split ( ',' ) . map ( ( columnName ) => columnName . trim ( ) ) ; } return [ ] ; } export function escapeValueString ( value ) { return value . replace ( /'/g , "''" ) ; }