tags:

views:

76

answers:

2

I have the below code that splits the sql statement and give its indices for the columns.

String sql = "INSERT INTO Table1(SSN, EMPID) VALUES (?,?)";

    public static List<Object[]> indices(String stmt) {
        List<Object[]> list = new ArrayList<Object[]>();
        String variables[] = null;
        try {
            variables = stmt.substring(stmt.indexOf('(')+1, 
                stmt.indexOf(')', stmt.indexOf('(')+1)).split("\\,");
        } catch (Exception e) {}

        for (int i=0; i < variables.length; ++i ) {
            Object [] str =  new Object [2] ;
            str[0] = variables[i].trim() ;
            str[1] = ((Integer)(i+1)) ;
            list.add(str) ;
        }
        return list;
    }


Result - 

list[0] >>

array[0] = SSN
array[1] = 1

list [1] >>
array[0] = EMPID
array[1] = 2

Can some one point me with appropriate regular expression to split the following sql instead -

sql = "if not exists (select * from Table1 where SSN = ? and EMPID =?)  
      INSERT INTO Table1(SSN, EMPID) VALUES (?,?)"

I guess the output would be something like -

list[0] >>
array[0] = SSN
array[1] = 1

list [1] >>
array[0] = EMPID
array[1] = 2

list[2] >>
array[0] = SSN
array[1] = 1

list [3] >>
array[0] = EMPID
array[1] = 2

Thank You

A: 
Andreas_D
I have an application depending on a framework and don't want to introduce new libraries!
HonorGod
I guess you missed my point...plz take a look at the output that needs to be produced from the second sql. It will be a list with 4 objects.
HonorGod
so can Zql give me the parameter column names? [as shown in my question]
HonorGod
I never used it but: yes. That's what a parser is for, create a syntaxtree for an expression in a parsable language. And as the columnnames are syntax elements, yes zql (or another sql parser) should be able to do it. Another option would be Antlr of JavaCC to create an SQL parser. Config files to parse SQL should be available for both.
Andreas_D
+3  A: 

Attempting to parse non-trivial languages like SQL using regexes or other low-level string bashing is a BAD IDEA. You end up with unreadable code and a fragile "parser" that breaks when supplied with input that is invalid or valid-but-different.

You either need to implement a proper SQL parser (or use an existing one), or change your code so that you are not parsing SQL but assembling it from something else.

I have an application depending on a framework and don't want to introduce new libraries!

That is a bad reason for not doing proper parsing. What is wrong with not using another library???

Stephen C