KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker.accesslayer.sql;
2
3 /* Copyright 2002-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 java.lang.ref.WeakReference JavaDoc;
19
20 import org.apache.ojb.broker.OJBRuntimeException;
21 import org.apache.ojb.broker.PersistenceBrokerException;
22 import org.apache.ojb.broker.metadata.ClassDescriptor;
23 import org.apache.ojb.broker.metadata.FieldDescriptor;
24 import org.apache.ojb.broker.util.logging.Logger;
25
26 /**
27  * Model simple Statements based on ClassDescriptor and/or PrimaryKey
28  *
29  * @author <a HREF="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
30  * @version $Id: SqlPkStatement.java,v 1.5.2.2 2005/12/21 22:23:44 tomdz Exp $
31  */

32 public abstract class SqlPkStatement implements SqlStatement
33 {
34     // arminw: Use weak reference to allow GC of removed metadata instances
35
private WeakReference JavaDoc m_classDescriptor;
36     private Logger m_logger;
37
38     /** Constructor for SqlPkStatement. */
39     public SqlPkStatement(ClassDescriptor aCld, Logger aLogger)
40     {
41         super();
42         m_classDescriptor = new WeakReference JavaDoc(aCld);
43         m_logger = aLogger;
44     }
45
46     /** append table name */
47     protected void appendTable(ClassDescriptor cld, StringBuffer JavaDoc stmt)
48     {
49         stmt.append(cld.getFullTableName());
50     }
51
52     /**
53      * Returns the logger.
54      *
55      * @return Logger
56      */

57     protected Logger getLogger()
58     {
59         return m_logger;
60     }
61
62     /**
63      * Returns the classDescriptor.
64      *
65      * @return ClassDescriptor
66      */

67     protected ClassDescriptor getClassDescriptor()
68     {
69         ClassDescriptor cld = (ClassDescriptor) m_classDescriptor.get();
70         if(cld == null)
71         {
72             throw new OJBRuntimeException("Requested ClassDescriptor instance was already GC by JVM");
73         }
74         return cld;
75     }
76
77     /**
78      * Generate a sql where-clause for the array of fields
79      *
80      * @param fields array containing all columns used in WHERE clause
81      */

82     protected void appendWhereClause(FieldDescriptor[] fields, StringBuffer JavaDoc stmt) throws PersistenceBrokerException
83     {
84         stmt.append(" WHERE ");
85
86         for(int i = 0; i < fields.length; i++)
87         {
88             FieldDescriptor fmd = fields[i];
89
90             stmt.append(fmd.getColumnName());
91             stmt.append(" = ? ");
92             if(i < fields.length - 1)
93             {
94                 stmt.append(" AND ");
95             }
96         }
97     }
98
99     /**
100      * Generate a where clause for a prepared Statement.
101      * Only primary key and locking fields are used in this where clause
102      *
103      * @param cld the ClassDescriptor
104      * @param useLocking true if locking fields should be included
105      * @param stmt the StatementBuffer
106      */

107     protected void appendWhereClause(ClassDescriptor cld, boolean useLocking, StringBuffer JavaDoc stmt)
108     {
109         FieldDescriptor[] pkFields = cld.getPkFields();
110         FieldDescriptor[] fields;
111
112         fields = pkFields;
113         if(useLocking)
114         {
115             FieldDescriptor[] lockingFields = cld.getLockingFields();
116             if(lockingFields.length > 0)
117             {
118                 fields = new FieldDescriptor[pkFields.length + lockingFields.length];
119                 System.arraycopy(pkFields, 0, fields, 0, pkFields.length);
120                 System.arraycopy(lockingFields, 0, fields, pkFields.length, lockingFields.length);
121             }
122         }
123
124         appendWhereClause(fields, stmt);
125     }
126
127 }
128
Popular Tags