/** * Substitute actual data for the parameter markers to simulate parameter substitution in a * PreparedStatement. * * @param sql The SQL containing parameter markers to substitute. * @param list The parameter descriptors. * @param connection The current connection. * @return The modified SQL statement. */ static String substituteParameters(String sql, ParamInfo[] list, ConnectionJDBC2 connection) throws SQLException { int len = sql.length(); for (int i = 0; i < list.length; i++) { if (!list[i].isRetVal && !list[i].isSet && !list[i].isOutput) { throw new SQLException( Messages.get("error.prepare.paramnotset", Integer.toString(i + 1)), "07000"); } Object value = list[i].value; if (value instanceof java.io.InputStream || value instanceof java.io.Reader) { try { if (list[i].jdbcType == java.sql.Types.LONGVARCHAR || list[i].jdbcType == java.sql.Types.CLOB || list[i].jdbcType == java.sql.Types.VARCHAR) { // TODO: Should improve the character set handling here value = list[i].getString("US-ASCII"); } else { value = list[i].getBytes("US-ASCII"); } // Replace the stream/reader with the String/byte[] list[i].value = value; } catch (java.io.IOException e) { throw new SQLException(Messages.get("error.generic.ioerror", e.getMessage()), "HY000"); } } if (value instanceof String) { len += ((String) value).length() + 5; } else if (value instanceof byte[]) { len += ((byte[]) value).length * 2 + 4; } else { len += 32; // Default size } } StringBuffer buf = new StringBuffer(len + 16); int start = 0; for (int i = 0; i < list.length; i++) { int pos = list[i].markerPos; if (pos > 0) { buf.append(sql.substring(start, list[i].markerPos)); start = pos + 1; final boolean isUnicode = connection.getTdsVersion() >= Driver.TDS70 && list[i].isUnicode; Support.embedData(buf, list[i].value, isUnicode, connection); } } if (start < sql.length()) { buf.append(sql.substring(start)); } return buf.toString(); }
/** * Main method used for test.<br> * Load a file (argument) with a list of dcu_id and check if we have the corresponding det_id. */ public static void main(String args[]) { try { DetIDGenerator d = new DetIDGenerator(); d.exportData( new ArrayList<ArrayList<String>>()); // only to connect to the configuration DB... ArrayList<Integer> dcuIds = loadFile(args[0]); System.out.println(dcuIds.size() + " modules : "); for (Integer dcuId : dcuIds) { try { System.out.println(dcuId + " -> " + d.getDetId(dcuId)); } catch (java.sql.SQLException e) { System.out.print(e.getMessage()); } } } catch (java.io.FileNotFoundException e2) { System.out.println(e2.getMessage()); } catch (java.io.IOException e) { System.out.println("Error while reading the file: \n" + e.getMessage()); } catch (java.sql.SQLException e) { System.out.print(e.getMessage()); } }
/** * When a very large binary value is input to a LONGVARBINARY parameter, it may be more practical * to send it via a java.io.InputStream. JDBC will read the data from the stream as needed, until * it reaches end-of-file. * * <p><B>Note:</B> This stream object can either be a standard Java stream object or your own * subclass that implements the standard interface. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the java input stream which contains the binary parameter value * @param length the number of bytes in the stream * @exception SQLException if a database-access error occurs. */ public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { if (length == 0) { setBytes(parameterIndex, null); } byte[] bs = new byte[length]; int actlen; try { actlen = x.read(bs); } catch (java.io.IOException e) { SQLException newE = new SQLException("setBinaryStream: IO-Exception occured reading Stream" + e.toString()); throw newE; } if (actlen != length) { throw new SQLException( "SetBinaryStream parameterized Length: " + Integer.toString(length) + " got length: " + Integer.toString(actlen)); } else { try { actlen = x.read(bs); } catch (java.io.IOException e) { SQLException newE = new SQLException("setBinaryStream: IO-Exception occured reading Stream" + e.toString()); throw newE; } if (actlen != -1) { throw new SQLException( "SetBinaryStream parameterized Length: " + Integer.toString(length) + " got more than that "); } } this.setBytes(parameterIndex, bs); }
/** * Parses the files passed into the <CODE>setTemplateFiles</CODE> method. The data extracted from * the template files is returned. */ public void parse() { setMessage("Parsing Files"); templates.clear(); importedFieldCount = 0; importedMacroCount = 0; File[] templateFiles = getTemplateFiles(); resetParseCanceled(); int totalFileSize = 0; for (int i = 0; i < templateFiles.length; i++) if (templateFiles[i].exists()) totalFileSize += (int) templateFiles[i].length(); setProgressMaximum(totalFileSize); int progress = 0; setProgressValue(0); setProgressIndeterminate(false); for (int i = 0; i < templateFiles.length; i++) { String currentFilePath = templateFiles[i].getAbsolutePath(); Timestamp modifiedDate = new Timestamp(templateFiles[i].lastModified()); Template currentTemplate = new Template(currentFilePath, modifiedDate); String[] nameParts = templateFiles[i].getName().split("\\."); if (nameParts != null && nameParts.length > 0) currentTemplate.setID(nameParts[0]); templates.add(currentTemplate); try { BufferedReader iStream = new BufferedReader(new FileReader(templateFiles[i])); try { String currentLine = iStream.readLine(); Signal currentSignal = null, archiveTag = null; ArchiveRequest request = null; ArchiveGroup group = null; HashMap archiveSignals = new HashMap(); int lineNumber = 0; int braceCount = 0; while (currentLine != null) // null indicates EOF { lineNumber++; if (currentLine.trim().startsWith("#")) // Comments start with # { // Comments start with #. Archive information is embedded in comments. ArchiveGroup newGroup = parseArchiveGroupTag(currentLine, currentTemplate); if (newGroup != null) group = newGroup; else { ArchiveRequest newRequest = parseArchiveRequestTag(currentLine, group, currentTemplate); if (newRequest != null) request = newRequest; else { Signal newArchiveTag = parseArchiveTag(currentLine); if (newArchiveTag != null) { if (archiveTag != null) // Tag was not used in request. Use for defaults. archiveSignals.put(archiveTag.getID(), archiveTag); archiveTag = newArchiveTag; } } } } else { Matcher macroMatcher = macroPattern.matcher(currentLine); if (macroMatcher.find()) { String macro = macroMatcher.group(1); if (!currentTemplate.containsMacro(macro)) { importedMacroCount++; currentTemplate.addMacro(macro); } } int linePosition = 0; int lineLength = currentLine.length(); while (linePosition < lineLength) { int openBracePosition = currentLine.indexOf('{', linePosition); int closeBracePosition = currentLine.indexOf('}', linePosition); if (currentSignal == null || braceCount == 0) { // Got no signal or the brace was never opened... Matcher recordMatcher = recordPattern.matcher(currentLine); if (recordMatcher.find(linePosition)) if (openBracePosition < 0 || recordMatcher.start() < openBracePosition) { linePosition = recordMatcher.end(); SignalType currentSignalType = new SignalType(); String recordType = recordMatcher.group(1); currentSignalType.setRecordType(new EpicsRecordType(recordType)); String signalID = recordMatcher.group(2); currentSignal = new Signal(signalID); currentSignal.setType(currentSignalType); if (archiveTag != null) archiveSignals.put(archiveTag.getID(), archiveTag); // Use as defaults. archiveTag = (Signal) archiveSignals.get(signalID); if (archiveTag != null) { currentSignal.setArchiveIndicator("Y"); currentSignal.setArchiveType(archiveTag.getArchiveType()); currentSignal.setArchiveFrequency(archiveTag.getArchiveFrequency()); // Must use a new instance of signal since each request has different // values for type, frequency, etc. if (request != null && request.getSignal(signalID) == null) request.addSignal(new Signal(signalID)); currentSignal.setArchiveIndicator("Y"); currentSignal.setArchiveType(archiveTag.getArchiveType()); currentSignal.setArchiveFrequency(archiveTag.getArchiveFrequency()); } currentTemplate.addSignal(currentSignal); archiveTag = null; // Reset so is not used twice. continue; // Go back and check the line position against length. } } if (braceCount == 0 && currentSignal != null && openBracePosition >= linePosition) { // Got the signal, need the open brace. linePosition = openBracePosition + 1; braceCount++; continue; // Go back and check the line position against length. } if (braceCount > 0) { // Looking for fields or the close brace. Matcher fieldMatcher = fieldPattern.matcher(currentLine); if (fieldMatcher.find(linePosition)) if (closeBracePosition < 0 || fieldMatcher.start() < closeBracePosition) { // Found a field... linePosition = fieldMatcher.end(); SignalField currentField = new SignalField(); String currentFieldID = fieldMatcher.group(1); currentField.setType(new SignalFieldType(currentFieldID)); currentField.setValue(fieldMatcher.group(2)); currentSignal.addField(currentField); importedFieldCount++; continue; } if (closeBracePosition >= 0) { // Found end of current signal. braceCount--; linePosition = closeBracePosition + 1; currentSignal = null; continue; } } linePosition = lineLength; if (isParseCanceled()) break; } } progress += currentLine.length() + 1; setProgressValue(progress); currentLine = iStream.readLine(); if (isParseCanceled()) break; } } finally { iStream.close(); } } catch (java.io.FileNotFoundException ex) { StringBuffer errorMessage = new StringBuffer("<HTML><FONT COLOR=RED>Unable to open file '"); errorMessage.append(templateFiles[i].getAbsoluteFile()); errorMessage.append("'.</FONT></HTML>"); addMessage(errorMessage.toString()); } catch (java.io.IOException ex) { ex.printStackTrace(); StringBuffer errorMessage = new StringBuffer("<HTML><FONT COLOR=RED>IO Error: "); errorMessage.append(ex.getMessage()); errorMessage.append("</FONT></HTML>"); addMessage(errorMessage.toString()); } if (isParseCanceled()) break; } }