1 23 24 package org.objectweb.medor.query.rdb.lib; 25 26 import org.objectweb.medor.api.MedorException; 27 import org.objectweb.medor.api.QueryLeafException; 28 import org.objectweb.medor.datasource.api.DataStore; 29 import org.objectweb.medor.expression.api.Expression; 30 import org.objectweb.medor.expression.api.ExpressionException; 31 import org.objectweb.medor.expression.api.ParameterOperand; 32 import org.objectweb.medor.query.api.NestQueryNode; 33 import org.objectweb.medor.query.api.NestedField; 34 import org.objectweb.medor.query.api.PropagatedField; 35 import org.objectweb.medor.query.api.QueryTreeField; 36 import org.objectweb.medor.query.lib.Nest; 37 import org.objectweb.medor.query.rdb.api.RdbExpField; 38 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf; 39 import org.objectweb.medor.query.rdb.api.RdbQueryLeaf; 40 41 import java.util.ArrayList ; 42 import java.util.Map ; 43 44 48 public class AggregateRdbQueryNode extends Nest 49 implements RdbQueryLeaf, NestQueryNode { 50 51 protected DataStore ds; 52 protected RdbExpField groupByField; 53 protected RdbExpQueryLeaf subRdbQL; 54 protected String query = null; 55 private boolean isSubquery = false; 56 57 public AggregateRdbQueryNode() { 58 } 59 60 67 public AggregateRdbQueryNode(QueryTreeField[] groupedFields, 68 String groupedFieldName, 69 QueryTreeField[] groupByFields, 70 BasicRdbExpQueryLeaf subQL, 71 DataStore ds, 72 String nodeName) 73 throws MedorException { 74 super(groupedFields, groupedFieldName, groupByFields, nodeName); 75 this.ds = ds; 76 this.subRdbQL = subQL; 77 removeField(groupedFieldName); 79 for (int i = 0; i < groupByFields.length; i++) { 81 subQL.addGroupBy((RdbExpField) groupByFields[i]); 82 } 83 } 84 85 public Object clone(Object clone, 86 Map obj2clone) throws CloneNotSupportedException { 87 clone = super.clone(clone, obj2clone); 88 AggregateRdbQueryNode a = (AggregateRdbQueryNode) clone; 89 a.ds = ds; 90 a.groupByField = (RdbExpField) getClone(groupByField, obj2clone); 91 a.subRdbQL = (RdbExpQueryLeaf) getClone(subRdbQL, obj2clone); 92 a.query = query; 93 a.isSubquery = isSubquery; 94 return clone; 95 } 96 97 public String getSqlRequest(ParameterOperand[] pos, 98 ArrayList al, 99 boolean rangeStartAt, 100 boolean rangeSize) 101 throws MedorException, ExpressionException { 102 if (al == null) { 103 return getSqlRequest(pos, rangeStartAt, rangeSize); 104 } else { 105 throw new MedorException("Request with selectList not supported"); 106 } 107 } 108 109 117 public String getSqlRequest(ParameterOperand[] pos, 118 boolean rangeStartAt, 119 boolean rangeSize) 120 throws MedorException, ExpressionException { 121 ArrayList selectFields = new ArrayList (); 122 for (int i = 0; i < fields.size(); i++) { 123 if (!(fields.get(i) instanceof NestedField)) { 124 if (fields.get(i) instanceof PropagatedField) { 125 selectFields.add(((PropagatedField)fields.get(i)).getPreviousFields()[0]); 126 } else { 127 selectFields.add(fields.get(i)); 128 } 129 } 130 } 131 if (query != null) { 132 return query; 133 } 134 if (subRdbQL.isSubquery()) { 135 boolean subRdbQLDistinct = subRdbQL.getDistinct(); 138 subRdbQL.setDistinct(this.getDistinct()); 139 query = subRdbQL.getSelectList("SELECT ", selectFields, false); 140 subRdbQL.setDistinct(subRdbQLDistinct); 141 subRdbQL.setNoGroupBy(true); 142 143 query += " FROM ("; 144 query += subRdbQL.getSqlRequest(pos, null, rangeStartAt, rangeSize); 145 query += ")"; 146 if (subRdbQL.getRdbAdapter() != null) { 147 query += subRdbQL.getRdbAdapter().getSubqueryAlias(); 148 } 149 query += subRdbQL.getGroupBy(); 150 } else { 151 query = subRdbQL.getSqlRequest(pos, 152 selectFields, 153 rangeStartAt, 154 rangeSize); 155 } 156 return query; 157 } 158 159 public String getSelectList(String selectList, ArrayList selectFields, 160 boolean qualified) 161 throws MedorException { 162 throw new QueryLeafException("Unsupported method getSelectList for AggregateRdbQueryNode"); 163 } 164 165 public DataStore getDataStore() { 166 return ds; 167 } 168 169 public void setIsSubquery(boolean subquery) { 170 isSubquery = subquery; 171 } 172 173 public boolean isSubquery() { 174 return isSubquery; 175 } 176 177 181 public Expression getQueryFilter() { 182 if (subRdbQL != null) { 183 return subRdbQL.getQueryFilter(); 184 } else { 185 return null; 186 } 187 } 188 } | Popular Tags |