<commit_before>'use babel'

var _ = require('lodash');
var fCommon = require('./common/file-common')
var dropSqlPrefix = 'DROP FUNCTION IF EXISTS';
var alterSqlPrefix = 'ALTER FUNCTION';

function parseFunction(fileContent) {
  parseFunctionRegex = /CREATE\s+(OR\s+REPLACE\s+)?FUNCTION\s+(\w+)\.([^(]*)([(\s+\w+,)='\[\]]*)\s+RETURNS\s+([(\s+\w+,)=\[\]]*)\s+AS/i;
  var matches = fileContent.match(parseFunctionRegex);
  var functionData = undefined;
  if (matches !== null) {
    var parsedParameters = parseParameters(matches[4]);
    functionData = {
      schema: matches[2],
      name: matches[3],
      params: parsedParameters,
      returnType: matches[5],
      content: fileContent,
      drop: createDropFunction(matches[2], matches[3], matches[4]),
      alter: createAlterFunction(matches[2], matches[3], matches[4])
    };
  }
  return functionData;
}

function createDropFunction(schema, functionName, params) {
  params = buildAlterDropParams(params, functionName);
  var dropSql = dropSqlPrefix + ' ' + schema + '.' + functionName + params + ';';
  return dropSql;
}

function createAlterFunction(schema, functionName, params) {
  params = buildAlterDropParams(params);
  var alterSql = alterSqlPrefix + ' ' + schema + '.' + functionName + params;
  return alterSql;
}

function buildAlterDropParams(params) {
  var paramsStr = '';
  params = params.trim().replace(/\)$/, '').replace(/^\(/, '');
  var parameters = params.split(',');
  var paramsTypes = [];
  parameters.forEach(function (value) {
    if (value != null && value.length > 0) {
      var param = parseParameter(value);
      if (param.argmode !== 'OUT') {
        paramsTypes.push(param.type);
      }
    }
  });
  return '(' + paramsTypes.join(', ').trim() + ')';
}

function parseParameters(paramsIn) {<commit_msg>Add signature to function object<commit_after>'use babel'

var _ = require('lodash');
var fCommon = require('./common/file-common')
var dropSqlPrefix = 'DROP FUNCTION IF EXISTS';
var alterSqlPrefix = 'ALTER FUNCTION';

function parseFunction(fileContent) {
  parseFunctionRegex = /CREATE\s+(OR\s+REPLACE\s+)?FUNCTION\s+(\w+)\.([^(]*)([(\s+\w+,)='\[\]]*)\s+RETURNS\s+([(\s+\w+,)=\[\]]*)\s+AS/i;
  var matches = fileContent.match(parseFunctionRegex);
  var functionData = undefined;
  if (matches !== null) {
    var parsedParameters = parseParameters(matches[4]);
    functionData = {
      schema: matches[2],
      name: matches[3],
      params: parsedParameters,
      returnType: matches[5],
      content: fileContent,
      drop: createDropFunction(matches[2], matches[3], matches[4]),
      alter: createAlterFunction(matches[2], matches[3], matches[4]),
      signature: createSignature(matches[2], matches[3], matches[4], matches[5])
    };
  }
  return functionData;
}

function createSignature(schema, functionName, params, returnType) {
  var signature = returnType.trim() + ':' + schema.trim() + '.' + functionName.trim() + params.trim();
  return signature;
}

function createDropFunction(schema, functionName, params) {
  params = buildAlterDropParams(params, functionName);
  var dropSql = dropSqlPrefix + ' ' + schema + '.' + functionName + params + ';';
  return dropSql;
}

function createAlterFunction(schema, functionName, params) {
  params = buildAlterDropParams(params);
  var alterSql = alterSqlPrefix + ' ' + schema + '.' + functionName + params;
  return alterSql;
}

function buildAlterDropParams(params) {
  var paramsStr = '';
  params = params.trim().replace(/\)$/, '').replace(/^\(/, '');
  var parameters = params.split(',');
  var paramsTypes = [];
  parameters.forEach(function (value) {
    if (value != null && value.length > 0) {
      var param = parseParameter(value);
      if (param.argmode !== 'OUT') {
        paramsTypes.push(param.type);
      }
    }
  });
  return '(' + paramsTypes.join(', ').trim() + ')';
}

function parseParameters(paramsIn) {
{'fraction_non_alphanumeric': 0.17719568567026195, 'fraction_numerical': 0.00667693888032871, 'mean_word_length': 4.104849279161206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2073882', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1223, 'n_words': 250}