1 package org.apache.ojb.broker.accesslayer.sql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.lang.ref.WeakReference;
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
28
29
30
31
32 public abstract class SqlPkStatement implements SqlStatement
33 {
34
35 private WeakReference m_classDescriptor;
36 private Logger m_logger;
37
38
39 public SqlPkStatement(ClassDescriptor aCld, Logger aLogger)
40 {
41 super();
42 m_classDescriptor = new WeakReference(aCld);
43 m_logger = aLogger;
44 }
45
46
47 protected void appendTable(ClassDescriptor cld, StringBuffer stmt)
48 {
49 stmt.append(cld.getFullTableName());
50 }
51
52
53
54
55
56
57 protected Logger getLogger()
58 {
59 return m_logger;
60 }
61
62
63
64
65
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
79
80
81
82 protected void appendWhereClause(FieldDescriptor[] fields, StringBuffer 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
101
102
103
104
105
106
107 protected void appendWhereClause(ClassDescriptor cld, boolean useLocking, StringBuffer 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 }