-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
PostgreSequence.java
304 lines (263 loc) · 11.1 KB
/
PostgreSequence.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2021 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.postgresql.model;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ext.postgresql.PostgreUtils;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBPQualifiedObject;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.meta.IPropertyCacheValidator;
import org.jkiss.dbeaver.model.meta.LazyProperty;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.meta.PropertyGroup;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.DBSEntityType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.rdb.DBSSequence;
import org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* PostgreSequence
*/
public class PostgreSequence extends PostgreTableBase implements DBSSequence, DBPQualifiedObject
{
private static final Log log = Log.getLog(PostgreSequence.class);
public static class AdditionalInfo {
private volatile boolean loaded = false;
private long startValue;
private Long lastValue;
private long minValue;
private long maxValue;
private long incrementBy;
private long cacheValue;
private boolean isCycled;
@Property(viewable = true, editable = true, updatable = true, order = 10)
public Long getLastValue() {
return lastValue;
}
public void setLastValue(long lastValue) {
this.lastValue = lastValue;
}
@Property(viewable = true, editable = true, updatable = true, order = 20)
public long getStartValue() {
return startValue;
}
public void setStartValue(long startValue) {
this.startValue = startValue;
}
@Property(viewable = true, editable = true, updatable = true, order = 21)
public long getMinValue() {
return minValue;
}
public void setMinValue(long minValue) {
this.minValue = minValue;
}
@Property(viewable = true, editable = true, updatable = true, order = 22)
public long getMaxValue() {
return maxValue;
}
public void setMaxValue(long maxValue) {
this.maxValue = maxValue;
}
@Property(viewable = true, editable = true, updatable = true, order = 23)
public long getIncrementBy() {
return incrementBy;
}
public void setIncrementBy(long incrementBy) {
this.incrementBy = incrementBy;
}
@Property(viewable = true, editable = true, updatable = true, order = 24)
public long getCacheValue() {
return cacheValue;
}
public void setCacheValue(long cacheValue) {
this.cacheValue = cacheValue;
}
@Property(viewable = true, editable = true, updatable = true, order = 25)
public boolean isCycled() {
return isCycled;
}
public void setCycled(boolean cycled) {
isCycled = cycled;
}
}
public static class AdditionalInfoValidator implements IPropertyCacheValidator<PostgreSequence> {
@Override
public boolean isPropertyCached(PostgreSequence object, Object propertyId)
{
return object.additionalInfo.loaded;
}
}
private final AdditionalInfo additionalInfo = new AdditionalInfo();
public PostgreSequence(PostgreSchema schema, JDBCResultSet dbResult) {
super(schema, dbResult);
}
public PostgreSequence(PostgreSchema catalog) {
super(catalog);
}
@PropertyGroup()
@LazyProperty(cacheValidator = AdditionalInfoValidator.class)
public AdditionalInfo getAdditionalInfo(DBRProgressMonitor monitor) throws DBCException
{
synchronized (additionalInfo) {
if (!additionalInfo.loaded) {
loadAdditionalInfo(monitor);
}
return additionalInfo;
}
}
private void loadAdditionalInfo(DBRProgressMonitor monitor) {
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load sequence additional info")) {
if (getDataSource().isServerVersionAtLeast(10, 0)) {
try (JDBCPreparedStatement dbSeqStat = session.prepareStatement(
"SELECT * from pg_catalog.pg_sequences WHERE schemaname=? AND sequencename=?")) {
dbSeqStat.setString(1, getSchema().getName());
dbSeqStat.setString(2, getName());
try (JDBCResultSet seqResults = dbSeqStat.executeQuery()) {
if (seqResults.next()) {
additionalInfo.startValue = JDBCUtils.safeGetLong(seqResults, "start_value");
additionalInfo.lastValue = JDBCUtils.safeGetLongNullable(seqResults, "last_value");
additionalInfo.minValue = JDBCUtils.safeGetLong(seqResults, "min_value");
additionalInfo.maxValue = JDBCUtils.safeGetLong(seqResults, "max_value");
additionalInfo.incrementBy = JDBCUtils.safeGetLong(seqResults, "increment_by");
additionalInfo.cacheValue = JDBCUtils.safeGetLong(seqResults, "cache_size");
additionalInfo.isCycled = JDBCUtils.safeGetBoolean(seqResults, "cycle");
}
}
}
} else {
try (JDBCPreparedStatement dbSeqStat = session.prepareStatement(
"SELECT * from " + getFullyQualifiedName(DBPEvaluationContext.DML))) {
try (JDBCResultSet seqResults = dbSeqStat.executeQuery()) {
if (seqResults.next()) {
additionalInfo.startValue = JDBCUtils.safeGetLong(seqResults, "start_value");
additionalInfo.lastValue = JDBCUtils.safeGetLongNullable(seqResults, "last_value");
additionalInfo.minValue = JDBCUtils.safeGetLong(seqResults, "min_value");
additionalInfo.maxValue = JDBCUtils.safeGetLong(seqResults, "max_value");
additionalInfo.incrementBy = JDBCUtils.safeGetLong(seqResults, "increment_by");
additionalInfo.cacheValue = JDBCUtils.safeGetLong(seqResults, "cache_size");
additionalInfo.isCycled = JDBCUtils.safeGetBoolean(seqResults, "is_cycled");
}
}
}
}
additionalInfo.loaded = true;
} catch (Exception e) {
log.warn("Error reading sequence values", e);
}
}
@Override
public Number getLastValue() {
return additionalInfo.lastValue;
}
@Override
public Number getMinValue() {
return additionalInfo.minValue;
}
@Override
public Number getMaxValue() {
return additionalInfo.maxValue;
}
@Override
public Number getIncrementBy() {
return additionalInfo.incrementBy;
}
///////////////////////////////////////////////////////////////////////
// Entity
@NotNull
@Override
public DBSEntityType getEntityType() {
return DBSEntityType.SEQUENCE;
}
@Override
public boolean isView() {
return false;
}
@Override
public Collection<? extends DBSTableIndex> getIndexes(DBRProgressMonitor monitor) throws DBException {
return null;
}
@Override
public void setObjectDefinitionText(String sourceText) throws DBException {
}
@Nullable
@Override
public String[] getRelOptions() {
return null;
}
@Override
public String getObjectDefinitionText(DBRProgressMonitor monitor, Map<String, Object> options) throws DBException {
AdditionalInfo info = getAdditionalInfo(monitor);
StringBuilder sql = new StringBuilder()
.append("-- DROP SEQUENCE ").append(getFullyQualifiedName(DBPEvaluationContext.DDL)).append(";\n\n")
.append("CREATE SEQUENCE ").append(getFullyQualifiedName(DBPEvaluationContext.DDL));
if (info.getIncrementBy() > 0) {
sql.append("\n\tINCREMENT BY ").append(info.getIncrementBy());
}
if (info.getMinValue() > 0) {
sql.append("\n\tMINVALUE ").append(info.getMinValue());
} else {
sql.append("\n\tNO MINVALUE");
}
if (info.getMaxValue() > 0) {
sql.append("\n\tMAXVALUE ").append(info.getMaxValue());
} else {
sql.append("\n\tNO MAXVALUE");
}
if (info.getStartValue() > 0) {
sql.append("\n\tSTART ").append(info.getStartValue());
}
if (info.getCacheValue() > 0) {
sql.append("\n\tCACHE ").append(info.getCacheValue());
sql.append("\n\t").append(info.isCycled ? "" : "NO ").append("CYCLE");
}
sql.append(';');
if (!CommonUtils.isEmpty(getDescription())) {
sql.append("\nCOMMENT ON SEQUENCE ").append(DBUtils.getQuotedIdentifier(this)).append(" IS ")
.append(SQLUtils.quoteString(this, getDescription())).append(";");
}
List<DBEPersistAction> actions = new ArrayList<>();
PostgreUtils.getObjectGrantPermissionActions(monitor, this, actions, options);
if (!actions.isEmpty()) {
sql.append("\n\n");
sql.append(SQLUtils.generateScript(getDataSource(), actions.toArray(new DBEPersistAction[actions.size()]), false));
}
return sql.toString();
}
public String generateChangeOwnerQuery(String owner) {
return "ALTER SEQUENCE " + DBUtils.getObjectFullName(this, DBPEvaluationContext.DDL) + " OWNER TO " + owner;
}
@Override
public DBSObject refreshObject(@NotNull DBRProgressMonitor monitor) throws DBException {
additionalInfo.loaded = false;
return super.refreshObject(monitor);
}
}