Source Code Cross Referenced for TableStatements.java in  » Database-Client » SQL-Workbench » workbench » db » importer » 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.db.importer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * TableStatements.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.db.importer;
013:
014:        import java.sql.SQLException;
015:        import java.sql.Savepoint;
016:        import java.sql.Statement;
017:        import workbench.db.TableIdentifier;
018:        import workbench.db.WbConnection;
019:        import workbench.log.LogMgr;
020:        import workbench.sql.wbcommands.CommonArgs;
021:        import workbench.util.ArgumentParser;
022:        import workbench.util.SqlUtil;
023:        import workbench.util.StringUtil;
024:
025:        /**
026:         * A class to manage and run table-level statements for the DataImporter
027:         * @see DataImporter#setPerTableStatements(TableStatements)
028:         * 
029:         * @author support@sql-workbench.net
030:         */
031:        public class TableStatements {
032:            private String preStatement;
033:            private String postStatement;
034:            private boolean ignoreErrors;
035:
036:            public TableStatements(String pre, String post) {
037:                this .preStatement = pre;
038:                this .postStatement = post;
039:            }
040:
041:            /**
042:             * Initialize this TableStatement using the given commandline
043:             * @param cmdLine
044:             * @see workbench.sql.wbcommands.CommonArgs#ARG_PRE_TABLE_STMT
045:             */
046:            public TableStatements(ArgumentParser cmdLine) {
047:                String sql = cmdLine.getValue(CommonArgs.ARG_PRE_TABLE_STMT);
048:                if (!StringUtil.isWhitespaceOrEmpty(sql)) {
049:                    this .preStatement = sql;
050:                }
051:
052:                sql = cmdLine.getValue(CommonArgs.ARG_POST_TABLE_STMT);
053:                if (!StringUtil.isWhitespaceOrEmpty(sql)) {
054:                    this .postStatement = sql;
055:                }
056:
057:                this .ignoreErrors = cmdLine.getBoolean(
058:                        CommonArgs.ARG_IGNORE_TABLE_STMT_ERRORS, true);
059:            }
060:
061:            public boolean hasStatements() {
062:                return (this .preStatement != null || this .postStatement != null);
063:            }
064:
065:            /**
066:             * Run the statement that is defined as the pre-processing statement
067:             * 
068:             * @param con the connection on which to run the statement
069:             * @param tbl the table for which to run the statement
070:             * @throws java.sql.SQLException
071:             * @see #getPreStatement(TableIdentifier)
072:             */
073:            public void runPreTableStatement(WbConnection con,
074:                    TableIdentifier tbl) throws SQLException {
075:                runStatement(con, tbl, getPreStatement(tbl));
076:            }
077:
078:            /**
079:             * Run the statement that is defined as the post-processing statement
080:             * 
081:             * @param con the connection on which to run the statement
082:             * @param tbl the table for which to run the statement
083:             * @throws java.sql.SQLException
084:             * @see #getPostStatement(TableIdentifier)
085:             */
086:            public void runPostTableStatement(WbConnection con,
087:                    TableIdentifier tbl) throws SQLException {
088:                runStatement(con, tbl, getPostStatement(tbl));
089:            }
090:
091:            /**
092:             * Runs the given SQL for the given Table.
093:             * 
094:             * @param con the connection to be used when running the statement
095:             * @param tbl the table for which to run the statement
096:             * @param sql
097:             * @throws java.sql.SQLException
098:             * @see #getPreStatement(TableIdentifier)
099:             * @see #getPostStatement(TableIdentifier)
100:             */
101:            protected void runStatement(WbConnection con, TableIdentifier tbl,
102:                    String sql) throws SQLException {
103:                if (StringUtil.isWhitespaceOrEmpty(sql))
104:                    return;
105:
106:                Savepoint sp = null;
107:                Statement stmt = null;
108:                boolean useSavepoint = con.getDbSettings()
109:                        .useSavepointForImport();
110:
111:                try {
112:                    if (useSavepoint && !con.getAutoCommit())
113:                        sp = con.setSavepoint();
114:                    stmt = con.createStatement();
115:                    LogMgr.logDebug("TableStatements.runStatement",
116:                            "Executing statement: " + sql);
117:                    stmt.execute(sql);
118:                    con.releaseSavepoint(sp);
119:                } catch (SQLException e) {
120:                    LogMgr.logError("TableStatements.runStatement",
121:                            "Error running statement: " + sql, e);
122:                    con.rollback(sp);
123:                    if (!ignoreErrors)
124:                        throw e;
125:                } catch (Throwable th) {
126:                    LogMgr.logError("TableStatements.runStatement",
127:                            "Error running statement: " + sql, th);
128:                    con.rollback(sp);
129:                } finally {
130:                    SqlUtil.closeStatement(stmt);
131:                    con.releaseSavepoint(sp);
132:                }
133:            }
134:
135:            /**
136:             * Return the post-processing SQL for the passed table.
137:             * 
138:             * Placeholders ${table.name} and ${table.expression} are replaced
139:             * before running the statement. 
140:             * 
141:             * @param tbl the table for which the statement should be returned.
142:             * 
143:             * @see workbench.db.TableIdentifier#getTableName()
144:             * @see workbench.db.TableIdentifier#getTableExpression(workbench.db.WbConnection)
145:             */
146:            public String getPostStatement(TableIdentifier tbl) {
147:                return getTableStatement(postStatement, tbl);
148:            }
149:
150:            /**
151:             * Return the post-processing SQL for the passed table.
152:             * 
153:             * Placeholders ${table.name} and ${table.expression} are replaced
154:             * before running the statement. 
155:             * 
156:             * @param tbl the table for which the statement should be returned.
157:             * 
158:             * @see workbench.db.TableIdentifier#getTableName()
159:             * @see workbench.db.TableIdentifier#getTableExpression(workbench.db.WbConnection)
160:             */
161:            public String getPreStatement(TableIdentifier tbl) {
162:                return getTableStatement(preStatement, tbl);
163:            }
164:
165:            private String getTableStatement(String source, TableIdentifier tbl) {
166:                if (source == null)
167:                    return null;
168:                String sql = StringUtil.replace(source, "${table.name}", tbl
169:                        .getTableName());
170:                sql = StringUtil.replace(sql, "${table.expression}", tbl
171:                        .getTableExpression());
172:                return sql;
173:            }
174:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.