Skip to content

Commit

Permalink
Add separate config for JDBC connector metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed May 1, 2015
1 parent 92109c0 commit f62150b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 24 deletions.
Expand Up @@ -14,7 +14,6 @@
package com.facebook.presto.plugin.jdbc;

import io.airlift.configuration.Config;
import io.airlift.configuration.ConfigDescription;

import javax.validation.constraints.NotNull;

Expand All @@ -23,7 +22,6 @@ public class BaseJdbcConfig
private String connectionUrl;
private String connectionUser;
private String connectionPassword;
private boolean allowDropTable;

@NotNull
public String getConnectionUrl()
Expand Down Expand Up @@ -61,17 +59,4 @@ public BaseJdbcConfig setConnectionPassword(String connectionPassword)
this.connectionPassword = connectionPassword;
return this;
}

public boolean getAllowDropTable()
{
return this.allowDropTable;
}

@Config("allow-drop-table")
@ConfigDescription("Allow connector to drop tables")
public BaseJdbcConfig setAllowDropTable(boolean allowDropTable)
{
this.allowDropTable = allowDropTable;
return this;
}
}
Expand Up @@ -47,12 +47,12 @@ public class JdbcMetadata
private final boolean allowDropTable;

@Inject
public JdbcMetadata(JdbcConnectorId connectorId, JdbcClient jdbcClient, BaseJdbcConfig config)
public JdbcMetadata(JdbcConnectorId connectorId, JdbcClient jdbcClient, JdbcMetadataConfig config)
{
this.jdbcClient = checkNotNull(jdbcClient, "client is null");

checkNotNull(config, "config is null");
allowDropTable = config.getAllowDropTable();
allowDropTable = config.isAllowDropTable();
}

@Override
Expand Down
@@ -0,0 +1,35 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.plugin.jdbc;

import io.airlift.configuration.Config;
import io.airlift.configuration.ConfigDescription;

public class JdbcMetadataConfig
{
private boolean allowDropTable;

public boolean isAllowDropTable()
{
return allowDropTable;
}

@Config("allow-drop-table")
@ConfigDescription("Allow connector to drop tables")
public JdbcMetadataConfig setAllowDropTable(boolean allowDropTable)
{
this.allowDropTable = allowDropTable;
return this;
}
}
Expand Up @@ -18,6 +18,7 @@
import com.google.inject.Scopes;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.airlift.configuration.ConfigurationModule.bindConfig;

public class JdbcModule
implements Module
Expand All @@ -39,5 +40,6 @@ public void configure(Binder binder)
binder.bind(JdbcHandleResolver.class).in(Scopes.SINGLETON);
binder.bind(JdbcRecordSinkProvider.class).in(Scopes.SINGLETON);
binder.bind(JdbcConnector.class).in(Scopes.SINGLETON);
bindConfig(binder).to(JdbcMetadataConfig.class);
}
}
Expand Up @@ -27,8 +27,7 @@ public void testDefaults()
ConfigAssertions.assertRecordedDefaults(ConfigAssertions.recordDefaults(BaseJdbcConfig.class)
.setConnectionUrl(null)
.setConnectionUser(null)
.setConnectionPassword(null)
.setAllowDropTable(false));
.setConnectionPassword(null));
}

@Test
Expand All @@ -38,14 +37,12 @@ public void testExplicitPropertyMappings()
.put("connection-url", "jdbc:h2:mem:config")
.put("connection-user", "user")
.put("connection-password", "password")
.put("allow-drop-table", "true")
.build();

BaseJdbcConfig expected = new BaseJdbcConfig()
.setConnectionUrl("jdbc:h2:mem:config")
.setConnectionUser("user")
.setConnectionPassword("password")
.setAllowDropTable(true);
.setConnectionPassword("password");

ConfigAssertions.assertFullMapping(properties, expected);
}
Expand Down
Expand Up @@ -52,7 +52,7 @@ public void setUp()
throws Exception
{
database = new TestingDatabase();
metadata = new JdbcMetadata(new JdbcConnectorId(CONNECTOR_ID), database.getJdbcClient(), new BaseJdbcConfig());
metadata = new JdbcMetadata(new JdbcConnectorId(CONNECTOR_ID), database.getJdbcClient(), new JdbcMetadataConfig());
tableHandle = metadata.getTableHandle(SESSION, new SchemaTableName("example", "numbers"));
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public void testDropTableTable()
assertEquals(e.getErrorCode(), PERMISSION_DENIED.toErrorCode());
}

BaseJdbcConfig config = new BaseJdbcConfig().setAllowDropTable(true);
JdbcMetadataConfig config = new JdbcMetadataConfig().setAllowDropTable(true);
metadata = new JdbcMetadata(new JdbcConnectorId(CONNECTOR_ID), database.getJdbcClient(), config);
metadata.dropTable(tableHandle);

Expand Down
@@ -0,0 +1,46 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.plugin.jdbc;

import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;

import java.util.Map;

import static io.airlift.configuration.testing.ConfigAssertions.assertFullMapping;
import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults;

public class TestJdbcMetadataConfig
{
@Test
public void testDefaults()
{
assertRecordedDefaults(recordDefaults(JdbcMetadataConfig.class)
.setAllowDropTable(false));
}

@Test
public void testExplicitPropertyMappings()
{
Map<String, String> properties = new ImmutableMap.Builder<String, String>()
.put("allow-drop-table", "true")
.build();

JdbcMetadataConfig expected = new JdbcMetadataConfig()
.setAllowDropTable(true);

assertFullMapping(properties, expected);
}
}

0 comments on commit f62150b

Please sign in to comment.