Skip to content

Commit

Permalink
NIFI-12530: Incorporated review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyb149 committed Dec 21, 2023
1 parent 4b9fbfb commit 4824311
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,7 @@ private List<String> getColumnsAssignment(Collection<String> columnsNames, Strin
List<String> conflictClause = new ArrayList<>();

for (String columnName : columnsNames) {

StringBuilder statementStringBuilder = new StringBuilder();

statementStringBuilder.append(getColumnAssignment(table, columnName, newTableAlias));

conflictClause.add(statementStringBuilder.toString());

conflictClause.add(getColumnAssignment(table, columnName, newTableAlias));
}

return conflictClause;
Expand Down Expand Up @@ -269,26 +263,27 @@ public boolean supportsCreateTableIfNotExists() {
*/
@Override
public String getCreateTableStatement(TableSchema tableSchema, boolean quoteTableName, boolean quoteColumnNames) {
StringBuilder createTableStatement = new StringBuilder();
StringBuilder createTableStatement = new StringBuilder()
.append("DECLARE\n\tsql_stmt long;\nBEGIN\n\tsql_stmt:='CREATE TABLE ")
.append(generateTableName(quoteTableName, tableSchema.getCatalogName(), tableSchema.getSchemaName(), tableSchema.getTableName(), tableSchema))
.append(" (");

List<ColumnDescription> columns = tableSchema.getColumnsAsList();
List<String> columnsAndDatatypes = new ArrayList<>(columns.size());
Set<String> primaryKeyColumnNames = tableSchema.getPrimaryKeyColumnNames();
for (ColumnDescription column : columns) {
String sb = (quoteColumnNames ? getColumnQuoteString() : "")
+ column.getColumnName()
+ (quoteColumnNames ? getColumnQuoteString() : "")
+ " " + getSQLForDataType(column.getDataType())
+ (column.isNullable() ? "" : " NOT NULL")
+ (primaryKeyColumnNames != null && primaryKeyColumnNames.contains(column.getColumnName()) ? " PRIMARY KEY" : "");
columnsAndDatatypes.add(sb);
for (int i = 0; i < columns.size(); i++) {
ColumnDescription column = columns.get(i);
createTableStatement
.append((i != 0) ? ", " : "")
.append(quoteColumnNames ? getColumnQuoteString() : "")
.append(column.getColumnName())
.append(quoteColumnNames ? getColumnQuoteString() : "")
.append(" ")
.append(getSQLForDataType(column.getDataType()))
.append(column.isNullable() ? "" : " NOT NULL")
.append(primaryKeyColumnNames != null && primaryKeyColumnNames.contains(column.getColumnName()) ? " PRIMARY KEY" : "");
}

createTableStatement
.append("DECLARE\n\tsql_stmt long;\nBEGIN\n\tsql_stmt:='CREATE TABLE ")
.append(generateTableName(quoteTableName, tableSchema.getCatalogName(), tableSchema.getSchemaName(), tableSchema.getTableName(), tableSchema))
.append(" (")
.append(String.join(", ", columnsAndDatatypes))
.append(")';\nEXECUTE IMMEDIATE sql_stmt;\nEXCEPTION\n\tWHEN OTHERS THEN\n\t\tIF SQLCODE = -955 THEN\n\t\t\t")
.append("NULL;\n\t\tELSE\n\t\t\tRAISE;\n\t\tEND IF;\nEND;");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,27 @@ public boolean supportsCreateTableIfNotExists() {
*/
@Override
public String getCreateTableStatement(TableSchema tableSchema, boolean quoteTableName, boolean quoteColumnNames) {
StringBuilder createTableStatement = new StringBuilder();
StringBuilder createTableStatement = new StringBuilder()
.append("DECLARE\n\tsql_stmt long;\nBEGIN\n\tsql_stmt:='CREATE TABLE ")
.append(generateTableName(quoteTableName, tableSchema.getCatalogName(), tableSchema.getSchemaName(), tableSchema.getTableName(), tableSchema))
.append(" (");

List<ColumnDescription> columns = tableSchema.getColumnsAsList();
List<String> columnsAndDatatypes = new ArrayList<>(columns.size());
Set<String> primaryKeyColumnNames = tableSchema.getPrimaryKeyColumnNames();
for (ColumnDescription column : columns) {
String sb = (quoteColumnNames ? getColumnQuoteString() : "")
+ column.getColumnName()
+ (quoteColumnNames ? getColumnQuoteString() : "")
+ " " + getSQLForDataType(column.getDataType())
+ (column.isNullable() ? "" : " NOT NULL")
+ (primaryKeyColumnNames != null && primaryKeyColumnNames.contains(column.getColumnName()) ? " PRIMARY KEY" : "");
columnsAndDatatypes.add(sb);
for (int i = 0; i < columns.size(); i++) {
ColumnDescription column = columns.get(i);
createTableStatement
.append((i != 0) ? ", " : "")
.append(quoteColumnNames ? getColumnQuoteString() : "")
.append(column.getColumnName())
.append(quoteColumnNames ? getColumnQuoteString() : "")
.append(" ")
.append(getSQLForDataType(column.getDataType()))
.append(column.isNullable() ? "" : " NOT NULL")
.append(primaryKeyColumnNames != null && primaryKeyColumnNames.contains(column.getColumnName()) ? " PRIMARY KEY" : "");
}

createTableStatement
.append("DECLARE\n\tsql_stmt long;\nBEGIN\n\tsql_stmt:='CREATE TABLE ")
.append(generateTableName(quoteTableName, tableSchema.getCatalogName(), tableSchema.getSchemaName(), tableSchema.getTableName(), tableSchema))
.append(" (")
.append(String.join(", ", columnsAndDatatypes))
.append(")';\nEXECUTE IMMEDIATE sql_stmt;\nEXCEPTION\n\tWHEN OTHERS THEN\n\t\tIF SQLCODE = -955 THEN\n\t\t\t")
.append("NULL;\n\t\tELSE\n\t\t\tRAISE;\n\t\tEND IF;\nEND;");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public void testGetUpsertStatement() {
// THEN
testGetUpsertStatement(tableName, columnNames, uniqueKeyColumnNames, expected);
}

@Test
public void testGetCreateTableStatement() {
assertTrue(db.supportsCreateTableIfNotExists());
Expand All @@ -171,6 +170,7 @@ public void testGetCreateTableStatement() {
assertEquals(expectedStatement, actualStatement);
}


private void testGetUpsertStatement(String tableName, List<String> columnNames, Collection<String> uniqueKeyColumnNames, IllegalArgumentException expected) {
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
testGetUpsertStatement(tableName, columnNames, uniqueKeyColumnNames, (String)null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
*/
package org.apache.nifi.processors.standard.db.impl;

import org.apache.nifi.processors.standard.db.ColumnDescription;
import org.apache.nifi.processors.standard.db.DatabaseAdapter;
import org.apache.nifi.processors.standard.db.TableSchema;
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestOracleDatabaseAdapter {

Expand Down Expand Up @@ -102,4 +110,22 @@ public void testPagingQueryUsingColumnValuesForPartitioning() {
String expected4 = "SELECT some(set),of(columns),that,might,contain,methods,a.* FROM database.tablename";
assertEquals(expected4, sql4);
}

@Test
public void testGetCreateTableStatement() {
assertTrue(db.supportsCreateTableIfNotExists());
final List<ColumnDescription> columns = Arrays.asList(
new ColumnDescription("col1", Types.INTEGER, true, 4, false),
new ColumnDescription("col2", Types.VARCHAR, false, 2000, true)
);
TableSchema tableSchema = new TableSchema("USERS", null, "TEST_TABLE", columns, true, Collections.singleton("COL1"), db.getColumnQuoteString());

String expectedStatement = "DECLARE\n\tsql_stmt long;\nBEGIN\n\tsql_stmt:='CREATE TABLE "
// Strings are returned as VARCHAR2(2000) regardless of reported size and that VARCHAR2 is not in java.sql.Types
+ "\"USERS\".\"TEST_TABLE\" (\"col1\" INTEGER NOT NULL, \"col2\" VARCHAR2(2000))';"
+ "\nEXECUTE IMMEDIATE sql_stmt;\nEXCEPTION\n\tWHEN OTHERS THEN\n\t\tIF SQLCODE = -955 THEN\n\t\t\t"
+ "NULL;\n\t\tELSE\n\t\t\tRAISE;\n\t\tEND IF;\nEND;";
String actualStatement = db.getCreateTableStatement(tableSchema, true, true);
assertEquals(expectedStatement, actualStatement);
}
}

0 comments on commit 4824311

Please sign in to comment.