001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.lucene.store.jdbc.support;
018:
019: import org.apache.lucene.index.IndexWriter;
020: import org.apache.lucene.store.jdbc.JdbcDirectorySettings;
021: import org.apache.lucene.store.jdbc.dialect.Dialect;
022:
023: /**
024: * An internal representation of a database table used to store the {@link org.apache.lucene.store.jdbc.JdbcDirectory}
025: * settings.
026: *
027: * @author kimchy
028: */
029: public class JdbcTable {
030:
031: private Dialect dialect;
032:
033: private JdbcDirectorySettings settings;
034:
035: private String name;
036:
037: private String schema;
038:
039: private String catalog;
040:
041: private boolean quoted;
042:
043: private boolean schemaQuoted;
044:
045: private String sqlCreate;
046: private String sqlDrop;
047: private String sqlSelectNames;
048: private String sqlSelectNameExists;
049: private String sqlSelecltLastModifiedByName;
050: private String sqlUpdateLastModifiedByName;
051: private String sqlDeleteByName;
052: private String sqlMarkDeleteByName;
053: private String sqlUpdateNameByName;
054: private String sqlSelectSizeByName;
055: private String sqlInsert;
056: private String sqlUpdateSizeLastModifiedByName;
057: private String sqlSelectSizeValueByName;
058: private String sqlDeletaAll;
059: private String sqlDeletaMarkDeleteByDelta;
060: private String sqlSelectNameForUpdateNoWait;
061:
062: private JdbcColumn nameColumn;
063: private JdbcColumn valueColumn;
064: private JdbcColumn sizeColumn;
065: private JdbcColumn lastModifiedColumn;
066: private JdbcColumn deletedColumn;
067:
068: public JdbcTable(JdbcDirectorySettings settings, Dialect dialect,
069: String name) {
070: this (settings, dialect, name, null, null);
071: }
072:
073: public JdbcTable(JdbcDirectorySettings settings, Dialect dialect,
074: String name, String catalog, String schema) {
075: this .dialect = dialect;
076: this .settings = settings;
077: setName(name);
078: setSchema(schema);
079: setCatalog(catalog);
080: nameColumn = new JdbcColumn(dialect, settings
081: .getNameColumnName(), 1, dialect
082: .getVarcharType(settings.getNameColumnLength()));
083: valueColumn = new JdbcColumn(dialect, settings
084: .getValueColumnName(), 2, dialect.getBlobType(settings
085: .getValueColumnLengthInK()));
086: sizeColumn = new JdbcColumn(dialect, settings
087: .getSizeColumnName(), 3, dialect.getNumberType());
088: lastModifiedColumn = new JdbcColumn(dialect, settings
089: .getLastModifiedColumnName(), 4, dialect
090: .getTimestampType());
091: deletedColumn = new JdbcColumn(dialect, settings
092: .getDeletedColumnName(), 5, dialect.getBitType());
093:
094: StringBuffer sb = new StringBuffer();
095:
096: sqlCreate = sb.append("create table ").append(
097: getQualifiedName()).append(" (").append(
098: nameColumn.getName()).append(' ').append(
099: nameColumn.getType()).append(" , ").append(
100: valueColumn.getName()).append(' ').append(
101: valueColumn.getType()).append(" , ").append(
102: sizeColumn.getName()).append(' ').append(
103: sizeColumn.getType()).append(" , ").append(
104: lastModifiedColumn.getName()).append(' ').append(
105: lastModifiedColumn.getType()).append(" , ").append(
106: deletedColumn.getName()).append(' ').append(
107: deletedColumn.getType()).append(", " + "primary key (")
108: .append(nameColumn.getName()).append(") ) ").append(
109: dialect.getTableTypeString()).toString();
110:
111: sb.setLength(0);
112: sb.append("drop table ");
113: if (dialect.supportsIfExistsBeforeTableName())
114: sb.append("if exists ");
115: sb.append(getQualifiedName()).append(
116: dialect.getCascadeConstraintsString());
117: if (dialect.supportsIfExistsAfterTableName())
118: sb.append(" if exists");
119: sqlDrop = sb.toString();
120:
121: sb.setLength(0);
122: sqlSelectNames = sb.append("select ").append(
123: nameColumn.getQuotedName()).append(" from ").append(
124: getQualifiedName()).append(" where ").append(
125: deletedColumn.getQuotedName()).append(" = ?")
126: .toString();
127:
128: sb.setLength(0);
129: sqlSelectNameExists = sb.append("select ").append(
130: deletedColumn.getQuotedName()).append(" from ").append(
131: getQualifiedName()).append(" where ").append(
132: nameColumn.getQuotedName()).append(" = ?").toString();
133:
134: sb.setLength(0);
135: sqlSelecltLastModifiedByName = sb.append("select ").append(
136: lastModifiedColumn.getQuotedName()).append(" from ")
137: .append(getQualifiedName()).append(" where ").append(
138: nameColumn.getQuotedName()).append(" = ?")
139: .toString();
140:
141: sb.setLength(0);
142: sqlUpdateLastModifiedByName = sb.append("update ").append(
143: getQualifiedName()).append(" set ").append(
144: lastModifiedColumn.getQuotedName()).append(" = ")
145: .append(dialect.getCurrentTimestampFunction()).append(
146: " where ").append(nameColumn.getQuotedName())
147: .append(" = ?").toString();
148:
149: sb.setLength(0);
150: sqlDeleteByName = sb.append("delete from ").append(
151: getQualifiedName()).append(" where ").append(
152: nameColumn.getQuotedName()).append(" = ?").toString();
153:
154: sb.setLength(0);
155: sqlDeletaMarkDeleteByDelta = sb.append("delete from ").append(
156: getQualifiedName()).append(" where ").append(
157: deletedColumn.getQuotedName()).append(" = ?").append(
158: " and ").append(lastModifiedColumn.getQuotedName())
159: .append(" < ?").toString();
160:
161: sb.setLength(0);
162: sqlUpdateNameByName = sb.append("update ").append(
163: getQualifiedName()).append(" set ").append(
164: nameColumn.getQuotedName()).append(" = ?" + " where ")
165: .append(nameColumn.getQuotedName()).append(" = ?")
166: .toString();
167:
168: sb.setLength(0);
169: sqlSelectNameForUpdateNoWait = sb.append("select ").append(
170: nameColumn.getQuotedName()).append(" from ").append(
171: getQualifiedName()).append(" where ").append(
172: nameColumn.getQuotedName()).append(" = ?").append(
173: dialect.getForUpdateNowaitString()).toString();
174:
175: sb.setLength(0);
176: sqlSelectSizeByName = sb.append("select ").append(
177: sizeColumn.getQuotedName()).append(" from ").append(
178: getQualifiedName()).append(" where ").append(
179: nameColumn.getQuotedName()).append(" = ?").toString();
180:
181: sb.setLength(0);
182: sqlInsert = sb.append("insert into ")
183: .append(getQualifiedName()).append(" (").append(
184: nameColumn.getQuotedName()).append(", ")
185: .append(valueColumn.getQuotedName()).append(", ")
186: .append(sizeColumn.getQuotedName()).append(", ")
187: .append(lastModifiedColumn.getQuotedName())
188: .append(", ").append(deletedColumn.getQuotedName())
189: .append(") values ( ?, ?, ?, ").append(
190: dialect.getCurrentTimestampFunction()).append(
191: ", ?").append(" )").toString();
192:
193: sb.setLength(0);
194: sqlUpdateSizeLastModifiedByName = sb.append("update ").append(
195: getQualifiedName()).append(" set ").append(
196: sizeColumn.getQuotedName()).append(" = ? , ").append(
197: lastModifiedColumn.getQuotedName()).append(" = ")
198: .append(dialect.getCurrentTimestampFunction()).append(
199: " where ").append(nameColumn.getQuotedName())
200: .append(" = ?").toString();
201:
202: sb.setLength(0);
203: sqlMarkDeleteByName = sb.append("update ").append(
204: getQualifiedName()).append(" set ").append(
205: deletedColumn.getQuotedName()).append(" = ? , ")
206: .append(lastModifiedColumn.getQuotedName()).append(
207: " = ").append(
208: dialect.getCurrentTimestampFunction()).append(
209: " where ").append(nameColumn.getQuotedName())
210: .append(" = ?").toString();
211:
212: sb.setLength(0);
213: sqlSelectSizeValueByName = sb.append("select ").append(
214: nameColumn.getQuotedName()).append(", ").append(
215: dialect.openBlobSelectQuote()).append(
216: valueColumn.getQuotedName()).append(
217: dialect.closeBlobSelectQuote()).append(" as x").append(
218: ", ").append(sizeColumn.getQuotedName()).append(
219: " from ").append(getQualifiedName()).append(" where ")
220: .append(nameColumn.getQuotedName()).append(" = ?")
221: .toString();
222:
223: sb.setLength(0);
224: sqlDeletaAll = sb.append("delete from ").append(
225: getQualifiedName()).append(" where ").append(
226: nameColumn.getQuotedName()).append(" <> '").append(
227: IndexWriter.WRITE_LOCK_NAME).append("'").toString();
228: }
229:
230: public void setName(String name) {
231: if (name.charAt(0) == dialect.openQuote()) {
232: quoted = true;
233: this .name = name.substring(1, name.length() - 1).replace(
234: '-', '_');
235: } else {
236: this .name = name.replace('-', '_');
237: }
238: }
239:
240: public String getName() {
241: return name;
242: }
243:
244: public void setSchema(String schema) {
245: if (schema != null && schema.charAt(0) == dialect.openQuote()) {
246: schemaQuoted = true;
247: this .schema = schema.substring(1, schema.length() - 1);
248: } else {
249: this .schema = schema;
250: }
251: }
252:
253: public String getSchema() {
254: return schema;
255: }
256:
257: public String getCatalog() {
258: return catalog;
259: }
260:
261: public void setCatalog(String catalog) {
262: this .catalog = catalog;
263: }
264:
265: public JdbcColumn getNameColumn() {
266: return this .nameColumn;
267: }
268:
269: public JdbcColumn getSizeColumn() {
270: return this .sizeColumn;
271: }
272:
273: public JdbcColumn getValueColumn() {
274: return this .valueColumn;
275: }
276:
277: public JdbcColumn getLastModifiedColumn() {
278: return this .lastModifiedColumn;
279: }
280:
281: public JdbcColumn getDeletedColumn() {
282: return this .deletedColumn;
283: }
284:
285: public String sqlSelectNames() {
286: return sqlSelectNames;
287: }
288:
289: public String sqlSelectNameExists() {
290: return sqlSelectNameExists;
291: }
292:
293: public String sqlSelecltLastModifiedByName() {
294: return sqlSelecltLastModifiedByName;
295: }
296:
297: public String sqlUpdateLastModifiedByName() {
298: return sqlUpdateLastModifiedByName;
299: }
300:
301: public String sqlDeleteByName() {
302: return sqlDeleteByName;
303: }
304:
305: public String sqlUpdateNameByName() {
306: return sqlUpdateNameByName;
307: }
308:
309: public String sqlSelectSizeByName() {
310: return sqlSelectSizeByName;
311: }
312:
313: public String sqlDeletaMarkDeleteByDelta() {
314: return sqlDeletaMarkDeleteByDelta;
315: }
316:
317: public String sqlInsert() {
318: return sqlInsert;
319: }
320:
321: public String sqlUpdateSizeLastModifiedByName() {
322: return sqlUpdateSizeLastModifiedByName;
323: }
324:
325: public String sqlSelectSizeValueByName() {
326: return sqlSelectSizeValueByName;
327: }
328:
329: public String sqlSelectNameForUpdateNoWait() {
330: return sqlSelectNameForUpdateNoWait;
331: }
332:
333: public String sqlMarkDeleteByName() {
334: return sqlMarkDeleteByName;
335: }
336:
337: public String sqlDeletaAll() {
338: return sqlDeletaAll;
339: }
340:
341: public String sqlCreate() {
342: return sqlCreate;
343: }
344:
345: public String sqlDrop() {
346: return sqlDrop;
347: }
348:
349: public String getQualifiedName() {
350: String quotedName = getQuotedName();
351: return qualify(catalog, getQuotedSchema(), quotedName);
352: }
353:
354: public String getQuotedName() {
355: return quoted ? dialect.openQuote() + name
356: + dialect.closeQuote() : name;
357: }
358:
359: public String getQuotedSchema() {
360: if (schema == null) {
361: return null;
362: }
363: return schemaQuoted ? dialect.openQuote() + schema
364: + dialect.closeQuote() : schema;
365: }
366:
367: public static String qualify(String catalog, String schema,
368: String table) {
369: StringBuffer qualifiedName = new StringBuffer();
370: if (catalog != null) {
371: qualifiedName.append(catalog).append('.');
372: }
373: if (schema != null) {
374: qualifiedName.append(schema).append('.');
375: }
376: return qualifiedName.append(table).toString();
377: }
378:
379: public String toString() {
380: StringBuffer buf = new StringBuffer();
381: if (getCatalog() != null)
382: buf.append(getCatalog()).append(".");
383: if (getSchema() != null)
384: buf.append(getSchema()).append(".");
385: buf.append(getName());
386: return buf.toString();
387: }
388:
389: public JdbcDirectorySettings getSettings() {
390: return settings;
391: }
392:
393: public Dialect getDialect() {
394: return this.dialect;
395: }
396: }
|