KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > dml > Set


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.dml;
6
7 import java.sql.SQLException JavaDoc;
8 import java.text.Collator JavaDoc;
9
10 import org.h2.command.Prepared;
11 import org.h2.compress.Compresser;
12 import org.h2.engine.Constants;
13 import org.h2.engine.Database;
14 import org.h2.engine.DbObject;
15 import org.h2.engine.Mode;
16 import org.h2.engine.Session;
17 import org.h2.engine.Setting;
18 import org.h2.expression.Expression;
19 import org.h2.expression.ValueExpression;
20 import org.h2.message.Message;
21 import org.h2.schema.Schema;
22 import org.h2.table.Table;
23 import org.h2.tools.CompressTool;
24 import org.h2.util.ObjectArray;
25 import org.h2.util.StringUtils;
26 import org.h2.value.CompareMode;
27 import org.h2.value.ValueInt;
28
29 /**
30  * @author Thomas
31  */

32
33 public class Set extends Prepared {
34
35     private int type;
36     private Expression expression;
37     private String JavaDoc stringValue;
38
39     public Set(Session session, int type) {
40         super(session);
41         this.type = type;
42     }
43
44     public void setString(String JavaDoc v) {
45         this.stringValue = v;
46     }
47
48     public boolean isTransactional() {
49         return false;
50     }
51
52     public int update() throws SQLException JavaDoc {
53         // Value v = expr.getValue();
54
Database database = session.getDatabase();
55         String JavaDoc name = SetTypes.getTypeName(type);
56         switch (type) {
57         case SetTypes.MAX_LOG_SIZE:
58             session.getUser().checkAdmin();
59             session.getDatabase().setMaxLogSize((long)getIntValue() * 1024 * 1024);
60             addOrUpdateSetting(name, null, getIntValue());
61             break;
62         case SetTypes.LOCK_TIMEOUT:
63             session.setLockTimeout(getIntValue());
64             break;
65         case SetTypes.LOCK_MODE:
66             session.getUser().checkAdmin();
67             database.setLockMode(getIntValue());
68             addOrUpdateSetting(name, null, getIntValue());
69             break;
70         case SetTypes.DEFAULT_LOCK_TIMEOUT:
71             session.getUser().checkAdmin();
72             addOrUpdateSetting(name, null, getIntValue());
73             break;
74         case SetTypes.DEFAULT_TABLE_TYPE:
75             session.getUser().checkAdmin();
76             addOrUpdateSetting(name, null, getIntValue());
77             break;
78         case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
79             session.getUser().checkAdmin();
80             database.getTraceSystem().setLevelSystemOut(getIntValue());
81             addOrUpdateSetting(name, null, getIntValue());
82             break;
83         case SetTypes.TRACE_LEVEL_FILE:
84             session.getUser().checkAdmin();
85             database.getTraceSystem().setLevelFile(getIntValue());
86             addOrUpdateSetting(name, null, getIntValue());
87             break;
88         case SetTypes.TRACE_MAX_FILE_SIZE: {
89             session.getUser().checkAdmin();
90             int size = getIntValue() * 1024 * 1024;
91             database.getTraceSystem().setMaxFileSize(size);
92             addOrUpdateSetting(name, null, getIntValue());
93             break;
94         }
95         case SetTypes.CACHE_SIZE:
96             session.getUser().checkAdmin();
97             database.setCacheSize(getIntValue());
98             addOrUpdateSetting(name, null, getIntValue());
99             break;
100         case SetTypes.MODE:
101             session.getUser().checkAdmin();
102             Mode mode = Mode.getMode(stringValue);
103             if(mode == null) {
104                 throw Message.getSQLException(Message.UNKNOWN_MODE_1, stringValue);
105             }
106             Mode.setCurrentMode(mode);
107             break;
108         case SetTypes.COLLATION: {
109             session.getUser().checkAdmin();
110             ObjectArray array = database.getAllSchemaObjects(DbObject.TABLE_OR_VIEW);
111             for(int i=0; i<array.size(); i++) {
112                 Table table = (Table) array.get(i);
113                 if(table.getCreateSQL() != null) {
114                     throw Message.getSQLException(Message.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
115                 }
116             }
117             CompareMode compareMode;
118             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(stringValue);
119             if(stringValue.equals(CompareMode.OFF)) {
120                 compareMode = new CompareMode(null, null);
121             } else {
122                 Collator JavaDoc coll = CompareMode.getCollator(stringValue);
123                 compareMode = new CompareMode(coll, stringValue);
124                 buff.append(" STRENGTH ");
125                 if(getIntValue() == Collator.IDENTICAL) {
126                     buff.append("IDENTICAL");
127                 } else if(getIntValue() == Collator.PRIMARY) {
128                     buff.append("PRIMARY");
129                 } else if(getIntValue() == Collator.SECONDARY) {
130                     buff.append("SECONDARY");
131                 } else if(getIntValue() == Collator.TERTIARY) {
132                     buff.append("TERTIARY");
133                 }
134                 coll.setStrength(getIntValue());
135             }
136             addOrUpdateSetting(name, buff.toString(), 0);
137             database.setCompareMode(compareMode);
138             break;
139         }
140         case SetTypes.IGNORECASE:
141             session.getUser().checkAdmin();
142             session.getDatabase().setIgnoreCase(getIntValue() == 1);
143             addOrUpdateSetting(name, null, getIntValue());
144             break;
145         case SetTypes.CLUSTER: {
146             session.getUser().checkAdmin();
147             database.setCluster(StringUtils.quoteStringSQL(stringValue));
148             addOrUpdateSetting(name, StringUtils.quoteStringSQL(stringValue), 0);
149             break;
150         }
151         case SetTypes.WRITE_DELAY: {
152             session.getUser().checkAdmin();
153             database.setWriteDelay(getIntValue());
154             addOrUpdateSetting(name, null, getIntValue());
155             break;
156         }
157         case SetTypes.DATABASE_EVENT_LISTENER: {
158             session.getUser().checkAdmin();
159             database.setEventListener(stringValue);
160             break;
161         }
162         case SetTypes.MAX_MEMORY_ROWS: {
163             session.getUser().checkAdmin();
164             database.setMaxMemoryRows(getIntValue());
165             addOrUpdateSetting(name, null, getIntValue());
166             break;
167         }
168         case SetTypes.ASSERT: {
169             session.getUser().checkAdmin();
170             Constants.CHECK = (getIntValue() == 1);
171             break;
172         }
173         case SetTypes.MULTI_THREADED: {
174             session.getUser().checkAdmin();
175             Constants.MULTI_THREADED_KERNEL = (getIntValue() == 1);
176             break;
177         }
178         case SetTypes.DB_CLOSE_DELAY: {
179             session.getUser().checkAdmin();
180             database.setCloseDelay(getIntValue());
181             addOrUpdateSetting(name, null, getIntValue());
182             break;
183         }
184         case SetTypes.LOG: {
185             int value = getIntValue();
186             if(value<0 || value>2) {
187                 throw Message.getInvalidValueException(""+getIntValue(), "LOG");
188             }
189             if(value==0) {
190                 session.getUser().checkAdmin();
191             }
192             database.setLog(value);
193             break;
194         }
195         case SetTypes.THROTTLE: {
196             if(getIntValue() < 0) {
197                 throw Message.getInvalidValueException(""+getIntValue(), "THROTTLE");
198             }
199             session.setThrottle(getIntValue());
200             break;
201         }
202         case SetTypes.MAX_MEMORY_UNDO: {
203             if(getIntValue() < 0) {
204                 throw Message.getInvalidValueException(""+getIntValue(), "MAX_MEMORY_UNDO");
205             }
206             session.getUser().checkAdmin();
207             database.setMaxMemoryUndo(getIntValue());
208             addOrUpdateSetting(name, null, getIntValue());
209             break;
210         }
211         case SetTypes.MAX_LENGTH_INPLACE_LOB: {
212             if(getIntValue() < 0) {
213                 throw Message.getInvalidValueException(""+getIntValue(), "MAX_LENGTH_INPLACE_LOB");
214             }
215             session.getUser().checkAdmin();
216             database.setMaxLengthInplaceLob(getIntValue());
217             addOrUpdateSetting(name, null, getIntValue());
218             break;
219         }
220         case SetTypes.COMPRESS_LOB: {
221             session.getUser().checkAdmin();
222             int algo = CompressTool.getInstance().getCompressAlgorithm(stringValue);
223             database.setLobCompressionAlgorithm(algo == Compresser.NO ? null : stringValue);
224             addOrUpdateSetting(name, stringValue, 0);
225             break;
226         }
227         case SetTypes.ALLOW_LITERALS: {
228             session.getUser().checkAdmin();
229             int value = getIntValue();
230             if(value < 0 || value > 2) {
231                 throw Message.getInvalidValueException(""+getIntValue(), "ALLOW_LITERALS");
232             }
233             database.setAllowLiterals(value);
234             addOrUpdateSetting(name, null, value);
235             break;
236         }
237         case SetTypes.SCHEMA: {
238             Schema schema = database.getSchema(stringValue);
239             session.setCurrentSchema(schema);
240             break;
241         }
242         case SetTypes.OPTIMIZE_REUSE_RESULTS: {
243             session.getUser().checkAdmin();
244             database.setOptimizeReuseResults(getIntValue() != 0);
245             break;
246         }
247         default:
248             throw Message.getInternalError("type="+type);
249         }
250         return 0;
251     }
252
253     private int getIntValue() throws SQLException JavaDoc {
254         expression = expression.optimize(session);
255         return expression.getValue(session).getInt();
256     }
257
258     public void setInt(int value) {
259         this.expression = ValueExpression.get(ValueInt.get(value));
260     }
261
262     public void setExpression(Expression expression) {
263         this.expression = expression;
264     }
265
266     private void addOrUpdateSetting(String JavaDoc name, String JavaDoc s, int v) throws SQLException JavaDoc {
267         Database database = session.getDatabase();
268         if(database.getReadOnly()) {
269             return;
270         }
271         Setting setting = database.findSetting(name);
272         boolean addNew = false;
273         if(setting == null) {
274             addNew = true;
275             int id = getObjectId(false, true);
276             setting = new Setting(database, id, name);
277         }
278         if(s != null) {
279             if(!addNew && setting.getStringValue().equals(s)) {
280                 return;
281             }
282             setting.setStringValue(s);
283         } else {
284             if(!addNew && setting.getIntValue() == v) {
285                 return;
286             }
287             setting.setIntValue(v);
288         }
289         if(addNew) {
290             database.addDatabaseObject(session, setting);
291         } else {
292             database.update(session, setting);
293         }
294     }
295
296     public boolean needRecompile() {
297         return false;
298     }
299
300 }
301
Popular Tags