SqlStatementFactory { String createInsertSqlString(String quotedTablePath, EntryColumnData entryColumnData) { StringBuilder columnNames = new StringBuilder("INSERT INTO ") .append(quotedTablePath) .append(" ("); StringBuilder columnValues = new StringBuilder(" VALUES ("); addColumnDataToSqlString(entryColumnData, columnNames, columnValues); columnNames.append(')'); columnValues.append(')'); return columnNames.append(columnValues).toString(); } SqlStatementFactory(String identifierQuoteString);   }
@Test public void getInsertSqlString() throws Exception { String expectedStatement = String.format("INSERT INTO %s (%s,%s,%s) VALUES (?,?,?)", QUOTED_TABLE_PATH, quoted(VALUE_COLUMN_1_NAME), quoted(VALUE_COLUMN_2_NAME), quoted(KEY_COLUMN_1_NAME)); String statement = factory.createInsertSqlString(QUOTED_TABLE_PATH, entryColumnData); assertThat(statement).isEqualTo(expectedStatement); }
@Test public void getInsertSqlStringGivenNoColumns() throws Exception { valueColumnData.clear(); keyColumnData.clear(); String statement = factory.createInsertSqlString(QUOTED_TABLE_PATH, entryColumnData); String expectedStatement = String.format("INSERT INTO %s () VALUES ()", QUOTED_TABLE_PATH); assertThat(statement).isEqualTo(expectedStatement); }
@Test public void getInsertSqlStringGivenMultipleKeys() throws Exception { keyColumnData.add(new ColumnData(KEY_COLUMN_2_NAME, null, JDBCType.NULL)); String statement = factory.createInsertSqlString(QUOTED_TABLE_PATH, entryColumnData); String expectedStatement = String.format("INSERT INTO %s (%s,%s,%s,%s) VALUES (?,?,?,?)", QUOTED_TABLE_PATH, quoted(VALUE_COLUMN_1_NAME), quoted(VALUE_COLUMN_2_NAME), quoted(KEY_COLUMN_1_NAME), quoted(KEY_COLUMN_2_NAME)); assertThat(statement).isEqualTo(expectedStatement); }