Use catalog name in SimpleJdbcInsert

This commit harmonizes SimpleJdbcCall and SimpleJdbcInsert to
consistently use a catalog name if one is set. Previously,
SimpleJdbcInsert only used the catalog name to retrieve database
metadata.

Closes gh-32124
This commit is contained in:
Stéphane Nicoll 2024-01-26 17:56:07 +01:00
parent b9bad56fc1
commit ad7c090f4c
2 changed files with 72 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -303,33 +303,25 @@ public class TableMetaDataContext {
String identifierQuoteString = (isQuoteIdentifiers() ?
obtainMetaDataProvider().getIdentifierQuoteString() : null);
boolean quoting = StringUtils.hasText(identifierQuoteString);
QuoteHandler quoteHandler = new QuoteHandler(identifierQuoteString);
StringBuilder insertStatement = new StringBuilder();
insertStatement.append("INSERT INTO ");
String catalogName = getCatalogName();
if (catalogName != null) {
quoteHandler.appendTo(insertStatement, catalogName);
insertStatement.append('.');
}
String schemaName = getSchemaName();
if (schemaName != null) {
if (quoting) {
insertStatement.append(identifierQuoteString);
insertStatement.append(schemaName);
insertStatement.append(identifierQuoteString);
}
else {
insertStatement.append(schemaName);
}
quoteHandler.appendTo(insertStatement, schemaName);
insertStatement.append('.');
}
String tableName = getTableName();
if (quoting) {
insertStatement.append(identifierQuoteString);
insertStatement.append(tableName);
insertStatement.append(identifierQuoteString);
}
else {
insertStatement.append(tableName);
}
quoteHandler.appendTo(insertStatement, tableName);
insertStatement.append(" (");
int columnCount = 0;
@ -339,14 +331,7 @@ public class TableMetaDataContext {
if (columnCount > 1) {
insertStatement.append(", ");
}
if (quoting) {
insertStatement.append(identifierQuoteString);
insertStatement.append(columnName);
insertStatement.append(identifierQuoteString);
}
else {
insertStatement.append(columnName);
}
quoteHandler.appendTo(insertStatement, columnName);
}
}
insertStatement.append(") VALUES(");
@ -440,4 +425,27 @@ public class TableMetaDataContext {
return obtainMetaDataProvider().isGeneratedKeysColumnNameArraySupported();
}
private static final class QuoteHandler {
@Nullable
private final String identifierQuoteString;
private final boolean quoting;
private QuoteHandler(@Nullable String identifierQuoteString) {
this.identifierQuoteString = identifierQuoteString;
this.quoting = StringUtils.hasText(identifierQuoteString);
}
public void appendTo(StringBuilder stringBuilder, String item) {
if (this.quoting) {
stringBuilder.append(this.identifierQuoteString)
.append(item).append(this.identifierQuoteString);
}
else {
stringBuilder.append(item);
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -225,4 +225,41 @@ class SimpleJdbcInsertTests {
assertThat(insert.getInsertString()).isEqualTo("INSERT INTO `my_schema`.`my_table` (`col1`, `col2`) VALUES(?, ?)");
}
@Test
void usingSchema() {
SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource)
.withTableName("my_table")
.withSchemaName("my_schema")
.usingColumns("col1", "col2");
insert.compile();
assertThat(insert.getInsertString()).isEqualTo("INSERT INTO my_schema.my_table (col1, col2) VALUES(?, ?)");
}
@Test
void usingCatalog() {
SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource)
.withTableName("my_table")
.withCatalogName("my_catalog")
.usingColumns("col1", "col2");
insert.compile();
assertThat(insert.getInsertString()).isEqualTo("INSERT INTO my_catalog.my_table (col1, col2) VALUES(?, ?)");
}
@Test
void usingSchemaAndCatalog() {
SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource)
.withTableName("my_table")
.withSchemaName("my_schema")
.withCatalogName("my_catalog")
.usingColumns("col1", "col2");
insert.compile();
assertThat(insert.getInsertString()).isEqualTo("INSERT INTO my_catalog.my_schema.my_table (col1, col2) VALUES(?, ?)");
}
}