Package org.xorm.tools.generator

Source Code of org.xorm.tools.generator.MineDatabase

/*
    $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/generator/MineDatabase.java,v 1.1 2003/09/19 06:45:16 dcheckoway Exp $

    This file is part of XORM.

    XORM is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    XORM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XORM; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.xorm.tools.generator;

import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.sql.DataSource;
import org.xorm.datastore.sql.SQLConnectionInfo;
import org.xorm.datastore.sql.SQLType;

public class MineDatabase {
    static HashMap methodToAttribute = new HashMap();
   
    static String getPropertyName(Method method) {
        String attrName = (String)methodToAttribute.get(method.getName());
        if (attrName == null) {
            attrName = method.getName();
            if (attrName.startsWith("get")) {
                boolean capitalize = false;
                if (attrName.length() > 4) {
                    char c = attrName.charAt(4);
                    capitalize = c >= 'A' && c <= 'Z';
                }
                if (capitalize) {
                    attrName = attrName.substring(3);
                }
                else {
                    attrName = Character.toLowerCase(attrName.charAt(3)) +
                        attrName.substring(4);
                }
            }
            methodToAttribute.put(method.getName(), attrName);
        }
        return attrName;
    }
       
    static void displayResultSet(String name, ResultSet results)
        throws SQLException
    {
        ResultSetMetaData md = results.getMetaData();
        int numColumns = md.getColumnCount();
        System.out.println("<h3>" + name + "</h3>");
        if (!results.next()) {
            System.out.println("NO DATA");
        }
        else {
            System.out.println("<table border=1 cellpadding=1 cellspacing=0>");
            System.out.println("<tr>");
            for (int k = 1; k <= md.getColumnCount(); ++k) {
                String columnName = md.getColumnName(k);
                System.out.println("<th>" + md.getColumnName(k) + "</th>");
            }
            System.out.println("</tr>");
            do {
                System.out.println("<tr>");
                for (int k = 1; k <= md.getColumnCount(); ++k) {
                    System.out.println("<td>" +
                                       results.getString(k) +
                                       "</td>");
                }
                System.out.println("</tr>");
            } while (results.next());
            System.out.println("</table>");
        }
        results.close();
    }
   
    public static void main(String[] argv) {
        int exitCode = 0;
        Connection connection = null;
        try {
            String propertiesFileName = null;

            for (int k = 0; k < argv.length; ++k) {
                if (argv[k].startsWith("-prop")) {
                    propertiesFileName = argv[++k];
                }
                else {
                    System.out.println("Unrecognized command line option: " + argv[k]);
                }
            }
           
            if (propertiesFileName == null || propertiesFileName.equals("")) {
                System.out.println("Usage: MineDatabase -properties path/to/xorm.properties");
                Runtime.getRuntime().exit(1);
            }

            Properties properties = new Properties();
            properties.load(new FileInputStream(propertiesFileName));
            SQLConnectionInfo info = new SQLConnectionInfo();
            info.setProperties(properties);
            DataSource dataSource = (DataSource) info.getDataSource();
            connection = dataSource.getConnection();
            DatabaseMetaData md = connection.getMetaData();

            System.out.println("<html><head><title>Database MetaData</title></head><body>");
            System.out.println("<h1>Database MetaData</h1>");
           
            // First call all the simple (no arg) methods
            ArrayList simpleMethods = new ArrayList();
            Method[] methods = DatabaseMetaData.class.getMethods();
            for (int k = 0; k < methods.length; ++k) {
                if (methods[k].getParameterTypes().length == 0 &&
                    methods[k].getReturnType() != ResultSet.class) {
                    simpleMethods.add(methods[k]);
                }
            }
            Collections.sort(simpleMethods,
                             new Comparator() {
                                 public int compare(Object o1, Object o2) {
                                     Method m1 = (Method)o1;
                                     Method m2 = (Method)o2;
                                     return String.CASE_INSENSITIVE_ORDER.compare(getPropertyName(m1), getPropertyName(m2));
                                 }
                             });
            System.out.println("<p><b>Attributes:</b><br/><table border=1 cellpadding=1 cellspacing=0><tr><th>Attribute</th><th>Value</th></tr>");
            for (Iterator iter = simpleMethods.iterator(); iter.hasNext(); ) {
                Method method = (Method)iter.next();
                String attrName = getPropertyName(method);
                try {
                    Object result = method.invoke(md, new Object[0]);
                    System.out.println("<tr><td>" + attrName + "</td><td>" +
                                       String.valueOf(result) + "</td></tr>");
                }
                catch (Throwable t) {
                    System.out.println("<tr><td>" + attrName + "</td><td>" +
                                       "Not available: " + t.getClass().getName() +
                                       "</td></tr>");
                }
            }
            System.out.println("</table></p>");

            // Now dive in deeper

            // Read list of tables
            ResultSet results = md.getTables(null, null, null,
                                            new String[] { "TABLE" });
            ArrayList tables = new ArrayList();
            while (results.next()) {
                tables.add(results.getString("TABLE_NAME"));
            }
            results.close();

            for (Iterator iter = tables.iterator(); iter.hasNext(); ) {
                String tableName = (String)iter.next();
                System.out.println("<hr/>");
                System.out.println("<h2>Table: " + tableName + "</h2>");
                displayResultSet("Columns", md.getColumns(null, null, tableName, null));
                displayResultSet("PrimaryKeys", md.getPrimaryKeys(null, null, tableName));
                displayResultSet("ExportedKeys", md.getExportedKeys(null, null, tableName));
                displayResultSet("ImportedKeys", md.getImportedKeys(null, null, tableName));
                displayResultSet("IndexInfo", md.getIndexInfo(null, null, tableName, false, false));
                displayResultSet("TablePrivileges", md.getTablePrivileges(null, null, tableName));
                displayResultSet("VersionColumns", md.getVersionColumns(null, null, tableName));
            }

            System.out.println("<hr/>");
            System.out.println("<h2>Other Database Attributes:</h2>");
           
            displayResultSet("TableTypes", md.getTableTypes());
            System.out.println("<hr/>");
            displayResultSet("TypeInfo", md.getTypeInfo());
            System.out.println("<hr/>");
            displayResultSet("Catalogs", md.getCatalogs());
            System.out.println("<hr/>");
            displayResultSet("Schemas", md.getSchemas());

            System.out.println("</body></html>");
        }
        catch (Throwable t) {
            t.printStackTrace();
            exitCode = 1;
        }
        finally {
            if (connection != null) {
                try {
                    connection.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Runtime.getRuntime().exit(exitCode);
        }
    }
}
TOP

Related Classes of org.xorm.tools.generator.MineDatabase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.