/** * This method is called when TemplateUtil.applyTemplate() encounters a parameter. * TemplateUtil.applyTemplate is called when someone makes a call to applyInputsToFormat() In this * class it is called in the above "runQuery()" method. * * @param template the source string * @param parameter the parameter value * @param parameterMatcher the regex parameter matcher * @param copyStart the start of the copy * @param results the output result * @return the next copystart */ @Override public int resolveParameter( final String template, final String parameter, final Matcher parameterMatcher, int copyStart, final StringBuffer results) { StringTokenizer tokenizer = new StringTokenizer(parameter, ":"); // $NON-NLS-1$ if (tokenizer.countTokens() == 2) { // Currently, the component only handles one bit of metadata String parameterPrefix = tokenizer.nextToken(); String inputName = tokenizer.nextToken(); // if the template contains a prepare later prefix, // mark a spot in the preparedParameters list and move on. if (parameterPrefix.equals(IPreparedComponent.PREPARE_LATER_PREFIX)) { if (!isDefinedOutput(IPreparedComponent.PREPARED_COMPONENT_NAME)) { error( Messages.getInstance() .getErrorString( "IPreparedComponent.ERROR_0003_INVALID_PARAMETER_STATE")); //$NON-NLS-1$ return -1; } preparedParameters.add(IPreparedComponent.PREPARE_LATER_PLACEHOLDER); int start = parameterMatcher.start(); int end = parameterMatcher.end(); results.append(template.substring(copyStart, start)); results.append( "{" + IPreparedComponent.PREPARE_LATER_INTER_PREFIX + ":" + inputName + "}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ return end; } if (parameterPrefix.equals(SQLBaseComponent.PREPARE_PARAMETER_PREFIX)) { // We know this parameter is for us. // First, is this a special input Object parameterValue = TemplateUtil.getSystemInput(inputName, getRuntimeContext()); if ((parameterValue == null) && isDefinedInput(inputName)) { parameterValue = this.getInputValue(inputName); } if (parameterValue != null) { // We have a parameter value - now, it's time to create a parameter and build up the // parameter string int start = parameterMatcher.start(); int end = parameterMatcher.end(); // First, find out if the parameter was quoted... if ((start > 0) && (end < template.length())) { if ((template.charAt(start - 1) == '\'') && (template.charAt(end) == '\'')) { // Ok, the parameter was quoted as near as we can tell. So, we need // to increase the size of the amount we overwrite by one in each // direction. This is for backward compatibility. start--; end++; } } // We now have a valid start and end. It's time to see whether we're dealing // with an array, a result set, or a scalar. StringBuffer parameterBuffer = new StringBuffer(); if (parameterValue instanceof String) { preparedParameters.add(parameterValue); parameterBuffer.append('?'); } else if (parameterValue instanceof Object[]) { Object[] pObj = (Object[]) parameterValue; for (Object element : pObj) { preparedParameters.add(element); parameterBuffer.append( (parameterBuffer.length() == 0) ? "?" : ",?"); // $NON-NLS-1$ //$NON-NLS-2$ } } else if (parameterValue instanceof IPentahoResultSet) { IPentahoResultSet rs = (IPentahoResultSet) parameterValue; // See if we can find a column in the metadata with the same // name as the input IPentahoMetaData md = rs.getMetaData(); int columnIdx = -1; if (md.getColumnCount() == 1) { columnIdx = 0; } else { columnIdx = md.getColumnIndex(new String[] {parameter}); } if (columnIdx < 0) { error( Messages.getInstance() .getErrorString( "Template.ERROR_0005_COULD_NOT_DETERMINE_COLUMN")); //$NON-NLS-1$ return -1; } int rowCount = rs.getRowCount(); Object valueCell = null; // TODO support non-string columns for (int i = 0; i < rowCount; i++) { valueCell = rs.getValueAt(i, columnIdx); preparedParameters.add(valueCell); parameterBuffer.append( (parameterBuffer.length() == 0) ? "?" : ",?"); // $NON-NLS-1$ //$NON-NLS-2$ } } else if (parameterValue instanceof List) { List pObj = (List) parameterValue; for (int i = 0; i < pObj.size(); i++) { preparedParameters.add(pObj.get(i)); parameterBuffer.append( (parameterBuffer.length() == 0) ? "?" : ",?"); // $NON-NLS-1$ //$NON-NLS-2$ } } else { // If we're here, we know parameterValue is not null and not a string this.preparedParameters.add(parameterValue); parameterBuffer.append('?'); } // OK - We have a parameterBuffer and have filled out the preparedParameters // list. It's time to change the SQL to insert our parameter marker and tell // the caller we've done our job. results.append(template.substring(copyStart, start)); copyStart = end; results.append(parameterBuffer); return copyStart; } } } return -1; // Nothing here for us - let default behavior through }