mirror of https://github.com/apache/druid.git
remove duplicate code in table creation
This commit is contained in:
parent
123296b767
commit
e8b4640875
|
@ -70,30 +70,12 @@ public class MySQLConnector extends SQLMetadataConnector
|
|||
}
|
||||
|
||||
@Override
|
||||
public void createTable(final IDBI dbi, final String tableName, final String sql)
|
||||
protected boolean tableExists(Handle handle, String tableName)
|
||||
{
|
||||
try {
|
||||
dbi.withHandle(
|
||||
new HandleCallback<Void>()
|
||||
{
|
||||
@Override
|
||||
public Void withHandle(Handle handle) throws Exception
|
||||
{
|
||||
List<Map<String, Object>> table = handle.select(String.format("SHOW tables LIKE '%s'", tableName));
|
||||
if (table.isEmpty()) {
|
||||
log.info("Creating table[%s]", tableName);
|
||||
handle.createStatement(sql).execute();
|
||||
} else {
|
||||
log.info("Table[%s] existed: [%s]", tableName, table);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn(e, "Exception creating table");
|
||||
}
|
||||
return !handle.createQuery("SHOW tables LIKE :tableName")
|
||||
.bind("tableName", tableName)
|
||||
.list()
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -29,9 +29,7 @@ import org.skife.jdbi.v2.DBI;
|
|||
import org.skife.jdbi.v2.Handle;
|
||||
import org.skife.jdbi.v2.IDBI;
|
||||
import org.skife.jdbi.v2.tweak.HandleCallback;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.skife.jdbi.v2.util.StringMapper;
|
||||
|
||||
public class PostgreSQLConnector extends SQLMetadataConnector
|
||||
{
|
||||
|
@ -61,30 +59,15 @@ public class PostgreSQLConnector extends SQLMetadataConnector
|
|||
}
|
||||
|
||||
@Override
|
||||
public void createTable(final IDBI dbi, final String tableName, final String sql)
|
||||
protected boolean tableExists(final Handle handle, final String tableName)
|
||||
{
|
||||
try {
|
||||
dbi.withHandle(
|
||||
new HandleCallback<Void>()
|
||||
{
|
||||
@Override
|
||||
public Void withHandle(Handle handle) throws Exception
|
||||
{
|
||||
List<Map<String, Object>> table = handle.select(String.format("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename LIKE '%s'", tableName));
|
||||
if (table.isEmpty()) {
|
||||
log.info("Creating table[%s]", tableName);
|
||||
handle.createStatement(sql).execute();
|
||||
} else {
|
||||
log.info("Table[%s] existed: [%s]", tableName, table);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn(e, "Exception creating table");
|
||||
}
|
||||
return !handle.createQuery(
|
||||
"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename LIKE :tableName"
|
||||
)
|
||||
.bind("tableName", tableName)
|
||||
.map(StringMapper.FIRST)
|
||||
.list()
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -24,9 +24,12 @@ import com.google.common.base.Supplier;
|
|||
import com.google.inject.Inject;
|
||||
import org.apache.derby.drda.NetworkServerControl;
|
||||
import org.skife.jdbi.v2.DBI;
|
||||
import org.skife.jdbi.v2.Handle;
|
||||
import org.skife.jdbi.v2.tweak.ConnectionFactory;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DerbyConnector extends SQLMetadataConnector
|
||||
{
|
||||
|
@ -40,6 +43,15 @@ public class DerbyConnector extends SQLMetadataConnector
|
|||
this.dbi = new DBI(getConnectionFactory("druidDerbyDb"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean tableExists(Handle handle, String tableName)
|
||||
{
|
||||
return !handle.createQuery("select * from SYS.SYSTABLES where tablename = :tableName")
|
||||
.bind("tableName", tableName.toUpperCase())
|
||||
.list()
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSerialType()
|
||||
{
|
||||
|
|
|
@ -77,6 +77,8 @@ public abstract class SQLMetadataConnector implements MetadataStorageConnector
|
|||
*/
|
||||
protected abstract String getSerialType();
|
||||
|
||||
protected abstract boolean tableExists(Handle handle, final String tableName);
|
||||
|
||||
public void createTable(final IDBI dbi, final String tableName, final String sql)
|
||||
{
|
||||
try {
|
||||
|
@ -86,12 +88,11 @@ public abstract class SQLMetadataConnector implements MetadataStorageConnector
|
|||
@Override
|
||||
public Void withHandle(Handle handle) throws Exception
|
||||
{
|
||||
List<Map<String, Object>> table = handle.select(String.format("select * from SYS.SYSTABLES where tablename = \'%s\'", tableName.toUpperCase()));
|
||||
if (table.isEmpty()) {
|
||||
if (!tableExists(handle, tableName)) {
|
||||
log.info("Creating table[%s]", tableName);
|
||||
handle.createStatement(sql).execute();
|
||||
} else {
|
||||
log.info("Table[%s] existed: [%s]", tableName, table);
|
||||
log.info("Table[%s] already exists", tableName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue