我需要为以下字符串创建父子关系:
((OPERATING_CARRIER='AB' OR OPERATING_CARRIER='EY' OR (OPERATING_CARRIER='VA' AND (FLIGHT_NO=604 OR FLIGHT_NO=603))))
我必须将它们插入到数据库表中,如下所示
ID PARENT_ID ENTITY OPERATOR VALUE
1 OPERATING_CARRIER = AB
2 OPERATING_CARRIER = EY
3 OPERATING_CARRIER = VA
4 3 FLIGHT_NO = 604
5 3 FLIGHT_NO = 603
使用以下代码
package whereclause;
import java.util.Iterator;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QueryMatcher {
public static void main(String[] args) {
// TODO Auto-generated method stub
String sa="((OPERATING_CARRIER='AB' OR OPERATING_CARRIER='AB' OR (OPERATING_CARRIER='VA' AND (FLIGHT_NO=604 OR FLIGHT_NO=603))))";
Matcher m = Pattern.compile("\\w+\\s*=\\s*(?:'[^']+'|\\d+)").matcher(sa);
System.out.println("contains "+sa.contains("((("));
Stack<String> in_cond = new Stack<String>();
Iterator<String> iter = in_cond.iterator();
String new_sa=sa;
while(m.find()) {
String aMatch = m.group();
// add aMatch to match list...
System.out.println(aMatch);
in_cond.push(aMatch);
}
System.out.println("string stack is "+in_cond);
int i=0;
for (String new_sa1:in_cond)
{
if(new_sa.contains(in_cond.get(i)))
{
new_sa=new_sa.replace(in_cond.get(i),"&"+i);
System.out.println("String Contains "+in_cond.get(i));
}
i++;
}
System.out.println("new String is "+new_sa);
}
}
我有以下输出
contains false
OPERATING_CARRIER='AB'
OPERATING_CARRIER='AB'
OPERATING_CARRIER='VA'
FLIGHT_NO=604
FLIGHT_NO=603
string stack is [OPERATING_CARRIER='AB', OPERATING_CARRIER='AB', OPERATING_CARRIER='VA', FLIGHT_NO=604, FLIGHT_NO=603]
String Contains OPERATING_CARRIER='AB'
String Contains OPERATING_CARRIER='VA'
String Contains FLIGHT_NO=604
String Contains FLIGHT_NO=603
new String is ((&0 OR &0 OR (&2 AND (&3 OR &4))))
但现在我对如何继续无能为力,需要帮助。
答案 0 :(得分:0)
我已设法使用以下代码解析它以分割字符串 并建立父子关系:
String input="name = 'name_1' AND in_stock IN {'in_stock_1','in_stock_2'} AND ( price BETWEEN '01-jan-2015' and '31-may-2015' OR price = 'price_3' )";
String sa =input;
String[] arr = sa.replaceAll("[()]+","").split("\\s*(\\sOR|\\sAND)\\s*");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
String og_st=orig_input;
Stack<String> temp_bool=new Stack<String>();
String[] bool_arr = og_st.split("\\s+");
String[] bool_op=new String[inout.length-1];
for(String bool:bool_arr)
{
if(bool.equals("AND") || bool.equals("OR"))
{
temp_bool.push(bool);
}
else
{
//nothing here
}
}
for (int i=0;i<temp_bool.size();i++)
{
bool_op[i]=temp_bool.get(i);
}
Conditions c=new Conditions();
String[] arr=null;
arr=inout;
//Stack<String> arr2 =new Stack<String>();
String[] atr=null;
if(arr[l].contains(" BETWEEN "))
{
atr=arr[l].split(" BETWEEN ");
c.id=l+1;
c.entity=atr[0];
c.operator=" BETWEEN ";
String c_value=atr[1];
//c_value=c_value.replace("'","");
c.value=c_value;
}
else
{
atr=arr[l].split(" ");
c.id=l+1;
c.entity=atr[0];
c.operator=atr[1];
String c_value=atr[2];
//c_value=c_value.replace("'","");
c.value=c_value;
}
/*for(int k=0;k<arr2.size();k++)
{
if(arr[l].contains(" BETWEEN "))
{
System.out.println("inside if");
atr=arr[l].split(" BETWEEN ");
c.id=l+1;
c.entity=atr[0];
c.operator=" BETWEEN ";
String c_value=atr[1];
c_value=c_value.replace("'","");
c.value=c_value;
System.out.println(c.entity+" "+c.operator+" "+c.value );
}
else
{
System.out.println("inside else");
atr=arr[l].split(" ");
for(int o=0;o<atr.length;o++)
{
arr2.push(atr[o].toString());
}
c.id=l+1;
c.entity=atr[0];
c.operator=atr[1];
String c_value=atr[2];
c_value=c_value.replace("'","");
c.value=c_value;
}
}*/
c.enopva=arr[l];
int c_id=getDecompressedString(arr,orig_input,l);
if (c_id==0)
{
c.parent_id=c_id;
}
else if(c_id>0)
{
c.parent_id=c_id;
}
if(l>=bool_op.length)
{
c.bool_op=null;
}
else if(l<bool_op.length)
{
c.bool_op=bool_op[l].toString();
}
IncentiveProLog.insertLog(" Class has been generated as "+c.toString(),id);
try
{
insertData(c.id,c_id,c.entity,c.operator,c.value,c.bool_op);
}
catch (SQLException e)
{
e.printStackTrace();
}