Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JBERET-169 JdbcItemWriter#open() produces ArrayIndexOutOfBoundsExcept… #52

Merged
merged 2 commits into from May 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -80,4 +80,7 @@ public interface SupportMessages {
@Message(id = 60020, value = "Expecting JMS message types: %s, but got %s, %s")
BatchRuntimeException unexpectedJmsMessageType(String expectedTypes, String actualType, Object val);

@Message(id = 60021, value = "Failed to determine parameterNames due to a complex SQL was supplied. please specify parameterNames explicitly")
BatchRuntimeException failedToDetermineParameterNames();

}
Expand Up @@ -123,30 +123,40 @@ public void writeItems(final List<Object> items) throws Exception {
public void open(final Serializable checkpoint) throws Exception {
init();

if (parameterNames == null) {
final String sqlLowerCase = sql.toLowerCase();
final int insertPos = sqlLowerCase.indexOf("insert");
int leftParenthesisPos = sqlLowerCase.indexOf('(', insertPos + 7);
int rightParenthesisPos = sqlLowerCase.indexOf(')', leftParenthesisPos + 1);
final String[] columns = sql.substring(leftParenthesisPos + 1, rightParenthesisPos).split(",");
final int valuesPos = sqlLowerCase.indexOf("values", rightParenthesisPos + 1);
leftParenthesisPos = sqlLowerCase.indexOf('(', valuesPos + 1);
rightParenthesisPos = sqlLowerCase.lastIndexOf(')');
if (parameterNames == null && beanType != java.util.List.class) {
parameterNames = determineParameterNames(sql);
}
}

if (rightParenthesisPos <= leftParenthesisPos) {
throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, sql, "sql");
}
static String[] determineParameterNames(final String sql) {
final String sqlLowerCase = sql.toLowerCase();
final int insertPos = sqlLowerCase.indexOf("insert");
int leftParenthesisPos = sqlLowerCase.indexOf('(', insertPos + 7);
int rightParenthesisPos = sqlLowerCase.indexOf(')', leftParenthesisPos + 1);
final String[] columns = sql.substring(leftParenthesisPos + 1, rightParenthesisPos).split(",");
final int valuesPos = sqlLowerCase.indexOf("values", rightParenthesisPos + 1);
leftParenthesisPos = sqlLowerCase.indexOf('(', valuesPos + 1);
rightParenthesisPos = sqlLowerCase.lastIndexOf(')');

final String[] values = sql.substring(leftParenthesisPos + 1, rightParenthesisPos).split(",");
final List<String> parameterNamesList = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
final String v = values[i].trim();
if (v.equals("?")) {
parameterNamesList.add(columns[i].trim());
}
if (rightParenthesisPos <= leftParenthesisPos) {
throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, sql, "sql");
}

final String[] values = sql.substring(leftParenthesisPos + 1, rightParenthesisPos).split(",");

if (values.length != columns.length) {
throw SupportMessages.MESSAGES.failedToDetermineParameterNames();
}

final List<String> parameterNamesList = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
final String v = values[i].trim();
if (v.equals("?")) {
parameterNamesList.add(columns[i].trim());
}
parameterNames = parameterNamesList.toArray(new String[parameterNamesList.size()]);
}

return parameterNamesList.toArray(new String[parameterNamesList.size()]);
}

@Override
Expand All @@ -164,7 +174,7 @@ private void mapParameters(final Object item) throws Exception {
final List itemAsList = (List) item;
//the item is a list and should contain data of proper types, e.g., String, Integer, Date, etc,
//and in the same order as SQL insert statement parameters.
for (int i = 0; i < parameterNames.length; ++i) {
for (int i = 0; i < itemAsList.size(); ++i) {
setParameter(i, itemAsList.get(i));
}
} else {
Expand Down
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2015 Red Hat, Inc. and/or its affiliates.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
*/
package org.jberet.support.io;

import org.junit.Test;

import javax.batch.operations.BatchRuntimeException;

import static org.junit.Assert.*;
import static org.jberet.support.io.JdbcItemWriter.determineParameterNames;

@SuppressWarnings("SpellCheckingInspection")
public class JdbcItemWriterTest {

@Test
public void normal() throws Exception {
final String sql = "INSERT INTO forex (symbol, ts, bid_open, bid_high, bid_low, bid_close, volume) " +
"values ('USDJPY', ?, ?, ?, ?, ?, ?)";
final String[] actual = determineParameterNames(sql);
final String[] expected = {"ts", "bid_open", "bid_high", "bid_low", "bid_close", "volume"};
assertArrayEquals(expected, actual);
}

@Test(expected = BatchRuntimeException.class)
public void canNotDetermine() throws Exception {
final String sql = "INSERT INTO forex (symbol, ts, bid_open, bid_high, bid_low, bid_close, volume) " +
"values ('USDJPY', parsedatetime('yyyyMMdd HHmmss', ?), ?, ?, ?, ?, ?)";
determineParameterNames(sql);
}
}