1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  
21  import org.hibernate.dialect.SQLServerDialect;
22  
23  /**
24   * <a href="SQLServer2008Dialect.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Steven Cao
27   */
28  public class SQLServer2008Dialect extends SQLServerDialect {
29  
30      public String getLimitString(String sql, int offset, int limit) {
31          String lowerCaseSql = sql.toLowerCase();
32  
33          int lastOrderByPos = lowerCaseSql.lastIndexOf("order by");
34  
35          if ((lastOrderByPos < 0) || (offset == 0) ||
36              StringUtil.endsWith(sql, StringPool.CLOSE_PARENTHESIS)) {
37  
38              return super.getLimitString(sql, 0, limit);
39          }
40          else {
41              int fromPos = lowerCaseSql.indexOf("from");
42  
43              String orderBy = sql.substring(lastOrderByPos, sql.length());
44  
45              String selectFrom = sql.substring(0, fromPos);
46  
47              String selectFromWhere = sql.substring(fromPos, lastOrderByPos);
48  
49              StringBundler sb = new StringBundler(10);
50  
51              sb.append("select * from (");
52              sb.append(selectFrom);
53              sb.append(", _page_row_num = row_number() over(");
54              sb.append(orderBy);
55              sb.append(")");
56              sb.append(selectFromWhere);
57              sb.append(" ) temp where _page_row_num between ");
58              sb.append(offset + 1);
59              sb.append(" and ");
60              sb.append(limit);
61  
62              return sb.toString();
63          }
64      }
65  
66      public boolean supportsLimitOffset() {
67          return _SUPPORTS_LIMIT_OFFSET;
68      }
69  
70      private static final boolean _SUPPORTS_LIMIT_OFFSET = true;
71  
72  }