001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.storage.sql.jdbc.db;
021
022import java.io.Serializable;
023
024/**
025 * A {@code SELECT} statement.
026 *
027 * @author Florent Guillaume
028 */
029public class Select implements Serializable {
030
031    private static final long serialVersionUID = 1L;
032
033    private String with;
034
035    private String what;
036
037    private String from;
038
039    private String where;
040
041    private String groupBy;
042
043    private String orderBy;
044
045    public Select(Table table) {
046        // table unused
047    }
048
049    public void setWith(String with) {
050        this.with = with;
051    }
052
053    public void setWhat(String what) {
054        this.what = what;
055    }
056
057    public String getWhat() {
058        return what;
059    }
060
061    public void setFrom(String from) {
062        this.from = from;
063    }
064
065    public String getFrom() {
066        return from;
067    }
068
069    public void setWhere(String where) {
070        this.where = where;
071    }
072
073    public String getWhere() {
074        return where;
075    }
076
077    public void setGroupBy(String groupBy) {
078        this.groupBy = groupBy;
079    }
080
081    public void setOrderBy(String orderBy) {
082        this.orderBy = orderBy;
083    }
084
085    public String getStatement() {
086        StringBuilder buf = new StringBuilder(128);
087        if (with != null && with.length() != 0) {
088            buf.append("WITH ");
089            buf.append(with);
090            buf.append(' ');
091        }
092        buf.append("SELECT ");
093        buf.append(what);
094        buf.append(" FROM ");
095        buf.append(from);
096        if (where != null && where.length() != 0) {
097            buf.append(" WHERE ");
098            buf.append(where);
099        }
100        if (groupBy != null && groupBy.length() != 0) {
101            buf.append(" GROUP BY ");
102            buf.append(groupBy);
103        }
104        if (orderBy != null && orderBy.length() != 0) {
105            buf.append(" ORDER BY ");
106            buf.append(orderBy);
107        }
108        // ... "for update" in some cases, see dialect.getForUpdateString and
109        // lock modes
110        return buf.toString();
111    }
112
113}