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 70 71 72 73 74 75 76 77 78 | 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x | const util = require('util');
const _ = require('lodash');
const debug = require('debug')('express-cassandra');
const UdfBuilder = function f(client) {
this._client = client;
};
UdfBuilder.prototype = {
validate_definition(functionName, functionDefinition) {
Iif (!functionDefinition.returnType) {
throw (new Error(util.format('No returnType defined for user defined function: %s', functionName)));
}
Iif (!functionDefinition.language) {
throw (new Error(util.format('No language defined for user defined function: %s', functionName)));
}
Iif (!functionDefinition.code) {
throw (new Error(util.format('No code defined for user defined function: %s', functionName)));
}
Iif (functionDefinition.inputs && !_.isPlainObject(functionDefinition.inputs)) {
throw (new Error(util.format('inputs must be an object for user defined function: %s', functionName)));
}
Iif (_.isArray(functionDefinition.inputs)) {
throw (new Error(util.format('inputs must be an object, not an array for user defined function: %s', functionName)));
}
},
create_udf(functionName, functionDefinition, callback) {
const udfInputs = [];
if (functionDefinition.inputs) {
Object.keys(functionDefinition.inputs).forEach((input) => {
udfInputs.push(util.format(
'%s %s',
input,
functionDefinition.inputs[input],
));
});
}
const query = util.format(
"CREATE OR REPLACE FUNCTION %s (%s) CALLED ON NULL INPUT RETURNS %s LANGUAGE %s AS '%s';",
functionName,
udfInputs.toString(),
functionDefinition.returnType,
functionDefinition.language,
functionDefinition.code,
);
debug('executing query: %s', query);
this._client.execute(query, (err) => {
callback(err);
});
},
get_udf(functionName, keyspaceName, callback) {
const query = util.format(
"SELECT * FROM system_schema.functions WHERE keyspace_name = '%s' AND function_name = '%s';",
keyspaceName,
functionName.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[0]);
return;
}
callback();
});
},
};
module.exports = UdfBuilder;
|