KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > AbstractEsqlConnection


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

16 package org.apache.cocoon.components.language.markup.xsp;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.commons.lang.BooleanUtils;
20
21 import java.sql.Connection JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.DatabaseMetaData JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
28  * @version CVS $Id: AbstractEsqlConnection.java 55977 2004-10-29 14:49:10Z tcurdt $
29  */

30 public abstract class AbstractEsqlConnection extends AbstractLogEnabled {
31
32     private String JavaDoc url = null;
33     private Properties JavaDoc properties = null;
34     private boolean multipleResults = false;
35
36     protected AbstractEsqlConnection() {
37     }
38
39     protected abstract Connection JavaDoc getConnection() throws SQLException JavaDoc;
40
41
42     /**
43      * It appears that some commercial DBMSs like Oracle and Informix
44      * are broken in that they don't follow the JDBC standard and
45      * calls to getUpdateCount after getMoreResults result either in
46      * an exception (Informix) or return the same value (i.e. not -1) (Oracle).
47      * In addition, this feature is only useful with stored procedures.
48      * Hence we disable it per default.
49      **/

50     public void setMultipleResults(String JavaDoc value) {
51         this.multipleResults = BooleanUtils.toBoolean(value);
52     }
53
54     public boolean getMultipleResults() {
55         return (this.multipleResults);
56     }
57
58
59
60     public Properties JavaDoc getProperties() {
61         return (properties);
62     }
63
64     public void setProperty(final String JavaDoc name, final Object JavaDoc value) {
65         if (properties == null) properties = new Properties JavaDoc();
66         properties.put(name, value);
67     }
68
69     public void setUser(String JavaDoc user) {
70         setProperty("user", user);
71     }
72
73     public void setPassword(String JavaDoc password) {
74         setProperty("password", password);
75     }
76
77     public void setAutoCommit(final boolean autocommit) throws SQLException JavaDoc {
78         getConnection().setAutoCommit(autocommit);
79     }
80
81     public boolean getAutoCommit() throws SQLException JavaDoc {
82         return (getConnection().getAutoCommit());
83     }
84
85     public String JavaDoc getURL() throws SQLException JavaDoc {
86         if (this.url == null) {
87             this.url = getConnection().getMetaData().getURL();
88         }
89         return (this.url);
90     }
91
92     public void setURL(final String JavaDoc url) {
93         this.url = url;
94     }
95
96
97     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
98         return (getConnection().getMetaData());
99     }
100
101     public void commit() throws SQLException JavaDoc {
102         getConnection().commit();
103     }
104
105     public void rollback() throws SQLException JavaDoc {
106         getConnection().rollback();
107     }
108
109     public void close() throws SQLException JavaDoc {
110         getConnection().close();
111     }
112
113
114
115     /**
116      * Factory method for creating an EsqlQuery object. If type is set to
117      * "" or "auto" it will try to find type from the JDBC connection URL.
118      * If this does not succeed the generic JDBC type will be assumed.
119      * (This type does not work for some databases like mssql though)
120      *
121      * @param type {sybase|postgresql|mysql|oracle|jdbc}
122      * @param queryString
123      * @return implementation of the AbstractEsqlQuery
124      * @throws SQLException
125      */

126     public AbstractEsqlQuery createQuery(final String JavaDoc type, final String JavaDoc queryString) throws SQLException JavaDoc {
127         AbstractEsqlQuery query;
128
129         Connection JavaDoc connection = getConnection();
130
131         if ("".equals(type) || "auto".equalsIgnoreCase(type)) {
132             String JavaDoc database = connection.getMetaData().getDatabaseProductName().toLowerCase();
133
134             if (database.indexOf("postgresql") > -1) {
135                 query = new PostgresEsqlQuery(connection,queryString);
136             }
137             else if (database.indexOf("mysql") > -1) {
138                 query = new MysqlEsqlQuery(connection,queryString);
139             }
140             else if (database.indexOf("adaptive server anywhere") > -1 ||
141                      database.indexOf("microsoft sql server") > -1) {
142                 query = new SybaseEsqlQuery(connection,queryString);
143             }
144             else if (database.indexOf("oracle") > -1) {
145                 query = new OracleEsqlQuery(connection,queryString);
146             }
147             else if (database.indexOf("pervasive") > -1) {
148                 query = new PervasiveEsqlQuery(connection,queryString);
149             }
150             else if (database.indexOf("hsql") > -1 ||
151                      database.indexOf("interbase") > -1 ||
152                      database.indexOf("access") > -1 ||
153                      database.indexOf("ingres") > -1 ||
154                      database.indexOf("sap db") > -1 ||
155                      database.indexOf("firebird") > -1 ||
156                      database.indexOf("informix-online") > -1 ||
157                      database.indexOf("sybase sql server") > -1) {
158                 query = new JdbcEsqlQuery(getConnection(),queryString);
159             }
160             else {
161                 getLogger().warn("Your database [" + String.valueOf(database) + "] is not being recognized yet." +
162                                  " Using the generic [jdbc] query as default. " +
163                                  " Please report this to dev@cocoon.apache.org");
164
165                 query = new JdbcEsqlQuery(getConnection(),queryString);
166             }
167         }
168         else if ("sybase".equalsIgnoreCase(type)) {
169             query = new SybaseEsqlQuery(connection,queryString);
170         }
171         else if ("postgresql".equalsIgnoreCase(type)) {
172             query = new PostgresEsqlQuery(connection,queryString);
173         }
174         else if ("postgresql-old".equalsIgnoreCase(type)) {
175             query = new PostgresOldEsqlQuery(connection,queryString);
176         }
177         else if ("mysql".equalsIgnoreCase(type)) {
178             query = new MysqlEsqlQuery(connection,queryString);
179         }
180         else if ("oracle".equalsIgnoreCase(type)) {
181             query = new OracleEsqlQuery(connection,queryString);
182         }
183         else if ("pervasive".equalsIgnoreCase(type)) {
184             query = new PervasiveEsqlQuery(connection,queryString);
185         }
186         else if ("jdbc".equalsIgnoreCase(type)) {
187             query = new JdbcEsqlQuery(connection,queryString);
188         }
189         else {
190             getLogger().error("Unknown database type: " + String.valueOf(type));
191             throw new SQLException JavaDoc("Unknown database type: " + String.valueOf(type));
192         }
193         setupLogger(query);
194         return(query);
195     }
196 }
197
198
Popular Tags