001: /*
002: * WbSelectBlob.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.wbcommands;
013:
014: import java.io.File;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.io.OutputStream;
019: import java.sql.ResultSet;
020: import java.sql.SQLException;
021: import workbench.log.LogMgr;
022: import workbench.resource.ResourceMgr;
023: import workbench.sql.SqlCommand;
024: import workbench.sql.StatementRunnerResult;
025: import workbench.sql.formatter.SQLLexer;
026: import workbench.sql.formatter.SQLToken;
027: import workbench.util.ExceptionUtil;
028: import workbench.util.FileUtil;
029: import workbench.util.SqlUtil;
030: import workbench.util.StringUtil;
031: import workbench.util.WbFile;
032:
033: /**
034: *
035: * @author support@sql-workbench.net
036: */
037: public class WbSelectBlob extends SqlCommand {
038: public static final String VERB = "WBSELECTBLOB";
039:
040: public WbSelectBlob() {
041: this .isUpdatingCommand = false;
042: }
043:
044: public String getVerb() {
045: return VERB;
046: }
047:
048: public StatementRunnerResult execute(final String sqlCommand)
049: throws SQLException {
050: StatementRunnerResult result = new StatementRunnerResult();
051: SQLLexer lexer = new SQLLexer(sqlCommand);
052:
053: StringBuilder sql = new StringBuilder(sqlCommand.length());
054:
055: WbFile outputFile = null;
056:
057: SQLToken token = lexer.getNextToken(false, false);
058: if (!token.getContents().equals("WBSELECTBLOB")) {
059: result.addMessage(ResourceMgr
060: .getString("ErrSelectBlobSyntax"));
061: result.setFailure();
062: return result;
063: }
064: sql.append("SELECT ");
065: while (token != null) {
066: token = lexer.getNextToken(false, true);
067: if (token.getContents().equals("INTO")) {
068: break;
069: }
070: sql.append(token.getContents());
071: }
072:
073: if (token != null && !token.getContents().equals("INTO")) {
074: result.addMessage(ResourceMgr
075: .getString("ErrSelectBlobSyntax"));
076: result.setFailure();
077: return result;
078: } else {
079: // Next token must be the filename
080: token = lexer.getNextToken(false, false);
081: String filename = token.getContents();
082: outputFile = new WbFile(StringUtil.trimQuotes(filename));
083: sql.append(' ');
084: sql.append(sqlCommand.substring(token.getCharEnd() + 1));
085: }
086:
087: LogMgr.logDebug("WbSelectBlob.execute()", "Using SQL=" + sql
088: + " for file: " + outputFile.getFullPath());
089: ResultSet rs = null;
090: OutputStream out = null;
091: InputStream in = null;
092: long filesize = 0;
093:
094: File outputDir = outputFile.getParentFile();
095: String baseFilename = outputFile.getFileName();
096: String extension = outputFile.getExtension();
097: if (StringUtil.isEmptyString(extension))
098: extension = "";
099: else
100: extension = "." + extension;
101:
102: try {
103: currentStatement = currentConnection
104: .createStatementForQuery();
105: rs = currentStatement.executeQuery(sql.toString());
106: int row = 0;
107: while (rs.next()) {
108: WbFile currentFile = null;
109:
110: in = rs.getBinaryStream(1);
111: if (in == null) {
112: //result.setFailure();
113: String msg = ResourceMgr
114: .getString("ErrSelectBlobNoStream");
115: result.addMessage(StringUtil.replace(msg, "%row%",
116: Integer.toString(row)));
117: result.setWarning(true);
118: continue;
119: }
120:
121: if (row == 0) {
122: currentFile = outputFile;
123: } else {
124: currentFile = new WbFile(outputDir, baseFilename
125: + "_" + Integer.toString(row) + extension);
126: }
127:
128: out = new FileOutputStream(currentFile);
129: filesize = FileUtil.copy(in, out);
130: String msg = ResourceMgr.getString("MsgBlobSaved");
131: msg = StringUtil.replace(msg, "%filename%", currentFile
132: .getFullPath());
133: msg = msg.replaceAll("%filesize%", Long
134: .toString(filesize));
135: result.addMessage(msg);
136: result.setSuccess();
137: row++;
138: }
139: this .appendSuccessMessage(result);
140: } catch (IOException e) {
141: String msg = StringUtil.replace(ResourceMgr
142: .getString("ErrSelectBlobFileError"), "%filename%",
143: outputFile.getFullPath());
144: result.addMessage(msg);
145: result.setFailure();
146: return result;
147: } catch (SQLException e) {
148: String msg = StringUtil.replace(ResourceMgr
149: .getString("ErrSelectBlobSqlError"), "%filename%",
150: outputFile.getFullPath());
151: result.addMessage(msg);
152: result.addMessage(ExceptionUtil.getDisplay(e));
153: result.setFailure();
154: return result;
155: } finally {
156: SqlUtil.closeAll(rs, currentStatement);
157: }
158:
159: return result;
160: }
161:
162: }
|