下面列出了怎么用java.sql.DriverPropertyInfo的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void driverPropertyInfoWithoutValues() throws SQLException {
Driver driver = getDriver();
DriverPropertyInfo[] properties =
driver.getPropertyInfo("jdbc:cloudspanner://localhost", null);
assertEquals(ConnectionProperties.NUMBER_OF_PROPERTIES, properties.length);
for (DriverPropertyInfo property : properties) {
if (property.name.equals("AllowExtendedMode") || property.name.equals("AsyncDdlOperations")
|| property.name.equals("AutoBatchDdlOperations")
|| property.name.equals("BatchReadOnlyMode") || property.name.equals("UseCustomHost"))
assertEquals("false", property.value);
else if (property.name.equals("ReportDefaultSchemaAsNull"))
assertEquals("true", property.value);
else
assertNull(property.value);
}
}
/**
* Fix for BUG#22628 - Driver.getPropertyInfo() throws NullPointerException
* for URL that only specifies host and/or port.
*
* @throws Exception
* if the test fails.
*/
public void testBug22628() throws Exception {
DriverPropertyInfo[] dpi = new NonRegisteringDriver().getPropertyInfo("jdbc:mysql://bogus:9999", new Properties());
boolean foundHost = false;
boolean foundPort = false;
for (int i = 0; i < dpi.length; i++) {
if ("bogus".equals(dpi[i].value)) {
foundHost = true;
}
if ("9999".equals(dpi[i].value)) {
foundPort = true;
}
}
assertTrue(foundHost && foundPort);
}
public DriverPropertyInfo[] getPropertyInfo(
String url, Properties info) throws SQLException {
List<DriverPropertyInfo> list = new ArrayList<DriverPropertyInfo>();
// First, add the contents of info
for (Map.Entry<Object, Object> entry : info.entrySet()) {
list.add(
new DriverPropertyInfo(
(String) entry.getKey(),
(String) entry.getValue()));
}
// Next, add property definitions not mentioned in info
for (ConnectionProperty p : getConnectionProperties()) {
if (info.containsKey(p.name())) {
continue;
}
list.add(new DriverPropertyInfo(p.name(), null));
}
return list.toArray(new DriverPropertyInfo[list.size()]);
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException
{
Properties properties = new PrestoDriverUri(url, info).getProperties();
return ConnectionProperties.allProperties().stream()
.map(property -> property.getDriverPropertyInfo(properties))
.toArray(DriverPropertyInfo[]::new);
}
@Override
public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties)
{
String currentValue = mergedProperties.getProperty(key);
DriverPropertyInfo result = new DriverPropertyInfo(key, currentValue);
result.required = isRequired.test(mergedProperties);
return result;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
List<DriverPropertyInfo> list = new ArrayList<DriverPropertyInfo>();
for (Map.Entry<Object, Object> entry : info.entrySet()) {
list.add(
new DriverPropertyInfo(
(String) entry.getKey(),
(String) entry.getValue()));
}
return list.toArray(new DriverPropertyInfo[list.size()]);
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
if (!acceptsURL(url))
return new DriverPropertyInfo[0];
ConnectionProperties properties = ConnectionProperties.parse(url);
properties.setAdditionalConnectionProperties(info);
return properties.getPropertyInfo();
}
@Test
public void driverPropertyInfoWithURLValues() throws SQLException {
Driver driver = getDriver();
DriverPropertyInfo[] properties = driver.getPropertyInfo(
"jdbc:cloudspanner://localhost;Project=adroit-hall-xxx;Instance=test-instance;Database=testdb;PvtKeyPath=C:\\Users\\MyUserName\\Documents\\CloudSpannerKeys\\cloudspanner3.json;SimulateProductName=PostgreSQL",
null);
assertEquals(ConnectionProperties.NUMBER_OF_PROPERTIES, properties.length);
assertEquals("adroit-hall-xxx", properties[0].value);
assertEquals("test-instance", properties[1].value);
assertEquals("testdb", properties[2].value);
assertEquals("C:\\Users\\MyUserName\\Documents\\CloudSpannerKeys\\cloudspanner3.json",
properties[3].value);
assertNull(properties[4].value);
assertEquals("PostgreSQL", properties[5].value);
}
private DriverPropertyInfo getAsDriverPropertyInfo(ReadableProperty<?> pr) {
PropertyDefinition<?> pdef = pr.getPropertyDefinition();
DriverPropertyInfo dpi = new DriverPropertyInfo(pdef.getName(), null);
dpi.choices = pdef.getAllowableValues();
dpi.value = (pr.getStringValue() != null) ? pr.getStringValue() : null;
dpi.required = false;
dpi.description = pdef.getDescription();
return dpi;
}
public DriverPropertyInfo[] getPropertyInfo()
{
ArrayList<DriverPropertyInfo> propInfos = new ArrayList<DriverPropertyInfo>();
addPropInfo(
propInfos,
AUTO_CONNECT_RETRY,
"false",
"If true, the driver will keep trying to connect to the same server in case that the socket "
+ "cannot be established. There is maximum amount of time to keep retrying, which is 15s by "
+ "default.", null);
addPropInfo(propInfos, CONNECTIONS_PER_HOST, "10", "The maximum number of connections allowed per "
+ "host for this Mongo instance. Those connections will be kept in a pool when idle.", null);
addPropInfo(propInfos, CONNECT_TIMEOUT, "10000", "The connection timeout in milliseconds. ", null);
addPropInfo(propInfos, CURSOR_FINALIZER_ENABLED, "true", "Sets whether there is a a finalize "
+ "method created that cleans up instances of DBCursor that the client does not close.",
null);
addPropInfo(propInfos, MAX_AUTO_CONNECT_RETRY_TIME, "0",
"The maximum amount of time in MS to spend retrying to open connection to the same server."
+ "Default is 0, which means to use the default 15s if autoConnectRetry is on.", null);
addPropInfo(propInfos, READ_PREFERENCE, "primary",
"represents preferred replica set members to which a query or command can be sent", new String[] {
"primary", "primary preferred", "secondary", "secondary preferred", "nearest" });
addPropInfo(propInfos, SOCKET_TIMEOUT, "0", "The socket timeout in milliseconds It is used for "
+ "I/O socket read and write operations "
+ "Socket.setSoTimeout(int) Default is 0 and means no timeout.", null);
return propInfos.toArray(new DriverPropertyInfo[propInfos.size()]);
}
private void addPropInfo(final ArrayList<DriverPropertyInfo> propInfos, final String propName,
final String defaultVal, final String description, final String[] choices)
{
DriverPropertyInfo newProp = new DriverPropertyInfo(propName, defaultVal);
newProp.description = description;
if (choices != null)
{
newProp.choices = choices;
}
propInfos.add(newProp);
}
public DriverPropertyInfo[] getPropertyInfo()
{
ArrayList<DriverPropertyInfo> propInfos = new ArrayList<DriverPropertyInfo>();
addPropInfo(
propInfos,
AUTO_CONNECT_RETRY,
"false",
"If true, the driver will keep trying to connect to the same server in case that the socket "
+ "cannot be established. There is maximum amount of time to keep retrying, which is 15s by "
+ "default.", null);
addPropInfo(propInfos, CONNECTIONS_PER_HOST, "10", "The maximum number of connections allowed per "
+ "host for this Mongo instance. Those connections will be kept in a pool when idle.", null);
addPropInfo(propInfos, CONNECT_TIMEOUT, "10000", "The connection timeout in milliseconds. ", null);
addPropInfo(propInfos, CURSOR_FINALIZER_ENABLED, "true", "Sets whether there is a a finalize "
+ "method created that cleans up instances of DBCursor that the client does not close.",
null);
addPropInfo(propInfos, MAX_AUTO_CONNECT_RETRY_TIME, "0",
"The maximum amount of time in MS to spend retrying to open connection to the same server."
+ "Default is 0, which means to use the default 15s if autoConnectRetry is on.", null);
addPropInfo(propInfos, READ_PREFERENCE, "primary",
"represents preferred replica set members to which a query or command can be sent", new String[] {
"primary", "primary preferred", "secondary", "secondary preferred", "nearest" });
addPropInfo(propInfos, SOCKET_TIMEOUT, "0", "The socket timeout in milliseconds It is used for "
+ "I/O socket read and write operations "
+ "Socket.setSoTimeout(int) Default is 0 and means no timeout.", null);
return propInfos.toArray(new DriverPropertyInfo[propInfos.size()]);
}
private void addPropInfo(final ArrayList<DriverPropertyInfo> propInfos, final String propName,
final String defaultVal, final String description, final String[] choices)
{
DriverPropertyInfo newProp = new DriverPropertyInfo(propName, defaultVal);
newProp.description = description;
if (choices != null)
{
newProp.choices = choices;
}
propInfos.add(newProp);
}
DriverPropertyInfo getAsDriverPropertyInfo() {
DriverPropertyInfo dpi = new DriverPropertyInfo(this.propertyName, null);
dpi.choices = getAllowableValues();
dpi.value = (this.valueAsObject != null) ? this.valueAsObject.toString() : null;
dpi.required = this.required;
dpi.description = this.description;
return dpi;
}
protected DriverPropertyInfo[] exposeAsDriverPropertyInfoInternal(Properties info, int slotsToReserve) throws SQLException {
initializeProperties(info);
int numProperties = PROPERTY_LIST.size();
int listSize = numProperties + slotsToReserve;
DriverPropertyInfo[] driverProperties = new DriverPropertyInfo[listSize];
for (int i = slotsToReserve; i < listSize; i++) {
java.lang.reflect.Field propertyField = PROPERTY_LIST.get(i - slotsToReserve);
try {
ConnectionProperty propToExpose = (ConnectionProperty) propertyField.get(this);
if (info != null) {
propToExpose.initializeFrom(info, getExceptionInterceptor());
}
driverProperties[i] = propToExpose.getAsDriverPropertyInfo();
} catch (IllegalAccessException iae) {
throw SQLError.createSQLException(Messages.getString("ConnectionProperties.InternalPropertiesFailure"), SQLError.SQL_STATE_GENERAL_ERROR,
getExceptionInterceptor());
}
}
return driverProperties;
}
private String formatDriverPropertyInfo( final DriverPropertyInfo info ) {
return
"[ "
+ "name = " + formatValue( info.name )
+ ", value = " + formatValue( info.value )
+ ", required = " + info.required
+ ", choices = " + formatValue( info.choices )
+ ", description = " + formatValue( info.description )
+ " ]";
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
Properties props = (Properties)parseURL(url, info)[3];
DriverPropertyInfo[] result = new DriverPropertyInfo[props.size()];
int index = 0;
for(Map.Entry<Object,Object> entry : props.entrySet()){
result[index] = new DriverPropertyInfo((String)entry.getKey(), entry.getValue().toString());
index++;
}
return result;
}
DriverPropertyInfo getAsDriverPropertyInfo() {
DriverPropertyInfo dpi = new DriverPropertyInfo(this.propertyName, null);
dpi.choices = getAllowableValues();
dpi.value = (this.valueAsObject != null) ? this.valueAsObject.toString() : null;
dpi.required = this.required;
dpi.description = this.description;
return dpi;
}
protected DriverPropertyInfo[] exposeAsDriverPropertyInfoInternal(Properties info, int slotsToReserve) throws SQLException {
initializeProperties(info);
int numProperties = PROPERTY_LIST.size();
int listSize = numProperties + slotsToReserve;
DriverPropertyInfo[] driverProperties = new DriverPropertyInfo[listSize];
for (int i = slotsToReserve; i < listSize; i++) {
java.lang.reflect.Field propertyField = PROPERTY_LIST.get(i - slotsToReserve);
try {
ConnectionProperty propToExpose = (ConnectionProperty) propertyField.get(this);
if (info != null) {
propToExpose.initializeFrom(info, getExceptionInterceptor());
}
driverProperties[i] = propToExpose.getAsDriverPropertyInfo();
} catch (IllegalAccessException iae) {
throw SQLError.createSQLException(Messages.getString("ConnectionProperties.InternalPropertiesFailure"), SQLError.SQL_STATE_GENERAL_ERROR,
getExceptionInterceptor());
}
}
return driverProperties;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) {
return new DriverPropertyInfo[0];
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return null;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return null;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return null;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return null;
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return new DriverPropertyInfo[0];
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException
{
return driver.getPropertyInfo(url, info);
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String s, Properties properties) throws SQLException {
return new DriverPropertyInfo[0];
}
@Override
public DriverPropertyInfo[] getPropertyInfo( String url, Properties info ) throws SQLException {
return driver.getPropertyInfo( url, info );
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}