<commit_before>var _ = require('underscore');
var Backbone = require('backbone');
var QuerySchemaModel = require('../../../../data/query-schema-model');

var queryTemplate = _.template('SELECT min(<%= column %>) as min, max(<%= column %>) as max FROM (<%= sql %>) _table_sql');

module.exports = Backbone.Model.extend({
  initialize: function (attrs, opts) {
    if (!opts.configModel) throw new Error('configModel param is required');
    if (!opts.nodeDefModel) throw new Error('nodeDefModel param is required');

    this._configModel = opts.configModel;
    this._nodeDefModel = opts.nodeDefModel;

    this._querySchemaModel = new QuerySchemaModel(null, {
      configModel: this._configModel
    });

    this._query = this._nodeDefModel.querySchemaModel.get('query');

    this._initBinds();
    this._setColumn();
  },

  _initBinds: function () {
    this.listenTo(this._querySchemaModel, 'change:status', this._onChangeStatus);
    this.on('change:column', this._onChangeColumn, this);
  },

  _onChangeColumn: function () {
    this._setColumn();
    this.fetch();
  },

  _onChangeStatus: function () {
    if (this._querySchemaModel.get('status') === 'fetching') {
      return;
    }

    if (this._querySchemaModel.rowsCollection.size() > 0) {
      var results = this._querySchemaModel.rowsCollection.models[0];
      this.trigger('columnsFetched', results);
    }
  },

  _setColumn: function () {
    if (!this.get('column')) {
      return;
    }

    this._querySchemaModel.set({
      rows_per_page: 40,
      query: queryTemplate({
        sql: this._query,
        column: this.get('column')
      })
    });
  },

  fetch: function () {
    if (this.get('column')) {
      this._querySchemaModel.set('status', null);
      this._querySchemaModel.fetch();
    }
  }
});<commit_msg>Migrated QuerySchemaModel to CDB.SQL in column-data<commit_after>var _ = require('underscore');
var Backbone = require('backbone');
var CDB = require('cartodb.js');
var queryTemplate = _.template('SELECT min(<%= column %>) as min, max(<%= column %>) as max FROM (<%= sql %>) _table_sql');

module.exports = Backbone.Model.extend({
  initialize: function (attrs, opts) {
    if (!opts.configModel) throw new Error('configModel param is required');
    if (!opts.nodeDefModel) throw new Error('nodeDefModel param is required');

    this._nodeDefModel = opts.nodeDefModel;

    this._SQL = new CDB.SQL({
      user: opts.configModel.get('user_name'),
      sql_api_template: opts.configModel.get('sql_api_template'),
      api_key: opts.configModel.get('api_key')
    });

    this._query = opts.nodeDefModel.querySchemaModel.get('query');

    this.on('change:column', this.fetch, this);
  },

  _onQueryDone: function (r) {
    if (_.size(r.rows) > 0) {
      this.trigger('columnsFetched', _.first(r.rows));
    }
  },

  fetch: function () {
    if (this.get('column')) {
      this._SQL.execute(
        queryTemplate({
          sql: this._query,
          column: this.get('column')
        }),
        null,
        {
          data: {
            rows_per_page: 40
          },
          success: this._onQueryDone.bind(this),
          error: function () {
            // TODO: what happens if fails?
          }
        }
      );
    }
  }
});
{'fraction_non_alphanumeric': 0.15750690819772797, 'fraction_numerical': 0.0021492170709241634, 'mean_word_length': 2.978021978021978, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5532166', 'n_tokens_mistral': 1031, 'n_tokens_neox': 992, 'n_words': 200}