<commit_before>/*
 * Copyright 2014 Next Century Corporation
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/**
 * Stores the parameters a user is using to connect to the database. Queries will be executed through this connection.
 * @class neon.query.Connection
 * @example
 *     var connection = new neon.query.Connection();
 *     // use a mongo database on localhost
 *     connection.connect(neon.query.Connection.MONGO, "localhost");
 *     // use database mydb
 *     connection.use("mydb");
 *
 *     // queries through this connection will use the parameters specified above
 *     connection.executeQuery(query1, callback);
 *     connection.executeQuery(query2, callback);
 * @constructor
 */
neon.query.Connection = function () {

    this.host_ = undefined;
    this.databaseType_ = undefined;
    this.database_ = undefined;
};

/**
 * Indicates the database type is mongo
 * @property MONGO
 * @type {String}
 */
neon.query.Connection.MONGO = 'mongo';

/**
 * Indicates the database type is shark
 * @property SHARK
 * @type {String}
 */
neon.query.Connection.SHARK = 'shark';


/**
 * Specifies what database type and host the queries will be executed against.
 * @method connect
 * @param {String} databaseType What type of database is being connected to. The constants in this class specify the
 * valid database types.
 * @param {String} host The host the database is running on
 */
neon.query.Connection.prototype.connect = function (databaseType, host) {
    this.host_ = host;
    this.databaseType_ = databaseType;
};

/**
 * Specifies what database to use for queries.
 * @param {String} database The name of the database to use for queries
 * @method use
 */
neon.query.Connection.prototype.use = function (database) {
    this.database_ = database;
};

/**
 * Executes the specified query and fires the callback when complete. This query object may be modified to have the
 * database name set if none was specified in the query.
 * @method executeQuery
 * @param {neon.query.Query} query the query to execute
 * @param {Function} successCallback The callback to fire when the query successfully completes
 * @param {Function} [errorCallback] The optional callback when an error occurs. This is a 3 parameter function that contains the xhr, a short error status and the full error message.
 * @return {neon.util.AjaxRequest} The xhr request object
 */
neon.query.Connection.prototype.executeQuery = function (query, successCallback, errorCallback) {
    this.populateQueryDatabaseField_(query);
    return this.executeQueryService_(query, successCallback, errorCallback, 'query');
};

/**
 * Executes a text based query.
 * Currently this does not use the value of the "use" method and a use statement must be specified as part of the query.
 * @method executeTextQuery
 * @param {String} queryText The query text
 * @param {Function} successCallback The callback to execute when the query is parsed, which contains the query result
 * @param {Function} [errorCallback] The optional callback when an error occurs. This is a 3 parameter function that
 * contains the xhr, a short error status and the full error message.
 * @return {neon.util.AjaxRequest} The xhr request object
 */
neon.query.Connection.prototype.executeTextQuery = function (queryText, successCallback, errorCallback) {
    return neon.util.ajaxUtils.doPost(
        neon.serviceUrl('languageservice', 'query/' + this.host_ + '/' + this.databaseType_),
        {
            data: { text: queryText },
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            responseType: 'json',
            success: successCallback,
            error: errorCallback
        }
    );
};


/**
 * Gets the metadata for each of the columns in the table
 * @method getColumnMetadata
 * @param {String} tableName The table whose metadata is being returned
 * @param {Function} successCallback
 * @return {neon.util.AjaxRequest}
 */
neon.query.Connection.prototype.getColumnMetadata = function (tableName, successCallback) {
    return neon.util.ajaxUtils.doGet(
        neon.serviceUrl('queryservice', 'columnmetadata/' + this.database_ + '/' + tableName),
        {
            success: successCallback
        }
    );
};

neon.query.Connection.prototype.populateQueryDatabaseField_ = function (query) {
    if (!query.filter.databaseName) {
        query.filter.databaseName = this.database_;
    }
};

/**
 * Executes the specified query group (a series of queries whose results are aggregate),
 * and fires the callback when complete. This query objects may be modified to have the
 * database name set if none was specified in the query.
 * @method executeQueryGroup
 * @param {neon.query.QueryGroup} queryGroup the query to execute
 * @param {Function} successCallback The callback to fire when the query successfully completes
 * @param {Function} [errorCallback] The optional callback when an error occurs. This is a 3 parameter function that contains the xhr, a short error status and the full error message.
 * @return {neon.util.AjaxRequest} The xhr request object
 */
neon.query.Connection.prototype.executeQueryGroup = function (queryGroup, successCallback, errorCallback) {
    this.populateQueryGroupDatabaseField_(queryGroup);
    return this.executeQueryService_(queryGroup, successCallback, errorCallback, 'querygroup');
};

neon.query.Connection.prototype.populateQueryGroupDatabaseField_ = function (queryGroup) {
    var me = this;
    _.each(queryGroup.queries, function (query) {
        me.populateQueryDatabaseField_(query);
    });
};

