001    package biz.hammurapi.sql.syntax;
002    
003    import java.sql.PreparedStatement;
004    import java.sql.SQLException;
005    import java.util.ArrayList;
006    import java.util.Iterator;
007    import java.util.List;
008    
009    public class SelectModifier implements StatementFragment {
010            
011            private StatementFragment where;
012            private StatementFragment groupBy;
013            private List orderBy = new ArrayList();
014            private StatementFragment having;
015            private Integer limit;
016            private Integer offset;
017            
018            public void setWhere(StatementFragment where) {
019                    this.where = where;
020            }
021            
022            public void setGroupBy(StatementFragment groupBy) {
023                    this.groupBy = groupBy;
024            }
025            
026            public void addOrderBy(String column, boolean desc) {
027                    orderBy.add(desc ? column + " DESC" : column);
028            }
029            
030            public void addOrderBy(String column) {
031                    orderBy.add(column);
032            }
033            
034            public void setHaving(StatementFragment having) {
035                    this.having = having;
036            }
037            
038            public void setLimit(int limit) {
039                    this.limit = new Integer(limit);
040            }
041            
042            public void setOffset(int offset) {
043                    this.offset = new Integer(offset);
044            }
045    
046            public String toSqlString() {
047                    StringBuffer ret = new StringBuffer();
048                    if (where!=null) {
049                            ret.append(" WHERE ");
050                            ret.append(where.toSqlString());
051                    }
052                    if (groupBy!=null) {
053                            ret.append(" GROUP BY ");
054                            ret.append(groupBy.toSqlString());
055                    }
056                    if (having!=null) {
057                            ret.append(" HAVING ");
058                            ret.append(having.toSqlString());
059                    }
060                    if (!orderBy.isEmpty()) {
061                            ret.append(" ORDER BY ");
062                            Iterator it = orderBy.iterator();
063                            while (it.hasNext()) {
064                                    ret.append(it.next());
065                                    if (it.hasNext()) {
066                                            ret.append(", ");
067                                    }
068                            }
069                    }
070                    if (limit!=null) {
071                            ret.append(" LIMIT ");
072                            ret.append(limit);
073                            if (offset!=null) {
074                                    ret.append(" OFFSET ");
075                                    ret.append(offset);
076                            }
077                    }
078                    
079                    return ret.toString();
080            }
081    
082            public int parameterize(PreparedStatement ps, int idx) throws SQLException {
083                    if (where!=null) {
084                            idx = where.parameterize(ps, idx);
085                    }
086                    if (groupBy!=null) {
087                            idx = groupBy.parameterize(ps, idx);
088                    }
089                    if (having!=null) {
090                            idx = having.parameterize(ps, idx);
091                    }
092                    return idx;
093            }
094    
095            public int hashCode() {
096                    final int prime = 31;
097                    int result = 1;
098                    result = prime * result + ((groupBy == null) ? 0 : groupBy.hashCode());
099                    result = prime * result + ((having == null) ? 0 : having.hashCode());
100                    result = prime * result + ((limit == null) ? 0 : limit.hashCode());
101                    result = prime * result + ((offset == null) ? 0 : offset.hashCode());
102                    result = prime * result + ((orderBy == null) ? 0 : orderBy.hashCode());
103                    result = prime * result + ((where == null) ? 0 : where.hashCode());
104                    return result;
105            }
106    
107            public boolean equals(Object obj) {
108                    if (this == obj)
109                            return true;
110                    if (obj == null)
111                            return false;
112                    if (getClass() != obj.getClass())
113                            return false;
114                    final SelectModifier other = (SelectModifier) obj;
115                    if (groupBy == null) {
116                            if (other.groupBy != null)
117                                    return false;
118                    } else if (!groupBy.equals(other.groupBy))
119                            return false;
120                    if (having == null) {
121                            if (other.having != null)
122                                    return false;
123                    } else if (!having.equals(other.having))
124                            return false;
125                    if (limit == null) {
126                            if (other.limit != null)
127                                    return false;
128                    } else if (!limit.equals(other.limit))
129                            return false;
130                    if (offset == null) {
131                            if (other.offset != null)
132                                    return false;
133                    } else if (!offset.equals(other.offset))
134                            return false;
135                    if (orderBy == null) {
136                            if (other.orderBy != null)
137                                    return false;
138                    } else if (!orderBy.equals(other.orderBy))
139                            return false;
140                    if (where == null) {
141                            if (other.where != null)
142                                    return false;
143                    } else if (!where.equals(other.where))
144                            return false;
145                    return true;
146            }
147    
148    }