<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, XM:true, io:true, Backbone:true, _:true, X:true */

(function () {
  "use strict";

  XT.dataSource = X.Database.create({

    /*
    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 = {},
        parameters = options.query.parameters,
        prop,
        conn = X.options.globalDatabase,
        query,
        complete = function (err, response) {
          var dataHash, params = {}, error;

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

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


      // Helper function to convert parameters to data source friendly formats
      var format = function (param) {
        var recordType = options.query.recordType,
          klass = recordType ? Backbone.Relational.store.getObjectByName(recordType) : null,
          relations = klass ? klass.prototype.relations : [],
          relation = _.find(relations, function (rel) {
            return rel.key === param.attribute;
          }),
          idAttribute;

        // Format date if applicable
        if (param.value instanceof Date) {
          param.value = param.value.toJSON();

        // Format record if applicable
        } else if (param.value instanceof XM.Model) {
          param.value = param.value.id;
        }

        // Format attribute if it's `HasOne` relation
        if (relation && relation.type === Backbone.HasOne && relation.isNested) {
          klass = Backbone.Relational.store.getObjectByName(relation.relatedModel);
          idAttribute = klass.prototype.idAttribute;
          param.attribute = param.attribute + '.' + idAttribute;
        }

      };

      for (prop in parameters) {
        format(parameters[prop]);
      }

      payload.query = options.query;
      payload.username = options.username;
      payload = JSON.stringify(payload);
      query = "select xt.fetch('%@')".f(payload);
      //X.log(query);
      this.query(query, conn, complete);
      return true;
    },

    /*
    Returns a single record.

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

          // Handle error
          if (err) {
            if (options && options.error) {
              params.error = err;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }
          dataHash = JSON.parse(response.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.recordType = recordType;
      payload.id = id;
      payload.options = { context: options.context };
      payload.username = options.username;
      payload = JSON.stringify(payload);
      query = "select xt.retrieve_record('%@')".f(payload);
      //X.log(query);
      this.query(query, conn, complete);
      return true;
    },

    /*
    Commit a single record.

    @param {XM.Model} model
    @param {Object} options
    */
    commitRecord: function (model, options) {
      var that = this,
        payload = {},
        conn = X.options.globalDatabase,
        query,
        complete = function (err, response) {
          var dataHash, params = {}, error;
          //console.log("complete ", err, response);
          // Handle error
          if (err) {
            if (options && options.error) {
              params.error = err;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

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

      payload.recordType = model.recordType;
      payload.requery = options.requery;
      payload.dataHash = model.changeSet();
      payload.username = options.username;
      payload = JSON.stringify(payload);
      query = "select xt.commit_record('%@')".f(payload);
      //X.log(query);
      this.query(query, conn, complete);
      return true;
    },

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

    @param {String} class name
    @param {String} function name
    @param {Object} parameters
    @param {Object} options
    @param {Function} options.conn alternate connection options. Defaults to global.
    @param {Function} options.success callback
    @param {Function} options.error callback
    */
    dispatch: function (name, func, params, options) {
      options = options ? _.clone(options) : {};
      var that = this,
        conn = _.defaults(options.conn || {}, X.options.globalDatabase),
        query,
        payload = {
          className: name,
          functionName: func,
          parameters: params,
          username: options.username
        },
        complete = function (err, response) {
          var dataHash, params = {}, error;

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

          // handle success
          if (options && options.success) {
            try {<commit_msg>issue #19224: No need to get funky with connection any more.<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, XM:true, io:true, Backbone:true, _:true, X:true */

(function () {
  "use strict";

  XT.dataSource = X.Database.create({

    /*
    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 = {},
        parameters = options.query.parameters,
        prop,
        conn = X.options.globalDatabase,
        query,
        complete = function (err, response) {
          var dataHash, params = {}, error;

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

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


      // Helper function to convert parameters to data source friendly formats
      var format = function (param) {
        var recordType = options.query.recordType,
          klass = recordType ? Backbone.Relational.store.getObjectByName(recordType) : null,
          relations = klass ? klass.prototype.relations : [],
          relation = _.find(relations, function (rel) {
            return rel.key === param.attribute;
          }),
          idAttribute;

        // Format date if applicable
        if (param.value instanceof Date) {
          param.value = param.value.toJSON();

        // Format record if applicable
        } else if (param.value instanceof XM.Model) {
          param.value = param.value.id;
        }

        // Format attribute if it's `HasOne` relation
        if (relation && relation.type === Backbone.HasOne && relation.isNested) {
          klass = Backbone.Relational.store.getObjectByName(relation.relatedModel);
          idAttribute = klass.prototype.idAttribute;
          param.attribute = param.attribute + '.' + idAttribute;
        }

      };

      for (prop in parameters) {
        format(parameters[prop]);
      }

      payload.query = options.query;
      payload.username = options.username;
      payload = JSON.stringify(payload);
      query = "select xt.fetch('%@')".f(payload);
      //X.log(query);
      this.query(query, conn, complete);
      return true;
    },

    /*
    Returns a single record.

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

          // Handle error
          if (err) {
            if (options && options.error) {
              params.error = err;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }
          dataHash = JSON.parse(response.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.recordType = recordType;
      payload.id = id;
      payload.options = { context: options.context };
      payload.username = options.username;
      payload = JSON.stringify(payload);
      query = "select xt.retrieve_record('%@')".f(payload);
      //X.log(query);
      this.query(query, conn, complete);
      return true;
    },

    /*
    Commit a single record.

    @param {XM.Model} model
    @param {Object} options
    */
    commitRecord: function (model, options) {
      var that = this,
        payload = {},
        conn = X.options.globalDatabase,
        query,
        complete = function (err, response) {
          var dataHash, params = {}, error;
          //console.log("complete ", err, response);
          // Handle error
          if (err) {
            if (options && options.error) {
              params.error = err;
              error = XT.Error.clone('xt1001', { params: params });
              options.error.call(that, error);
            }
            return;
          }

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

      payload.recordType = model.recordType;
      payload.requery = options.requery;
      payload.dataHash = model.changeSet();
      payload.username = options.username;
      payload = JSON.stringify(payload);
      query = "select xt.commit_record('%@')".f(payload);
      //X.log(query);
      this.query(query, conn, complete);
      return true;
    },

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

    @param {String} class name
    @param {String} function name
    @param {Object} parameters
    @param {Object} options
    @param {Function} options.conn alternate connection options. Defaults to global.
    @param {Function} options.success callback
    @param {Function} options.error callback
    */
    dispatch: function (name, func, params, options) {
      var that = this,
        conn = X.options.globalDatabase,
        query,
        payload = {
          className: name,
          functionName: func,
          parameters: params,
          username: options.username
        },
        complete = function (err, response) {
          var dataHash, params = {}, error;

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

          // handle success
          if (options && options.success) {
            try {
{'fraction_non_alphanumeric': 0.12053169119855812, 'fraction_numerical': 0.0039801742264944425, 'mean_word_length': 1.8053507478407416, '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': '1032505', 'n_tokens_mistral': 3641, 'n_tokens_neox': 3553, 'n_words': 902}