1 29 30 package com.caucho.quercus.lib.db; 31 32 import com.caucho.quercus.annotation.Optional; 33 import com.caucho.quercus.env.Env; 34 import com.caucho.quercus.env.LongValue; 35 import com.caucho.quercus.env.StringBuilderValue; 36 import com.caucho.quercus.env.StringValue; 37 import com.caucho.quercus.env.StringValueImpl; 38 import com.caucho.util.L10N; 39 40 import java.lang.reflect.Method ; 41 import java.sql.Connection ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.sql.Statement ; 45 import java.util.HashMap ; 46 import java.util.logging.Level ; 47 import java.util.logging.Logger ; 48 49 52 public class Postgres extends JdbcConnectionResource { 53 private static final Logger log = Logger.getLogger(Postgres.class.getName()); 54 private static final L10N L = new L10N(Postgres.class); 55 56 PostgresResult _asyncResult; 57 PostgresStatement _asyncStmt; 58 59 private HashMap <String ,PostgresStatement> _stmtTable 61 = new HashMap <String ,PostgresStatement>(); 62 63 Object _serverErrorMessage; 66 67 public Postgres(Env env, 68 @Optional("localhost") String host, 69 @Optional String user, 70 @Optional String password, 71 @Optional String db, 72 @Optional("5432") int port, 73 @Optional String driver, 74 @Optional String url) 75 { 76 super(env); 77 78 connectInternal(env, host, user, password, db, port, "", 0, driver, url); 79 } 80 81 84 public boolean connectInternal(Env env, 85 @Optional("localhost") String host, 86 @Optional String userName, 87 @Optional String password, 88 @Optional String dbname, 89 @Optional("5432") int port, 90 @Optional String socket, 91 @Optional int flags, 92 @Optional String driver, 93 @Optional String url) 94 { 95 if (isConnected()) { 96 env.warning(L.l("Connection is already opened to '{0}'", this)); 97 return false; 98 } 99 100 try { 101 102 if (host == null || host.equals("")) 103 host = "localhost"; 104 105 if (driver == null || driver.equals("")) { 106 driver = "org.postgresql.Driver"; 107 } 108 109 if (url == null || url.equals("")) { 110 url = "jdbc:postgresql://" + host + ":" + port + "/" + dbname; 111 } 112 113 Connection jConn = env.getConnection(driver, url, userName, password); 114 115 setConnection(host, userName, password, dbname, port, jConn, driver, url); 116 117 return true; 118 119 } catch (SQLException e) { 120 env.warning("A link to the server could not be established. " + e.toString()); 121 env.setSpecialValue("postgres.connectErrno",new LongValue(e.getErrorCode())); 122 env.setSpecialValue("postgres.connectError", new StringValueImpl(e.getMessage())); 123 124 log.log(Level.FINE, e.toString(), e); 125 126 return false; 127 } catch (Exception e) { 128 env.warning("A link to the server could not be established. " + e.toString()); 129 env.setSpecialValue("postgres.connectError", new StringValueImpl(e.getMessage())); 130 131 log.log(Level.FINE, e.toString(), e); 132 return false; 133 } 134 } 135 136 139 public PostgresStatement prepare(Env env, String query) 140 { 141 PostgresStatement stmt = new PostgresStatement((Postgres)validateConnection()); 142 143 stmt.prepare(query); 144 145 return stmt; 146 } 147 148 155 public PostgresResult query(String sql) 156 { 157 return (PostgresResult) realQuery(sql); 158 } 159 160 163 protected JdbcResultResource createResult(Statement stmt, 164 ResultSet rs) 165 { 166 return new PostgresResult(stmt, rs, this); 167 } 168 169 public void setAsynchronousResult(PostgresResult asyncResult) 170 { 171 _asyncResult = asyncResult; 172 } 173 174 public PostgresResult getAsynchronousResult() 175 { 176 return _asyncResult; 177 } 178 179 public PostgresStatement getAsynchronousStatement() 180 { 181 return _asyncStmt; 182 } 183 184 public void setAsynchronousStatement(PostgresStatement asyncStmt) 185 { 186 _asyncStmt = asyncStmt; 187 } 188 189 public void putStatement(String name, 190 PostgresStatement stmt) 191 { 192 _stmtTable.put(name, stmt); 193 } 194 195 public PostgresStatement getStatement(String name) 196 { 197 return _stmtTable.get(name); 198 } 199 200 public PostgresStatement removeStatement(String name) 201 { 202 return _stmtTable.remove(name); 203 } 204 205 209 protected void keepResourceValues(Statement stmt) 210 { 211 setResultResource(createResult(stmt, null)); 212 } 213 214 218 protected boolean keepStatementOpen() 219 { 220 return true; 221 } 222 223 229 protected StringBuilderValue realEscapeString(StringValue str) 230 { 231 StringBuilderValue buf = new StringBuilderValue(str.length()); 232 233 final int strLength = str.length(); 234 235 for (int i = 0; i < strLength; i++) { 236 char c = str.charAt(i); 237 238 switch (c) { 239 case '\u0000': 240 buf.append('\\'); 241 buf.append('\u0000'); 242 break; 243 case '\n': 244 buf.append('\\'); 245 buf.append('n'); 246 break; 247 case '\r': 248 buf.append('\\'); 249 buf.append('r'); 250 break; 251 case '\\': 252 buf.append('\\'); 253 buf.append('\\'); 254 break; 255 case '\'': 256 buf.append('\''); 257 buf.append('\''); 258 break; 259 case '"': 260 buf.append('\"'); 263 break; 264 case '\032': 265 buf.append('\\'); 266 buf.append('Z'); 267 break; 268 default: 269 buf.append(c); 270 break; 271 } 272 } 273 274 return buf; 275 } 276 277 281 protected void clearErrors() 282 { 283 super.clearErrors(); 284 _serverErrorMessage = null; 285 } 286 287 291 protected void saveErrors(SQLException e) 292 { 293 try { 294 super.saveErrors(e); 295 296 Class cl = Class.forName("org.postgresql.util.PSQLException"); 298 Method method = cl.getDeclaredMethod("getServerErrorMessage", null); 299 _serverErrorMessage = method.invoke(e, new Object [] {}); 300 } catch (Exception ex) { 301 log.log(Level.FINE, ex.toString(), ex); 302 } 303 } 304 305 308 protected Object getServerErrorMessage() 309 { 310 return _serverErrorMessage; 311 } 312 313 public String toString() 314 { 315 if (isConnected()) 316 return "Postgres[" + getHost() + "]"; 317 else 318 return "Postgres[]"; 319 } 320 } 321 | Popular Tags |