<commit_before>'use strict';

var _ = require('underscore');

var Pg  = require('./../pg');
const errorHandlerFactory = require('../../../services/error_handler_factory');

function JsonFormat() {
    this.buffer = '';
    this.lastKnownResult = {};
}

JsonFormat.prototype = new Pg('json');

JsonFormat.prototype._contentType = "application/json; charset=utf-8";

// jshint maxcomplexity:10
JsonFormat.prototype.formatResultFields = function(flds) {
  flds = flds || [];
  var nfields = {};
  for (var i=0; i<flds.length; ++i) {
    var f = flds[i];
    var cname = this.client.typeName(f.dataTypeID);
    var tname;

    if ( ! cname ) {
      tname = 'unknown(' + f.dataTypeID + ')';
    } else {
      if ( cname.match('bool') ) {
        tname = 'boolean';
      }
      else if ( cname.match(/int|float|numeric/) ) {
        tname = 'number';
      }
      else if ( cname.match(/text|char|unknown/) ) {
        tname = 'string';
      }
      else if ( cname.match(/date|time/) ) {
        tname = 'date';
      }
      else {
        tname = cname;
      }
      if ( tname && cname.match(/^_/) ) {
        tname += '[]';
      }
    }

    if (['geography', 'geometry'].includes(cname)) {
        let { wkbtype, ndims, srid } = this.client.typeModInfo(f.dataTypeModifier);
        nfields[f.name] = { type: tname, wkbtype, dims: ndims, srid };
    } else {
        nfields[f.name] = { type: tname, pgtype: cname };
    }

  }
  return nfields;
};

JsonFormat.prototype.startStreaming = function() {
    this.total_rows = 0;
    if (this.opts.beforeSink) {
        this.opts.beforeSink();
    }
    if (this.opts.callback) {
        this.buffer += this.opts.callback + '(';
    }
    this.buffer += '{"rows":[';
    this._streamingStarted = true;
};

JsonFormat.prototype.handleQueryRow = function(row, result) {
    if ( ! this._streamingStarted ) {
        this.startStreaming();
    }

    this.lastKnownResult = result;

    this.buffer += (this.total_rows++ ? ',' : '') + JSON.stringify(row, function (key, value) {<commit_msg>Includes raster<commit_after>'use strict';

var _ = require('underscore');

var Pg  = require('./../pg');
const errorHandlerFactory = require('../../../services/error_handler_factory');

function JsonFormat() {
    this.buffer = '';
    this.lastKnownResult = {};
}

JsonFormat.prototype = new Pg('json');

JsonFormat.prototype._contentType = "application/json; charset=utf-8";

// jshint maxcomplexity:10
JsonFormat.prototype.formatResultFields = function(flds) {
  flds = flds || [];
  var nfields = {};
  for (var i=0; i<flds.length; ++i) {
    var f = flds[i];
    var cname = this.client.typeName(f.dataTypeID);
    var tname;

    if ( ! cname ) {
      tname = 'unknown(' + f.dataTypeID + ')';
    } else {
      if ( cname.match('bool') ) {
        tname = 'boolean';
      }
      else if ( cname.match(/int|float|numeric/) ) {
        tname = 'number';
      }
      else if ( cname.match(/text|char|unknown/) ) {
        tname = 'string';
      }
      else if ( cname.match(/date|time/) ) {
        tname = 'date';
      }
      else {
        tname = cname;
      }
      if ( tname && cname.match(/^_/) ) {
        tname += '[]';
      }
    }

    if (['geography', 'geometry', 'raster'].includes(cname)) {
        let { wkbtype, ndims, srid } = this.client.typeModInfo(f.dataTypeModifier);
        nfields[f.name] = { type: tname, wkbtype, dims: ndims, srid };
    } else {
        nfields[f.name] = { type: tname, pgtype: cname };
    }

  }
  return nfields;
};

JsonFormat.prototype.startStreaming = function() {
    this.total_rows = 0;
    if (this.opts.beforeSink) {
        this.opts.beforeSink();
    }
    if (this.opts.callback) {
        this.buffer += this.opts.callback + '(';
    }
    this.buffer += '{"rows":[';
    this._streamingStarted = true;
};

JsonFormat.prototype.handleQueryRow = function(row, result) {
    if ( ! this._streamingStarted ) {
        this.startStreaming();
    }

    this.lastKnownResult = result;

    this.buffer += (this.total_rows++ ? ',' : '') + JSON.stringify(row, function (key, value) {
{'fraction_non_alphanumeric': 0.17521994134897362, 'fraction_numerical': 0.0024437927663734115, 'mean_word_length': 2.465707027942422, 'pattern_counts': {'":': 2, '<': 5, '<?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': '4984758', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1419, 'n_words': 252}