01: package org.apache.ojb.broker.accesslayer.sql;
02:
03: /* Copyright 2004-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import org.apache.ojb.broker.OJBRuntimeException;
19: import org.apache.ojb.broker.metadata.ClassDescriptor;
20: import org.apache.ojb.broker.metadata.FieldDescriptor;
21: import org.apache.ojb.broker.util.logging.Logger;
22:
23: /**
24: * Generate a select to check existence of an object.
25: * Something like "SELECT id_1 FROM myTable where id_1 = 123 and id_2 = 'kjngzt'".
26: *
27: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
28: * @version $Id: SqlExistStatement.java,v 1.4.2.2 2005/12/21 22:23:44 tomdz Exp $
29: */
30: public class SqlExistStatement extends SqlPkStatement {
31: private static final String SELECT = "SELECT ";
32: private static final String FROM = " FROM ";
33:
34: private String sql;
35:
36: public SqlExistStatement(ClassDescriptor aCld, Logger aLogger) {
37: super (aCld, aLogger);
38: }
39:
40: /** Return SELECT clause for object existence call */
41: public String getStatement() {
42: if (sql == null) {
43: StringBuffer stmt = new StringBuffer(128);
44: ClassDescriptor cld = getClassDescriptor();
45:
46: FieldDescriptor[] fieldDescriptors = cld.getPkFields();
47: if (fieldDescriptors == null
48: || fieldDescriptors.length == 0) {
49: throw new OJBRuntimeException(
50: "No PK fields defined in metadata for "
51: + cld.getClassNameOfObject());
52: }
53: FieldDescriptor field = fieldDescriptors[0];
54:
55: stmt.append(SELECT);
56: stmt.append(field.getColumnName());
57: stmt.append(FROM);
58: stmt.append(cld.getFullTableName());
59: appendWhereClause(cld, false, stmt);
60:
61: sql = stmt.toString();
62: }
63: return sql;
64: }
65: }
|