1 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 ; 22 import java.util.Properties ; 23 import java.sql.SQLException ; 24 import java.sql.DatabaseMetaData ; 25 26 30 public abstract class AbstractEsqlConnection extends AbstractLogEnabled { 31 32 private String url = null; 33 private Properties properties = null; 34 private boolean multipleResults = false; 35 36 protected AbstractEsqlConnection() { 37 } 38 39 protected abstract Connection getConnection() throws SQLException ; 40 41 42 50 public void setMultipleResults(String value) { 51 this.multipleResults = BooleanUtils.toBoolean(value); 52 } 53 54 public boolean getMultipleResults() { 55 return (this.multipleResults); 56 } 57 58 59 60 public Properties getProperties() { 61 return (properties); 62 } 63 64 public void setProperty(final String name, final Object value) { 65 if (properties == null) properties = new Properties (); 66 properties.put(name, value); 67 } 68 69 public void setUser(String user) { 70 setProperty("user", user); 71 } 72 73 public void setPassword(String password) { 74 setProperty("password", password); 75 } 76 77 public void setAutoCommit(final boolean autocommit) throws SQLException { 78 getConnection().setAutoCommit(autocommit); 79 } 80 81 public boolean getAutoCommit() throws SQLException { 82 return (getConnection().getAutoCommit()); 83 } 84 85 public String getURL() throws SQLException { 86 if (this.url == null) { 87 this.url = getConnection().getMetaData().getURL(); 88 } 89 return (this.url); 90 } 91 92 public void setURL(final String url) { 93 this.url = url; 94 } 95 96 97 public DatabaseMetaData getMetaData() throws SQLException { 98 return (getConnection().getMetaData()); 99 } 100 101 public void commit() throws SQLException { 102 getConnection().commit(); 103 } 104 105 public void rollback() throws SQLException { 106 getConnection().rollback(); 107 } 108 109 public void close() throws SQLException { 110 getConnection().close(); 111 } 112 113 114 115 126 public AbstractEsqlQuery createQuery(final String type, final String queryString) throws SQLException { 127 AbstractEsqlQuery query; 128 129 Connection connection = getConnection(); 130 131 if ("".equals(type) || "auto".equalsIgnoreCase(type)) { 132 String 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 ("Unknown database type: " + String.valueOf(type)); 192 } 193 setupLogger(query); 194 return(query); 195 } 196 } 197 198 | Popular Tags |