简体   繁体   中英

How to create a parent child relationship in Java?

I need to create a parent child relationship for the following string:

((OPERATING_CARRIER='AB' OR OPERATING_CARRIER='EY' OR (OPERATING_CARRIER='VA' AND (FLIGHT_NO=604 OR FLIGHT_NO=603))))

I have to insert them into a database table as following

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

using the following code

  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);
        }

    }

i have got to the following output

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))))

But now I am clueless on how to proceed, need help.

I have managed to solve it using following code for splitting the string and to build the parent child relationship:

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();
            } 

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