<commit_before>'use strict';

var Node = require('../node');

var TYPE = 'georeference-postal-code';
var PARAMS = {
    source: Node.PARAM.NODE(Node.GEOMETRY.ANY),
    output_geometry_type: Node.PARAM.NULLABLE(
        Node.PARAM.ENUM(Node.GEOMETRY.POINT, Node.GEOMETRY.POLYGON),
        Node.GEOMETRY.POLYGON
    ),
    postal_code: Node.PARAM.STRING(),
    country: Node.PARAM.STRING()
};

var GeoreferencePostalCode = Node.create(TYPE, PARAMS, { cache: true });

module.exports = GeoreferencePostalCode;
module.exports.TYPE = TYPE;
module.exports.PARAMS = PARAMS;

GeoreferencePostalCode.prototype.sql = function() {
    return queryTemplate({
        source: this.source.getQuery(),
        columns: this.source.getColumns(true).join(', '),
        geocoder_function: getGecoderFunction(this.output_geometry_type),
        postal_code: this.postal_code,
        country: this.country
    });
};

function getGecoderFunction(geometryType) {
    switch (geometryType){
        case Node.GEOMETRY.POLYGON:
            return 'cdb_geocode_postalcode_polygon';
        case Node.GEOMETRY.POINT:
            return 'cdb_google_geocode_street_point';
    default:
        return 'cdb_google_geocode_street_point';
     }
}

var queryTemplate = Node.template([
    'SELECT',
    '  {{=it.columns}},',
    '  cdb_dataservices_client.{{=it.geocoder_function}}(' +
    '    {{=it.postal_code}},',
    '    {{=it.country}}',
    '  ) AS the_geom',
    'FROM ({{=it.source}}) AS _camshaft_georeference_postal_code_analysis'
 ].join('\n'));
<commit_msg>Added params to georeference-postal-code to define if a param is a custom test or a column value.<commit_after>'use strict';

var Node = require('../node');
var debug = require('../../util/debug')('analysis:georeference-postal-code');

var TYPE = 'georeference-postal-code';
var PARAMS = {
    source: Node.PARAM.NODE(Node.GEOMETRY.ANY),
    output_geometry_type: Node.PARAM.NULLABLE(
        Node.PARAM.ENUM(Node.GEOMETRY.POINT, Node.GEOMETRY.POLYGON),
        Node.GEOMETRY.POLYGON
    ),
    postal_code: Node.PARAM.STRING(),
    country: Node.PARAM.NULLABLE(Node.PARAM.STRING()),
    country_column: Node.PARAM.NULLABLE(Node.PARAM.STRING())
};

var GeoreferencePostalCode = Node.create(TYPE, PARAMS, {
    cache: true,
    beforeCreate: function (node) {
        if (!node.country && !node.country_column) {
            throw new Error('You must indicate "country" or "country_column" param');
        }
    }
});

module.exports = GeoreferencePostalCode;
module.exports.TYPE = TYPE;
module.exports.PARAMS = PARAMS;

GeoreferencePostalCode.prototype.sql = function() {
    var sql = queryTemplate({
        source: this.source.getQuery(),
        columns: this.source.getColumns(true).join(', '),
        geocoder_function: getGecoderFunction(this.output_geometry_type),
        postal_code: this.postal_code,
        country: this.getFunctionParam('country')
    });

    debug(sql);

    return sql;
};

GeoreferencePostalCode.prototype.getFunctionParam = function (name) {
    if (this[name + '_column']) {
        return this[name + '_column'];
    }

    if (this[name]) {
        return '\'' + this[name] + '\'';
    }

    return 'NULL';
};

function getGecoderFunction(geometryType) {
    switch (geometryType){
        case Node.GEOMETRY.POLYGON:
            return 'cdb_geocode_postalcode_polygon';
        case Node.GEOMETRY.POINT:
            return 'cdb_google_geocode_street_point';
    default:
        return 'cdb_google_geocode_street_point';
     }
}

var queryTemplate = Node.template([
    'SELECT',
    '  {{=it.columns}},',
    '  cdb_dataservices_client.{{=it.geocoder_function}}(' +
    '    {{=it.postal_code}},',
    '    {{=it.country}}',
    '  ) AS the_geom',
    'FROM ({{=it.source}}) AS _camshaft_georeference_postal_code_analysis'
 ].join('\n'));

{'fraction_non_alphanumeric': 0.14423830676770316, 'fraction_numerical': 0.0, 'mean_word_length': 3.3156708004509583, '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': '240759', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1296, 'n_words': 209}