diff --git a/org/postgresql/ds/common/BaseDataSource.java b/org/postgresql/ds/common/BaseDataSource.java index bfe54f7..b7743df 100644 --- a/org/postgresql/ds/common/BaseDataSource.java +++ b/org/postgresql/ds/common/BaseDataSource.java @@ -43,15 +43,15 @@ // Standard properties, defined in the JDBC 2.0 Optional Package spec private String serverName = "localhost"; - private String databaseName; + private String databaseName = ""; private String user; private String password; private int portNumber = 0; private int prepareThreshold = 5; private int unknownLength = Integer.MAX_VALUE; private boolean binaryTransfer = true; - private String binaryTransferEnable = ""; - private String binaryTransferDisable = ""; + private String binaryTransferEnable; + private String binaryTransferDisable; private int loginTimeout = 0; // in seconds private int socketTimeout = 0; // in seconds private int receiveBufferSize = -1; // off (-1), not in use @@ -63,6 +63,7 @@ private int logLevel = 0; private int protocolVersion = 0; private String applicationName; + private boolean logLevelSet = false; /** * Gets a connection to the PostgreSQL database. The database is identified by the @@ -184,6 +185,7 @@ public void setLogLevel(int logLevel) { this.logLevel = logLevel; + logLevelSet = true; } public int getProtocolVersion() @@ -295,6 +297,14 @@ } /** + * Gets the write buffer size of TCP/IP socket. + */ + public int getReceiveBufferSize() + { + return receiveBufferSize; + } + + /** * Sets the write buffer size of TCP/IP socket. */ public void setReceiveBufferSize(int nbytes) @@ -302,6 +312,14 @@ this.receiveBufferSize = nbytes; } + /** + * Gets the send buffer size of TCP/IP socket. + */ + public int getSendBufferSize() + { + return sendBufferSize; + } + /** * Sets the send buffer size of TCP/IP socket. */ @@ -473,7 +491,7 @@ /** * Generates a DriverManager URL from the other properties supplied. */ - private String getUrl() + public String getUrl() { StringBuffer sb = new StringBuffer(100); sb.append("jdbc:postgresql://"); @@ -486,7 +504,15 @@ sb.append("&socketTimeout=").append(socketTimeout); sb.append("&prepareThreshold=").append(prepareThreshold); sb.append("&unknownLength=").append(unknownLength); - sb.append("&loglevel=").append(logLevel); + if(user != null) { + sb.append("&user=").append(user); + } + if(password != null) { + sb.append("&password=").append(password); + } + if (logLevelSet) { + sb.append("&loglevel=").append(logLevel); + } if (protocolVersion != 0) { sb.append("&protocolVersion=").append(protocolVersion); } @@ -510,10 +536,14 @@ sb.append("&ApplicationName="); sb.append(applicationName); } - if (binaryTransfer) { - sb.append("&binaryTransfer=true"); + sb.append("&binaryTransfer=").append(binaryTransfer); + if (binaryTransferEnable != null) { + sb.append("&binaryTransferEnable=").append(binaryTransferEnable); } - + if (binaryTransferDisable != null) { + sb.append("&binaryTransferDisable=").append(binaryTransferDisable); + } + return sb.toString(); } @@ -548,11 +578,22 @@ ref.add(new StringRefAddr("prepareThreshold", Integer.toString(prepareThreshold))); ref.add(new StringRefAddr("unknownLength", Integer.toString(unknownLength))); ref.add(new StringRefAddr("binaryTransfer", Boolean.toString(binaryTransfer))); + if (binaryTransferEnable != null) + { + ref.add(new StringRefAddr("binaryTransferEnable", binaryTransferEnable)); + } + if (binaryTransferDisable != null) + { + ref.add(new StringRefAddr("binaryTransferDisable", binaryTransferDisable)); + } ref.add(new StringRefAddr("loginTimeout", Integer.toString(loginTimeout))); ref.add(new StringRefAddr("socketTimeout", Integer.toString(socketTimeout))); ref.add(new StringRefAddr("ssl", Boolean.toString(ssl))); - ref.add(new StringRefAddr("sslfactory", sslfactory)); + if(sslfactory !=null) + { + ref.add(new StringRefAddr("sslfactory", sslfactory)); + } ref.add(new StringRefAddr("receiveBufferSize", Integer.toString(receiveBufferSize))); ref.add(new StringRefAddr("sendBufferSize", Integer.toString(sendBufferSize))); @@ -562,9 +603,15 @@ ref.add(new StringRefAddr("compatible", compatible)); } - ref.add(new StringRefAddr("logLevel", Integer.toString(logLevel))); + if(logLevelSet) + { + ref.add(new StringRefAddr("logLevel", Integer.toString(logLevel))); + } ref.add(new StringRefAddr("protocolVersion", Integer.toString(protocolVersion))); - ref.add(new StringRefAddr("ApplicationName", applicationName)); + if(applicationName != null) + { + ref.add(new StringRefAddr("ApplicationName", applicationName)); + } return ref; } @@ -592,6 +639,7 @@ out.writeBoolean(binaryTransfer); out.writeObject(binaryTransferEnable); out.writeObject(binaryTransferDisable); + out.writeBoolean(logLevelSet); } protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException @@ -617,6 +665,7 @@ binaryTransfer = in.readBoolean(); binaryTransferEnable = (String)in.readObject(); binaryTransferDisable = (String)in.readObject(); + logLevelSet = in.readBoolean(); } public void initializeFrom(BaseDataSource source) throws IOException, ClassNotFoundException { diff --git a/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java b/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java index 8871d4a..32d1fb0 100644 --- a/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java +++ b/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java @@ -11,6 +11,7 @@ import org.postgresql.test.TestUtil; import org.postgresql.test.util.MiniJndiContextFactory; import org.postgresql.ds.common.BaseDataSource; +import org.postgresql.jdbc2.optional.SimpleDataSource; import org.postgresql.PGConnection; import java.sql.*; @@ -262,4 +263,33 @@ protected void compareJndiDataSource(BaseDataSource oldbds, BaseDataSource bds) { assertTrue("DataSource was dereferenced, should have been serialized or recreated", bds != oldbds); } + + public void testGetURL() + { + BaseDataSource ds; + ds = (BaseDataSource)new SimpleDataSource(); + assertTrue("Got database name of 'null' from getUrl() !", ds.getUrl().indexOf("/null?")<0); + + assertTrue("Got 'loglevel=' from getUrl(),Driver.setLoglevel() should not be overrided without BaseDataSource.setLoglevel() called!", ds.getUrl().indexOf("loglevel=")<0); + ds.setLogLevel(0); + assertTrue("Should include 'loglevel=0' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("loglevel=0")>0); + ds.setLogLevel(1); + assertTrue("Should include 'loglevel=1' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("loglevel=1")>0); + + ds.setBinaryTransfer(false); + assertTrue("Should include 'binaryTransfer=false' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("binaryTransfer=false")>0); + ds.setBinaryTransfer(true); + ds.setBinaryTransferEnable("1"); + ds.setBinaryTransferDisable("2"); + assertTrue("Should include 'binaryTransfer=true' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("binaryTransfer=true")>0); + assertTrue("Should include 'binaryTransferEnable=1' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("binaryTransferEnable=1")>0); + assertTrue("Should include 'binaryTransferDisable=2' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("binaryTransferDisable=2")>0); + + ds.setUser("aaa"); + ds.setPassword("bbb"); + assertTrue("Should include 'user=aaa' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("user=aaa")>0); + assertTrue("Should include 'password=bbb' in getUrl() :" + ds.getUrl(), ds.getUrl().indexOf("password=bbb")>0); + + } + }