neon.query.Connection.prototype.executeQueryService_ = function (query, successCallback, errorCallback, serviceName) {
    var opts = [];
    if (query.ignoreFilters_) {
        opts.push("ignoreFilters=true");
    }
    else if ( query.ignoredFilterIds_ ) {
        var filterIds = [];
        query.ignoredFilterIds_.forEach(function(id) {
            filterIds.push("ignoredFilterIds[]=" + id);
        });
        opts.push(filterIds.join("&"));<commit_msg>changing syntax of array field in filter id exclusion. it worked in phantomjs but not chrome<commit_after>/*
 * Copyright 2014 Next Century Corporation
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/**
 * Stores the parameters a user is using to connect to the database. Queries will be executed through this connection.
 * @class neon.query.Connection
 * @example
 *     var connection = new neon.query.Connection();
 *     // use a mongo database on localhost
 *     connection.connect(neon.query.Connection.MONGO, "localhost");
 *     // use database mydb
 *     connection.use("mydb");
 *
 *     // queries through this connection will use the parameters specified above
 *     connection.executeQuery(query1, callback);
 *     connection.executeQuery(query2, callback);
 * @constructor
 */
neon.query.Connection = function () {

    this.host_ = undefined;
    this.databaseType_ = undefined;
    this.database_ = undefined;
};

/**
 * Indicates the database type is mongo
 * @property MONGO
 * @type {String}
 */
neon.query.Connection.MONGO = 'mongo';

/**
 * Indicates the database type is shark
 * @property SHARK
 * @type {String}
 */
neon.query.Connection.SHARK = 'shark';


/**
 * Specifies what database type and host the queries will be executed against.
 * @method connect
 * @param {String} databaseType What type of database is being connected to. The constants in this class specify the
 * valid database types.
 * @param {String} host The host the database is running on
 */
neon.query.Connection.prototype.connect = function (databaseType, host) {
    this.host_ = host;
    this.databaseType_ = databaseType;
};

/**
 * Specifies what database to use for queries.
 * @param {String} database The name of the database to use for queries
 * @method use
 */
neon.query.Connection.prototype.use = function (database) {
    this.database_ = database;
};

/**
 * Executes the specified query and fires the callback when complete. This query object may be modified to have the
 * database name set if none was specified in the query.
 * @method executeQuery
 * @param {neon.query.Query} query the query to execute
 * @param {Function} successCallback The callback to fire when the query successfully completes
 * @param {Function} [errorCallback] The optional callback when an error occurs. This is a 3 parameter function that contains the xhr, a short error status and the full error message.
 * @return {neon.util.AjaxRequest} The xhr request object
 */
neon.query.Connection.prototype.executeQuery = function (query, successCallback, errorCallback) {
    this.populateQueryDatabaseField_(query);
    return this.executeQueryService_(query, successCallback, errorCallback, 'query');
};

/**
 * Executes a text based query.
 * Currently this does not use the value of the "use" method and a use statement must be specified as part of the query.
 * @method executeTextQuery
 * @param {String} queryText The query text
 * @param {Function} successCallback The callback to execute when the query is parsed, which contains the query result
 * @param {Function} [errorCallback] The optional callback when an error occurs. This is a 3 parameter function that
 * contains the xhr, a short error status and the full error message.
 * @return {neon.util.AjaxRequest} The xhr request object
 */
neon.query.Connection.prototype.executeTextQuery = function (queryText, successCallback, errorCallback) {
    return neon.util.ajaxUtils.doPost(
        neon.serviceUrl('languageservice', 'query/' + this.host_ + '/' + this.databaseType_),
        {
            data: { text: queryText },
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            responseType: 'json',
            success: successCallback,
            error: errorCallback
        }
    );
};


/**
 * Gets the metadata for each of the columns in the table
 * @method getColumnMetadata
 * @param {String} tableName The table whose metadata is being returned
 * @param {Function} successCallback
 * @return {neon.util.AjaxRequest}
 */
neon.query.Connection.prototype.getColumnMetadata = function (tableName, successCallback) {
    return neon.util.ajaxUtils.doGet(
        neon.serviceUrl('queryservice', 'columnmetadata/' + this.database_ + '/' + tableName),
        {
            success: successCallback
        }
    );
};

neon.query.Connection.prototype.populateQueryDatabaseField_ = function (query) {
    if (!query.filter.databaseName) {
        query.filter.databaseName = this.database_;
    }
};

/**
 * Executes the specified query group (a series of queries whose results are aggregate),
 * and fires the callback when complete. This query objects may be modified to have the
 * database name set if none was specified in the query.
 * @method executeQueryGroup
 * @param {neon.query.QueryGroup} queryGroup the query to execute
 * @param {Function} successCallback The callback to fire when the query successfully completes
 * @param {Function} [errorCallback] The optional callback when an error occurs. This is a 3 parameter function that contains the xhr, a short error status and the full error message.
 * @return {neon.util.AjaxRequest} The xhr request object
 */
neon.query.Connection.prototype.executeQueryGroup = function (queryGroup, successCallback, errorCallback) {
    this.populateQueryGroupDatabaseField_(queryGroup);
    return this.executeQueryService_(queryGroup, successCallback, errorCallback, 'querygroup');
};

neon.query.Connection.prototype.populateQueryGroupDatabaseField_ = function (queryGroup) {
    var me = this;
    _.each(queryGroup.queries, function (query) {
        me.populateQueryDatabaseField_(query);
    });
};

neon.query.Connection.prototype.executeQueryService_ = function (query, successCallback, errorCallback, serviceName) {
    var opts = [];
    if (query.ignoreFilters_) {
        opts.push("ignoreFilters=true");
    }
    else if ( query.ignoredFilterIds_ ) {
        var filterIds = [];
        query.ignoredFilterIds_.forEach(function(id) {
            filterIds.push("ignoredFilterIds=" + id);
        });
        opts.push(filterIds.join("&"));
{'fraction_non_alphanumeric': 0.09422215473388505, 'fraction_numerical': 0.002125882620909574, 'mean_word_length': 3.756951968219574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7144915', 'n_tokens_mistral': 3465, 'n_tokens_neox': 3418, 'n_words': 1288}