[英]Replace or append to string with Lodash
I have an array with SQL statements: 我有一个带有SQL语句的数组:
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. 我需要在每个语句上放置一个where子句。 The problem is when i have other clauses than WHERE i need to put the WHERE clause before all others.
问题是,当我有WHERE以外的其他子句时,我需要将WHERE子句放在所有其他子句之前。 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. 我建议您使用RegExp来解决您的问题。
Using the following RegExp you can find if the Select has GROUP or HAVING operators. 使用以下RegExp可以查找Select是否具有GROUP或HAVING运算符。
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. 以下代码将在
from ..
语句之后添加新的where子句。 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
我只替换
: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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.