/
Ingres9Dialect.java
206 lines (174 loc) · 5.42 KB
/
Ingres9Dialect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.dialect;
import java.sql.Types;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.dialect.identity.IdentityColumnSupport;
import org.hibernate.dialect.identity.Ingres9IdentityColumnSupport;
import org.hibernate.dialect.pagination.AbstractLimitHandler;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.LimitHelper;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.type.StandardBasicTypes;
/**
* A SQL dialect for Ingres 9.3 and later versions.
* <p/>
* Changes:
* <ul>
* <li>Support for the SQL functions current_time, current_timestamp and current_date added</li>
* <li>Type mapping of <code>Types.TIMESTAMP</code> changed from "timestamp with time zone" to "timestamp(9) with time zone"</li>
* <li>Improved handling of "SELECT...FOR UPDATE" statements</li>
* <li>Added support for pooled sequences</li>
* <li>Added support for SELECT queries with limit and offset</li>
* <li>Added getIdentitySelectString</li>
* <li>Modified concatination operator</li>
* </ul>
*
* @author Enrico Schenk
* @author Raymond Fan
*/
public class Ingres9Dialect extends IngresDialect {
private static final LimitHandler LIMIT_HANDLER = new AbstractLimitHandler() {
@Override
public String processSql(String sql, RowSelection selection) {
final String soff = " offset " + selection.getFirstRow();
final String slim = " fetch first " + getMaxOrLimit( selection ) + " rows only";
final StringBuilder sb = new StringBuilder( sql.length() + soff.length() + slim.length() )
.append( sql );
if (LimitHelper.hasFirstRow( selection )) {
sb.append( soff );
}
if (LimitHelper.hasMaxRows( selection )) {
sb.append( slim );
}
return sb.toString();
}
@Override
public boolean supportsLimit() {
return true;
}
@Override
public boolean supportsVariableLimit() {
return false;
}
};
/**
* Constructs a Ingres9Dialect
*/
public Ingres9Dialect() {
super();
registerDateTimeFunctions();
registerDateTimeColumnTypes();
registerFunction( "concat", new VarArgsSQLFunction( StandardBasicTypes.STRING, "(", "||", ")" ) );
}
/**
* Register functions current_time, current_timestamp, current_date
*/
protected void registerDateTimeFunctions() {
registerFunction( "current_time", new NoArgSQLFunction( "current_time", StandardBasicTypes.TIME, false ) );
registerFunction(
"current_timestamp", new NoArgSQLFunction(
"current_timestamp",
StandardBasicTypes.TIMESTAMP,
false
)
);
registerFunction( "current_date", new NoArgSQLFunction( "current_date", StandardBasicTypes.DATE, false ) );
}
/**
* Register column types date, time, timestamp
*/
protected void registerDateTimeColumnTypes() {
registerColumnType( Types.DATE, "ansidate" );
registerColumnType( Types.TIMESTAMP, "timestamp(9) with time zone" );
}
// lock acquisition support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public boolean supportsOuterJoinForUpdate() {
return false;
}
@Override
public boolean forUpdateOfColumns() {
return true;
}
// SEQUENCE support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public String getQuerySequencesString() {
return "select seq_name from iisequences";
}
@Override
public boolean supportsPooledSequences() {
return true;
}
// current timestamp support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public boolean isCurrentTimestampSelectStringCallable() {
return false;
}
@Override
public boolean supportsCurrentTimestampSelection() {
return true;
}
@Override
public String getCurrentTimestampSelectString() {
return "select current_timestamp";
}
@Override
public String getCurrentTimestampSQLFunctionName() {
return "current_timestamp";
}
// union subclass support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public boolean supportsUnionAll() {
return true;
}
// Informational metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public boolean doesReadCommittedCauseWritersToBlockReaders() {
return true;
}
@Override
public boolean doesRepeatableReadCauseReadersToBlockWriters() {
return true;
}
// limit/offset support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public LimitHandler getDefaultLimitHandler() {
return LIMIT_HANDLER;
}
@Override
public boolean supportsLimitOffset() {
return true;
}
@Override
public boolean supportsVariableLimit() {
return false;
}
@Override
public boolean useMaxForLimit() {
return false;
}
@Override
public String getLimitString(String querySelect, int offset, int limit) {
final StringBuilder soff = new StringBuilder( " offset " + offset );
final StringBuilder slim = new StringBuilder( " fetch first " + limit + " rows only" );
final StringBuilder sb = new StringBuilder( querySelect.length() + soff.length() + slim.length() )
.append( querySelect );
if ( offset > 0 ) {
sb.append( soff );
}
if ( limit > 0 ) {
sb.append( slim );
}
return sb.toString();
}
@Override
public IdentityColumnSupport getIdentityColumnSupport() {
return new Ingres9IdentityColumnSupport();
}
}