1 17 18 package org.pentaho.plugin.sql; 19 20 import java.sql.SQLException ; 21 import java.util.StringTokenizer ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.pentaho.core.connection.memory.MemoryMetaData; 26 import org.pentaho.core.connection.memory.MemoryResultSet; 27 import org.pentaho.data.connection.sql.SQLConnection; 28 import org.pentaho.messages.Messages; 29 public class SQLExecute extends SQLLookupRule { 30 31 private static final long serialVersionUID = 2480019361917802106L; 32 33 private String multiStatementSeparatorToken = ";"; 35 public Log getLogger() { 36 return LogFactory.getLog(SQLExecute.class); 37 } 38 39 protected boolean runQuery(String rawQuery, boolean live) { 40 SQLConnection conn = (SQLConnection)connection; 41 return runSqlQuery(conn, rawQuery, live); 42 } 43 44 protected boolean runSqlQuery(SQLConnection conn, String rawQuery, boolean live) { 45 boolean executed=false; 46 boolean continueOnException = false; 47 String [] columnHeaders = new String [] { 48 Messages.getString("SQLExecute.USER_AFFECTED_ROWS_COLUMN_NAME"), Messages.getString("SQLExecute.USER_AFFECTED_ROW_STATUS") }; 51 MemoryMetaData metaData = new MemoryMetaData(new String [][]{ columnHeaders }, null); 52 metaData.setColumnTypes(new String [] {"int", "string"} ); MemoryResultSet affectedRowsResultSet = new MemoryResultSet(metaData); 54 String successMsg = Messages.getString("SQLExecute.USER_SUCCESS"); String failMsg = Messages.getString("SQLExecute.USER_FAILED"); try { 57 if (conn == null) { 58 error(Messages.getErrorString( "SQLBaseComponent.ERROR_0007_NO_CONNECTION" )); return false; 60 } 61 if (!conn.initialized()) { 62 error(Messages.getErrorString( "SQLBaseComponent.ERROR_0007_NO_CONNECTION" )); return false; 64 } 65 66 if (isDefinedInput("continue_on_exception")) { continueOnException = "true".equals(getInputStringValue("continue_on_exception")); } 72 73 boolean forceSingleStatement = false; 78 if (isDefinedInput("force_single_statement")) { forceSingleStatement = "true".equals(getInputStringValue("force_single_statement")); } 81 82 if (isDefinedInput("multi_statement_separator")) { multiStatementSeparatorToken = getInputStringValue("multi_statement_separator"); } 87 88 try { 89 if (forceSingleStatement) { 90 String query = applyInputsToFormat(rawQuery); 107 if (debug) { 108 debug(Messages.getString("SQLBaseComponent.DEBUG_RUNNING_QUERY", query)); } 110 int affectedRows = conn.execute(query); 111 executed = true; 112 affectedRowsResultSet.addRow(new Object [] { new Integer (affectedRows), successMsg} ); 113 } else { 114 rawQuery = removeLineTerminators(rawQuery.trim()).toString(); 118 119 StringTokenizer st = new StringTokenizer (rawQuery, multiStatementSeparatorToken); 121 122 while (st.hasMoreTokens() ) { 123 rawQuery = st.nextToken(); 125 String query = applyInputsToFormat(rawQuery.trim()); 126 if (debug) 127 debug(Messages.getString("SQLBaseComponent.DEBUG_RUNNING_QUERY", query)); try{ 129 int affectedRows = conn.execute(query); 130 affectedRowsResultSet.addRow(new Object [] { new Integer (affectedRows), successMsg} ); 133 executed = true; 134 debug(Messages.getString("SQLBaseComponent.DEBUG_UPDATED_QUERY", query)); } catch (SQLException e) { 136 error(Messages.getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName() + " : " + e.getLocalizedMessage())); executed = continueOnException; 138 if (!continueOnException) { 139 break; 140 } 141 addErrorCode(affectedRowsResultSet, e, failMsg); 142 } 143 } } 145 if (getResultOutputName() != null) { 146 setOutputValue(this.getResultOutputName(), affectedRowsResultSet); 147 } 148 } finally { 149 conn.close(); 156 } 157 } catch (SQLException e) { 158 error(Messages.getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName() + " : " + e.getLocalizedMessage())); executed = continueOnException; 160 addErrorCode(affectedRowsResultSet, e, e.getLocalizedMessage()); 161 } 162 finally { 164 conn.close(); 165 } 166 return executed; 167 } 168 169 public void addErrorCode(MemoryResultSet affectedRowsResultSet, SQLException e, String failMsg) { 170 int eCode = e.getErrorCode(); 171 if (eCode > 0) { 172 eCode *= -1; } 174 affectedRowsResultSet.addRow(new Object [] { new Integer (eCode), e.getLocalizedMessage()} ); 175 } 176 177 public static String removeLineTerminators(String inputStr) { 178 char[] rtn = new char[inputStr.length()]; 179 char ch; 180 for (int i=0; i<inputStr.length(); i++) { 181 ch = inputStr.charAt(i); 182 switch (ch) { 183 case '\r': 184 case '\n': 185 rtn[i] = ' '; 186 break; 187 default: 188 rtn[i] = (ch); 189 } 190 } 191 return new String (rtn); 192 } 193 194 } | Popular Tags |