KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > mysql > MySQLProcedureAction


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.dba.mysql;
20
21 import java.sql.CallableStatement JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import org.apache.cayenne.access.OperationObserver;
27 import org.apache.cayenne.access.QueryLogger;
28 import org.apache.cayenne.access.jdbc.ProcedureAction;
29 import org.apache.cayenne.access.jdbc.RowDescriptor;
30 import org.apache.cayenne.access.trans.ProcedureTranslator;
31 import org.apache.cayenne.dba.DbAdapter;
32 import org.apache.cayenne.map.EntityResolver;
33 import org.apache.cayenne.query.ProcedureQuery;
34
35 /**
36  * @since 3.0
37  * @author Andrus Adamchik
38  */

39 class MySQLProcedureAction extends ProcedureAction {
40
41     public MySQLProcedureAction(ProcedureQuery query, DbAdapter adapter,
42             EntityResolver entityResolver) {
43         super(query, adapter, entityResolver);
44     }
45
46     public void performAction(Connection JavaDoc connection, OperationObserver observer)
47             throws SQLException JavaDoc, Exception JavaDoc {
48
49         processedResultSets = 0;
50
51         ProcedureTranslator transl = createTranslator(connection);
52         CallableStatement JavaDoc statement = (CallableStatement JavaDoc) transl.createStatement();
53
54         try {
55
56             // this is one difference with super - we need to read the first result set
57
// without calling 'getMoreResults' - which may actually be a good default
58
// strategy?
59
boolean firstResult = statement.execute();
60
61             // read out parameters
62
readProcedureOutParameters(statement, observer);
63
64             // read first result
65
if (firstResult) {
66                 processResultSet(statement, observer);
67             }
68             else if (!processUpdate(statement, observer)) {
69                 return;
70             }
71
72             // read the rest of the query
73
while (true) {
74                 if (statement.getMoreResults()) {
75                     processResultSet(statement, observer);
76                 }
77                 else if (!processUpdate(statement, observer)) {
78                     break;
79                 }
80             }
81         }
82         finally {
83             try {
84                 statement.close();
85             }
86             catch (SQLException JavaDoc ex) {
87
88             }
89         }
90     }
91
92     private void processResultSet(CallableStatement JavaDoc statement, OperationObserver observer)
93             throws Exception JavaDoc {
94         ResultSet JavaDoc rs = statement.getResultSet();
95
96         try {
97             RowDescriptor descriptor = describeResultSet(rs, processedResultSets++);
98             readResultSet(rs, descriptor, query, observer);
99         }
100         finally {
101             try {
102                 rs.close();
103             }
104             catch (SQLException JavaDoc ex) {
105             }
106         }
107     }
108
109     private boolean processUpdate(CallableStatement JavaDoc statement, OperationObserver observer)
110             throws Exception JavaDoc {
111         int updateCount = statement.getUpdateCount();
112         if (updateCount == -1) {
113             return false;
114         }
115         QueryLogger.logUpdateCount(updateCount);
116         observer.nextCount(query, updateCount);
117
118         return true;
119     }
120
121     /**
122      * Creates a translator that adds parenthesis to no-param queries.
123      */

124     // see CAY-750 for the problem description
125
protected ProcedureTranslator createTranslator(Connection JavaDoc connection) {
126         ProcedureTranslator translator = new MySQLProcedureTranslator();
127         translator.setAdapter(getAdapter());
128         translator.setQuery(query);
129         translator.setEntityResolver(getEntityResolver());
130         translator.setConnection(connection);
131         return translator;
132     }
133
134     // same as postgres translator - should we make this the default?
135
static class MySQLProcedureTranslator extends ProcedureTranslator {
136
137         protected String JavaDoc createSqlString() {
138
139             String JavaDoc sql = super.createSqlString();
140
141             // add empty parameter parenthesis
142
if (sql.endsWith("}") && !sql.endsWith(")}")) {
143                 sql = sql.substring(0, sql.length() - 1) + "()}";
144             }
145
146             return sql;
147         }
148     }
149 }
150
Popular Tags