mirror of https://github.com/apache/druid.git
support PostgreSQL >= 9.5 upsert capability
This commit is contained in:
parent
18b9ea62cf
commit
0f8a037bcd
|
@ -31,6 +31,7 @@ import org.skife.jdbi.v2.Handle;
|
||||||
import org.skife.jdbi.v2.tweak.HandleCallback;
|
import org.skife.jdbi.v2.tweak.HandleCallback;
|
||||||
import org.skife.jdbi.v2.util.StringMapper;
|
import org.skife.jdbi.v2.util.StringMapper;
|
||||||
|
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class PostgreSQLConnector extends SQLMetadataConnector
|
public class PostgreSQLConnector extends SQLMetadataConnector
|
||||||
|
@ -41,6 +42,8 @@ public class PostgreSQLConnector extends SQLMetadataConnector
|
||||||
|
|
||||||
private final DBI dbi;
|
private final DBI dbi;
|
||||||
|
|
||||||
|
private volatile Boolean canUpsert;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public PostgreSQLConnector(Supplier<MetadataStorageConnectorConfig> config, Supplier<MetadataStorageTablesConfig> dbTables)
|
public PostgreSQLConnector(Supplier<MetadataStorageConnectorConfig> config, Supplier<MetadataStorageTablesConfig> dbTables)
|
||||||
{
|
{
|
||||||
|
@ -68,6 +71,18 @@ public class PostgreSQLConnector extends SQLMetadataConnector
|
||||||
return SERIAL_TYPE;
|
return SERIAL_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean canUpsert(Handle handle) throws SQLException
|
||||||
|
{
|
||||||
|
if (canUpsert == null) {
|
||||||
|
DatabaseMetaData metaData = handle.getConnection().getMetaData();
|
||||||
|
canUpsert = metaData.getDatabaseMajorVersion() > 9 || (
|
||||||
|
metaData.getDatabaseMajorVersion() == 9 &&
|
||||||
|
metaData.getDatabaseMinorVersion() >= 5
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return canUpsert;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean tableExists(final Handle handle, final String tableName)
|
public boolean tableExists(final Handle handle, final String tableName)
|
||||||
{
|
{
|
||||||
|
@ -95,6 +110,19 @@ public class PostgreSQLConnector extends SQLMetadataConnector
|
||||||
@Override
|
@Override
|
||||||
public Void withHandle(Handle handle) throws Exception
|
public Void withHandle(Handle handle) throws Exception
|
||||||
{
|
{
|
||||||
|
if (canUpsert(handle)) {
|
||||||
|
handle.createStatement(
|
||||||
|
String.format(
|
||||||
|
"INSERT INTO %1$s (%2$s, %3$s) VALUES (:key, :value) ON CONFLICT (%2$s) DO UPDATE SET %3$s = EXCLUDED.%3$s",
|
||||||
|
tableName,
|
||||||
|
keyColumn,
|
||||||
|
valueColumn
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.bind("key", key)
|
||||||
|
.bind("value", value)
|
||||||
|
.execute();
|
||||||
|
} else {
|
||||||
handle.createStatement(
|
handle.createStatement(
|
||||||
String.format(
|
String.format(
|
||||||
"BEGIN;\n" +
|
"BEGIN;\n" +
|
||||||
|
@ -110,6 +138,7 @@ public class PostgreSQLConnector extends SQLMetadataConnector
|
||||||
.bind("key", key)
|
.bind("key", key)
|
||||||
.bind("value", value)
|
.bind("value", value)
|
||||||
.execute();
|
.execute();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue