Skip to content

Commit

Permalink
Use catalog name in SimpleJdbcInsert
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jan 26, 2024
1 parent b9bad56 commit ad7c090
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -303,33 +303,25 @@ public String createInsertString(String... generatedKeyNames) {

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;
Expand All @@ -339,14 +331,7 @@ public String createInsertString(String... generatedKeyNames) {
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(");
Expand Down Expand Up @@ -440,4 +425,27 @@ public boolean isGeneratedKeysColumnNameArraySupported() {
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);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -225,4 +225,41 @@ void usingColumnsAndQuotedIdentifiersWithSchemaName() throws Exception {
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(?, ?)");
}

}

0 comments on commit ad7c090

Please sign in to comment.