package dk.lessismore.nojpa.db.statements.mysql;

import dk.lessismore.nojpa.db.statements.*;

import java.util.*;

import org.apache.log4j.Logger;

/**
 * Mysql statement implementation
 *
 * @author LESS-IS-MORE ApS
 * @version 1.0 25-7-02
 */
public class MySqlContainerExpression implements ContainerExpression {

    private final static org.apache.log4j.Logger log = Logger.getLogger(MySqlContainerExpression.class);

    List expressions = new LinkedList();
    List conditions = new LinkedList();

    public ContainerExpression addExpression(Expression expression) {
        expressions.add(expression);
        return this;
    }

    public ContainerExpression addExpression(int condition, Expression expression) {
        addExpression(expression);
        addCondition(condition);
        return this;
    }

    public ContainerExpression addCondition(int condition) {
        conditions.add(WhereSQLStatement.conditionAsStr[condition]);
        return this;
    }

//    public ContainerExpression reAddCondition(int condition) {
//        return null;  //To change body of implemented methods use File | Settings | File Templates.
//    }
//
    public int nrOfExpressions() {
        int n = 0;
        Iterator iterator = expressions.iterator();
        while (iterator.hasNext()) {
            Expression expression = (Expression) iterator.next();
            if (expression instanceof ContainerExpression) {
                n += ((ContainerExpression) expression).nrOfExpressions();
            } else {
                n++;
            }
        }
        return n;
    }

    public String makeStatement() {
//        log.debug("makeStatement():1");
//        Thread.dumpStack();
        if (expressions.isEmpty()) {
            return "";
        }

        List statements = new LinkedList();
        String statement = "";
        Iterator expressionIterator = expressions.iterator();
        while (expressionIterator.hasNext()) {

            Expression expression = (Expression) expressionIterator.next();
            String s = expression.makeStatement().trim();
            if(!s.equals("")){
                statements.add(s);
            }
        }
        Iterator statementsIterator = statements.iterator();
        for (int i = 0; statementsIterator.hasNext(); i++) {
            String stmt = (String) statementsIterator.next();
            if (i > 0) {
                String condition = WhereSQLStatement.conditionAsStr[WhereSQLStatement.AND];
                if ((i - 1) < conditions.size()) {
                    condition = (String) conditions.get((i - 1));
                }
                statement = statement + " " + condition + "\n\t" + stmt;
//                log.debug("tmp.1.statement = " + statement);
            } else {
                statement = "\n\t" + stmt;
            }
//            log.debug("tmp.2.statement = " + statement);
        }

        if (expressions.size() > 1) {
            return "(" + statement + ")";
        } else {
            return statement;
        }
    }

//    public int getLastCondition() {
//        return 0;  //To change body of implemented methods use File | Settings | File Templates.
//    }
//
//    public void setLastCondition(int lastCondition) {
//        //To change body of implemented methods use File | Settings | File Templates.
//    }


    public String makePreparedStatement(PreparedSQLStatement preparedSQLStatement) {

        if (expressions.isEmpty()) {
            return "";
        }

        List statements = new LinkedList();
        String statement = "";
        Iterator expressionIterator = expressions.iterator();
        while (expressionIterator.hasNext()) {
            Expression expression = (Expression) expressionIterator.next();
            String s = expression.makePreparedStatement(preparedSQLStatement);
            //System.out.println("makePreparedStatement: adding " + s);
            statements.add(s);
        }
        Iterator statementsIterator = statements.iterator();
        for (int i = 0; statementsIterator.hasNext(); i++) {
            String stmt = (String) statementsIterator.next();
            if (i > 0) {
                String condition = WhereSQLStatement.conditionAsStr[WhereSQLStatement.AND];
                if ((i - 1) < conditions.size()) {
                    condition = (String) conditions.get((i - 1));
                }
                statement = statement + " " + condition + "\n\t" + stmt;
                //log.debug("tmp.1.statement = " + statement);
            } else {
                statement = "\n\t" + stmt;
            }
            //log.debug("tmp.2.statement = " + statement);
        }

        if (expressions.size() > 1) {
            return "(" + statement + ")";
        } else {
            return statement;
        }
    }
}

@@ -307,24 +307,26 @@
 pression %7B%0A%0A
+//
     private 
@@ -427,16 +427,28 @@
     List
+%3CExpression%3E
  express
@@ -460,32 +460,44 @@
 = new LinkedList
+%3CExpression%3E
 ();%0A    List con
@@ -492,16 +492,24 @@
     List
+%3CString%3E
  conditi
@@ -524,24 +524,32 @@
 w LinkedList
+%3CString%3E
 ();%0A%0A    pub
@@ -1259,154 +1259,47 @@
     
-Iterator iterator = expressions.iterator();%0A        while (iterator.hasNext()) %7B%0A            Expression expression = (Expression) iterator.next();
+for (Object expression : expressions) %7B
 %0A   
@@ -1705,32 +1705,40 @@
  %7D%0A%0A        List
+%3CString%3E
  statements = ne
@@ -1741,32 +1741,40 @@
 = new LinkedList
+%3CString%3E
 ();%0A        Stri
@@ -1796,34 +1796,30 @@
 %22%22;%0A        
-Iterator e
+for (E
 xpressionIte
@@ -1811,34 +1811,24 @@
  (Expression
-Iterator =
  expressions
@@ -1830,145 +1830,26 @@
 sion
-s.iterator();%0A        while (expressionIterator.hasNext()) %7B%0A%0A            Expression expression = (Expression) expressionIterator.next();
+ : expressions) %7B%0A
 %0A   
@@ -1917,16 +1917,17 @@
       if
+ 
 (!s.equa
@@ -1933,16 +1933,17 @@
 als(%22%22))
+ 
 %7B%0A      
@@ -2374,33 +2374,24 @@
  condition =
- (String)
  conditions.
@@ -3321,16 +3321,24 @@
     List
+%3CString%3E
  stateme
@@ -3357,16 +3357,24 @@
 nkedList
+%3CString%3E
 ();%0A    
@@ -3412,18 +3412,14 @@
     
-Iterator e
+for (E
 xpre
@@ -3423,26 +3423,16 @@
 pression
-Iterator =
  express
@@ -3438,144 +3438,25 @@
 sion
-s.iterator();%0A        while (expressionIterator.hasNext()) %7B%0A            Expression expression = (Expression) expressionIterator.next();
+ : expressions) %7B
 %0A   
@@ -4027,25 +4027,16 @@
 dition =
- (String)
  conditi

build fixies