Changeset 37 for trunk/fmgVen/src


Ignore:
Timestamp:
Mar 20, 2012, 2:40:52 PM (12 years ago)
Author:
fmguler
Message:

Refs #7 - Implemented orderAsc and orderDesc methods of Criteria. Have been testing these for a while, no problem so far. Added BigDecimal to db classes (Numeric db type). If the column name is "order" it is escaped while insert/update. (This should be done for all db keywords). Fixed missing mapping of one to many assc. (lists) of many to one (object) assc (obj.obj.list).

Location:
trunk/fmgVen/src/com/fmguler/ven
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/fmgVen/src/com/fmguler/ven/Criteria.java

    r32 r37  
    4343public class Criteria { 
    4444    private StringBuffer criteriaStringBuffer = new StringBuffer(); //for string criteria 
     45    private StringBuffer orderStringBuffer = new StringBuffer(); //for ordering 
    4546    private LinkedList criterionList = new LinkedList(); //for typed criteria 
    4647    private Map parameters = new HashMap(); //the parameters used in criteria string 
     
    99100    } 
    100101 
     102    /** 
     103     * Return order string to SQL 
     104     */ 
     105    public String orderStringToSQL() { 
     106        if (orderStringBuffer.length() == 0) return ""; 
     107        orderStringBuffer.insert(0, " order by"); 
     108        return orderStringBuffer.toString(); 
     109    } 
     110 
    101111    //-------------------------------------------------------------------------- 
    102112    //Typed Criteria Methods 
     
    153163     */ 
    154164    public Criteria orderAsc(String attribute) { 
    155         //Not implemented yet. 
     165        if (this.orderStringBuffer.length() != 0) this.orderStringBuffer.append(","); 
     166        this.orderStringBuffer.append(" ").append(convertAttributeToAlias(attribute)).append(" asc"); 
    156167        return this; 
    157168    } 
     
    161172     */ 
    162173    public Criteria orderDesc(String attribute) { 
    163         //Not implemented yet. 
     174        if (this.orderStringBuffer.length() != 0) this.orderStringBuffer.append(","); 
     175        this.orderStringBuffer.append(" ").append(convertAttributeToAlias(attribute)).append(" desc"); 
    164176        return this; 
    165177    } 
     
    223235                    stack.push(sr); 
    224236                } else if (conn.equals(Criterion.CONN_OR)) { 
    225                         String s1 = (String)stack.pop(); 
    226                         String s2 = (String)stack.pop(); 
    227                         String sr = "(" + s1 + " or " + s2 + ")"; 
    228                         stack.push(sr); 
    229                     } else if (conn.equals(Criterion.CONN_NOT)) { 
    230                             String s = (String)stack.pop(); 
    231                             String sr = "not (" + s + ")"; 
    232                             stack.push(sr); 
    233                         } 
     237                    String s1 = (String)stack.pop(); 
     238                    String s2 = (String)stack.pop(); 
     239                    String sr = "(" + s1 + " or " + s2 + ")"; 
     240                    stack.push(sr); 
     241                } else if (conn.equals(Criterion.CONN_NOT)) { 
     242                    String s = (String)stack.pop(); 
     243                    String sr = "not (" + s + ")"; 
     244                    stack.push(sr); 
     245                } 
    234246            } 
    235247        } 
     
    284296                .and() //connect previous criteria with and 
    285297                .isNull("SomeDomainObject.description") //attribute is null 
    286                 .or(); //connect previous criteria with and 
     298                .or() //connect previous criteria with or 
     299                .orderDesc("SomeDomainObject.name"); //order by some attribute 
    287300 
    288301        //print the resulting where clause SQL 
    289302        System.out.println(criteria.criteriaToSQL()); 
     303 
     304        //print the resulting order clause SQL 
     305        System.out.println(criteria.orderStringToSQL()); 
    290306 
    291307        //the result is; 
  • trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java

    r32 r37  
    2121import com.fmguler.ven.util.VenList; 
    2222import java.beans.PropertyDescriptor; 
     23import java.math.BigDecimal; 
    2324import java.util.Date; 
    2425import java.util.HashSet; 
     
    4748        this.dbClasses.add(Double.class); 
    4849        this.dbClasses.add(Boolean.class); 
     50        this.dbClasses.add(BigDecimal.class); 
    4951    } 
    5052 
     
    9193            //if (fieldName.equals("id")) continue; //remove if it does not break the sequence 
    9294            if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) 
    93                 query.append(columnName); 
     95                query.append(columnName.equals("order")?"\"order\"":columnName); 
    9496                query.append(","); 
    9597                values.append(":").append(fieldName); 
     
    129131            String fieldName = pdArr[i].getName(); //field name 
    130132            if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) 
    131                 query.append(columnName).append("=:").append(fieldName); 
     133                query.append(columnName.equals("order")?"\"order\"":columnName).append("=:").append(fieldName); 
    132134                query.append(","); 
    133135            } 
  • trunk/fmgVen/src/com/fmguler/ven/QueryMapper.java

    r35 r37  
    2121import com.fmguler.ven.util.VenList; 
    2222import java.beans.PropertyDescriptor; 
     23import java.math.BigDecimal; 
    2324import java.sql.ResultSet; 
    2425import java.sql.SQLException; 
     
    5657        this.dbClasses.add(Double.class); 
    5758        this.dbClasses.add(Boolean.class); 
     59        this.dbClasses.add(BigDecimal.class); 
    5860    } 
    5961 
     
    125127                        if (debug) System.out.println("--field not found: " + columnName); 
    126128                    } 
     129                    continue; //if this is a primitive property, it cannot be an object or list 
    127130                } 
    128131 
    129132                //many to one association (object property) 
    130                 if (map && fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { 
     133                if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { 
    131134                    if (columns.contains(columnName + "_id")) { 
    132135                        if (debug) System.out.println(">>object is found " + columnName); 
    133136                        List list = new ArrayList(1); //we know there will be single result 
     137                        if (!map) list.add(fieldValue); //otherwise we cannot catch one to many assc. (lists) of many to one (object) assc. 
    134138                        mapRecursively(rs, columns, columnName, fieldClass, list); 
    135139                        if (list.size() > 0) wr.setPropertyValue(pd.getName(), list.get(0)); 
     
    151155            } 
    152156        } catch (Exception ex) { 
     157            System.out.println("Ven - error while mapping row, table: " + tableName + " object class: " + objectClass + " error: " + ex.getMessage()); 
    153158            if (debug) { 
    154                 System.out.println("Ven - error while mapping row; "); 
    155159                ex.printStackTrace(); 
    156160            } 
  • trunk/fmgVen/src/com/fmguler/ven/Ven.java

    r32 r37  
    5353     * <p> 
    5454     * By default none of the associations will be retrieved. 
    55      * To include the object associations (retrieve the object graph) joins should be specified, e.g. 
    56      * <code>SomeObject.anotherObject</code> 
    57      *  
     55     * To include the object associations (retrieve the object graph) joins 
     56     * should be specified, e.g. 
     57     * <code>SomeObject.anotherObject</code> 
     58     * 
    5859     * @param id the id of the object to be retrieved 
    5960     * @param objectClass the class of the object to be retrieved 
     
    7677 
    7778    /** 
     79     * Get the object with the specified id, of the specified objectClass type. 
     80     * <p> 
     81     * By default none of the associations will be retrieved. 
     82     * To include the object associations (retrieve the object graph) joins 
     83     * should be specified, e.g. 
     84     * <code>SomeObject.anotherObject</code> 
     85     * 
     86     * @param id the id of the object to be retrieved 
     87     * @param objectClass the class of the object to be retrieved 
     88     * @param joins the set of object graphs to be included with the object 
     89     * @param criteria to filter and order the result according to some criteria 
     90     * (e.g. for associations) 
     91     * @return the retrieved object including specified associations 
     92     */ 
     93    public Object get(int id, Class objectClass, Set joins, Criteria criteria) { 
     94        String query = generator.generateSelectQuery(objectClass, joins); 
     95        query += " where 1=1 and " + Convert.toDB(Convert.toSimpleName(objectClass.getName())) + ".id = :___id " + criteria.criteriaStringToSQL() + " and " + criteria.criteriaToSQL() + criteria.orderStringToSQL(); 
     96 
     97        criteria.getParameters().put("___id", new Integer(id)); 
     98        if (debug) System.out.println("Ven - SQL: " + query); 
     99 
     100        List result = mapper.list(query, criteria.getParameters(), objectClass); 
     101        if (result.isEmpty()) return null; 
     102        if (result.size() > 1) System.out.println("Ven - WARNING >> get(id) returns more than one row"); 
     103        return result.get(0); 
     104    } 
     105 
     106    /** 
    78107     * List the objects of the specified objectClass type. 
    79108     * <p> 
    80109     * By default none of the associations will be retrieved. 
    81      * To include the object associations (retrieve the object graph) joins should be specified, e.g. 
    82      * <code>SomeObject.anotherObject</code> 
    83      *  
     110     * To include the object associations (retrieve the object graph) joins 
     111     * should be specified, e.g. 
     112     * <code>SomeObject.anotherObject</code> 
     113     * 
    84114     * @param objectClass the class of the objects to be retrieved 
    85115     * @param joins the set of object graphs to be included with objects 
     
    87117     */ 
    88118    public List list(Class objectClass, Set joins) { 
    89         String query = generator.generateSelectQuery(objectClass, joins); 
    90  
    91         Map paramMap = new HashMap(); 
    92         if (debug) System.out.println("Ven - SQL: " + query); 
    93  
    94         List result = mapper.list(query, paramMap, objectClass); 
    95         return result; 
    96     } 
    97  
    98     /** 
    99      * List the objects of the specified objectClass type, filtering according to some criteria. 
    100      * <p> 
    101      * By default none of the associations will be retrieved. 
    102      * To include the object associations (retrieve the object graph) joins should be specified, e.g. 
    103      * <code>SomeObject.anotherObject</code> 
    104      *  
     119        return list(objectClass, joins, new Criteria()); 
     120    } 
     121 
     122    /** 
     123     * List the objects of the specified objectClass type, filtering according 
     124     * to some criteria. 
     125     * <p> 
     126     * By default none of the associations will be retrieved. 
     127     * To include the object associations (retrieve the object graph) joins 
     128     * should be specified, e.g. 
     129     * <code>SomeObject.anotherObject</code> 
     130     * 
    105131     * @param objectClass the class of the objects to be retrieved 
    106132     * @param joins the set of object graphs to be included with objects 
    107133     * @param criteria to filter and order the result according to some criteria 
    108      * @return the list of objects including the specified associations filtered according to the specified criteria 
     134     * @return the list of objects including the specified associations filtered 
     135     * according to the specified criteria 
    109136     */ 
    110137    public List list(Class objectClass, Set joins, Criteria criteria) { 
    111138        String query = generator.generateSelectQuery(objectClass, joins); 
    112         query += " where 1=1 " + criteria.criteriaStringToSQL() + " and " + criteria.criteriaToSQL(); 
     139        query += " where 1=1 " + criteria.criteriaStringToSQL() + " and " + criteria.criteriaToSQL() + criteria.orderStringToSQL(); 
    113140 
    114141        if (debug) System.out.println("Ven - SQL: " + query); 
     
    119146 
    120147    /** 
    121      * Save the object. If it has a non null (or non zero) "id" property it will be updated. 
     148     * Save the object. If it has a non null (or non zero) "id" property it will 
     149     * be updated. 
    122150     * It will be inserted otherwise. 
    123151     * <p> 
    124152     * The object will be saved to a table with the same name as the object, 
    125153     * The fields of object will be mapped to the table fields. 
    126      *  
     154     * 
    127155     * @param object the object to be saved 
    128156     */ 
     
    144172 
    145173    /** 
    146      * Delete the the object with the specified id of the specified objectClass type 
     174     * Delete the the object with the specified id of the specified objectClass 
     175     * type 
    147176     * @param id the id of the object to be deleted 
    148177     * @param objectClass the class of the object to be deleted 
     
    200229     * Set debug mode, true will log all debug messages to System.out 
    201230     * <p> 
    202      * Note: Use debug mode to detect problems only. It is not a general purpose logging mode. 
     231     * Note: Use debug mode to detect problems only. It is not a general purpose 
     232     * logging mode. 
    203233     * @param debug set true to enable debug mode 
    204234     */ 
     
    208238        mapper.setDebug(debug); 
    209239    } 
     240 
     241    //-------------------------------------------------------------------------- 
     242    //GETTERS 
     243     
     244    /** 
     245     * @return the underlying query generator, for advanced usage. 
     246     */ 
     247    public QueryGenerator getQueryGenerator() { 
     248        return generator; 
     249    } 
     250 
     251    /** 
     252     * @return the underlying query mapper, for advanced usage. 
     253     */ 
     254    public QueryMapper getQueryMapper() { 
     255        return mapper; 
     256    } 
    210257} 
Note: See TracChangeset for help on using the changeset viewer.