import { CassandraPrimaryKeyMissing , CassandraStatementNotAllowed , } from '@/services/datastores/cassandra/lib/errors' ; import { getRestrictedQueries } from '@/services/datastores/cassandra/lib/restricted-queries' ; import { IKeyQuery , ITableSchema , } from '@/services/datastores/cassandra/typings/cassandra' ; export function validateName ( keyspaceOrTableName , objectType ) { const INVALID_CHARS = /[^a-zA-Z0-9_]/ ; if ( keyspaceOrTableName . match ( INVALID_CHARS ) ) { throw new Error ( `invalid ${ objectType } name ${ keyspaceOrTableName }` ) ; } } param query throws export function checkQueryRestrictions ( query ) { const illegalStatements = new Array < string > ( ) ; getRestrictedQueries ( ) . forEach ( ( forbiddenQuery ) => { const re = new RegExp ( forbiddenQuery . regex , 'gi' ) ; if ( query . match ( re ) ) { illegalStatements . push ( forbiddenQuery . message ) ; } } ) ; if ( illegalStatements . length > 0 ) { throw new CassandraStatementNotAllowed ( query , illegalStatements [ 0 ] ) ; } } param schema param keyQuery export function checkCompletePrimaryKey ( schema , keyQuery ) { let key ; if ( keyQuery . primaryKey && keyQuery . options ) { key = Object . entries ( ( keyQuery as IKeyQuery ) . primaryKey ) . reduce ( ( prev , [ name , details ] ) => { prev [ name ] = details . value ; return prev ; } , { } as { [ column ] : any ; } ) ; } else { key = keyQuery ; } if ( ! isCompletePrimaryKey ( schema , key ) ) { throw new CassandraPrimaryKeyMissing ( schema . keyspace , schema . name ) ; } } param schema param keyQuery return function isCompletePrimaryKey ( schema , primaryKey ) { const { primaryKey : pkColumns } = schema ; const requiredFields = pkColumns . map ( ( col ) => col . name ) ; const primaryKeySet = new Set ( Object . keys ( primaryKey ) ) ; return requiredFields . every ( ( key ) => primaryKeySet . has ( key ) ) ; }