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

📄 csvdriver.java

📁 数据仓库工具
💻 JAVA
字号:
/**
    Copyright (C) 2002-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.relique.jdbc.csv;

import java.sql.*;
import java.util.Properties;
import java.util.StringTokenizer;
import java.io.File;

/**
 * This class implements the Driver interface for the CsvJdbc driver.
 *
 * @author     Zoran Milakovic
 */

public class CsvDriver implements Driver
{

//PARAMETER NAMES
  public static final String FILE_EXTENSION="fileExtension";
  public static final String SEPARATOR="separator";
  public static final String MAXFILESIZE = "maxFileSize";
  public static final String CREATE="create";
  public static final String SUPPRESS_HEADERS="suppressHeaders";
  public static final String CHARSET = "charset";
  public static final String LINE_BREAK_ESCAPE = "lineBreakEscape";
  public static final String CARRIAGE_RETURN_ESCAPE = "carriageReturnEscape";
  //public static final String DOUBLE_QUOTE_ESCAPE = "doubleQuoteEscape";
//DEFAULT VALUES
  public static final String DEFAULT_EXTENSION = ".csv";
  public static final char DEFAULT_SEPARATOR = ',';
  public static final boolean DEFAULT_SUPPRESS = false;
  public static final boolean DEFAULT_CREATE = false;
  public static final long DEFAULT_FILE_MAXSIZE = 1500000000;
  public static final String DEFAULT_LINE_BREAK_ESCAPE = "~CSVLB~";
  public static final String DEFAULT_DOUBLE_QUOTE_ESCAPE = "\"\"";
  public static final String DEFAULT_CARRIAGE_RETURN_ESCAPE = "~CSVCR~";

//data types
  public static final String BINARY_TYPE = "BINARY";
  public static final String VARCHAR_TYPE = "VARCHAR";


  public static String FILE_NAME_EXT = "extension";
  private final static String URL_PREFIX = "jdbc:relique:csv:";
  private Properties info = null;

  /* If set to true, driver will log into csvdriver.log file, in working directory */
  private static boolean ENABLE_LOG = false;
  
  /* If set to true, driver will show stack traces and other debug info */
	public static boolean DEBUG = false;

  /**
   *Gets the propertyInfo attribute of the CsvDriver object
   *
   * @param  url               Description of Parameter
   * @param  info              Description of Parameter
   * @return                   The propertyInfo value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
       throws SQLException
  {
    return new DriverPropertyInfo[0];
  }


  /**
   *Gets the majorVersion attribute of the CsvDriver object
   *
   * @return    The majorVersion value
   * @since
   */
  public int getMajorVersion()
  {
    return 1;
  }


  /**
   *Gets the minorVersion attribute of the CsvDriver object
   *
   * @return    The minorVersion value
   * @since
   */
  public int getMinorVersion()
  {
    return 0;
  }


  /**
   *Description of the Method
   *
   * @param  url               Description of Parameter
   * @param  info              Description of Parameter
   * @return                   Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public Connection connect(String url, Properties info) throws SQLException
  {
    DriverManager.println("CsvJdbc - CsvDriver:connect() - url=" + url);
    // check for correct url
    if (!url.startsWith(URL_PREFIX))
    {
      return null;
    }
    // get filepath from url
    String filePath = url.substring(URL_PREFIX.length());
    String filePathAll = filePath;
    StringTokenizer st = new StringTokenizer( filePath , ";" );
    filePath = st.nextToken();
    if (!filePath.endsWith(File.separator))
    {
      filePath += File.separator;
    }
    DriverManager.println("CsvJdbc - CsvDriver:connect() - filePath=" + filePath);
    return new CsvConnection(filePathAll, info);
  }


  /**
   * Description of the Method
   *
   * @param  url               Description of Parameter
   * @return                   Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public boolean acceptsURL(String url) throws SQLException
  {
    DriverManager.println("CsvJdbc - CsvDriver:accept() - url=" + url);
    return url.startsWith(URL_PREFIX);
  }


  /**
   *Description of the Method
   *
   * @return    Description of the Returned Value
   * @since
   */
  public boolean jdbcCompliant()
  {
    return false;
  }
  // This static block inits the driver when the class is loaded by the JVM.
  static
  {
    try
    {
      java.sql.DriverManager.registerDriver(new CsvDriver());
    }
    catch (SQLException e)
    {
      throw new RuntimeException(
          "FATAL ERROR: Could not initialise CSV driver ! Message was: "
           + e.getMessage());
    }
  }

  public static void log( String message) {
    if ( CsvDriver.ENABLE_LOG ) {
      try {
        File file = new File("csvdriver.log");
        if (!file.exists())
          file.createNewFile();
        java.io.RandomAccessFile fileLogr = new java.io.RandomAccessFile(file,
            "rw");
        fileLogr.seek(fileLogr.length());
        fileLogr.writeBytes("CsvJdbc, "+message + "\r\n");
        fileLogr.close();
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }

}

⌨️ 快捷键说明

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