⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csvtabledesignreader.java

📁 数据仓库工具
💻 JAVA
字号:

/*
LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.


    Copyright (C) 2003  Together

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

    This library 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
    Lesser General Public License for more details.

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

package org.webdocwf.util.loader.generator;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.StringTokenizer;

import org.webdocwf.util.loader.LoaderException;
import org.webdocwf.util.loader.logging.Logger;
import org.webdocwf.util.loader.logging.StandardLogger;
import org.webdocwf.util.loader.wizard.AddClassPath;


/**
 * CsvTableDesignReader class retrieves the input data from csv tables, and placed them in to
 * ImportDefinition class.
 * @author Radoslav Dutina
 * @version 1.0
 */
public class CsvTableDesignReader {

  private ImportDefinitionAttributes importDefinitionAttributes= new ImportDefinitionAttributes();
  private MappingTypeData mappingTypeData;
  private Logger logger;
  /**
   * Construct object LoaderGenerator with associated parameters.
   * @param tableName is name of the table form which we retrieve data.
   * @param generatorParameters represents the references to InputParameter object.
   * @throws LoaderException
   */
  public CsvTableDesignReader(String tableName, InputParameters generatorParameters)
      throws LoaderException {
		setLogger();
		this.logger.write("full", "CsvTableDesignReader is started.");
    importDefinitionAttributes.setName(tableName);
    importDefinitionAttributes.setTableName(tableName);

    try{
      JdbcParameters sourceJdbc= new JdbcParameters("source", generatorParameters);
      Connection conn = null;
      URL url=null;
      String app="";
      String path=AddClassPath.getClassPathString();
      if(path!=null){
        StringTokenizer st=new StringTokenizer(path,";");
        int count=0;
        while(st.hasMoreTokens()) {
          GeneratorClassLoader.addURL(
              new File(st.nextElement().toString()).toURL());
        }
        Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver"));
      }else{
        Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver"));
      }
      conn=DriverManager.getConnection(sourceJdbc.getJdbcParameters("Connection.Url"),
                                       sourceJdbc.getJdbcParameters("User"),sourceJdbc.getJdbcParameters("Password"));


      Statement stmt=conn.createStatement();
      ResultSet results=stmt.executeQuery("SELECT * FROM "+""+tableName+"");
      int numberC=results.getMetaData().getColumnCount();
      String[] columnNames=new String[numberC];
      String[] targetTableNames= new String[numberC];
      String[] targetTableID= new String[numberC];
      String[] columnTargetTypes=new String[numberC];
      String[] columnLength=new String[numberC];
      String[] columnIsNullable=new String[numberC];

      for(int x=1; x<numberC+1; x++){
        columnNames[x-1] =results.getMetaData().getColumnName(x);
        targetTableNames[x-1]=tableName;
        targetTableID[x-1]="0";

        if(generatorParameters.getSourceType().equalsIgnoreCase("access")){

          String sourceTypeData=results.getMetaData().getColumnTypeName(x);
          mappingTypeData=new MappingTypeData(sourceTypeData, generatorParameters);
          String targetTypeData=mappingTypeData.getSQLType();
          columnTargetTypes[x-1]=targetTypeData;
         // if(sourceTypeData.equalsIgnoreCase("decimal")||targetTypeData.equalsIgnoreCase("decimal")){
          if(generatorParameters.getIsDecimal(sourceTypeData).equalsIgnoreCase("true")||
             generatorParameters.getIsDecimal(targetTypeData).equalsIgnoreCase("true")){
            columnLength[x-1]=results.getMetaData().getPrecision(x)+","+
                              results.getMetaData().getScale(x);
//ZK change this 6.5.2004
//            }else if(sourceTypeData.equalsIgnoreCase("int")||sourceTypeData.equalsIgnoreCase("tinyint")||
//                     sourceTypeData.equalsIgnoreCase("smallint")||sourceTypeData.equalsIgnoreCase("tinyint")||
//                     sourceTypeData.equalsIgnoreCase("datetime")||sourceTypeData.equalsIgnoreCase("ntext")||
//                     sourceTypeData.equalsIgnoreCase("longchar")||sourceTypeData.equalsIgnoreCase("longbinary")||
//                     sourceTypeData.equalsIgnoreCase("binary")||sourceTypeData.equalsIgnoreCase("longinteger")){
          }else if(generatorParameters.getHasSize(sourceTypeData).equalsIgnoreCase("false")){
			
              columnLength[x-1]=" ";
            }else{
              columnLength[x-1]=String.valueOf(results.getMetaData().getPrecision(x));
            }
            if(results.getMetaData().isNullable(x)==0){
              columnIsNullable[x-1]="NOT NULL";
            }else{
              columnIsNullable[x-1]="";
            }
        }
      }

      importDefinitionAttributes.setTagSourceColumnName(columnNames);
      importDefinitionAttributes.setTagTargetColumnName(columnNames);
      importDefinitionAttributes.setTagTargetTableName(targetTableNames);
      importDefinitionAttributes.setTagTargetTableID(targetTableID);
      //only for access db
      if(generatorParameters.getSourceType().equalsIgnoreCase("access")){

        importDefinitionAttributes.setTagColumnType(columnTargetTypes);
        importDefinitionAttributes.setTagColumnLenght(columnLength);
        importDefinitionAttributes.setTagAllowNulls(columnIsNullable);
      }

      results.close();
      stmt.close();
      conn.close();
    }catch(Exception e){
      String msg="Exception in CsvTableDesignReader class:";
      //e.printStackTrace();
      LoaderException le=new LoaderException(msg+"\n"+e.getMessage(), (Throwable)e);
	  	this.logger.write("full", "Error:Exception in CsvTableDesignReader class."+"\n"+le.getStackTraceAsString());
      throw le;
    }
	this.logger.write("full", "CsvTableDesignReader is finished.");
  }

  /**
   * This method read value of importDefinitionAttributes parameters.
   * @return references to ImportDefinitionAttributes objects.
   */
  public ImportDefinitionAttributes getImportDefinition(){
    return importDefinitionAttributes;
  }

  /**
	  * This method will set logger object
	  * @param logger
	  */
	 private void setLogger() {
		 this.logger = StandardLogger.getCentralLogger();
	 }
}

⌨️ 快捷键说明

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