1 12 package org.openbravo.database; 13 14 import java.sql.*; 15 import javax.servlet.ServletException ; 16 import java.util.*; 17 import org.openbravo.data.UtilSql; 18 19 public class RDBMSIndependent { 20 21 public static Vector<String > getCallableResult(Connection conn, ConnectionProvider connectionProvider, String sql, Vector<String > parameters, Vector<String > types, int totalOutParameters) throws Exception { 22 StringBuffer strSql = new StringBuffer (); 23 sql = sql.toUpperCase().replace("CALL ", "SELECT * FROM "); 24 sql = sql.toUpperCase().replace("ALL TRIGGERS", "TRIGGER ALL"); 25 if (totalOutParameters==1) { 26 int init = sql.indexOf("("); 27 if (init==-1) throw new ServletException ("Badly formed sql: " + sql); 28 strSql.append(sql.substring(0, init+1)); 29 int end = sql.lastIndexOf(")"); 30 if (end==-1) throw new ServletException ("Badly formed sql: " + sql); 31 boolean found = false, first=true; 32 int count = 0; 33 StringTokenizer stoken = new StringTokenizer(sql.substring(init+1, end), ",", false); 34 while (stoken.hasMoreTokens()) { 35 String token = stoken.nextToken(); 36 if (token.indexOf("?")!=-1) { 37 if (!found && types.elementAt(count).equalsIgnoreCase("out")) { 38 found=true; 39 } else { 40 strSql.append((!first?",":"")).append(token); 41 first=false; 42 } 43 count++; 44 } else { 45 strSql.append((!first?",":"")).append(token); 46 first=false; 47 } 48 } 49 strSql.append(sql.substring(end)); 50 } else strSql.append(sql); 51 52 PreparedStatement st = null; 53 if (conn==null) st = connectionProvider.getPreparedStatement(strSql.toString()); 54 else st = connectionProvider.getPreparedStatement(conn, strSql.toString()); 55 ResultSet result; 56 Vector<String > total = new Vector<String >(); 57 58 int iParameter = 0; 59 try { 60 if (parameters!=null) { 61 for (int i=0;i<parameters.size();i++) { 62 String typeAux = types.elementAt(i); 63 if (!typeAux.equalsIgnoreCase("out")) { 64 iParameter++; 65 UtilSql.setValue(st, iParameter, 12, "Test", parameters.elementAt(i)); 66 } 67 } 68 } 69 if (!strSql.toString().toUpperCase().trim().startsWith("ALTER ")) { 70 result = st.executeQuery(); 71 int pos=0; 72 if (result.next() && totalOutParameters>0 && parameters!=null) { 73 for (int i=0;i<parameters.size();i++) { 74 if (types.elementAt(i).equalsIgnoreCase("out")) { 75 pos++; 76 total.addElement(UtilSql.getValue(result, pos)); 77 } 78 } 79 } 80 result.close(); 81 } else{ 82 st.executeUpdate(); 83 } 84 } catch(SQLException e){ 85 throw e; 87 } finally { 88 if (conn==null) connectionProvider.releasePreparedStatement(st); 89 else connectionProvider.releaseTransactionalStatement(st); 90 } 91 return(total); 92 } 93 } 94 | Popular Tags |