objectconverter.java

来自「一个java 代码生成器」· Java 代码 · 共 130 行

JAVA
130
字号
/**
 * Copyright (c) 2002, Siddhartha P. Chandurkar siddhartha@visioncodified.com
 * All rights reserved.
 * Licensed under the Academic Free License version 1.1
 * See the file LICENSE.TXT for details.
 * LICENSE.txt is located in the directory  <install-directory>\Jenerator
 * of your Jenertaor Installation.
 *
 **/
package com.jenerator.converter;


//<Imports>


import com.jenerator.struct.jendb.JenColumn;
import com.jenerator.struct.jendb.JenDb;
import com.jenerator.struct.jendb.JenTable;
import com.jenerator.util.ConfigurationException;
import com.jenerator.util.Configurator;
import org.apache.log4j.Logger;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

//</Imports>

/**
 * ObjectConvertor
 * It sublclasses the abstract class Converter.
 * It converts the Database MetaData into the JenDb, JenTable and JenColumn
 *
 * @author Siddhartha P. Chandurkar
 * @version 0.9.0
 */
public class ObjectConverter extends Converter {

    //ATTRIBUTES
    private JenDb db;
    private static Logger log = Logger.getLogger(com.jenerator.converter.ObjectConverter.class.getName());

    //CONSTRUCTORS
    public ObjectConverter() throws ConfigurationException, ConversionException {
    }
    //METHODS
    /**
     * convert
     * The conversion logic from the Database MetaData to JenDb, JenTable and JenColumn is
     * written here
     */
    public Object convert() throws ConversionException {
        log.info("Generating Meta information. Please wait it will take a moment .....");
        db = new JenDb("DB");
        try {
            convertTables();
            return db;
        } catch (SQLException ex) {
            log.fatal(ex);
            throw new ConversionException(ex);
        } catch (Sql2JavaMapNotFoundException ex) {
            log.fatal(ex);
            throw new ConversionException(ex);
        }
        //return null;
    }//convert

    /**
     * convertTables
     * Converts the Database tables meta information into JenTable
     */
    private void convertTables() throws SQLException, Sql2JavaMapNotFoundException {


        ResultSet tables = dbInfo.getTables();
        log.debug(tables);
        while (tables.next()) {
            String tableName = tables.getString("TABLE_NAME");
            log.debug(tableName);
            JenTable table = new JenTable(tableName);
            ResultSet rsPrimKeys = dbInfo.getPrimaryKeys(tableName);
            while (rsPrimKeys.next())
                table.addPrimaryKey(rsPrimKeys.getString("COLUMN_NAME"));
            rsPrimKeys.close();
            convertColumns(table);
            db.addTable(table);
        }


    }//convertTables

    /**
     * convertColumns
     * Converts the Database columns meta information into JenColumn
     *
     * @param   JenTable    the JenTable object to which this columns has to be added
     */
    private void convertColumns(JenTable table) throws SQLException, Sql2JavaMapNotFoundException {
        ResultSet rsColumns = dbInfo.getColumns(table.getTableName());
        while (rsColumns.next()) {
            String tmpType = rsColumns.getString("TYPE_NAME");
            //String type = props.getProperty(tmpType);
            String type = Configurator.getInstance().getConfigStructs().getSql2JavaMappings().getProperty(tmpType);
            if (type == null)
                throw new Sql2JavaMapNotFoundException("The Mapping for SQL Type " + tmpType + " was not found in SQL2JAVA.properties file, \nAdd a mapping for the sql type e.g. CHAR=java.lang.String");
            String name = rsColumns.getString("COLUMN_NAME");
            JenColumn column = new JenColumn(name, type, isPrimKey(table, name));
            table.addColumn(column);
        }
        rsColumns.close();
    }//convertColumns

    /**
     * isPrimKey
     * Checks whether the column of a table is a primary key or not
     *
     * @param   table           A JenTable object for which the primary key has to be determined
     * @param   _columnName     The column which has to be checked
     */
    private boolean isPrimKey(JenTable table, String _columnName) {
        Vector columns = table.getPrimaryKeys();
        for (int i = 0; i < columns.size(); i++) {
            String columnName = columns.elementAt(i).toString();
            if (_columnName.equals(columnName))
                return true;
        }
        return false;
    }//isPrimKey

}//ObjectConvertor

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?