Source Code Cross Referenced for WbCopy.java in  » Database-Client » SQL-Workbench » workbench » sql » wbcommands » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » SQL Workbench » workbench.sql.wbcommands 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * WbCopy.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.sql.SQLException;
015:        import java.util.List;
016:        import workbench.WbManager;
017:        import workbench.db.ConnectionMgr;
018:        import workbench.db.ConnectionProfile;
019:        import workbench.db.TableIdentifier;
020:        import workbench.db.WbConnection;
021:        import workbench.gui.profiles.ProfileKey;
022:        import workbench.util.ExceptionUtil;
023:        import workbench.log.LogMgr;
024:        import workbench.resource.ResourceMgr;
025:        import workbench.sql.SqlCommand;
026:        import workbench.sql.StatementRunnerResult;
027:        import workbench.util.ArgumentParser;
028:        import workbench.util.ArgumentType;
029:
030:        /**
031:         * A command to copy data from one DBMS to another. This is the commandline
032:         * version of the DataPumper.
033:         * @author  support@sql-workbench.net
034:         */
035:        public class WbCopy extends SqlCommand {
036:            public static final String VERB = "WBCOPY";
037:
038:            public static final String PARAM_SOURCETABLE = "sourceTable";
039:            public static final String PARAM_SOURCEQUERY = "sourceQuery";
040:            public static final String PARAM_TARGETTABLE = "targetTable";
041:            public static final String PARAM_SOURCEPROFILE = "sourceProfile";
042:            public static final String PARAM_SOURCEPROFILE_GROUP = "sourceGroup";
043:            public static final String PARAM_TARGETPROFILE = "targetProfile";
044:            public static final String PARAM_TARGETPROFILE_GROUP = "targetGroup";
045:            public static final String PARAM_COLUMNS = "columns";
046:            public static final String PARAM_SOURCEWHERE = "sourceWhere";
047:            public static final String PARAM_DELETETARGET = "deleteTarget";
048:            public static final String PARAM_KEYS = "keyColumns";
049:            public static final String PARAM_DROPTARGET = "dropTarget";
050:            public static final String PARAM_CREATETARGET = "createTarget";
051:
052:            private static final String ID_PREFIX = "$Wb-Copy$";
053:
054:            private CopyTask copier;
055:
056:            public WbCopy() {
057:                cmdLine = new ArgumentParser();
058:                CommonArgs.addCommitParameter(cmdLine);
059:                CommonArgs.addImportModeParameter(cmdLine);
060:                CommonArgs.addContinueParameter(cmdLine);
061:                CommonArgs.addProgressParameter(cmdLine);
062:                CommonArgs.addCommitAndBatchParams(cmdLine);
063:                CommonArgs.addCheckDepsParameter(cmdLine);
064:                CommonArgs.addTableStatements(cmdLine);
065:                CommonArgs.addTransactionControL(cmdLine);
066:
067:                cmdLine.addArgument(PARAM_SOURCETABLE);
068:                cmdLine.addArgument(PARAM_SOURCEQUERY);
069:                cmdLine.addArgument(PARAM_TARGETTABLE);
070:                cmdLine.addArgument(PARAM_SOURCEPROFILE,
071:                        ArgumentType.ProfileArgument);
072:                cmdLine.addArgument(PARAM_TARGETPROFILE,
073:                        ArgumentType.ProfileArgument);
074:                cmdLine.addArgument(PARAM_SOURCEPROFILE_GROUP);
075:                cmdLine.addArgument(PARAM_TARGETPROFILE_GROUP);
076:                cmdLine.addArgument(PARAM_COLUMNS);
077:                cmdLine.addArgument(PARAM_SOURCEWHERE);
078:                cmdLine.addArgument(PARAM_DELETETARGET,
079:                        ArgumentType.BoolArgument);
080:                cmdLine.addArgument(PARAM_KEYS);
081:                cmdLine
082:                        .addArgument(PARAM_DROPTARGET,
083:                                ArgumentType.BoolArgument);
084:                cmdLine.addArgument(PARAM_CREATETARGET,
085:                        ArgumentType.BoolArgument);
086:                cmdLine.addArgument(CommonArgs.ARG_BATCHSIZE);
087:            }
088:
089:            public String getVerb() {
090:                return VERB;
091:            }
092:
093:            protected boolean isConnectionRequired() {
094:                return false;
095:            }
096:
097:            private void addWrongParams(StatementRunnerResult result) {
098:                if (!WbManager.getInstance().isBatchMode()) {
099:                    result.addMessageNewLine();
100:                    result.addMessage(ResourceMgr
101:                            .getString("ErrCopyWrongParameters"));
102:                    result.setFailure();
103:                }
104:            }
105:
106:            public StatementRunnerResult execute(final String sql)
107:                    throws SQLException {
108:                StatementRunnerResult result = new StatementRunnerResult();
109:
110:                cmdLine.parse(getCommandLine(sql));
111:
112:                if (cmdLine.hasUnknownArguments()) {
113:                    setUnknownMessage(result, cmdLine, ResourceMgr
114:                            .getString("ErrCopyWrongParameters"));
115:                    return result;
116:                }
117:
118:                String sourceProfile = cmdLine.getValue(PARAM_SOURCEPROFILE);
119:                String sourceGroup = cmdLine
120:                        .getValue(PARAM_SOURCEPROFILE_GROUP);
121:                ProfileKey sourceKey = null;
122:                if (sourceProfile != null)
123:                    sourceKey = new ProfileKey(sourceProfile, sourceGroup);
124:
125:                String targetProfile = cmdLine.getValue(PARAM_TARGETPROFILE);
126:                String targetGroup = cmdLine
127:                        .getValue(PARAM_TARGETPROFILE_GROUP);
128:                ProfileKey targetKey = null;
129:                if (targetProfile != null)
130:                    targetKey = new ProfileKey(targetProfile, targetGroup);
131:
132:                String sourcetable = cmdLine.getValue(PARAM_SOURCETABLE);
133:                String sourcequery = cmdLine.getValue(PARAM_SOURCEQUERY);
134:
135:                if (sourcetable == null && sourcequery == null) {
136:                    result.addMessage(ResourceMgr
137:                            .getString("ErrCopyNoSourceSpecified"));
138:                    addWrongParams(result);
139:                    return result;
140:                }
141:
142:                WbConnection targetCon = getConnection(result, targetKey,
143:                        ID_PREFIX + "-Target$");
144:                if (targetCon == null || !result.isSuccess()) {
145:                    return result;
146:                }
147:
148:                WbConnection sourceCon = getConnection(result, sourceKey,
149:                        ID_PREFIX + "-Source$");
150:                if (sourceCon == null || !result.isSuccess()) {
151:                    return result;
152:                }
153:
154:                List<TableIdentifier> tablesToExport = null;
155:                try {
156:                    SourceTableArgument argParser = new SourceTableArgument(
157:                            sourcetable, sourceCon);
158:                    tablesToExport = argParser.getTables();
159:                    if (tablesToExport.size() == 0
160:                            && argParser.wasWildCardArgument()) {
161:                        result.addMessage(ResourceMgr.getFormattedString(
162:                                "ErrExportNoTablesFound", sourcetable));
163:                        result.setFailure();
164:                        return result;
165:                    }
166:                } catch (SQLException e) {
167:                    LogMgr.logError("WbExport.runTableExports()",
168:                            "Could not retrieve table list", e);
169:                    result.addMessage(ExceptionUtil.getDisplay(e));
170:                    result.setFailure();
171:                    return result;
172:                }
173:
174:                boolean schemaCopy = tablesToExport.size() > 1;
175:
176:                String targettable = cmdLine.getValue(PARAM_TARGETTABLE);
177:                if (targettable == null && !schemaCopy) {
178:                    result.addMessage(ResourceMgr.getString("ErrCopyNoTarget"));
179:                    addWrongParams(result);
180:                    return result;
181:                }
182:
183:                if (schemaCopy) {
184:                    this .copier = new SchemaCopy(tablesToExport);
185:                } else {
186:                    this .copier = new TableCopy();
187:                }
188:
189:                try {
190:                    copier.init(sourceCon, targetCon, result, cmdLine,
191:                            rowMonitor);
192:                    copier.copyData();
193:                    if (copier.isSuccess()) {
194:                        result.setSuccess();
195:                    } else {
196:                        result.setFailure();
197:                    }
198:                    result.addMessage(copier.getMessages());
199:                } catch (SQLException e) {
200:                    LogMgr.logError("WbCopy.execute()",
201:                            "SQL Error when copying data", e);
202:                    CharSequence msg = copier.getMessages();
203:                    if (msg.length() == 0) {
204:                        String err = ResourceMgr.getFormattedString("ErrCopy",
205:                                ExceptionUtil.getDisplay(e, false));
206:                        result.addMessage(err);
207:                    } else {
208:                        result.addMessage(msg);
209:                    }
210:                    result.setFailure();
211:                } catch (Exception e) {
212:                    LogMgr.logError("WbCopy.execute()",
213:                            "Error when copying data", e);
214:                    result.setFailure();
215:                    addErrorInfo(result, sql, e);
216:                    result.addMessage(copier.getMessages());
217:                } finally {
218:                    closeConnections(sourceCon, targetCon);
219:                }
220:
221:                return result;
222:            }
223:
224:            public void done() {
225:                super .done();
226:                this .copier = null;
227:            }
228:
229:            public void cancel() throws SQLException {
230:                super .cancel();
231:                if (this .copier != null) {
232:                    this .copier.cancel();
233:                }
234:            }
235:
236:            private void closeConnections(WbConnection sourceCon,
237:                    WbConnection targetCon) {
238:                try {
239:                    if (sourceCon != null
240:                            && sourceCon.getId().startsWith(ID_PREFIX)) {
241:                        sourceCon.disconnect();
242:                    }
243:                } catch (Exception e) {
244:                    LogMgr.logError("WbCopy.execute()",
245:                            "Error when disconnecting source connection", e);
246:                }
247:
248:                try {
249:                    if (targetCon != null
250:                            && targetCon.getId().startsWith(ID_PREFIX)) {
251:                        targetCon.disconnect();
252:                    }
253:                } catch (Exception e) {
254:                    LogMgr.logError("WbCopy.execute()",
255:                            "Error when disconnecting target connection", e);
256:                }
257:            }
258:
259:            private WbConnection getConnection(StatementRunnerResult result,
260:                    ProfileKey profileKey, String id) {
261:                if (profileKey == null
262:                        || (currentConnection != null && currentConnection
263:                                .getProfile().isProfileForKey(profileKey))) {
264:                    return currentConnection;
265:                } else {
266:                    ConnectionProfile tprof = ConnectionMgr.getInstance()
267:                            .getProfile(profileKey);
268:                    if (tprof == null) {
269:                        String msg = ResourceMgr
270:                                .getFormattedString("ErrCopyProfileNotFound",
271:                                        profileKey.toString());
272:                        result.addMessage(msg);
273:                        result.setFailure();
274:                        return null;
275:                    }
276:
277:                    try {
278:                        return ConnectionMgr.getInstance().getConnection(
279:                                profileKey, id);
280:                    } catch (Exception e) {
281:                        LogMgr.logError("Wbcopy.getConnection()",
282:                                "Error connecting to database", e);
283:                        result.addMessage(ResourceMgr
284:                                .getFormattedString("ErrCopyCouldNotConnect",
285:                                        profileKey.toString()));
286:                        result.addMessage(ExceptionUtil.getDisplay(e));
287:                        result.setFailure();
288:                        return null;
289:                    }
290:                }
291:
292:            }
293:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.