001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.markup.xsp;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.apache.commons.lang.BooleanUtils;
021:
022: import java.sql.Connection;
023: import java.util.Properties;
024: import java.sql.SQLException;
025: import java.sql.DatabaseMetaData;
026:
027: /**
028: * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
029: * @version CVS $Id: AbstractEsqlConnection.java 433543 2006-08-22 06:22:54Z crossley $
030: */
031: public abstract class AbstractEsqlConnection extends AbstractLogEnabled {
032:
033: private String url = null;
034: private Properties properties = null;
035: private boolean multipleResults = false;
036:
037: protected AbstractEsqlConnection() {
038: }
039:
040: protected abstract Connection getConnection() throws SQLException;
041:
042: /**
043: * It appears that some commercial DBMSs like Oracle and Informix
044: * are broken in that they don't follow the JDBC standard and
045: * calls to getUpdateCount after getMoreResults result either in
046: * an exception (Informix) or return the same value (i.e. not -1) (Oracle).
047: * In addition, this feature is only useful with stored procedures.
048: * Hence we disable it per default.
049: **/
050: public void setMultipleResults(String value) {
051: this .multipleResults = BooleanUtils.toBoolean(value);
052: }
053:
054: public boolean getMultipleResults() {
055: return (this .multipleResults);
056: }
057:
058: public Properties getProperties() {
059: return (properties);
060: }
061:
062: public void setProperty(final String name, final Object value) {
063: if (properties == null)
064: properties = new Properties();
065: properties.put(name, value);
066: }
067:
068: public void setUser(String user) {
069: setProperty("user", user);
070: }
071:
072: public void setPassword(String password) {
073: setProperty("password", password);
074: }
075:
076: public void setAutoCommit(final boolean autocommit)
077: throws SQLException {
078: getConnection().setAutoCommit(autocommit);
079: }
080:
081: public boolean getAutoCommit() throws SQLException {
082: return (getConnection().getAutoCommit());
083: }
084:
085: public String getURL() throws SQLException {
086: if (this .url == null) {
087: this .url = getConnection().getMetaData().getURL();
088: }
089: return (this .url);
090: }
091:
092: public void setURL(final String url) {
093: this .url = url;
094: }
095:
096: public DatabaseMetaData getMetaData() throws SQLException {
097: return (getConnection().getMetaData());
098: }
099:
100: public void commit() throws SQLException {
101: getConnection().commit();
102: }
103:
104: public void rollback() throws SQLException {
105: getConnection().rollback();
106: }
107:
108: public void close() throws SQLException {
109: getConnection().close();
110: }
111:
112: /**
113: * Factory method for creating an EsqlQuery object. If type is set to
114: * "" or "auto" it will try to find type from the JDBC connection URL.
115: * If this does not succeed the generic JDBC type will be assumed.
116: * (This type does not work for some databases like mssql though)
117: *
118: * @param type {sybase|postgresql|mysql|oracle|jdbc}
119: * @param queryString
120: * @return implementation of the AbstractEsqlQuery
121: * @throws SQLException
122: */
123: public AbstractEsqlQuery createQuery(final String type,
124: final String queryString) throws SQLException {
125: AbstractEsqlQuery query;
126:
127: Connection connection = getConnection();
128:
129: if ("".equals(type) || "auto".equalsIgnoreCase(type)) {
130: String database = connection.getMetaData()
131: .getDatabaseProductName().toLowerCase();
132:
133: if (database.indexOf("postgresql") > -1) {
134: query = new PostgresEsqlQuery(connection, queryString);
135: } else if (database.indexOf("mysql") > -1) {
136: query = new MysqlEsqlQuery(connection, queryString);
137: } else if (database.indexOf("adaptive server anywhere") > -1
138: || database.indexOf("microsoft sql server") > -1) {
139: query = new SybaseEsqlQuery(connection, queryString);
140: } else if (database.indexOf("oracle") > -1) {
141: query = new OracleEsqlQuery(connection, queryString);
142: } else if (database.indexOf("pervasive") > -1) {
143: query = new PervasiveEsqlQuery(connection, queryString);
144: } else if (database.indexOf("hsql") > -1
145: || database.indexOf("interbase") > -1
146: || database.indexOf("access") > -1
147: || database.indexOf("ingres") > -1
148: || database.indexOf("sap db") > -1
149: || database.indexOf("firebird") > -1
150: || database.indexOf("informix-online") > -1
151: || database.indexOf("sybase sql server") > -1) {
152: query = new JdbcEsqlQuery(getConnection(), queryString);
153: } else {
154: getLogger()
155: .warn(
156: "Your database ["
157: + String.valueOf(database)
158: + "] is not being recognized yet."
159: + " Using the generic [jdbc] query as default. "
160: + " Please report this to dev@cocoon.apache.org");
161:
162: query = new JdbcEsqlQuery(getConnection(), queryString);
163: }
164: } else if ("sybase".equalsIgnoreCase(type)) {
165: query = new SybaseEsqlQuery(connection, queryString);
166: } else if ("postgresql".equalsIgnoreCase(type)) {
167: query = new PostgresEsqlQuery(connection, queryString);
168: } else if ("postgresql-old".equalsIgnoreCase(type)) {
169: query = new PostgresOldEsqlQuery(connection, queryString);
170: } else if ("mysql".equalsIgnoreCase(type)) {
171: query = new MysqlEsqlQuery(connection, queryString);
172: } else if ("oracle".equalsIgnoreCase(type)) {
173: query = new OracleEsqlQuery(connection, queryString);
174: } else if ("pervasive".equalsIgnoreCase(type)) {
175: query = new PervasiveEsqlQuery(connection, queryString);
176: } else if ("jdbc".equalsIgnoreCase(type)) {
177: query = new JdbcEsqlQuery(connection, queryString);
178: } else {
179: getLogger().error(
180: "Unknown database type: " + String.valueOf(type));
181: throw new SQLException("Unknown database type: "
182: + String.valueOf(type));
183: }
184: setupLogger(query);
185: return (query);
186: }
187: }
|