Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Adding mysql indices #349

Open
enyachoke opened this issue Mar 15, 2018 · 0 comments
Open

Adding mysql indices #349

enyachoke opened this issue Mar 15, 2018 · 0 comments

Comments

@enyachoke
Copy link

I needed to be able to set index optimizations for mysql so I created this block

class CreateIndexBlock extends Squel.cls.Block {

  addIndexDirectives(directives) {
    let column = '';
    for (const directive of directives) {
      column = column + ` ${directive.type.toUpperCase()} INDEX ${this._buildJoinFor(directive.for)} ${this._buildIndexList(directive.index_list)}`;
    }
    this._column = column;
  }

  _toParamString() {
    return {
      text: this._column || '',
      values: [],
    };
  }

  _buildIndexList(list) {
    return `(${list.join()})`;
  }

  _buildJoinFor(value) {
    let joinFor = ''
    if (!this._isEmpty(value)) {
      joinFor = `FOR ${value.toUpperCase()}`
    }
    return joinFor;
  }

  _isEmpty(val) {
    if (val === undefined)
      return true;

    if (typeof (val) == 'function' || typeof (val) == 'number' || typeof (val) == 'boolean' || Object.prototype.toString.call(val) === '[object Date]')
      return false;

    if (val == null || val.length === 0) // null or 0 length array
      return true;

    if (typeof (val) == "object") {
      // empty object

      var r = true;

      for (var f in val)
        r = false;

      return r;
    }

    return false;
  }

Then added custom select using this block

Squel.myselect = function (options) {
      return Squel.select(options, [
        new Squel.cls.StringBlock(options, 'SELECT'),
        new Squel.cls.FunctionBlock(options),
        new Squel.cls.DistinctBlock(options),
        new Squel.cls.GetFieldBlock(options),
        new Squel.cls.FromTableBlock(options),
        new CreateIndexBlock(options),
        new Squel.cls.JoinBlock(options),
        new Squel.cls.WhereBlock(options),
        new Squel.cls.GroupByBlock(options),
        new Squel.cls.HavingBlock(options),
        new Squel.cls.OrderByBlock(options),
        new Squel.cls.LimitBlock(options),
        new Squel.cls.OffsetBlock(options),
        new Squel.cls.UnionBlock(options),
      ]);
    }

Is this worthy having in core?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant