Changeset 28 for trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java
- Timestamp:
- Feb 9, 2011, 10:53:45 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java
r27 r28 19 19 20 20 import com.fmguler.ven.util.Convert; 21 import com.fmguler.ven.util.VenList; 21 22 import java.beans.PropertyDescriptor; 22 23 import java.util.Date; 23 24 import java.util.HashSet; 25 import java.util.Iterator; 26 import java.util.List; 24 27 import java.util.Set; 25 28 import org.springframework.beans.BeanWrapper; … … 33 36 private Set domainPackages; 34 37 private Set dbClasses; 38 private boolean debug = true; 35 39 36 40 public QueryGenerator() { … … 45 49 } 46 50 47 public String generateSelectQuery() { 48 return null; 51 /** 52 * Generates select query for the specified object class and specified joins. 53 * @param joins set of joins that the query will contain 54 * @param objectClass the object class to select from 55 * @return the select SQL query 56 */ 57 public String generateSelectQuery(Class objectClass, Set joins) { 58 long t1 = System.currentTimeMillis(); 59 String objectName = Convert.toSimpleName(objectClass.getName()); 60 String tableName = Convert.toDB(objectName); 61 StringBuffer selectClause = new StringBuffer("select "); 62 StringBuffer fromClause = new StringBuffer("from " + tableName); 63 generateRecursively(0, tableName, objectName, objectClass, joins, selectClause, fromClause); 64 selectClause.append(" 1=1"); 65 if (debug) System.out.println("Ven - query generation time = " + (System.currentTimeMillis() - t1)); 66 return selectClause.toString() + " \n" + fromClause.toString(); 49 67 } 50 68 … … 129 147 * @return the delete SQL query 130 148 */ 131 public String generateDeleteQuery(Class objectClass) {149 public String generateDeleteQuery(Class objectClass) { 132 150 StringBuffer query = new StringBuffer(); 133 151 query.append("delete from ").append(Convert.toDB(Convert.toSimpleName(objectClass.getName()))).append(" where id = :id;"); 134 152 return query.toString(); 135 153 } 136 154 137 155 /** 138 156 * Generates sequence query for the specified object … … 147 165 148 166 //-------------------------------------------------------------------------- 167 //PRIVATE METHODS 168 //recursively generate select query 169 private void generateRecursively(int level, String tableName, String objectPath, Class objectClass, Set joins, StringBuffer selectClause, StringBuffer fromClause) { 170 BeanWrapper wr = new BeanWrapperImpl(objectClass); 171 PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); 172 173 for (int i = 0; i < pdArr.length; i++) { 174 Class fieldClass = pdArr[i].getPropertyType(); //field class 175 String fieldName = pdArr[i].getName(); //field name 176 Object fieldValue = wr.getPropertyValue(fieldName); 177 String columnName = Convert.toDB(pdArr[i].getName()); //column name 178 179 //direct database class (Integer, String, Date, etc) 180 if (dbClasses.contains(fieldClass)) { 181 selectClause.append(tableName).append(".").append(columnName).append(" as ").append(tableName).append("_").append(columnName); //column 182 selectClause.append(", "); 183 } 184 185 //many to one association (object property) 186 if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName()) && joinsContain(joins, objectPath + "." + fieldName)) { 187 String joinTableAlias = tableName + "_" + columnName; //alias for table to join since there can be multiple joins to the same table 188 String joinTable = Convert.toDB(Convert.toSimpleName(fieldClass.getName())); //table to join 189 fromClause.append(" left join ").append(joinTable).append(" ").append(joinTableAlias); 190 fromClause.append(" on ").append(joinTableAlias).append(".id = ").append(tableName).append(".").append(columnName).append("_id"); 191 generateRecursively(++level, joinTableAlias, objectPath + "." + fieldName, fieldClass, joins, selectClause, fromClause); 192 } 193 194 //one to many association (list property) 195 if (fieldValue instanceof List && joinsContain(joins, objectPath + "." + fieldName)) { 196 Class elementClass = VenList.findElementClass((List)fieldValue); 197 String joinTableAlias = tableName + "_" + columnName; //alias for table to join since there can be multiple joins to the same table 198 String joinTable = Convert.toDB(Convert.toSimpleName(elementClass.getName())); //table to join 199 String joinField = Convert.toDB(findJoinField((List)fieldValue)); //field to join 200 fromClause.append(" left join ").append(joinTable).append(" ").append(joinTableAlias); 201 fromClause.append(" on ").append(joinTableAlias).append(".").append(joinField).append("_id = ").append(tableName).append(".id"); 202 generateRecursively(++level, joinTableAlias, objectPath + "." + fieldName, elementClass, joins, selectClause, fromClause); 203 } 204 } 205 } 206 207 //check if the joins contain the specified join 208 private boolean joinsContain(Set joins, String join) { 209 Iterator it = joins.iterator(); 210 while (it.hasNext()) { 211 String str = (String)it.next(); 212 if (str.startsWith(join)) { 213 if (str.length() == join.length()) return true; 214 else if (str.charAt(join.length()) == '.') return true; 215 } 216 } 217 return false; 218 } 219 220 //return the join field of the elements in the list 221 private String findJoinField(List list) { 222 if (list instanceof VenList) { 223 return ((VenList)list).getJoinField(); 224 } else { 225 //find according to 1.5 generic or some convention (e.g. parent_obj_id) 226 return null; 227 } 228 } 229 230 //-------------------------------------------------------------------------- 149 231 //SETTERS 232 /** 233 * Add the domain packages that will be considered persistent 234 * @param domainPackage the domain package 235 */ 150 236 public void addDomainPackage(String domainPackage) { 151 237 domainPackages.add(domainPackage);
Note: See TracChangeset
for help on using the changeset viewer.