001: package org.apache.ojb.broker.accesslayer.sql;
002:
003: /* Copyright 2002-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 java.lang.ref.WeakReference;
019:
020: import org.apache.ojb.broker.OJBRuntimeException;
021: import org.apache.ojb.broker.PersistenceBrokerException;
022: import org.apache.ojb.broker.metadata.ClassDescriptor;
023: import org.apache.ojb.broker.metadata.FieldDescriptor;
024: import org.apache.ojb.broker.util.logging.Logger;
025:
026: /**
027: * Model simple Statements based on ClassDescriptor and/or PrimaryKey
028: *
029: * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
030: * @version $Id: SqlPkStatement.java,v 1.5.2.2 2005/12/21 22:23:44 tomdz Exp $
031: */
032: public abstract class SqlPkStatement implements SqlStatement {
033: // arminw: Use weak reference to allow GC of removed metadata instances
034: private WeakReference m_classDescriptor;
035: private Logger m_logger;
036:
037: /** Constructor for SqlPkStatement. */
038: public SqlPkStatement(ClassDescriptor aCld, Logger aLogger) {
039: super ();
040: m_classDescriptor = new WeakReference(aCld);
041: m_logger = aLogger;
042: }
043:
044: /** append table name */
045: protected void appendTable(ClassDescriptor cld, StringBuffer stmt) {
046: stmt.append(cld.getFullTableName());
047: }
048:
049: /**
050: * Returns the logger.
051: *
052: * @return Logger
053: */
054: protected Logger getLogger() {
055: return m_logger;
056: }
057:
058: /**
059: * Returns the classDescriptor.
060: *
061: * @return ClassDescriptor
062: */
063: protected ClassDescriptor getClassDescriptor() {
064: ClassDescriptor cld = (ClassDescriptor) m_classDescriptor.get();
065: if (cld == null) {
066: throw new OJBRuntimeException(
067: "Requested ClassDescriptor instance was already GC by JVM");
068: }
069: return cld;
070: }
071:
072: /**
073: * Generate a sql where-clause for the array of fields
074: *
075: * @param fields array containing all columns used in WHERE clause
076: */
077: protected void appendWhereClause(FieldDescriptor[] fields,
078: StringBuffer stmt) throws PersistenceBrokerException {
079: stmt.append(" WHERE ");
080:
081: for (int i = 0; i < fields.length; i++) {
082: FieldDescriptor fmd = fields[i];
083:
084: stmt.append(fmd.getColumnName());
085: stmt.append(" = ? ");
086: if (i < fields.length - 1) {
087: stmt.append(" AND ");
088: }
089: }
090: }
091:
092: /**
093: * Generate a where clause for a prepared Statement.
094: * Only primary key and locking fields are used in this where clause
095: *
096: * @param cld the ClassDescriptor
097: * @param useLocking true if locking fields should be included
098: * @param stmt the StatementBuffer
099: */
100: protected void appendWhereClause(ClassDescriptor cld,
101: boolean useLocking, StringBuffer stmt) {
102: FieldDescriptor[] pkFields = cld.getPkFields();
103: FieldDescriptor[] fields;
104:
105: fields = pkFields;
106: if (useLocking) {
107: FieldDescriptor[] lockingFields = cld.getLockingFields();
108: if (lockingFields.length > 0) {
109: fields = new FieldDescriptor[pkFields.length
110: + lockingFields.length];
111: System.arraycopy(pkFields, 0, fields, 0,
112: pkFields.length);
113: System.arraycopy(lockingFields, 0, fields,
114: pkFields.length, lockingFields.length);
115: }
116: }
117:
118: appendWhereClause(fields, stmt);
119: }
120:
121: }
|