From: SVN by r. <sv...@ca...> - 2008-09-15 12:02:11
|
Author: roy Date: 2008-09-15 21:01:58 +0200 (Mon, 15 Sep 2008) New Revision: 305 Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java Log: added binary support for read added integer/float support for read added 'save' command to save the previous history as addon to 'spool' Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-09-15 19:00:41 UTC (rev 304) +++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-09-15 19:01:58 UTC (rev 305) @@ -16,6 +16,7 @@ package nl.improved.sqlclient; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; @@ -65,6 +66,7 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; +import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** @@ -155,6 +157,7 @@ commands.register("READ[\\s]*.*[A-Z]+.*", new ReadCommand()); commands.register("QUIT[\\s]*", new QuitCommand("quit")); commands.register("EXIT[\\s]*", new QuitCommand("exit")); + commands.register("SAVE[\\s]*.*", new SaveCommand()); //commands.register("\\\\Q[\\s]*", new QuitCommand("\\q")); commands.register("@.*", new ExecuteBatchCommand()); commands.register("(SELECT|UPDATE|ALTER|INSERT|DELETE).*;[\\s]*", new QueryCommand()); @@ -1715,6 +1718,7 @@ atts.addAttribute("","","name","",metaData.getColumnName(col)); if (metaData.getColumnType(col) == Types.DATE) { atts.addAttribute("","","type","","date"); + atts.addAttribute("","","type_name","",metaData.getColumnTypeName(col)); hd.startElement("","","col",atts); Date date = rs.getDate(col); if (date != null) { @@ -1723,6 +1727,7 @@ } } else if (isBinary(metaData.getColumnType(col))) { atts.addAttribute("","","type","","binary"); + atts.addAttribute("","","type_name","",metaData.getColumnTypeName(col)); hd.startElement("","","col",atts); BASE64Encoder enc = new BASE64Encoder(); @@ -1744,7 +1749,8 @@ } } } else { - atts.addAttribute("","","type","",metaData.getColumnTypeName(col)); + atts.addAttribute("","","type","",Integer.toString(metaData.getColumnType(col))); + atts.addAttribute("","","type_name","",metaData.getColumnTypeName(col)); hd.startElement("","","col",atts); String value= rs.getString(col); if (value != null) { @@ -1868,12 +1874,43 @@ for (int colNr = 0; colNr < columns.getLength(); colNr++) { Element column = (Element) columns.item(colNr); Node type = column.getAttributes().getNamedItem("type"); - if (type != null && type.getNodeValue().equals("date")) { - String nodeValue = column.getTextContent(); - if (nodeValue == null || nodeValue.equals("")) { - pstmt.setDate(colNr+1, null); + if (type != null) { + String typeString = type.getNodeValue(); + if (typeString.equals("date")) { + String nodeValue = column.getTextContent(); + if (nodeValue == null || nodeValue.equals("")) { + pstmt.setDate(colNr+1, null); + } else { + pstmt.setDate(colNr+1, new Date(formatter.parse(nodeValue).getTime())); + } + } else if (typeString.equals("binary")) { // (isBinary(metaData.getColumnType(col))) + String nodeValue = column.getTextContent(); + BASE64Decoder decoder = new BASE64Decoder(); + byte[] value = decoder.decodeBuffer(nodeValue); + //pstmt.setBinaryStream(colNr+1, new ByteArrayInputStream(value)); + pstmt.setBytes(colNr+1, value); } else { - pstmt.setDate(colNr+1, new Date(formatter.parse(nodeValue).getTime())); + String nodeValue = column.getTextContent(); + int iType = Integer.parseInt(typeString); + switch (iType) { + case Types.INTEGER : + case Types.SMALLINT : + case Types.BIGINT : + pstmt.setInt(colNr+1, Integer.parseInt(nodeValue)); + break; + case Types.DOUBLE : pstmt.setDouble(colNr+1, Double.parseDouble(nodeValue)); + break; + case Types.VARCHAR : + case Types.CHAR : + case Types.LONGNVARCHAR : + case Types.NCHAR : + case Types.NVARCHAR : + pstmt.setString(colNr+1, nodeValue); + break; + default: + debug("WARNING Unhandled type: "+ typeString +" trying to fallback to String"); + pstmt.setString(colNr+1, nodeValue); + } } } else { //debug(nodeNr +" ? "+column.getTextContent()); @@ -2015,6 +2052,68 @@ } /** + * Simple command to save the current visible screen history (commands and it"s output). + */ + private class SaveCommand implements Command { + public CharSequence execute(SQLCommand cmd) { + String command = cmd.getCommandString().substring("save".length()).trim(); + if (command.startsWith("history")) { + String fileName = toFileName(command.substring("history".length()).trim()); + File f = new File(fileName); + if (f.exists() && !f.canWrite()) { + return "Unable to overwrite existing file : " + f.getAbsolutePath(); + } + FileWriter writer = null; + try { + writer = new FileWriter(f); + for (CharSequence c : getScreen().getScreenBuffer()) { + writer.write(c.toString()); + writer.write('\n'); + } + return "History successfully written to : "+ f.getAbsolutePath(); + } catch (IOException ex) { + Logger.getLogger(AbstractSQLShellWindow.class.getName()).log(Level.SEVERE, null, ex); + return "Unable to write to file: "+ f.getAbsolutePath() +"("+ ex+")"; + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ex) { + Logger.getLogger(AbstractSQLShellWindow.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + } + + return null; + } + + public CharSequence getCommandString() { + return "save"; + } + + public TabCompletionInfo getTabCompletionInfo(SQLCommand commandInfo, Point commandPoint) { + return null; + } + + public CharSequence getHelp() { + return "history filename\nSaves the history of the screen to the filename provided\n"+ + "This command is an addition to the 'dump' command. It doesn't write future output, "+ + "but it saves the output that is still in the history of 'SQLShell' to the file specified.\n"+ + "Please note that it doesn't write new command output to that file.\n"+ + "For example:'save history ~/history.txt'"; + } + + public boolean abort() { + return false; + } + + public boolean backgroundProcessSupported() { + return false; + } + + } + /** * Command class to execute a 'custom command'. * this makes it possible to have 'automated' commands executed. * E.g.: @@ -2046,6 +2145,7 @@ } } + /** * Command thread responsible for executing commands in the background. * It holds a reference to the current command that is being executed. |