📄 torquejdbctransformtask.java
字号:
package org.apache.torque.task;/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. 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. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 THE APACHE SOFTWARE FOUNDATION OR * ITS 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. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */import java.io.FileOutputStream;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Types;import java.util.ArrayList;import java.util.Collection;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Task;import org.apache.torque.engine.database.model.TypeMap;import org.apache.torque.engine.database.transform.DTDResolver;import org.apache.xerces.dom.DocumentImpl;import org.apache.xerces.dom.DocumentTypeImpl;import org.apache.xml.serialize.Method;import org.apache.xml.serialize.OutputFormat;import org.apache.xml.serialize.XMLSerializer;import org.w3c.dom.Element;import org.w3c.dom.Node;/** * This class generates an XML schema of an existing database from * JDBC metadata. * * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> * @author <a href="mailto:fedor.karpelevitch@barra.com">Fedor Karpelevitch</a> * @version $Id: TorqueJDBCTransformTask.java,v 1.6 2003/08/22 17:01:42 mpoeschl Exp $ */public class TorqueJDBCTransformTask extends Task{ /** Name of XML database schema produced. */ protected String xmlSchema; /** JDBC URL. */ protected String dbUrl; /** JDBC driver. */ protected String dbDriver; /** JDBC user name. */ protected String dbUser; /** JDBC password. */ protected String dbPassword; /** DB schema to use. */ protected String dbSchema; /** DOM document produced. */ protected DocumentImpl doc; /** The document root element. */ protected Node databaseNode; /** Hashtable of columns that have primary keys. */ protected Hashtable primaryKeys; /** Hashtable to track what table a column belongs to. */ protected Hashtable columnTableMap; protected boolean sameJavaName; private XMLSerializer xmlSerializer; public String getDbSchema() { return dbSchema; } public void setDbSchema(String dbSchema) { this.dbSchema = dbSchema; } public void setDbUrl(String v) { dbUrl = v; } public void setDbDriver(String v) { dbDriver = v; } public void setDbUser(String v) { dbUser = v; } public void setDbPassword(String v) { dbPassword = v; } public void setOutputFile (String v) { xmlSchema = v; } public void setSameJavaName(boolean v) { this.sameJavaName = v; } public boolean isSameJavaName() { return this.sameJavaName; } /** * Default constructor. * * @throws BuildException */ public void execute() throws BuildException { log("Torque - JDBCToXMLSchema starting"); log("Your DB settings are:"); log("driver : " + dbDriver); log("URL : " + dbUrl); log("user : " + dbUser); // log("password : " + dbPassword); log("schema : " + dbSchema); DocumentTypeImpl docType = new DocumentTypeImpl(null, "database", null, DTDResolver.WEB_SITE_DTD); doc = new DocumentImpl(docType); doc.appendChild(doc.createComment( " Autogenerated by JDBCToXMLSchema! ")); try { generateXML(); log(xmlSchema); xmlSerializer = new XMLSerializer( new PrintWriter( new FileOutputStream(xmlSchema)), new OutputFormat(Method.XML, null, true)); xmlSerializer.serialize(doc); } catch (Exception e) { throw new BuildException(e); } log("Torque - JDBCToXMLSchema finished"); } /** * Generates an XML database schema from JDBC metadata. * * @throws Exception a generic exception. */ public void generateXML() throws Exception { // Load the Interbase Driver. Class.forName(dbDriver); log("DB driver sucessfuly instantiated"); // Attemtp to connect to a database. Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword); log("DB connection established"); // Get the database Metadata. DatabaseMetaData dbMetaData = con.getMetaData(); // The database map. List tableList = getTableNames(dbMetaData); databaseNode = doc.createElement("database"); // Build a database-wide column -> table map. columnTableMap = new Hashtable(); log("Building column/table map..."); for (int i = 0; i < tableList.size(); i++) { String curTable = (String) tableList.get(i); List columns = getColumns(dbMetaData, curTable); for (int j = 0; j < columns.size(); j++) { List col = (List) columns.get(j); String name = (String) col.get(0); columnTableMap.put(name, curTable); } } for (int i = 0; i < tableList.size(); i++) { // Add Table. String curTable = (String) tableList.get(i); // dbMap.addTable(curTable); log("Processing table: " + curTable); Element table = doc.createElement("table"); table.setAttribute("name", curTable); if (isSameJavaName()) { table.setAttribute("javaName", curTable); } // Add Columns. // TableMap tblMap = dbMap.getTable(curTable); List columns = getColumns(dbMetaData, curTable); List primKeys = getPrimaryKeys(dbMetaData, curTable); Collection forgnKeys = getForeignKeys(dbMetaData, curTable);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -