KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > SQLCachingStatementsForClass


1 package org.apache.ojb.broker.accesslayer;
2
3 /* Copyright 2002-2004 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.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import org.apache.ojb.broker.PersistenceBrokerException;
23 import org.apache.ojb.broker.metadata.ClassDescriptor;
24 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
25 import org.apache.ojb.broker.query.Query;
26 import org.apache.ojb.broker.util.logging.Logger;
27 import org.apache.ojb.broker.util.logging.LoggerFactory;
28
29 /**
30  * @version $Id: SQLCachingStatementsForClass.java,v 1.12 2004/04/04 23:53:31 brianm Exp $
31  */

32 public class SQLCachingStatementsForClass extends StatementsForClassImpl
33 {
34     private Logger logger = LoggerFactory.getLogger(SQLCachingStatementsForClass.class);
35     /**
36      * the SQL String used for PK lookups
37      */

38     private String JavaDoc selectByPKSQL;
39     /**
40      * the SQL String used for inserts
41      */

42     private String JavaDoc insertSQL;
43     /**
44      * the SQL String used for updates
45      */

46     private String JavaDoc updateSQL;
47     /**
48      * the SQL String used for deletes
49      */

50     private String JavaDoc deleteSQL;
51
52     public SQLCachingStatementsForClass(final JdbcConnectionDescriptor jcd, final ClassDescriptor mif)
53     {
54         super(jcd, mif);
55     }
56
57 // /**
58
// * Obtains the connection from the ConnectionManager.
59
// * This method is used for jdbc 1.0 drivers.
60
// */
61
// protected Connection getConnection() throws SQLException
62
// {
63
// try
64
// {
65
// return connectionManager.getConnection();
66
// }
67
// catch (PersistenceBrokerException e)
68
// {
69
// throw new SQLException("OJB Error: could not obtain a Connection");
70
// }
71
// catch (LookupException e)
72
// {
73
// throw new SQLException("OJB Error: could not obtain a Connection");
74
// }
75
//
76
// }
77

78
79     public PreparedStatement JavaDoc getDeleteStmt(Connection JavaDoc con) throws SQLException JavaDoc
80     {
81         if (deleteSQL == null)
82         {
83             synchronized (this)
84             {
85                 deleteSQL = sqlGenerator.getPreparedDeleteStatement(classDescriptor);
86                 if (deleteSQL == null)
87                     throw new PersistenceBrokerException("Could not generate delete Key SQL for class (" + classDescriptor.getClassOfObject().getName() + ")");
88             }
89         }
90         try
91         {
92             return prepareStatement(con, deleteSQL, Query.NOT_SCROLLABLE, true);
93         }
94         catch (SQLException JavaDoc ex)
95         {
96             logger.error("Error getting delete statement for class (" + classDescriptor.getClassOfObject().getName() + ")", ex);
97             throw ex;
98         }
99     }
100
101     public PreparedStatement JavaDoc getInsertStmt(Connection JavaDoc con) throws SQLException JavaDoc
102     {
103         if (insertSQL == null)
104         {
105             synchronized (this)
106             {
107                 insertSQL = sqlGenerator.getPreparedInsertStatement(classDescriptor);
108                 if (insertSQL == null)
109                     throw new PersistenceBrokerException("Could not generate insert SQL for class (" + classDescriptor.getClassOfObject().getName() + ")");
110             }
111         }
112         try
113         {
114             return prepareStatement(con, insertSQL, Query.NOT_SCROLLABLE, true);
115         }
116         catch (SQLException JavaDoc ex)
117         {
118             logger.error("Error getting insert statement for class (" + classDescriptor.getClassOfObject().getName() + ")", ex);
119             throw ex;
120         }
121     }
122
123     public PreparedStatement JavaDoc getSelectByPKStmt(Connection JavaDoc con) throws SQLException JavaDoc
124     {
125         if (selectByPKSQL == null)
126         {
127             synchronized (this)
128             {
129                 selectByPKSQL = sqlGenerator.getPreparedSelectByPkStatement(classDescriptor);
130                 if (selectByPKSQL == null)
131                     throw new PersistenceBrokerException("Could not generate Select by Primary Key SQL for class (" + classDescriptor.getClassOfObject().getName() + ")");
132             }
133         }
134         try
135         {
136             return prepareStatement(con, selectByPKSQL, Query.NOT_SCROLLABLE, true);
137         }
138         catch (SQLException JavaDoc ex)
139         {
140             logger.error("Error getting select by primary key statement for class (" + classDescriptor.getClassOfObject().getName() + ")", ex);
141             throw ex;
142         }
143     }
144
145     public PreparedStatement JavaDoc getUpdateStmt(Connection JavaDoc con) throws SQLException JavaDoc
146     {
147         if (updateSQL == null)
148         {
149             synchronized (this)
150             {
151                 updateSQL = sqlGenerator.getPreparedUpdateStatement(classDescriptor);
152                 if (updateSQL == null)
153                     throw new PersistenceBrokerException("Could not generate Update SQL for class (" + classDescriptor.getClassOfObject().getName() + ")");
154             }
155         }
156         try
157         {
158             return prepareStatement(con, updateSQL, Query.NOT_SCROLLABLE, true);
159         }
160         catch (SQLException JavaDoc ex)
161         {
162             logger.error("Error getting update statement for class (" + classDescriptor.getClassOfObject().getName() + ")", ex);
163             throw ex;
164         }
165     }
166 }
167
Popular Tags