001
014
015 package com.liferay.portal.dao.orm.hibernate;
016
017 import com.liferay.portal.kernel.util.StringBundler;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020
021
026 public class DB2Dialect extends org.hibernate.dialect.DB2Dialect {
027
028 public DB2Dialect() {
029 super();
030
031 registerKeyword("for");
032 registerKeyword("optimize");
033 }
034
035 @Override
036 public String getForUpdateString() {
037 return " for read only with rs use and keep exclusive locks";
038 }
039
040 @Override
041 public String getLimitString(String sql, int offset, int limit) {
042 boolean hasOffset = false;
043
044 if ((offset > 0) || forceLimitUsage()) {
045 hasOffset = true;
046 }
047
048 StringBundler sb = null;
049
050 if (hasOffset) {
051 sb = new StringBundler(11);
052 }
053 else {
054 sb = new StringBundler(5);
055 }
056
057 if (!hasOffset) {
058 addQueryForLimitedRows(sb, sql, limit);
059 addOptimizeForLimitedRows(sb, limit);
060
061 return sb.toString();
062 }
063
064
065
066 sb.append("SELECT outerQuery.* FROM (");
067 sb.append("SELECT ROW_NUMBER() OVER() AS rowNumber_, ");
068
069
070
071 sb.append("innerQuery.* FROM (");
072
073 addQueryForLimitedRows(sb, sql, limit);
074
075 sb.append(") AS innerQuery");
076
077
078
079 sb.append(") AS outerQuery WHERE rowNumber_ > ");
080 sb.append(offset);
081
082 addOptimizeForLimitedRows(sb, limit);
083
084 return sb.toString();
085 }
086
087 @Override
088 public boolean supportsVariableLimit() {
089 return _SUPPORTS_VARIABLE_LIMIT;
090 }
091
092 protected void addOptimizeForLimitedRows(StringBundler sb, int limit) {
093 sb.append(StringPool.SPACE);
094 sb.append(
095 StringUtil.replace(
096 _SQL_OPTIMIZE_FOR_LIMITED_ROWS, "[$LIMIT$]",
097 String.valueOf(limit)));
098 }
099
100 protected void addQueryForLimitedRows(
101 StringBundler sb, String sql, int limit) {
102
103 sb.append(sql);
104 sb.append(StringPool.SPACE);
105 sb.append(
106 StringUtil.replace(
107 _SQL_FETCH_FIRST_LIMITED_ROWS_ONLY, "[$LIMIT$]",
108 String.valueOf(limit)));
109 }
110
111 private static final String _SQL_FETCH_FIRST_LIMITED_ROWS_ONLY =
112 "FETCH FIRST [$LIMIT$] ROWS ONLY";
113
114 private static final String _SQL_OPTIMIZE_FOR_LIMITED_ROWS =
115 "OPTIMIZE FOR [$LIMIT$] ROWS";
116
117 private static final boolean _SUPPORTS_VARIABLE_LIMIT = false;
118
119 }