File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\Set.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import java.text.Collator;
10 import org.h2.command.CommandInterface;
11 import org.h2.command.Prepared;
12 import org.h2.compress.Compressor;
13 import org.h2.constant.ErrorCode;
14 import org.h2.engine.Constants;
15 import org.h2.engine.Database;
16 import org.h2.engine.Mode;
17 import org.h2.engine.Session;
18 import org.h2.engine.Setting;
19 import org.h2.expression.Expression;
20 import org.h2.expression.ValueExpression;
21 import org.h2.message.DbException;
22 import org.h2.result.ResultInterface;
23 import org.h2.schema.Schema;
24 import org.h2.table.Table;
25 import org.h2.tools.CompressTool;
26 import org.h2.util.StringUtils;
27 import org.h2.value.CompareMode;
28 import org.h2.value.ValueInt;
29
30 /**
31 * This class represents the statement
32 * SET
33 */
34 public class Set extends Prepared {
35
36 private final int type;
37 private Expression expression;
38 private String stringValue;
39 private String[] stringValueList;
40
41 public Set(Session session, int type) {
42 super(session);
43 this.type = type;
44 }
45
46 public void setString(String v) {
47 this.stringValue = v;
48 }
49
50 public boolean isTransactional() {
51 switch (type) {
52 case SetTypes.CLUSTER:
53 case SetTypes.VARIABLE:
54 case SetTypes.QUERY_TIMEOUT:
55 case SetTypes.LOCK_TIMEOUT:
56 case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
57 case SetTypes.TRACE_LEVEL_FILE:
58 case SetTypes.THROTTLE:
59 case SetTypes.SCHEMA:
60 case SetTypes.SCHEMA_SEARCH_PATH:
61 return true;
62 default:
63 }
64 return false;
65 }
66
67 public int update() {
68 Database database = session.getDatabase();
69 String name = SetTypes.getTypeName(type);
70 switch (type) {
71 case SetTypes.ALLOW_LITERALS: {
72 session.getUser().checkAdmin();
73 int value = getIntValue();
74 if (value < 0 || value > 2) {
75 throw DbException.getInvalidValueException("ALLOW_LITERALS", getIntValue());
76 }
77 database.setAllowLiterals(value);
78 addOrUpdateSetting(name, null, value);
79 break;
80 }
81 case SetTypes.CACHE_SIZE:
82 if (getIntValue() < 0) {
83 throw DbException.getInvalidValueException("CACHE_SIZE", getIntValue());
84 }
85 session.getUser().checkAdmin();
86 database.setCacheSize(getIntValue());
87 addOrUpdateSetting(name, null, getIntValue());
88 break;
89 case SetTypes.CLUSTER: {
90 if (Constants.CLUSTERING_ENABLED.equals(stringValue)) {
91 // this value is used when connecting
92 // ignore, as the cluster setting is checked later
93 break;
94 }
95 String value = StringUtils.quoteStringSQL(stringValue);
96 if (!value.equals(database.getCluster()) && !value.equals(Constants.CLUSTERING_DISABLED)) {
97 // anybody can disable the cluster
98 // (if he can't access a cluster node)
99 session.getUser().checkAdmin();
100 }
101 database.setCluster(value);
102 // use the system session so that the current transaction
103 // (if any) is not committed
104 addOrUpdateSetting(database.getSystemSession(), name, value, 0);
105 database.getSystemSession().commit(true);
106 break;
107 }
108 case SetTypes.COLLATION: {
109 session.getUser().checkAdmin();
110 Table table = database.getFirstUserTable();
111 if (table != null) {
112 throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
113 }
114 CompareMode compareMode;
115 StringBuilder buff = new StringBuilder(stringValue);
116 if (stringValue.equals(CompareMode.OFF)) {
117 compareMode = CompareMode.getInstance(null, 0);
118 } else {
119 int strength = getIntValue();
120 buff.append(" STRENGTH ");
121 if (strength == Collator.IDENTICAL) {
122 buff.append("IDENTICAL");
123 } else if (strength == Collator.PRIMARY) {
124 buff.append("PRIMARY");
125 } else if (strength == Collator.SECONDARY) {
126 buff.append("SECONDARY");
127 } else if (strength == Collator.TERTIARY) {
128 buff.append("TERTIARY");
129 }
130 compareMode = CompareMode.getInstance(stringValue, strength);
131 }
132 addOrUpdateSetting(name, buff.toString(), 0);
133 database.setCompareMode(compareMode);
134 break;
135 }
136 case SetTypes.COMPRESS_LOB: {
137 session.getUser().checkAdmin();
138 int algo = CompressTool.getCompressAlgorithm(stringValue);
139 database.setLobCompressionAlgorithm(algo == Compressor.NO ? null : stringValue);
140 addOrUpdateSetting(name, stringValue, 0);
141 break;
142 }
143 case SetTypes.CREATE_BUILD: {
144 session.getUser().checkAdmin();
145 if (database.isStarting()) {
146 // just ignore the command if not starting
147 // this avoids problems when running recovery scripts
148 int value = getIntValue();
149 addOrUpdateSetting(name, null, value);
150 }
151 break;
152 }
153 case SetTypes.DATABASE_EVENT_LISTENER: {
154 session.getUser().checkAdmin();
155 database.setEventListenerClass(stringValue);
156 break;
157 }
158 case SetTypes.DB_CLOSE_DELAY: {
159 int x = getIntValue();
160 if (x == -1) {
161 // -1 is a special value for in-memory databases,
162 // which means "keep the DB alive and use the same DB for all connections"
163 } else if (x < 0) {
164 throw DbException.getInvalidValueException("DB_CLOSE_DELAY", x);
165 }
166 session.getUser().checkAdmin();
167 database.setCloseDelay(getIntValue());
168 addOrUpdateSetting(name, null, getIntValue());
169 break;
170 }
171 case SetTypes.DEFAULT_LOCK_TIMEOUT:
172 if (getIntValue() < 0) {
173 throw DbException.getInvalidValueException("DEFAULT_LOCK_TIMEOUT", getIntValue());
174 }
175 session.getUser().checkAdmin();
176 addOrUpdateSetting(name, null, getIntValue());
177 break;
178 case SetTypes.DEFAULT_TABLE_TYPE:
179 session.getUser().checkAdmin();
180 database.setDefaultTableType(getIntValue());
181 addOrUpdateSetting(name, null, getIntValue());
182 break;
183 case SetTypes.EXCLUSIVE: {
184 session.getUser().checkAdmin();
185 int value = getIntValue();
186 switch (value) {
187 case 0:
188 database.setExclusiveSession(null, false);
189 break;
190 case 1:
191 database.setExclusiveSession(session, false);
192 break;
193 case 2:
194 database.setExclusiveSession(session, true);
195 break;
196 default:
197 throw DbException.getInvalidValueException("EXCLUSIVE", value);
198 }
199 break;
200 }
201 case SetTypes.IGNORECASE:
202 session.getUser().checkAdmin();
203 database.setIgnoreCase(getIntValue() == 1);
204 addOrUpdateSetting(name, null, getIntValue());
205 break;
206 case SetTypes.LOCK_MODE:
207 session.getUser().checkAdmin();
208 database.setLockMode(getIntValue());
209 addOrUpdateSetting(name, null, getIntValue());
210 break;
211 case SetTypes.LOCK_TIMEOUT:
212 if (getIntValue() < 0) {
213 throw DbException.getInvalidValueException("LOCK_TIMEOUT", getIntValue());
214 }
215 session.setLockTimeout(getIntValue());
216 break;
217 case SetTypes.LOG: {
218 int value = getIntValue();
219 if (database.isPersistent() && value != database.getLogMode()) {
220 session.getUser().checkAdmin();
221 database.setLogMode(value);
222 }
223 break;
224 }
225 case SetTypes.MAX_LENGTH_INPLACE_LOB: {
226 if (getIntValue() < 0) {
227 throw DbException.getInvalidValueException("MAX_LENGTH_INPLACE_LOB", getIntValue());
228 }
229 session.getUser().checkAdmin();
230 database.setMaxLengthInplaceLob(getIntValue());
231 addOrUpdateSetting(name, null, getIntValue());
232 break;
233 }
234 case SetTypes.MAX_LOG_SIZE:
235 if (getIntValue() < 0) {
236 throw DbException.getInvalidValueException("MAX_LOG_SIZE", getIntValue());
237 }
238 session.getUser().checkAdmin();
239 database.setMaxLogSize((long) getIntValue() * 1024 * 1024);
240 addOrUpdateSetting(name, null, getIntValue());
241 break;
242 case SetTypes.MAX_MEMORY_ROWS: {
243 if (getIntValue() < 0) {
244 throw DbException.getInvalidValueException("MAX_MEMORY_ROWS", getIntValue());
245 }
246 session.getUser().checkAdmin();
247 database.setMaxMemoryRows(getIntValue());
248 addOrUpdateSetting(name, null, getIntValue());
249 break;
250 }
251 case SetTypes.MAX_MEMORY_UNDO: {
252 if (getIntValue() < 0) {
253 throw DbException.getInvalidValueException("MAX_MEMORY_UNDO", getIntValue());
254 }
255 session.getUser().checkAdmin();
256 database.setMaxMemoryUndo(getIntValue());
257 addOrUpdateSetting(name, null, getIntValue());
258 break;
259 }
260 case SetTypes.MAX_OPERATION_MEMORY: {
261 if (getIntValue() < 0) {
262 throw DbException.getInvalidValueException("MAX_OPERATION_MEMORY", getIntValue());
263 }
264 session.getUser().checkAdmin();
265 int value = getIntValue();
266 database.setMaxOperationMemory(value);
267 break;
268 }
269 case SetTypes.MODE:
270 Mode mode = Mode.getInstance(stringValue);
271 if (mode == null) {
272 throw DbException.get(ErrorCode.UNKNOWN_MODE_1, stringValue);
273 }
274 if (database.getMode() != mode) {
275 session.getUser().checkAdmin();
276 database.setMode(mode);
277 }
278 break;
279 case SetTypes.MULTI_THREADED: {
280 session.getUser().checkAdmin();
281 database.setMultiThreaded(getIntValue() == 1);
282 break;
283 }
284 case SetTypes.MVCC: {
285 if (database.isMultiVersion() != (getIntValue() == 1)) {
286 throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, "MVCC");
287 }
288 break;
289 }
290 case SetTypes.OPTIMIZE_REUSE_RESULTS: {
291 session.getUser().checkAdmin();
292 database.setOptimizeReuseResults(getIntValue() != 0);
293 break;
294 }
295 case SetTypes.QUERY_TIMEOUT: {
296 if (getIntValue() < 0) {
297 throw DbException.getInvalidValueException("QUERY_TIMEOUT", getIntValue());
298 }
299 int value = getIntValue();
300 session.setQueryTimeout(value);
301 break;
302 }
303 case SetTypes.REDO_LOG_BINARY: {
304 int value = getIntValue();
305 session.setRedoLogBinary(value == 1);
306 break;
307 }
308 case SetTypes.REFERENTIAL_INTEGRITY: {
309 session.getUser().checkAdmin();
310 int value = getIntValue();
311 if (value < 0 || value > 1) {
312 throw DbException.getInvalidValueException("REFERENTIAL_INTEGRITY", getIntValue());
313 }
314 database.setReferentialIntegrity(value == 1);
315 break;
316 }
317 case SetTypes.SCHEMA: {
318 Schema schema = database.getSchema(stringValue);
319 session.setCurrentSchema(schema);
320 break;
321 }
322 case SetTypes.SCHEMA_SEARCH_PATH: {
323 session.setSchemaSearchPath(stringValueList);
324 break;
325 }
326 case SetTypes.TRACE_LEVEL_FILE:
327 session.getUser().checkAdmin();
328 if (getCurrentObjectId() == 0) {
329 // don't set the property when opening the database
330 // this is for compatibility with older versions, because
331 // this setting was persistent
332 database.getTraceSystem().setLevelFile(getIntValue());
333 }
334 break;
335 case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
336 session.getUser().checkAdmin();
337 if (getCurrentObjectId() == 0) {
338 // don't set the property when opening the database
339 // this is for compatibility with older versions, because
340 // this setting was persistent
341 database.getTraceSystem().setLevelSystemOut(getIntValue());
342 }
343 break;
344 case SetTypes.TRACE_MAX_FILE_SIZE: {
345 if (getIntValue() < 0) {
346 throw DbException.getInvalidValueException("TRACE_MAX_FILE_SIZE", getIntValue());
347 }
348 session.getUser().checkAdmin();
349 int size = getIntValue() * 1024 * 1024;
350 database.getTraceSystem().setMaxFileSize(size);
351 addOrUpdateSetting(name, null, getIntValue());
352 break;
353 }
354 case SetTypes.THROTTLE: {
355 if (getIntValue() < 0) {
356 throw DbException.getInvalidValueException("THROTTLE", getIntValue());
357 }
358 session.setThrottle(getIntValue());
359 break;
360 }
361 case SetTypes.UNDO_LOG: {
362 int value = getIntValue();
363 if (value < 0 || value > 1) {
364 throw DbException.getInvalidValueException("UNDO_LOG", getIntValue());
365 }
366 session.setUndoLogEnabled(value == 1);
367 break;
368 }
369 case SetTypes.VARIABLE: {
370 Expression expr = expression.optimize(session);
371 session.setVariable(stringValue, expr.getValue(session));
372 break;
373 }
374 case SetTypes.WRITE_DELAY: {
375 if (getIntValue() < 0) {
376 throw DbException.getInvalidValueException("WRITE_DELAY", getIntValue());
377 }
378 session.getUser().checkAdmin();
379 database.setWriteDelay(getIntValue());
380 addOrUpdateSetting(name, null, getIntValue());
381 break;
382 }
383 default:
384 DbException.throwInternalError("type="+type);
385 }
386 // the meta data information has changed
387 database.getNextModificationDataId();
388 // query caches might be affected as well, for example
389 // when changing the compatibility mode
390 database.getNextModificationMetaId();
391 return 0;
392 }
393
394 private int getIntValue() {
395 expression = expression.optimize(session);
396 return expression.getValue(session).getInt();
397 }
398
399 public void setInt(int value) {
400 this.expression = ValueExpression.get(ValueInt.get(value));
401 }
402
403 public void setExpression(Expression expression) {
404 this.expression = expression;
405 }
406
407 private void addOrUpdateSetting(String name, String s, int v) {
408 addOrUpdateSetting(session, name, s, v);
409 }
410
411 private void addOrUpdateSetting(Session session, String name, String s, int v) {
412 Database database = session.getDatabase();
413 if (database.isReadOnly()) {
414 return;
415 }
416 Setting setting = database.findSetting(name);
417 boolean addNew = false;
418 if (setting == null) {
419 addNew = true;
420 int id = getObjectId();
421 setting = new Setting(database, id, name);
422 }
423 if (s != null) {
424 if (!addNew && setting.getStringValue().equals(s)) {
425 return;
426 }
427 setting.setStringValue(s);
428 } else {
429 if (!addNew && setting.getIntValue() == v) {
430 return;
431 }
432 setting.setIntValue(v);
433 }
434 if (addNew) {
435 database.addDatabaseObject(session, setting);
436 } else {
437 database.update(session, setting);
438 }
439 }
440
441 public boolean needRecompile() {
442 return false;
443 }
444
445 public ResultInterface queryMeta() {
446 return null;
447 }
448
449 public void setStringArray(String[] list) {
450 this.stringValueList = list;
451 }
452
453 public int getType() {
454 return CommandInterface.SET;
455 }
456
457 }
458