<commit_before>import _ from 'lodash';
import { template } from '../utils';

function generateIndexName(table, columns, options) {
  const table_name = typeof table === 'object'
    ? table.name
    : table;
  return options.name
    ? options.name
    : template`${table_name}_${_.isArray(columns) ? columns.join('_') : columns}${options.unique ? '_unique' : ''}_index`;
}

function generateColumnsString(columns) {
  if (_.isArray(columns)) {
    return columns.map(name => template`"${name}"`).join(', ');
  }
  if (/.+\(.*\)/.test(columns)) { // expression
    return columns;
  }
  return template`"${columns}"`; // single column
}

export const create = (table_name, columns, options = {}) => {
  /*
   columns - the column, columns, or expression to create the index on

   Options
   name - explicitly specify the name for the index
   unique - is this a unique index
   where - where clause
   concurrently -
   options.method -  [ btree | hash | gist | spgist | gin ]
   */
  const indexName = generateIndexName(table_name, columns, options);
  const columnsString = generateColumnsString(columns);
  const unique = options.unique ? ' UNIQUE ' : '';
  const concurrently = options.concurrently ? ' CONCURRENTLY ' : '';
  const method = options.method ? ` USING ${options.method}` : '';
  const where = options.where ? ` WHERE ${options.where}` : '';

  return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${table_name}"${method} (${columnsString})${where};`;
};

export const drop = (table_name, columns, options = {}) => `DROP INDEX "${generateIndexName(table_name, columns, options)}";`;

// setup reverse functions
create.reverse = drop;
<commit_msg>Possibility to use function in multi-column index (#140)<commit_after>import _ from 'lodash';
import { template } from '../utils';

function generateIndexName(table, columns, options) {
  const table_name = typeof table === 'object'
    ? table.name
    : table;
  return options.name
    ? options.name
    : template`${table_name}_${_.isArray(columns) ? columns.join('_') : columns}${options.unique ? '_unique' : ''}_index`;
}

function generateColumnString(column) {
  return /.+\(.*\)/.test(column)
    ? column // expression
    : template`"${column}"`; // single column
}

function generateColumnsString(columns) {
  return _.isArray(columns)
    ? columns.map(generateColumnString).join(', ')
    : generateColumnString(columns);
}

export const create = (table_name, columns, options = {}) => {
  /*
   columns - the column, columns, or expression to create the index on

   Options
   name - explicitly specify the name for the index
   unique - is this a unique index
   where - where clause
   concurrently -
   options.method -  [ btree | hash | gist | spgist | gin ]
   */
  const indexName = generateIndexName(table_name, columns, options);
  const columnsString = generateColumnsString(columns);
  const unique = options.unique ? ' UNIQUE ' : '';
  const concurrently = options.concurrently ? ' CONCURRENTLY ' : '';
  const method = options.method ? ` USING ${options.method}` : '';
  const where = options.where ? ` WHERE ${options.where}` : '';

  return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${table_name}"${method} (${columnsString})${where};`;
};

export const drop = (table_name, columns, options = {}) => `DROP INDEX "${generateIndexName(table_name, columns, options)}";`;

// setup reverse functions
create.reverse = drop;

{'fraction_non_alphanumeric': 0.15375722543352602, 'fraction_numerical': 0.0008670520231213873, 'mean_word_length': 4.045189504373178, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '2195444', 'n_tokens_mistral': 1017, 'n_tokens_neox': 1009, 'n_words': 276}