1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.jdbc4;
import java.sql.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.concurrent.Executor;
import org.postgresql.core.Oid;
import org.postgresql.core.Utils;
import org.postgresql.core.TypeInfo;
import org.postgresql.util.GT;
import org.postgresql.util.HostSpec;
import org.postgresql.util.PSQLState;
import org.postgresql.util.PSQLException;
import org.postgresql.jdbc2.AbstractJdbc2Array;
abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.AbstractJdbc3gConnection
{
private final Properties _clientInfo;
public AbstractJdbc4Connection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException {
super(hostSpecs, user, database, info, url);
TypeInfo types = getTypeInfo();
if (haveMinimumServerVersion("8.3")) {
types.addCoreType("xml", Oid.XML, java.sql.Types.SQLXML, "java.sql.SQLXML", Oid.XML_ARRAY);
}
_clientInfo = new Properties();
if (haveMinimumServerVersion("9.0")) {
String appName = info.getProperty("ApplicationName");
if (appName == null) {
appName = "";
}
_clientInfo.put("ApplicationName", appName);
}
}
public Clob createClob() throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "createClob()");
}
public Blob createBlob() throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "createBlob()");
}
public NClob createNClob() throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "createNClob()");
}
public SQLXML createSQLXML() throws SQLException
{
checkClosed();
return new Jdbc4SQLXML(this);
}
public Struct createStruct(String typeName, Object[] attributes) throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "createStruct(String, Object[])");
}
public Array createArrayOf(String typeName, Object[] elements) throws SQLException
{
checkClosed();
int oid = getTypeInfo().getPGArrayType(typeName);
if (oid == Oid.UNSPECIFIED)
throw new PSQLException(GT.tr("Unable to find server array type for provided name {0}.", typeName), PSQLState.INVALID_NAME);
StringBuffer sb = new StringBuffer();
appendArray(sb, elements);
// This will not work once we have a JDBC 5,
// but it'll do for now.
return new Jdbc4Array(this, oid, sb.toString());
}
private static void appendArray(StringBuffer sb, Object elements)
{
sb.append('{');
int nElements = java.lang.reflect.Array.getLength(elements);
for (int i=0; i<nElements; i++) {
if (i > 0) {
sb.append(',');
}
Object o = java.lang.reflect.Array.get(elements, i);
if (o == null) {
sb.append("NULL");
} else if (o.getClass().isArray()) {
appendArray(sb, o);
} else {
String s = o.toString();
AbstractJdbc2Array.escapeArrayElement(sb, s);
}
}
sb.append('}');
}
public boolean isValid(int timeout) throws SQLException
{
checkClosed();
if (timeout < 0) {
throw new PSQLException(GT.tr("Invalid timeout ({0}<0).", timeout), PSQLState.INVALID_PARAMETER_VALUE);
}
boolean valid = false;
Statement stmt = null;
try {
if (!isClosed()) {
stmt = createStatement();
stmt.setQueryTimeout( timeout );
stmt.executeQuery( "SELECT 1" );
valid = true;
}
}
catch ( SQLException e) {
getLogger().log(GT.tr("Validating connection."),e);
}
finally
{
if(stmt!=null) try {stmt.close();}catch(Exception ex){}
}
return valid;
}
public void setClientInfo(String name, String value) throws SQLClientInfoException
{
if (haveMinimumServerVersion("9.0") && "ApplicationName".equals(name)) {
if (value == null)
value = "";
try {
StringBuffer sql = new StringBuffer("SET application_name = '");
Utils.appendEscapedLiteral(sql, value, getStandardConformingStrings());
sql.append("'");
execSQLUpdate(sql.toString());
} catch (SQLException sqle) {
Map<String, ClientInfoStatus> failures = new HashMap<String, ClientInfoStatus>();
failures.put(name, ClientInfoStatus.REASON_UNKNOWN);
throw new SQLClientInfoException(GT.tr("Failed to set ClientInfo property: {0}", "ApplicationName"), sqle.getSQLState(), failures, sqle);
}
_clientInfo.put(name, value);
return;
}
Map<String, ClientInfoStatus> failures = new HashMap<String, ClientInfoStatus>();
failures.put(name, ClientInfoStatus.REASON_UNKNOWN_PROPERTY);
throw new SQLClientInfoException(GT.tr("ClientInfo property not supported."), PSQLState.NOT_IMPLEMENTED.getState(), failures);
}
public void setClientInfo(Properties properties) throws SQLClientInfoException
{
if (properties == null || properties.size() == 0)
return;
Map<String, ClientInfoStatus> failures = new HashMap<String, ClientInfoStatus>();
Iterator<String> i = properties.stringPropertyNames().iterator();
while (i.hasNext()) {
String name = i.next();
if (haveMinimumServerVersion("9.0") && "ApplicationName".equals(name)) {
String value = properties.getProperty(name);
setClientInfo(name, value);
} else {
failures.put(i.next(), ClientInfoStatus.REASON_UNKNOWN_PROPERTY);
}
}
if (!failures.isEmpty())
throw new SQLClientInfoException(GT.tr("ClientInfo property not supported."), PSQLState.NOT_IMPLEMENTED.getState(), failures);
}
public String getClientInfo(String name) throws SQLException
{
checkClosed();
return _clientInfo.getProperty(name);
}
public Properties getClientInfo() throws SQLException
{
checkClosed();
return _clientInfo;
}
public <T> T createQueryObject(Class<T> ifc) throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "createQueryObject(Class<T>)");
}
public boolean isWrapperFor(Class<?> iface) throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "isWrapperFor(Class<?>)");
}
public <T> T unwrap(Class<T> iface) throws SQLException
{
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "unwrap(Class<T>)");
}
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException
{
throw org.postgresql.Driver.notImplemented(this.getClass(), "getParentLogger()");
}
public void setSchema(String schema) throws SQLException
{
throw org.postgresql.Driver.notImplemented(this.getClass(), "setSchema(String)");
}
public String getSchema() throws SQLException
{
throw org.postgresql.Driver.notImplemented(this.getClass(), "getSchema()");
}
public void abort(Executor executor) throws SQLException
{
throw org.postgresql.Driver.notImplemented(this.getClass(), "abort(Executor)");
}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
throw org.postgresql.Driver.notImplemented(this.getClass(), "setNetworkTimeout(Executor, int)");
}
public int getNetworkTimeout() throws SQLException {
throw org.postgresql.Driver.notImplemented(this.getClass(), "getNetworkTimeout()");
}
}
|