1 19 package org.apache.cayenne.dba.mysql; 20 21 import java.sql.CallableStatement ; 22 import java.sql.Connection ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 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 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 connection, OperationObserver observer) 47 throws SQLException , Exception { 48 49 processedResultSets = 0; 50 51 ProcedureTranslator transl = createTranslator(connection); 52 CallableStatement statement = (CallableStatement ) transl.createStatement(); 53 54 try { 55 56 boolean firstResult = statement.execute(); 60 61 readProcedureOutParameters(statement, observer); 63 64 if (firstResult) { 66 processResultSet(statement, observer); 67 } 68 else if (!processUpdate(statement, observer)) { 69 return; 70 } 71 72 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 ex) { 87 88 } 89 } 90 } 91 92 private void processResultSet(CallableStatement statement, OperationObserver observer) 93 throws Exception { 94 ResultSet 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 ex) { 105 } 106 } 107 } 108 109 private boolean processUpdate(CallableStatement statement, OperationObserver observer) 110 throws Exception { 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 124 protected ProcedureTranslator createTranslator(Connection 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 static class MySQLProcedureTranslator extends ProcedureTranslator { 136 137 protected String createSqlString() { 138 139 String sql = super.createSqlString(); 140 141 if (sql.endsWith("}") && !sql.endsWith(")}")) { 143 sql = sql.substring(0, sql.length() - 1) + "()}"; 144 } 145 146 return sql; 147 } 148 } 149 } 150 | Popular Tags |