diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java index 601a6c334056..b9c4505eeb7b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java @@ -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 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; @@ -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("); @@ -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); + } + } + } + } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java index dcb4305d9289..25d07b5794a1 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java @@ -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 @@ 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(?, ?)"); + } + }