2 * Copyright 2000-2018 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.data.util.sqlcontainer.query.generator;
18 import java.util.List;
20 import com.vaadin.v7.data.Container.Filter;
21 import com.vaadin.v7.data.util.sqlcontainer.query.OrderBy;
22 import com.vaadin.v7.data.util.sqlcontainer.query.generator.filter.QueryBuilder;
25 * @deprecated As of 8.0, no replacement available.
27 @SuppressWarnings("serial")
29 public class MSSQLGenerator extends DefaultSQLGenerator {
31 public MSSQLGenerator() {
36 * Construct a MSSQLGenerator with the specified identifiers for start and
37 * end of quoted strings. The identifiers may be different depending on the
38 * database engine and it's settings.
41 * the identifier (character) denoting the start of a quoted
44 * the identifier (character) denoting the end of a quoted string
46 public MSSQLGenerator(String quoteStart, String quoteEnd) {
47 super(quoteStart, quoteEnd);
53 * @see com.vaadin.addon.sqlcontainer.query.generator.DefaultSQLGenerator#
54 * generateSelectQuery(java.lang.String, java.util.List,
55 * com.vaadin.addon.sqlcontainer.query.FilteringMode, java.util.List, int,
56 * int, java.lang.String)
59 public StatementHelper generateSelectQuery(String tableName,
60 List<Filter> filters, List<OrderBy> orderBys, int offset,
61 int pagelength, String toSelect) {
62 if (tableName == null || tableName.trim().equals("")) {
63 throw new IllegalArgumentException("Table name must be given.");
65 /* Adjust offset and page length parameters to match "row numbers" */
66 offset = pagelength > 1 ? ++offset : offset;
67 pagelength = pagelength > 1 ? --pagelength : pagelength;
68 toSelect = toSelect == null ? "*" : toSelect;
69 StatementHelper sh = getStatementHelper();
70 StringBuffer query = new StringBuffer();
72 /* Row count request is handled here */
73 if ("COUNT(*)".equalsIgnoreCase(toSelect)) {
74 query.append(String.format(
75 "SELECT COUNT(*) AS %s FROM (SELECT * FROM %s",
76 QueryBuilder.quote("rowcount"), tableName));
77 if (filters != null && !filters.isEmpty()) {
79 QueryBuilder.getWhereStringForFilters(filters, sh));
81 query.append(") AS t");
82 sh.setQueryString(query.toString());
86 /* SELECT without row number constraints */
87 if (offset == 0 && pagelength == 0) {
88 query.append("SELECT ").append(toSelect).append(" FROM ")
90 if (filters != null) {
92 QueryBuilder.getWhereStringForFilters(filters, sh));
94 if (orderBys != null) {
95 for (OrderBy o : orderBys) {
96 generateOrderBy(query, o, orderBys.indexOf(o) == 0);
99 sh.setQueryString(query.toString());
103 /* Remaining SELECT cases are handled here */
104 query.append("SELECT * FROM (SELECT row_number() OVER (");
105 if (orderBys != null) {
106 for (OrderBy o : orderBys) {
107 generateOrderBy(query, o, orderBys.indexOf(o) == 0);
110 query.append(") AS rownum, " + toSelect + " FROM ").append(tableName);
111 if (filters != null) {
112 query.append(QueryBuilder.getWhereStringForFilters(filters, sh));
114 query.append(") AS a WHERE a.rownum BETWEEN ").append(offset)
115 .append(" AND ").append(offset + pagelength);
116 sh.setQueryString(query.toString());