KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > sql > SqlExistStatement


1 package org.apache.ojb.broker.accesslayer.sql;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 {
32     private static final String JavaDoc SELECT = "SELECT ";
33     private static final String JavaDoc FROM = " FROM ";
34
35     private String JavaDoc sql;
36
37     public SqlExistStatement(ClassDescriptor aCld, Logger aLogger)
38     {
39         super(aCld, aLogger);
40     }
41
42     /** Return SELECT clause for object existence call */
43     public String JavaDoc getStatement()
44     {
45         if(sql == null)
46         {
47             StringBuffer JavaDoc stmt = new StringBuffer JavaDoc(128);
48             ClassDescriptor cld = getClassDescriptor();
49
50             FieldDescriptor[] fieldDescriptors = cld.getPkFields();
51             if(fieldDescriptors == null || fieldDescriptors.length == 0)
52             {
53                 throw new OJBRuntimeException("No PK fields defined in metadata for " + cld.getClassNameOfObject());
54             }
55             FieldDescriptor field = fieldDescriptors[0];
56
57             stmt.append(SELECT);
58             stmt.append(field.getColumnName());
59             stmt.append(FROM);
60             stmt.append(cld.getFullTableName());
61             appendWhereClause(cld, false, stmt);
62
63             sql = stmt.toString();
64         }
65         return sql;
66     }
67 }
Popular Tags