<commit_before>/*jshint indent:2, curly:true eqeqeq:true, immed:true, latedef:true,
newcap:true, noarg:true, regexp:true, undef:true, strict:true, trailing:true
white:true*/
/*global XT:true, io:true, _:true, console:true */

(function () {
  "use strict";

  XT.dataSource = {

    datasourceUrl: "purpletie.xtuple.com",
    //datasourceUrl: "localhost",
    datasourcePort: 9000,
    isConnected: false,

    /*
    Returns a record array based on a query.

    @param {Object} query
    @param {Object} options
    */
    fetch: function (options) {
      options = options ? _.clone(options) : {};
      var that = this,
        payload = {},
        complete = function (response) {
          var dataHash, params = {}, error;

          // Handle error
          if (response.data.isError) {
            if (options && options.error) {
              params.error = response.data.reason;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

          // Handle success
          dataHash = JSON.parse(response.data.rows[0].fetch);
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      payload.requestType = 'fetch';
      payload.query = options.query;

      return XT.Request
               .handle("function/fetch")
               .notify(complete)
               .send(payload);
    },

    /*
    Returns a single record.

    @param {String} record type
    @param {Number} id
    @param {Object} options
    */
    retrieveRecord: function (recordType, id, options) {
      var that = this,
        payload = {},
        complete = function (response) {
          var dataHash, params = {}, error;

          // Handle error
          if (response.data.isError) {
            if (options && options.error) {
              params.error = response.data.reason;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }
          dataHash = JSON.parse(response.data.rows[0].retrieve_record);

          // Handle no data as error
          if (_.isEmpty(dataHash)) {
            if (options && options.error) {
              error = XT.Error.clone('xt1007');
              options.error.call(that, error);
            }
            return;
          }

          // Handle success
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      payload.requestType = 'retrieveRecord';
      payload.recordType = recordType;
      payload.id = id;

      return XT.Request
               .handle("function/retrieveRecord")
               .notify(complete)
               .send(payload);
    },

    /*
    Commit a single record.

    @param {XT.Model} model
    @param {Object} options
    */
    commitRecord: function (model, options) {
      var that = this,
        payload = {},
        complete = function (response) {
          var dataHash, params = {}, error;

          // Handle error
          if (response.data.isError) {
            if (options && options.error) {
              params.error = response.data.reason;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

          // Handle ok or complete hash response
          dataHash = JSON.parse(response.data.rows[0].commit_record);
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      payload.requestType = 'commitRecord';
      payload.recordType = model.recordType;
      payload.requery = options.requery;
      payload.dataHash = model.changeSet();

      return XT.Request
               .handle("function/commitRecord")
               .notify(complete)
               .send(payload);
    },

    /*
    Dispatch a server side function call to the datasource.

    @param {String} class name
    @param {String} function name
    @param {Object} parameters
    @param {Function} success callback
    @param {Function} error callback
    */
    /** @private */
    dispatch: function (name, func, params, options) {
      var that = this,
        payload = {
          requestType: 'dispatch',
          className: name,
          functionName: func,
          parameters: params
        },
        complete = function (response) {
          var dataHash, params = {}, error;

          // handle error
          if (response.data.isError) {
            if (options && options.error) {
              params.error = response.data.reason;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

          // handle success
          dataHash = JSON.parse(response.data.rows[0].dispatch);
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      return XT.Request
               .handle('function/dispatch')<commit_msg>update error handling for new datasource.<commit_after>/*jshint indent:2, curly:true eqeqeq:true, immed:true, latedef:true,
newcap:true, noarg:true, regexp:true, undef:true, strict:true, trailing:true
white:true*/
/*global XT:true, io:true, _:true, console:true */

(function () {
  "use strict";

  XT.dataSource = {

    datasourceUrl: "purpletie.xtuple.com",
    //datasourceUrl: "localhost",
    datasourcePort: 9000,
    isConnected: false,

    /*
    Returns a record array based on a query.

    @param {Object} query
    @param {Object} options
    */
    fetch: function (options) {
      options = options ? _.clone(options) : {};
      var that = this,
        payload = {},
        complete = function (response) {
          var dataHash, params = {}, error;

          // Handle error
          if (response.isError) {
            if (options && options.error) {
              params.error = response.message.data.detail;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

          // Handle success
          dataHash = JSON.parse(response.data.rows[0].fetch);
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      payload.requestType = 'fetch';
      payload.query = options.query;

      return XT.Request
               .handle("function/fetch")
               .notify(complete)
               .send(payload);
    },

    /*
    Returns a single record.

    @param {String} record type
    @param {Number} id
    @param {Object} options
    */
    retrieveRecord: function (recordType, id, options) {
      var that = this,
        payload = {},
        complete = function (response) {
          var dataHash, params = {}, error;

          // Handle error
          if (response.isError) {
            if (options && options.error) {
              params.error = response.message.data.detail;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }
          dataHash = JSON.parse(response.data.rows[0].retrieve_record);

          // Handle no data as error
          if (_.isEmpty(dataHash)) {
            if (options && options.error) {
              error = XT.Error.clone('xt1007');
              options.error.call(that, error);
            }
            return;
          }

          // Handle success
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      payload.requestType = 'retrieveRecord';
      payload.recordType = recordType;
      payload.id = id;

      return XT.Request
               .handle("function/retrieveRecord")
               .notify(complete)
               .send(payload);
    },

    /*
    Commit a single record.

    @param {XT.Model} model
    @param {Object} options
    */
    commitRecord: function (model, options) {
      var that = this,
        payload = {},
        complete = function (response) {
          var dataHash, params = {}, error;

          // Handle error
          if (response.isError) {
            if (options && options.error) {
              params.error = response.message.data.detail;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

          // Handle ok or complete hash response
          dataHash = JSON.parse(response.data.rows[0].commit_record);
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      payload.requestType = 'commitRecord';
      payload.recordType = model.recordType;
      payload.requery = options.requery;
      payload.dataHash = model.changeSet();

      return XT.Request
               .handle("function/commitRecord")
               .notify(complete)
               .send(payload);
    },

    /*
    Dispatch a server side function call to the datasource.

    @param {String} class name
    @param {String} function name
    @param {Object} parameters
    @param {Function} success callback
    @param {Function} error callback
    */
    /** @private */
    dispatch: function (name, func, params, options) {
      var that = this,
        payload = {
          requestType: 'dispatch',
          className: name,
          functionName: func,
          parameters: params
        },
        complete = function (response) {
          var dataHash, params = {}, error;

          // handle error
          if (response.isError) {
            if (options && options.error) {
              params.error = response.message.data.detail;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

          // handle success
          dataHash = JSON.parse(response.data.rows[0].dispatch);
          if (options && options.success) {
            options.success.call(that, dataHash);
          }
        };

      return XT.Request
               .handle('function/dispatch')
{'fraction_non_alphanumeric': 0.11919831223628692, 'fraction_numerical': 0.0055619485999232835, 'mean_word_length': 1.6584246749936273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2176155', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2756, 'n_words': 654}