Changeset 26 for trunk/fmgVen/src
- Timestamp:
- Jan 2, 2011, 8:51:21 PM (14 years ago)
- Location:
- trunk/fmgVen/src
- Files:
-
- 3 added
- 7 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); -
trunk/fmgVen/src/com/fmguler/ven/Ven.java
r25 r26 18 18 package com.fmguler.ven; 19 19 20 import java.util.HashMap; 20 21 import java.util.List; 21 22 import javax.sql.DataSource; 23 import org.springframework.beans.BeanWrapper; 24 import org.springframework.beans.BeanWrapperImpl; 25 import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; 22 26 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 27 import org.springframework.jdbc.core.namedparam.SqlParameterSource; 23 28 24 29 /** … … 44 49 } 45 50 46 public Object get(int no, Class objectClass) {51 public Object get(int id, Class objectClass) { 47 52 return null; 48 53 } 49 54 55 /** 56 * Save the object. If it has a "id" property it will be updated. 57 * It will be inserted otherwise. 58 * <p> 59 * The object will be saved to a table with the same name as the object, 60 * The fields of object will be mapped to the table fields. 61 * 62 * @param object the object to be saved 63 */ 50 64 public void save(Object object) { 65 String query = null; 66 67 if (isObjectNew(object)) { 68 //if this is a new object assign a new id first 69 generateId(object); 70 query = generator.generateInsertQuery(object); 71 } else { 72 query = generator.generateUpdateQuery(object); 73 } 74 75 //execute the insert/update query 76 SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(object); 77 template.update(query, parameterSource); 51 78 } 52 79 53 public void delete(int no, Class objectClass) {80 public void delete(int id, Class objectClass) { 54 81 } 55 82 56 //SETTERS------------------------------------------------------------------- 83 //-------------------------------------------------------------------------- 84 //PRIVATE METHODS 85 //return true if the object id is zero or null false otherwise 86 private boolean isObjectNew(Object object) throws VenException { 87 BeanWrapper beanWrapper = new BeanWrapperImpl(object); 88 Object objectId = beanWrapper.getPropertyValue("id"); 89 if (objectId == null) return true; 90 if (!(objectId instanceof Integer)) throw new VenException(VenException.EC_GENERATOR_OBJECT_ID_TYPE_INVALID); 91 return ((Integer)objectId).intValue() == 0; 92 } 93 94 //set new object id 95 private void generateId(Object object) { 96 Integer newObjectId = new Integer(template.queryForInt(generator.generateSequenceQuery(object), new HashMap())); 97 BeanWrapper beanWrapper = new BeanWrapperImpl(object); 98 beanWrapper.setPropertyValue("id", newObjectId); 99 } 100 101 //-------------------------------------------------------------------------- 102 //SETTERS 57 103 public void setDataSource(DataSource dataSource) { 58 104 if (dataSource == null) throw new RuntimeException("fmgVen - DataSource cannot be null"); -
trunk/fmgVen/src/net/fmg/ven/Olcut.java
r22 r26 7 7 * Ven - Ayar Yerine Gelenek veritabanı erişim nesnesi 8 8 */ 9 10 9 package net.fmg.ven; 11 10 … … 25 24 private Map parametreler; 26 25 private Set baglaclar; 27 26 28 27 /** 29 28 * Yeni ölçüt … … 32 31 this.metin = ""; 33 32 this.parametreler = new HashMap(); 34 this.baglaclar = 33 this.baglaclar = new HashSet(); 35 34 //{"and", "or", "=", "<>", "<", ">"} 36 35 } 37 36 38 37 /** 39 38 * YAPILMADI 40 39 */ 41 public Olcut ekle(Olcut olcut) {40 public Olcut ekle(Olcut olcut) { 42 41 return null; 43 42 } 44 43 45 44 /** 46 45 * YAPILMADI 47 46 */ 48 public Olcut ekle(List olcutler) {47 public Olcut ekle(List olcutler) { 49 48 return null; 50 49 } 51 50 52 51 /** 53 52 * YAPILMADI 54 53 */ 55 public Olcut ve() {54 public Olcut ve() { 56 55 return null; 57 56 } 58 57 59 58 /** 60 59 * koşul ekle 61 60 */ 62 public Olcut ekle(String kosul) {63 this.metin += " "+kosul;61 public Olcut ekle(String kosul) { 62 this.metin += " " + kosul; 64 63 return this; 65 64 } 66 65 67 66 /** 68 67 * koşullarda kullanılan parametreleri ekle 69 68 */ 70 public Olcut ekle(String parametre, Object nesne) {71 this.parametreler.put(parametre, nesne);69 public Olcut ekle(String parametre, Object nesne) { 70 this.parametreler.put(parametre, nesne); 72 71 return this; 73 72 } 74 73 75 74 /** 76 75 * koşullarda kullanılan parametreleri tümden ekle 77 76 */ 78 public Olcut ekle(Map parametreler) {77 public Olcut ekle(Map parametreler) { 79 78 this.parametreler.putAll(parametreler); 80 79 return this; 81 80 } 82 83 public String olcutleriAl() {81 82 public String olcutleriAl() { 84 83 //((1=1) and ((1=1) and (musteri_numuneler_numune_sahibi.rapor_tarihi is null) and (musteri.no = 4))) 85 84 //Musteri.numuneler.deneyler.deneyTip.ad like :p1 86 85 String sonuc = ""; 87 metin = metin.replace ("(","( ");88 metin = metin.replace (")",")");86 metin = metin.replaceAll("\\(", "\\( "); 87 metin = metin.replaceAll("\\)", " \\)"); 89 88 String[] parcalar = metin.split(" "); 90 89 for (int i = 0; i < parcalar.length; i++) { 91 if (!parcalar[i].startsWith(":") && parcalar[i]. contains(".")){92 int sonNokta = parcalar[i].lastIndexOf('.'); 90 if (!parcalar[i].startsWith(":") && parcalar[i].indexOf(".") >= 0) { 91 int sonNokta = parcalar[i].lastIndexOf('.'); 93 92 sonuc += " "; 94 int u = parcalar[i].length(); 95 sonuc += Cevir.vt(parcalar[i].substring(0,sonNokta).replace('.','_'));96 sonuc += Cevir.vt(parcalar[i].substring(sonNokta,u));97 } else{98 sonuc += " " +parcalar[i];93 int u = parcalar[i].length(); 94 sonuc += Cevir.vt(parcalar[i].substring(0, sonNokta).replace('.', '_')); 95 sonuc += Cevir.vt(parcalar[i].substring(sonNokta, u)); 96 } else { 97 sonuc += " " + parcalar[i]; 99 98 } 100 99 } 101 100 return sonuc; 102 101 } 103 104 public Map parametreler() {102 103 public Map parametreler() { 105 104 return this.parametreler; 106 105 } 107 106 108 107 public String toString() { 109 return olcutleriAl() +" "+this.parametreler;108 return olcutleriAl() + " " + this.parametreler; 110 109 } 111 112 110 } -
trunk/fmgVen/src/net/fmg/ven/SorguEslestirici.java
r23 r26 57 57 long t1 = System.currentTimeMillis(); 58 58 final List sonuclar = new LinkedList(); 59 final String tabloAdi = Cevir.vt( nesneSinifi.getSimpleName());59 final String tabloAdi = Cevir.vt(Cevir.isim(nesneSinifi.getName())); 60 60 final Set sutunlar = new HashSet(); 61 61 -
trunk/fmgVen/src/net/fmg/ven/SorguUretici.java
r22 r26 59 59 if (alanSinifi.getPackage()!=null && nesnePaketleri.contains(alanSinifi.getPackage().getName()) && baglarKapsar(baglar,nesneYolu+"."+alanAdi)){ //domain nesnesi 1-1 join 60 60 String bagTablosuDigerAd = tabloAdi+"_"+sutunAdi; // bağ tablosu için diğer ad, çünkü aynı isimde birden fazla bağ olabilir, karışmasın 61 String bagTablosu = Cevir.vt( alanSinifi.getSimpleName());//gerçek bağ tablosu61 String bagTablosu = Cevir.vt(Cevir.isim(alanSinifi.getName()));//gerçek bağ tablosu 62 62 fromCumlesi.append(" left join "+bagTablosu+" "+bagTablosuDigerAd); 63 63 fromCumlesi.append(" on "+bagTablosuDigerAd+".no = "+tabloAdi+"."+sutunAdi+"_no"); … … 67 67 Class cokluAlandakiNesneSinifi = (Class)wr.getPropertyValue(alanAdi+".nesneSinifi"); 68 68 String bagTablosuDigerAd = tabloAdi+"_"+sutunAdi; // bağ tablosu için diğer ad, çünkü aynı isimde birden fazla bağ olabilir, karışmasın 69 String bagTablosu = Cevir.vt( cokluAlandakiNesneSinifi.getSimpleName());//gerçek bağ tablosu69 String bagTablosu = Cevir.vt(Cevir.isim(cokluAlandakiNesneSinifi.getName()));//gerçek bağ tablosu 70 70 String bagAlani = Cevir.vt((String)wr.getPropertyValue(alanAdi+".bagAlani")); //YAP: bunu vermeden de varsayılan birşey yapsın 71 71 fromCumlesi.append(" left join "+bagTablosu+" "+bagTablosuDigerAd); … … 93 93 public String secmeSorgusuUret(Set baglar, Class nesneSinifi){ 94 94 //long t1 = System.currentTimeMillis(); 95 String nesneAdi = nesneSinifi.getSimpleName();95 String nesneAdi = Cevir.isim(nesneSinifi.getName()); 96 96 String tabloAdi = Cevir.vt(nesneAdi); 97 97 StringBuffer selectCumlesi = new StringBuffer("select "); … … 108 108 public String saymaSorgusuUret(Set baglar, Class nesneSinifi){ 109 109 //long t1 = System.currentTimeMillis(); 110 String nesneAdi = nesneSinifi.getSimpleName();110 String nesneAdi = Cevir.isim(nesneSinifi.getName()); 111 111 String tabloAdi = Cevir.vt(nesneAdi); 112 112 StringBuffer selectCumlesi = new StringBuffer(); … … 123 123 public String guncellemeSorgusuUret(Object nesne){ 124 124 BeanWrapper wr = new BeanWrapperImpl(nesne); 125 String nesneAdi = nesne.getClass().getSimpleName();125 String nesneAdi = Cevir.isim(nesne.getClass().getName()); 126 126 String tabloAdi = Cevir.vt(nesneAdi); 127 127 StringBuffer sorgu; -
trunk/fmgVen/src/net/fmg/ven/Ven.java
r22 r26 48 48 Set baglar = new HashSet(); 49 49 kullanimlar.put(kullanimNo,baglar); 50 baglar.add( nesneSinifi.getSimpleName());50 baglar.add(Cevir.isim(nesneSinifi.getName())); 51 51 52 52 String sorgu = uretici.secmeSorgusuUret(baglar, nesneSinifi); … … 111 111 public Object nesneAl(Class nesneSinifi, Integer no, Set baglar){ 112 112 String sorgu = uretici.secmeSorgusuUret(baglar, nesneSinifi); 113 Olcut olcut = new Olcut().ekle("and "+Cevir.vt( nesneSinifi.getSimpleName())+".no = :___no").ekle("___no",no);113 Olcut olcut = new Olcut().ekle("and "+Cevir.vt(Cevir.isim(nesneSinifi.getName()))+".no = :___no").ekle("___no",no); 114 114 sorgu += " where 1=1"+olcut.olcutleriAl(); 115 115 if(hataAyiklama) System.out.println("SQL: "+sorgu); … … 127 127 public Object nesneAl(Class nesneSinifi, Integer no, Set baglar, Olcut olcut){ 128 128 String sorgu = uretici.secmeSorgusuUret(baglar, nesneSinifi); 129 sorgu += " where 1=1 and "+Cevir.vt( nesneSinifi.getSimpleName())+".no = :___no "+olcut.olcutleriAl(); //No diğer ölçütlerden önce gelmeli order-limit için129 sorgu += " where 1=1 and "+Cevir.vt(Cevir.isim(nesneSinifi.getName()))+".no = :___no "+olcut.olcutleriAl(); //No diğer ölçütlerden önce gelmeli order-limit için 130 130 olcut.ekle("___no",no); 131 131 if(hataAyiklama) System.out.println("SQL: "+sorgu); … … 145 145 BeanWrapper wr = new BeanWrapperImpl(nesne); 146 146 if(wr.getPropertyValue("no")==null){ 147 wr.setPropertyValue("no",new Integer(sablon.queryForInt("select currval('"+Cevir.vt( nesne.getClass().getSimpleName())+"_no')", new HashMap())));147 wr.setPropertyValue("no",new Integer(sablon.queryForInt("select currval('"+Cevir.vt(Cevir.isim(nesne.getClass().getName()))+"_no')", new HashMap()))); 148 148 } 149 149 } 150 150 151 151 public void nesneSil(Integer no, Class nesneSinifi){ 152 String sorgu = "delete from "+Cevir.vt( nesneSinifi.getSimpleName())+" where no = :no ;";152 String sorgu = "delete from "+Cevir.vt(Cevir.isim(nesneSinifi.getName()))+" where no = :no ;"; 153 153 Map parametreler = new HashMap(2); 154 154 parametreler.put("no",no); -
trunk/fmgVen/src/net/fmg/ven/arac/Cevir.java
r22 r26 35 35 return sonuc.toString(); 36 36 } 37 38 public static String isim(String isim) { 39 int i = isim.lastIndexOf("."); 40 if (i < 0) return isim; 41 return isim.substring(i + 1); 42 } 37 43 38 44 }
Note: See TracChangeset
for help on using the changeset viewer.