1 | /* |
---|
2 | * fmgVen - A Convention over Configuration Java ORM Tool |
---|
3 | * Copyright 2010 Fatih Mehmet Güler |
---|
4 | * |
---|
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
6 | * you may not use this file except in compliance with the License. |
---|
7 | * You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | * under the License. |
---|
17 | */ |
---|
18 | package com.fmguler.ven; |
---|
19 | |
---|
20 | import com.fmguler.ven.util.Convert; |
---|
21 | import java.beans.PropertyDescriptor; |
---|
22 | import java.util.Date; |
---|
23 | import java.util.HashSet; |
---|
24 | import java.util.Set; |
---|
25 | import org.springframework.beans.BeanWrapper; |
---|
26 | import org.springframework.beans.BeanWrapperImpl; |
---|
27 | |
---|
28 | /** |
---|
29 | * Generates queries in the form of 'Convention over Configuration' for the specified objects. |
---|
30 | * @author Fatih Mehmet Güler |
---|
31 | */ |
---|
32 | public class QueryGenerator { |
---|
33 | private Set domainPackages; |
---|
34 | private Set dbClasses; |
---|
35 | |
---|
36 | public QueryGenerator() { |
---|
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); |
---|
45 | } |
---|
46 | |
---|
47 | public String generateSelectQuery() { |
---|
48 | return null; |
---|
49 | } |
---|
50 | |
---|
51 | public String generateCountQuery() { |
---|
52 | return null; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Generates insert query for the specified object |
---|
57 | * @param object the object to generate insert query for |
---|
58 | * @return the insert SQL query |
---|
59 | */ |
---|
60 | public String generateInsertQuery(Object object) { |
---|
61 | BeanWrapper wr = new BeanWrapperImpl(object); |
---|
62 | String objectName = Convert.toSimpleName(object.getClass().getName()); |
---|
63 | String tableName = Convert.toDB(objectName); |
---|
64 | PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); |
---|
65 | |
---|
66 | //generate insert query |
---|
67 | StringBuffer query = new StringBuffer("insert into " + tableName + "("); |
---|
68 | StringBuffer values = new StringBuffer(" values("); |
---|
69 | for (int i = 0; i < pdArr.length; i++) { |
---|
70 | Class fieldClass = pdArr[i].getPropertyType(); //field class |
---|
71 | String columnName = Convert.toDB(pdArr[i].getName()); //column name |
---|
72 | String fieldName = pdArr[i].getName(); //field name |
---|
73 | //if (fieldName.equals("id")) continue; //remove if it does not break the sequence |
---|
74 | if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) |
---|
75 | query.append(columnName); |
---|
76 | query.append(","); |
---|
77 | values.append(":").append(fieldName); |
---|
78 | values.append(","); |
---|
79 | } |
---|
80 | if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object |
---|
81 | query.append(Convert.toDB(fieldName)).append("_id"); |
---|
82 | query.append(","); |
---|
83 | values.append(":").append(fieldName).append(".id"); |
---|
84 | values.append(","); |
---|
85 | } |
---|
86 | } |
---|
87 | query.deleteCharAt(query.length() - 1); |
---|
88 | query.append(")"); |
---|
89 | values.deleteCharAt(values.length() - 1); |
---|
90 | values.append(");"); |
---|
91 | query.append(values); |
---|
92 | |
---|
93 | return query.toString(); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Generates update query for the specified object |
---|
98 | * @param object the object to generate update query for |
---|
99 | * @return the update SQL query |
---|
100 | */ |
---|
101 | public String generateUpdateQuery(Object object) throws VenException { |
---|
102 | BeanWrapper wr = new BeanWrapperImpl(object); |
---|
103 | String objectName = Convert.toSimpleName(object.getClass().getName()); |
---|
104 | String tableName = Convert.toDB(objectName); |
---|
105 | PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); |
---|
106 | |
---|
107 | StringBuffer query = new StringBuffer("update " + tableName + " set "); |
---|
108 | for (int i = 0; i < pdArr.length; i++) { |
---|
109 | Class fieldClass = pdArr[i].getPropertyType(); //field class |
---|
110 | String columnName = Convert.toDB(pdArr[i].getName()); //column name |
---|
111 | String fieldName = pdArr[i].getName(); //field name |
---|
112 | if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) |
---|
113 | query.append(columnName).append("=:").append(fieldName); |
---|
114 | query.append(","); |
---|
115 | } |
---|
116 | if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object |
---|
117 | query.append(columnName).append("_id=:").append(fieldName).append(".id"); |
---|
118 | query.append(","); |
---|
119 | } |
---|
120 | } |
---|
121 | query.deleteCharAt(query.length() - 1); |
---|
122 | query.append(" where id = :id ;"); |
---|
123 | return query.toString(); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Generates delete query for the specified object class |
---|
128 | * @param objectClass the object class to generate query for |
---|
129 | * @return the delete SQL query |
---|
130 | */ |
---|
131 | public String generateDeleteQuery(Class objectClass){ |
---|
132 | StringBuffer query = new StringBuffer(); |
---|
133 | query.append("delete from ").append(Convert.toDB(Convert.toSimpleName(objectClass.getName()))).append(" where id = :id;"); |
---|
134 | return query.toString(); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Generates sequence query for the specified object |
---|
139 | * @param object the objectc to generate sequence query for |
---|
140 | * @return the SQL query to select next id |
---|
141 | */ |
---|
142 | public String generateSequenceQuery(Object object) throws VenException { |
---|
143 | String objectName = Convert.toSimpleName(object.getClass().getName()); |
---|
144 | String tableName = Convert.toDB(objectName); |
---|
145 | return "select nextval('" + tableName + "_id_seq');"; |
---|
146 | } |
---|
147 | |
---|
148 | //-------------------------------------------------------------------------- |
---|
149 | //SETTERS |
---|
150 | public void addDomainPackage(String domainPackage) { |
---|
151 | domainPackages.add(domainPackage); |
---|
152 | } |
---|
153 | } |
---|