简体   繁体   中英

Replace or append to string with Lodash

I have an array with SQL statements:

    var sql=[ "select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL ",
              "select mes,sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL GROUP BY mes"
   ];

and i need to put a where clause on each statement. The problem is when i have other clauses than WHERE i need to put the WHERE clause before all others. I try to do it like this but no luck.

var clauses=['GROUP','HAVING'];
    _.forEach(sql, function (sq, key) {
        clauses.map(function (clause) {
            if (sq.indexOf(clause) !== -1) {
                debugger;
                sql[key]=[sq.slice(0, sq.indexOf(clause)), ' where '+obj.where_str+' ', sq.slice(sq.indexOf(clause))].join('');

                return false;
            }else{
                debugger;
                sql[key]=sq+" where " + obj.where_str;
                return false;
            }
        });
    });

I recommend for you to use RegExp to solve your problem.

Using the following RegExp you can find if the Select has GROUP or HAVING operators.

var pattern = /(select.*)((?:GROUP|HAVING).*)/i;

And then you can test this pattern against your array:

var arr = ['select mes,sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL GROUP BY mes',
    'select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL ',
    'select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL HAVING sadsa'];

arr.forEach(function(select, index){
    var n = pattern.exec(select); // execute the pattern agains the current Select
    if(!n){ // if it not match any GROUP or HAVING,
        arr[index] += arr[index] + ' your_WHERE '; // append your Where at the end
        return;
    } // if match, append yout Where in the middle
    arr[index] = n[1] + ' your_WHERE ' + n[2];
});

The result will be:

0:"select mes,sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL  your_WHERE GROUP BY mes"
1:"select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL "
2:"select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL  your_WHERE HAVING sadsa"

Hope it helps.

This is just a try from my side..please have a look.

The following code will add the new where clause just after the from .. statement. So it will always be first.

 var str= "select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL groupBy...."; var newStr = 'where something > 12' var first_part = str.substr(0,str.indexOf('from')+5); var second_part_tmp = str.substr(str.indexOf('from')+6); var second_part = second_part_tmp.substr(0,second_part_tmp.indexOf(' ')); var third_part = second_part_tmp.substr(second_part_tmp.indexOf(' ')); if(second_part.indexOf(' ')!=0){ } console.log(first_part,second_part, third_part); console.log([first_part,second_part,newStr, third_part].join(' ')) 

Thanks everybody, but i've simplified things. I just replace :where

var sql=[
                    "select sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL :where",
                    "select mes,sum(custo) as TOTAL from PRS_CUSTOS_MATERIAL :where GROUP BY mes"
                ];

using

_.forEach(sql, function (sq, key) {
        sq=sq.replace(":where", ' where '+obj.where_str);
        sql[key]=sq
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM