package org.opencloudb.parser.druid;

import java.util.Map;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.stat.TableStat.Condition;

/**
 * Druid解析器中用来从ast语法中提取表名、条件、字段等的vistor
 * @author wang.dw
 *
 */
public class MycatSchemaStatVisitor extends MySqlSchemaStatVisitor {
	public boolean visit(SQLBetweenExpr x) {
    	String begin = x.beginExpr.toString();
    	String end = x.endExpr.toString();
    	
    	Column column = getColumn(x);
        if (column == null) {
            return true;
        }

        Condition condition = null;
        for (Condition item : this.getConditions()) {
            if (item.getColumn().equals(column) && item.getOperator().equals("between")) {
                condition = item;
                break;
            }
        }

        if (condition == null) {
            condition = new Condition();
            condition.setColumn(column);
            condition.setOperator("between");
            this.conditions.add(condition);
        }

      
        condition.getValues().add(begin);
        condition.getValues().add(end);

    	
    	return true;
    }
	
	protected Column getColumn(SQLExpr expr) {
        Map<String, String> aliasMap = getAliasMap();
        if (aliasMap == null) {
            return null;
        }

        if (expr instanceof SQLPropertyExpr) {
            SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
            String column = ((SQLPropertyExpr) expr).getName();

            if (owner instanceof SQLIdentifierExpr) {
                String tableName = ((SQLIdentifierExpr) owner).getName();
                String table = tableName;
                if (aliasMap.containsKey(table)) {
                    table = aliasMap.get(table);
                }

                if (variants.containsKey(table)) {
                    return null;
                }

                if (table != null) {
                    return new Column(table, column);
                }

                return handleSubQueryColumn(tableName, column);
            }

            return null;
        }

        if (expr instanceof SQLIdentifierExpr) {
            Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN);
            if (attrColumn != null) {
                return attrColumn;
            }

            String column = ((SQLIdentifierExpr) expr).getName();
            String table = getCurrentTable();
            if (table != null && aliasMap.containsKey(table)) {
                table = aliasMap.get(table);
                if (table == null) {
                    return null;
                }
            }

            if (table != null) {
                return new Column(table, column);
            }

            if (variants.containsKey(column)) {
                return null;
            }

            return new Column("UNKNOWN", column);
        }
        
        if(expr instanceof SQLBetweenExpr) {
        	SQLBetweenExpr betweenExpr = (SQLBetweenExpr)expr;
        	
        	String tableName = ((SQLIdentifierExpr)((SQLPropertyExpr) betweenExpr.getTestExpr()).getOwner()).getName();
            String column = ((SQLPropertyExpr) betweenExpr.getTestExpr()).getName();


            String table = tableName;
            if (aliasMap.containsKey(table)) {
                table = aliasMap.get(table);
            }

            if (variants.containsKey(table)) {
                return null;
            }

            if (table != null) {
                return new Column(table, column);
            }

            return handleSubQueryColumn(tableName, column);
        }
        return null;
    }
}

@@ -3283,24 +3283,130 @@
  %09%0A        %09
+if(betweenExpr.getTestExpr() != null && betweenExpr.getTestExpr() instanceof SQLPropertyExpr) %7B%0A        %09%09
 String table
@@ -3505,32 +3505,36 @@
 ();%0A            
+    
 String column = 
@@ -3596,32 +3596,36 @@
 ;%0A%0A%0A            
+ 
+   
 String table = t
@@ -3638,32 +3638,36 @@
 me;%0A            
+  
+  
 if (aliasMap.con
@@ -3677,32 +3677,36 @@
 nsKey(table)) %7B%0A
+    
                 
@@ -3738,32 +3738,36 @@
 e);%0A            
+    
 %7D%0A%0A            i
@@ -3757,32 +3757,36 @@
  %7D%0A%0A            
+  
+  
 if (variants.con
@@ -3812,32 +3812,36 @@
                 
+    
 return null;%0A   
@@ -3841,32 +3841,36 @@
 ll;%0A            
+    
 %7D%0A%0A            i
@@ -3860,32 +3860,36 @@
  %7D%0A%0A            
+ 
+   
 if (table != nul
@@ -3885,32 +3885,36 @@
 able != null) %7B%0A
+    
                 
@@ -3951,32 +3951,36 @@
 n);%0A            
+    
 %7D%0A%0A            r
@@ -3970,32 +3970,36 @@
  %7D%0A%0A            
+ 
+   
 return handleSub
@@ -4022,32 +4022,63 @@
 eName, column);%0A
+        %09%7D%0A        %09%0A        %09%0A
         %7D%0A      

解决该语句报错：SELECT COUNT(1) as s FROM price_change WHERE 1=1 and '2015-01-08' between start_time and end_time and '2015-01-08' between start_time and end_time