0

代码最初使用“knex”:“0.12.9”升级“knex”:“0.95.4”

0.95.4升级版本需要实现同样的场景

在场景处理列表中的表达式之前,通过以下方式处理了 1000 多个但在 knex 的升级版本中不支持该代码:

const inherits = require('inherits');
const OracleClient = require('knex/lib/dialects/oracledb');
const OracleQueryCompiler = require('knex/lib/dialects/oracledb/query/compiler');
const dbConfig = require('./config.js').get('db');

// inherits the property of OracleQueryCompiler 
function EMSOracleQueryCompiler() {
    OracleQueryCompiler.apply(this, arguments);
}
inherits(EMSOracleQueryCompiler, OracleQueryCompiler);

 /**
 * fix of whereIn oracle db restriction (max number of values in where is 1000)
 * @param statement
 * @returns {*}
 */
EMSOracleQueryCompiler.prototype.whereIn = function whereIn(statement) {
  function chunk(arr, start, amount){
     var result = [],
            i,
            start = start || 0,
            amount = amount || 500,
            len = arr.length;

        do {
            //console.log('appending ', start, '-', start + amount, 'of ', len, '.');
            result.push(arr.slice(start, start+amount));
            start += amount;

        } while (start< len);

        return result;
    }
    // checking condition for statement of column is array
    if (Array.isArray(statement.column))
        return this.multiWhereIn(statement);

    // checking condition for statement value length more than 1000 records
    if(statement.value.length > 1000) {    
        let values = chunk(statement.value, 0, 1000);    
        return '(' + values.map(function(array) {
            statement.value = array;
            return this.whereIn(statement);
        }.bind(this)).join(' OR ') + ')';
    }

    return this.formatter.wrap(statement.column)
        + ' ' + this._not(statement, 'in ')
        + this.wrap(this.formatter.parameterize(statement.value));
};

 function EMSOracleClient () {
    OracleClient.apply(this, arguments);
}
inherits(EMSOracleClient, OracleClient);

EMSOracleClient.prototype.queryCompiler = function () {
   return new (Function.prototype.bind.apply(EMSOracleQueryCompiler,
        [null].concat([this], Array.prototype.slice.call(arguments))))();
}
4

0 回答 0