KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > database > RDBMSIndependent


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.database;
13
14 import java.sql.*;
15 import javax.servlet.ServletException JavaDoc;
16 import java.util.*;
17 import org.openbravo.data.UtilSql;
18
19 public class RDBMSIndependent {
20
21   public static Vector<String JavaDoc> getCallableResult(Connection conn, ConnectionProvider connectionProvider, String JavaDoc sql, Vector<String JavaDoc> parameters, Vector<String JavaDoc> types, int totalOutParameters) throws Exception JavaDoc {
22     StringBuffer JavaDoc strSql = new StringBuffer JavaDoc();
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 JavaDoc("Badly formed sql: " + sql);
28       strSql.append(sql.substring(0, init+1));
29       int end = sql.lastIndexOf(")");
30       if (end==-1) throw new ServletException JavaDoc("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 JavaDoc 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 JavaDoc> total = new Vector<String JavaDoc>();
57
58     int iParameter = 0;
59     try {
60       if (parameters!=null) {
61         for (int i=0;i<parameters.size();i++) {
62           String JavaDoc 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       //System.out.println("SQL error in query: " + strSql + "Exception:"+ e);
86
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