001 package org.apache.ojb.broker.accesslayer.sql;
002
003 /* Copyright 2004-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import org.apache.ojb.broker.OJBRuntimeException;
019 import org.apache.ojb.broker.metadata.ClassDescriptor;
020 import org.apache.ojb.broker.metadata.FieldDescriptor;
021 import org.apache.ojb.broker.util.logging.Logger;
022
023 /**
024 * Generate a select to check existence of an object.
025 * Something like "SELECT id_1 FROM myTable where id_1 = 123 and id_2 = 'kjngzt'".
026 *
027 * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
028 * @version $Id: SqlExistStatement.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
029 */
030 public class SqlExistStatement extends SqlPkStatement
031 {
032 private static final String SELECT = "SELECT ";
033 private static final String FROM = " FROM ";
034
035 private String sql;
036
037 public SqlExistStatement(ClassDescriptor aCld, Logger aLogger)
038 {
039 super(aCld, aLogger);
040 }
041
042 /** Return SELECT clause for object existence call */
043 public String getStatement()
044 {
045 if(sql == null)
046 {
047 StringBuffer stmt = new StringBuffer(128);
048 ClassDescriptor cld = getClassDescriptor();
049
050 FieldDescriptor[] fieldDescriptors = cld.getPkFields();
051 if(fieldDescriptors == null || fieldDescriptors.length == 0)
052 {
053 throw new OJBRuntimeException("No PK fields defined in metadata for " + cld.getClassNameOfObject());
054 }
055 FieldDescriptor field = fieldDescriptors[0];
056
057 stmt.append(SELECT);
058 stmt.append(field.getColumnName());
059 stmt.append(FROM);
060 stmt.append(cld.getFullTableName());
061 appendWhereClause(cld, false, stmt);
062
063 sql = stmt.toString();
064 }
065 return sql;
066 }
067 }