📄 transferdb.java
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb.util;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.sql.Types;import java.util.Vector;// fredt@users 20020215 - patch 516309 by Nicolas Bazin - enhancements// sqlbob@users 20020401 - patch 1.7.0 - reengineering// nicolas BAZIN 20020430 - add support of Catalog and mckoi db helper// Stephan Frind 20040508 - speed improvements/** * Conversions between different databases * * @version 1.7.0 */class TransferDb extends DataAccessPoint { Connection conn; DatabaseMetaData meta; protected Statement srcStatement = null; TransferDb(Connection c, Traceable t) throws DataAccessPointException { super(t); conn = c; if (c != null) { String productLowerName; try { meta = c.getMetaData(); databaseToConvert = c.getCatalog(); productLowerName = meta.getDatabaseProductName(); if (productLowerName == null) { productLowerName = ""; } else { productLowerName = productLowerName.toLowerCase(); } helper = HelperFactory.getHelper(productLowerName); helper.set(this, t, meta.getIdentifierQuoteString()); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } } } boolean isConnected() { return (conn != null); } boolean getAutoCommit() throws DataAccessPointException { boolean result = false; try { result = conn.getAutoCommit(); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } return result; } void commit() throws DataAccessPointException { if (srcStatement != null) { try { srcStatement.close(); } catch (SQLException e) {} srcStatement = null; } try { conn.commit(); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } } void rollback() throws DataAccessPointException { if (srcStatement != null) { try { srcStatement.close(); } catch (SQLException e) {} srcStatement = null; } try { conn.rollback(); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } } void setAutoCommit(boolean flag) throws DataAccessPointException { try { conn.setAutoCommit(flag); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } } boolean execute(String statement) throws DataAccessPointException { boolean result = false; Statement stmt = null; try { stmt = conn.createStatement(); result = stmt.execute(statement); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) {} } } return result; } TransferResultSet getData(String statement) throws DataAccessPointException { ResultSet rsData = null; try { if (srcStatement != null) { srcStatement.close(); } srcStatement = conn.createStatement(); rsData = srcStatement.executeQuery(statement); } catch (SQLException e) { try { srcStatement.close(); } catch (Exception e1) {} srcStatement = null; rsData = null; throw new DataAccessPointException(e.getMessage()); } return new TransferResultSet(rsData); } void putData(String statement, TransferResultSet r, int iMaxRows) throws DataAccessPointException { if ((statement == null) || statement.equals("") || (r == null)) { return; } PreparedStatement destPrep = null; try { destPrep = conn.prepareStatement(statement); int i = 0; int tmpLength; int len = r.getColumnCount(); int[] tmpTypes = null; while (r.next()) { if (tmpTypes == null) { tmpTypes = new int[len + 1]; for (int j = 1; j <= len; j++) { tmpTypes[j] = r.getColumnType(j); } } transferRow(r, destPrep, len, tmpTypes); if (iMaxRows != 0 && i == iMaxRows) { break; } i++; if (iMaxRows != 0 || i % 100 == 0) { tracer.trace("Transfered " + i + " rows"); } } } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } finally { if (destPrep != null) { try { destPrep.close(); } catch (SQLException e) {} } } }/* private void transferRow(TransferResultSet r, PreparedStatement p) throws DataAccessPointException, SQLException { // TODO // what is this never used variable for? // looks like missing debug flags because constructing these strings consumes a lot // of time String sLast = ""; if (p != null) { p.clearParameters(); } int len = r.getColumnCount(); for (int i = 0; i < len; i++) { int t = r.getColumnType(i + 1); sLast = "column=" + r.getColumnName(i + 1) + " datatype=" + (String) helper.getSupportedTypes().get(new Integer(t)); Object o = r.getObject(i + 1); if (o == null) { if (p != null) { p.setNull(i + 1, t); } sLast += " value=<null>"; } else { o = helper.convertColumnValue(o, i + 1, t); p.setObject(i + 1, o); sLast += " value=\'" + o.toString() + "\'"; } } if (p != null) { p.execute(); } sLast = ""; }*/ Vector getSchemas() throws DataAccessPointException { Vector ret = new Vector(); ResultSet result = null; try { result = meta.getSchemas(); } catch (SQLException e) { result = null; } try { if (result != null) { while (result.next()) { ret.addElement(result.getString(1)); } result.close(); } } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } return (ret); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -