CSNW / sql-bricks

Transparent, Schemaless SQL Generation
http://csnw.github.io/sql-bricks
MIT License
204 stars 25 forks source link

Alternative complex alias hook #50

Closed prust closed 9 years ago

prust commented 9 years ago

@Suor: This way you should be able to add columns support by overriding _aliasToString & you wouldn't need to re-implement the _alias bits, like this:

  Values.prototype.columns = function () {
    this._columns = true;
    return this;
  }
  Values.prototype._aliasToString = function (opts) {
    var alias = Values.super_.prototype._aliasToString.apply(this, arguments);
    if (!alias || !this._columns)
      return alias;

    var cols = _.keys(this._values[0]).map(sql._quoteColOrTbl).join(', ');
    return alias + ' (' + cols + ')';
  }
prust commented 9 years ago

(I moved .as() to the Statement class and put _aliasToString() in the Statement class so they would be together & so you could easily access & override them... I suppose I could leave it on the Select class and you could manually copy the methods from there... or put them in a separate mixin or something... not quite sure what's best)

Suor commented 9 years ago

This should work. It's up to you if you want it on Statement or Insert. I am ok with copying it. A separate mixin sounds like too much hassle to me.

prust commented 9 years ago

It's up to you ... I am ok with copying it. A separate mixin sounds like too much hassle

Agreed. I'm thinking it would make the most sense to move them back to Select (since it doesn't make sense for Update/Insert/Delete).

You should be able to copy it via something like this:

// copy nested aliasing methods
_.extend(Values.prototype, _.pick(Select.prototype, 'as', '_toNestedString', '_aliasToString'));
prust commented 9 years ago

Published as v1.0.2