All files / src/builders uda.js

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 701x 1x   1x   1x 1x     1x   4x     4x     4x     4x     4x                                             4x         4x 4x 4x         4x 4x 4x               1x  
const util = require('util');
const _ = require('lodash');
 
const debug = require('debug')('express-cassandra');
 
const UdaBuilder = function f(client) {
  this._client = client;
};
 
UdaBuilder.prototype = {
  validate_definition(aggregateName, aggregateDefinition) {
    Iif (!aggregateDefinition.input_types) {
      throw (new Error(util.format('No input_types defined for user defined function: %s', aggregateName)));
    }
    Iif (!_.isArray(aggregateDefinition.input_types)) {
      throw (new Error(util.format('input_types must be an array for user defined function: %s', aggregateName)));
    }
    Iif (aggregateDefinition.input_types.length < 1) {
      throw (new Error(util.format('input_types array cannot be blank for user defined function: %s', aggregateName)));
    }
    Iif (!aggregateDefinition.sfunc) {
      throw (new Error(util.format('No sfunc defined for user defined aggregate: %s', aggregateName)));
    }
    Iif (!aggregateDefinition.stype) {
      throw (new Error(util.format('No stype defined for user defined aggregate: %s', aggregateName)));
    }
  },
 
  create_uda(aggregateName, aggregateDefinition, callback) {
    let query = util.format(
      'CREATE OR REPLACE AGGREGATE %s (%s) SFUNC %s STYPE %s',
      aggregateName,
      aggregateDefinition.input_types.toString(),
      aggregateDefinition.sfunc,
      aggregateDefinition.stype,
    );
    if (aggregateDefinition.finalfunc) query += util.format(' FINALFUNC %s', aggregateDefinition.finalfunc);
    query += util.format(' INITCOND %s;', aggregateDefinition.initcond);
 
    debug('executing query: %s', query);
    this._client.execute(query, (err) => {
      callback(err);
    });
  },
 
  get_uda(aggregateName, keyspaceName, callback) {
    const query = util.format(
      "SELECT * FROM system_schema.aggregates WHERE keyspace_name = '%s' AND aggregate_name = '%s';",
      keyspaceName,
      aggregateName.toLowerCase(),
    );
    debug('executing query: %s', query);
    this._client.execute(query, (err, result) => {
      Iif (err) {
        callback(err);
        return;
      }
 
      Eif (result.rows && result.rows.length > 0) {
        callback(null, result.rows);
        return;
      }
 
      callback();
    });
  },
};
 
module.exports = UdaBuilder;