source: trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java @ 26

Last change on this file since 26 was 26, checked in by fmguler, 14 years ago

Refs #3 - Ven.save() is transferred to the new package. Save use case is completed. Convert.toDB() converts camel case object names to database names with underscores. Unchecked VenException is thrown by Ven methods. Liquibase changelog (test-db-changelog.xml) is changed to include only a simple object to test save operation. It creates a sample database table and inserts a sample row. On rollback it removes sample data. Using liquibase, Sample.java builds database, tests save operation and rolls back.

QueryGenerator generates insert and update queries for save and generates sequence query to assign ids to new objects. Ven calls query generator and runs the query using spring jdbc template.

Old codebase updated to run at Java 1.4

File size: 5.3 KB
Line 
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 */
18package com.fmguler.ven;
19
20import com.fmguler.ven.util.Convert;
21import java.beans.PropertyDescriptor;
22import java.util.Date;
23import java.util.HashSet;
24import java.util.Set;
25import org.springframework.beans.BeanWrapper;
26import org.springframework.beans.BeanWrapperImpl;
27
28/**
29 * Generates queries in the form of 'Convention over Configuration' of the specified class.
30 * @author Fatih Mehmet Güler
31 */
32public 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    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();
89    }
90
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
128    public void addDomainPackage(String domainPackage) {
129        domainPackages.add(domainPackage);
130    }
131}
Note: See TracBrowser for help on using the repository browser.