001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.storage.sql.jdbc.db;
014
015import java.io.Serializable;
016
017/**
018 * A {@code SELECT} statement.
019 *
020 * @author Florent Guillaume
021 */
022public class Select implements Serializable {
023
024    private static final long serialVersionUID = 1L;
025
026    private String with;
027
028    private String what;
029
030    private String from;
031
032    private String where;
033
034    private String groupBy;
035
036    private String orderBy;
037
038    public Select(Table table) {
039        // table unused
040    }
041
042    public void setWith(String with) {
043        this.with = with;
044    }
045
046    public void setWhat(String what) {
047        this.what = what;
048    }
049
050    public String getWhat() {
051        return what;
052    }
053
054    public void setFrom(String from) {
055        this.from = from;
056    }
057
058    public String getFrom() {
059        return from;
060    }
061
062    public void setWhere(String where) {
063        this.where = where;
064    }
065
066    public String getWhere() {
067        return where;
068    }
069
070    public void setGroupBy(String groupBy) {
071        this.groupBy = groupBy;
072    }
073
074    public void setOrderBy(String orderBy) {
075        this.orderBy = orderBy;
076    }
077
078    public String getStatement() {
079        StringBuilder buf = new StringBuilder(128);
080        if (with != null && with.length() != 0) {
081            buf.append("WITH ");
082            buf.append(with);
083            buf.append(' ');
084        }
085        buf.append("SELECT ");
086        buf.append(what);
087        buf.append(" FROM ");
088        buf.append(from);
089        if (where != null && where.length() != 0) {
090            buf.append(" WHERE ");
091            buf.append(where);
092        }
093        if (groupBy != null && groupBy.length() != 0) {
094            buf.append(" GROUP BY ");
095            buf.append(groupBy);
096        }
097        if (orderBy != null && orderBy.length() != 0) {
098            buf.append(" ORDER BY ");
099            buf.append(orderBy);
100        }
101        // ... "for update" in some cases, see dialect.getForUpdateString and
102        // lock modes
103        return buf.toString();
104    }
105
106}