Changeset 26 for trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java
- Timestamp:
- Jan 2, 2011, 8:51:21 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java
r23 r26 18 18 package com.fmguler.ven; 19 19 20 import com.fmguler.ven.util.Convert; 21 import java.beans.PropertyDescriptor; 22 import java.util.Date; 20 23 import java.util.HashSet; 21 24 import java.util.Set; 25 import org.springframework.beans.BeanWrapper; 26 import org.springframework.beans.BeanWrapperImpl; 22 27 23 28 /** … … 27 32 public class QueryGenerator { 28 33 private Set domainPackages; 34 private Set dbClasses; 29 35 30 36 public QueryGenerator() { 31 37 domainPackages = new HashSet(); 38 dbClasses = new HashSet(); 39 //the predefined database classes; 40 this.dbClasses.add(Integer.class); 41 this.dbClasses.add(String.class); 42 this.dbClasses.add(Date.class); 43 this.dbClasses.add(Double.class); 44 this.dbClasses.add(Boolean.class); 32 45 } 33 46 … … 40 53 } 41 54 42 public String generateUpdateQuery() { 43 return null; 55 public String generateInsertQuery(Object object) throws VenException { 56 BeanWrapper wr = new BeanWrapperImpl(object); 57 String objectName = Convert.toSimpleName(object.getClass().getName()); 58 String tableName = Convert.toDB(objectName); 59 PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); 60 61 //generate insert query 62 StringBuffer query = new StringBuffer("insert into " + tableName + "("); 63 StringBuffer values = new StringBuffer(" values("); 64 for (int i = 0; i < pdArr.length; i++) { 65 Class fieldClass = pdArr[i].getPropertyType(); //field class 66 String columnName = Convert.toDB(pdArr[i].getName()); //column name 67 String fieldName = pdArr[i].getName(); //field name 68 //if (fieldName.equals("id")) continue; //remove if it does not break the sequence 69 if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) 70 query.append(columnName); 71 query.append(","); 72 values.append(":").append(fieldName); 73 values.append(","); 74 } 75 if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object 76 query.append(Convert.toDB(fieldName)).append("_id"); 77 query.append(","); 78 values.append(":").append(fieldName).append(".id"); 79 values.append(","); 80 } 81 } 82 query.deleteCharAt(query.length() - 1); 83 query.append(")"); 84 values.deleteCharAt(values.length() - 1); 85 values.append(");"); 86 query.append(values); 87 88 return query.toString(); 44 89 } 45 90 46 //SETTERS------------------------------------------------------------------- 91 /** 92 * Generates insert/update query 93 * @return the insert update SQL query 94 */ 95 public String generateUpdateQuery(Object object) throws VenException { 96 BeanWrapper wr = new BeanWrapperImpl(object); 97 String objectName = Convert.toSimpleName(object.getClass().getName()); 98 String tableName = Convert.toDB(objectName); 99 PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); 100 101 StringBuffer query = new StringBuffer("update " + tableName + " set "); 102 for (int i = 0; i < pdArr.length; i++) { 103 Class fieldClass = pdArr[i].getPropertyType(); //field class 104 String columnName = Convert.toDB(pdArr[i].getName()); //column name 105 String fieldName = pdArr[i].getName(); //field name 106 if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) 107 query.append(columnName).append("=:").append(fieldName); 108 query.append(","); 109 } 110 if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object 111 query.append(columnName).append("_id=:").append(fieldName).append(".id"); 112 query.append(","); 113 } 114 } 115 query.deleteCharAt(query.length() - 1); 116 query.append(" where id = :id ;"); //TODO: remove the last comma 117 return query.toString(); 118 } 119 120 public String generateSequenceQuery(Object object) throws VenException { 121 String objectName = Convert.toSimpleName(object.getClass().getName()); 122 String tableName = Convert.toDB(objectName); 123 return "select nextval('" + tableName + "_id_seq');"; 124 } 125 126 //-------------------------------------------------------------------------- 127 //SETTERS 47 128 public void addDomainPackage(String domainPackage) { 48 129 domainPackages.add(domainPackage);
Note: See TracChangeset
for help on using the changeset viewer.