Skip to content

Commit

Permalink
Extract renameTableSql method in BaseJdbcClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Aug 9, 2022
1 parent ca54530 commit 28a62e1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,17 +700,22 @@ protected void renameTable(ConnectorSession session, String catalogName, String
ConnectorIdentity identity = session.getIdentity();
String newRemoteSchemaName = identifierMapping.toRemoteSchemaName(identity, connection, newSchemaName);
String newRemoteTableName = identifierMapping.toRemoteTableName(identity, connection, newRemoteSchemaName, newTableName);
String sql = format(
"ALTER TABLE %s RENAME TO %s",
quoted(catalogName, remoteSchemaName, remoteTableName),
quoted(catalogName, newRemoteSchemaName, newRemoteTableName));
String sql = renameTableSql(catalogName, remoteSchemaName, remoteTableName, newRemoteSchemaName, newRemoteTableName);
execute(connection, sql);
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

protected String renameTableSql(String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
{
return format(
"ALTER TABLE %s RENAME TO %s",
quoted(catalogName, remoteSchemaName, remoteTableName),
quoted(catalogName, newRemoteSchemaName, newRemoteTableName));
}

@Override
public void finishInsertTable(ConnectorSession session, JdbcOutputTableHandle handle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import io.trino.spi.connector.ColumnMetadata;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.type.CharType;
import io.trino.spi.type.DecimalType;
import io.trino.spi.type.Decimals;
Expand Down Expand Up @@ -476,14 +475,13 @@ public void dropTable(ConnectorSession session, JdbcTableHandle handle)
}

@Override
protected void renameTable(ConnectorSession session, String catalogName, String schemaName, String tableName, SchemaTableName newTable)
protected String renameTableSql(String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
{
String sql = format("RENAME TABLE %s.%s TO %s.%s",
quoted(schemaName),
quoted(tableName),
quoted(newTable.getSchemaName()),
quoted(newTable.getTableName()));
execute(session, sql);
return format("RENAME TABLE %s.%s TO %s.%s",
quoted(remoteSchemaName),
quoted(remoteTableName),
quoted(newRemoteSchemaName),
quoted(newRemoteTableName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,17 @@ protected void renameTable(ConnectorSession session, String catalogName, String
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming tables across schemas");
}

String newTableName = newTable.getTableName().toUpperCase(ENGLISH);
String sql = format(
super.renameTable(session, catalogName, schemaName, tableName, newTable);
}

@Override
protected String renameTableSql(String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
{
String newTableName = newRemoteTableName.toUpperCase(ENGLISH);
return format(
"ALTER TABLE %s RENAME TO %s",
quoted(catalogName, schemaName, tableName),
quoted(catalogName, remoteSchemaName, remoteTableName),
quoted(newTableName));

try (Connection connection = connectionFactory.openConnection(session)) {
execute(connection, sql);
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,16 @@ protected void renameTable(ConnectorSession session, String catalogName, String
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming tables across schemas");
}

String sql = format(
super.renameTable(session, catalogName, schemaName, tableName, newTable);
}

@Override
protected String renameTableSql(String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
{
return format(
"ALTER TABLE %s RENAME TO %s",
quoted(catalogName, schemaName, tableName),
quoted(newTable.getTableName()));
execute(session, sql);
quoted(catalogName, remoteSchemaName, remoteTableName),
quoted(newRemoteTableName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ protected void renameTable(ConnectorSession session, String catalogName, String
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming tables across schemas");
}

String sql = format(
super.renameTable(session, catalogName, schemaName, tableName, newTable);
}

@Override
protected String renameTableSql(String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
{
return format(
"ALTER TABLE %s RENAME TO %s",
quoted(catalogName, schemaName, tableName),
quoted(newTable.getTableName()));
execute(session, sql);
quoted(catalogName, remoteSchemaName, remoteTableName),
quoted(newRemoteTableName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,16 @@ protected void renameTable(ConnectorSession session, String catalogName, String
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming tables across schemas");
}

String sql = format(
super.renameTable(session, catalogName, schemaName, tableName, newTable);
}

@Override
protected String renameTableSql(String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
{
return format(
"sp_rename %s, %s",
singleQuote(catalogName, schemaName, tableName),
singleQuote(newTable.getTableName()));
execute(session, sql);
singleQuote(catalogName, remoteSchemaName, remoteTableName),
singleQuote(newRemoteTableName));
}

@Override
Expand Down

0 comments on commit 28a62e1

Please sign in to comment.