XmlStatement.java :  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » xml » Java Open Source

Java Open Source » Database JDBC Connection Pool » octopus 
octopus » org » webdocwf » util » xml » XmlStatement.java
/*
    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.xml;

import java.sql.*;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Enumeration;
//import java.util.Vector;
import java.util.ArrayList;

/**
 * Class implements the JDBC Statement interface for the XmlJdbc driver.
 *
 * @author Zoran Milakovic
 */

public class XmlStatement implements Statement
{
  private XmlConnection connection;
  private String fileName;


  /**
   *Constructor for the XmlStatement object
   *
   * @param  connection  Description of Parameter
   * @since
   */
  protected XmlStatement(XmlConnection connection)
  {
    DriverManager.println("XmlJdbc - XmlStatement() - connection=" + connection);
    this.connection = connection;
    this.fileName = connection.getPath() + connection.getExtension();
  }


  /**
   *Sets the maxFieldSize attribute of the SmlStatement object
   *
   * @param  p0                The new maxFieldSize value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setMaxFieldSize(int p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Sets the maxRows attribute of the XmlStatement object
   *
   * @param  p0                The new maxRows value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setMaxRows(int p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Sets the escapeProcessing attribute of the XmlStatement object
   *
   * @param  p0                The new escapeProcessing value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setEscapeProcessing(boolean p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Sets the queryTimeout attribute of the XmlStatement object
   *
   * @param  p0                The new queryTimeout value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setQueryTimeout(int p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Sets the cursorName attribute of the XmlStatement object
   *
   * @param  p0                The new cursorName value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setCursorName(String p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Sets the fetchDirection attribute of the XmlStatement object
   *
   * @param  p0                The new fetchDirection value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setFetchDirection(int p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Sets the fetchSize attribute of the XmlStatement object
   *
   * @param  p0                The new fetchSize value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void setFetchSize(int p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the maxFieldSize attribute of the XmlStatement object
   *
   * @return                   The maxFieldSize value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getMaxFieldSize() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the maxRows attribute of the XmlStatement object
   *
   * @return                   The maxRows value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getMaxRows() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the queryTimeout attribute of the XmlStatement object
   *
   * @return                   The queryTimeout value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getQueryTimeout() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the warnings attribute of the XmlStatement object
   *
   * @return                   The warnings value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public SQLWarning getWarnings() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the resultSet attribute of the XmlStatement object
   *
   * @return                   The resultSet value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public ResultSet getResultSet() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the updateCount attribute of the XmlStatement object
   *
   * @return                   The updateCount value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getUpdateCount() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the moreResults attribute of the XmlStatement object
   *
   * @return                   The moreResults value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public boolean getMoreResults() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the fetchDirection attribute of the XmlStatement object
   *
   * @return                   The fetchDirection value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getFetchDirection() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the fetchSize attribute of the XmlStatement object
   *
   * @return                   The fetchSize value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getFetchSize() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the resultSetConcurrency attribute of the XmlStatement object
   *
   * @return                   The resultSetConcurrency value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getResultSetConcurrency() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the resultSetType attribute of the XmlStatement object
   *
   * @return                   The resultSetType value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int getResultSetType() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Gets the connection attribute of the XmlStatement object
   *
   * @return                   The connection value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public Connection getConnection() throws SQLException
  {
    return connection;
  }


  /**
   *Description of the Method
   *
   * @param  sql               Sql statement
   * @return                   ResultSet
   * @exception  SQLException
   * @since
   */
  public ResultSet executeQuery(String sql) throws SQLException
  {
    DriverManager.println("XmlJdbc - XmlStatement:executeQuery() - sql= " + sql);
    XmlWriter writer;
    if( this.connection.getAutoCommit() )
      writer = new XmlWriter(fileName,true);
    else
      writer = new XmlWriter(fileName,false);
    XmlSqlParser parser = new XmlSqlParser( this.fileName, this.connection.getAutoCommit() );
    try
    {
      parser.parse(sql);
    }
    catch (Exception e)
    {
      throw new SQLException("Syntax Error. " + e.getMessage());
    }
    if (parser.sqlType.equals(parser.DROP_TABLE)) {
      execute(sql);
      return null;
    }
    if (parser.sqlType.equals(parser.DELETE)) {
      executeUpdate(sql);
      return null;
    }
    if (parser.sqlType.equals(parser.UPDATE)) {
      executeUpdate(sql);
      return null;
    }
    else if (parser.sqlType.equals(parser.INSERT)) {
      executeUpdate(sql);
      return null;
    }
    else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
      execute( sql );
      return null;
    } else if(parser.sqlType.equals(parser.SELECT) ) {

    String fileName = connection.getPath() + connection.getExtension();
    File checkFile = new File(fileName);
    XmlReader reader;
    try
    {
      reader = new XmlReader(fileName);
      String[] xxx = parser.getColumnNames();
      String[] yyy = (String[])writer.getTableProperties( parser.getTableName() ).get(0);
      boolean isOK = true;
      for(int i=0; i< xxx.length; i++) {
        out:
      for(int j=0; j< yyy.length; j++) {
        if(xxx[i].equalsIgnoreCase(yyy[j])) {
          isOK=true;
          break out;
        }
        else
          isOK=false;
      }
      if(!isOK)
        throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName());
      }
    }
    catch (Exception e)
    {
      throw new SQLException("Error reading data file. Message was: " + e);
    }
    XmlResultSet resultSet = new XmlResultSet(this, reader,
        parser.getTableName(), parser.getColumnNames() , parser.getWhereColumnNames() , parser.getWhereColumnValues());
    resultSet.select();
    return resultSet;
    } else {
      throw new SQLException("Syntax error.Not supported sql statement.");
    }
  }



  /**
   * Insert data into XML database
   *
   * @param  sql               Description of Parameter
   * @return                   Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int executeUpdate(String sql) throws SQLException
  {
    DriverManager.println("XmlJdbc - XmlStatement:executeUpdate() - sql= " + sql);
    XmlSqlParser parser = new XmlSqlParser();
    try
    {
      parser.parse(sql);
    }
    catch (Exception e)
    {
      throw new SQLException("Syntax Error. " + e.getMessage());
    }
    if(parser.sqlType.equals(parser.SELECT))
      throw new SQLException("Not supported SELECT statement - use executeQuery() method");
    else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
      throw new SQLException("Not supported CREATE_TABLE statement - use execute() method");
    }
    else if (parser.sqlType.equals(parser.DROP_TABLE)) {
      throw new SQLException("Not supported DROP_TABLE statement - use execute() method");
    }
    else if (parser.sqlType.equals(parser.INSERT)) {
      String fileName = connection.getPath() + connection.getExtension();
      XmlWriter writeXml;
      try
      {
        if( this.connection.getAutoCommit() )
          writeXml = new XmlWriter(fileName,true);
        else
          writeXml = new XmlWriter(fileName,false);
        ArrayList properties = writeXml.getTableProperties( parser.tableName );
        //check for existance of columns
        String[] xxx = parser.getColumnNames();
        String[] yyy = (String[])properties.get(0);
        boolean isOK = true;
        for(int i=0; i< xxx.length; i++) {
          if(!xxx[i].endsWith("*")) {
            out:
        for(int j=0; j< yyy.length; j++) {
          if(xxx[i].equalsIgnoreCase(yyy[j])) {
            isOK=true;
            break out;
          }
          else
            isOK=false;
        }
        if(!isOK)
          throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName());
          }
        }
        //check if NOTNULL columns is set
        if( !properties.get(1).toString().equalsIgnoreCase("NO CREATE TABLE") ) {
          yyy = parser.getColumnNames();
          xxx = (String[])writeXml.getTableProperties( parser.tableName ).get(2);
          isOK = true;
          for(int i=0; i< xxx.length; i++) {
            out:
          for(int j=0; j< yyy.length; j++) {
            if(xxx[i].equalsIgnoreCase(yyy[j])) {
              isOK=true;
              break out;
            }
            else
              isOK=false;
          }
          if(!isOK)
            throw new SQLException("Column '" + xxx[i] + "' can not be NULL.");
          }
        }
        writeXml.insert( parser.getTableName() , parser.getColumnNames() , parser.getColumnValues() );

      }
      catch (Exception e)
      {
        throw new SQLException("Error reading data file. Message was: " + e);
      }

    }
    else if (parser.sqlType.equals(parser.UPDATE)) {
      String fileName = connection.getPath() + connection.getExtension();
      XmlWriter writeXml;
      try
      {
        if( this.connection.getAutoCommit() )
          writeXml = new XmlWriter(fileName,true);
        else
          writeXml = new XmlWriter(fileName,false);
        String[] xxx = parser.getColumnNames();
        String[] yyy = (String[])writeXml.getTableProperties( parser.tableName ).get(0);
        boolean isOK = true;
        for(int i=0; i< xxx.length; i++) {
          if(!xxx[i].endsWith("*")) {
            out:
        for(int j=0; j< yyy.length; j++) {
          if(xxx[i].equalsIgnoreCase(yyy[j])) {
            isOK=true;
            break out;
          }
          else
            isOK=false;
        }
        if(!isOK)
          throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName());
          }
        }
        writeXml.update( parser.getTableName() , parser.getColumnNames() , parser.getColumnValues() , parser.getWhereColumnNames() , parser.getWhereColumnValues() );
      }
      catch (Exception e)
      {
        throw new SQLException("Error reading data file. Message was: " + e);
      }

    }
    else if (parser.sqlType.equals(parser.DELETE)) {
      String fileName = connection.getPath() + connection.getExtension();
      XmlWriter writeXml;
      try
      {
        if( this.connection.getAutoCommit() )
          writeXml = new XmlWriter(fileName,true);
        else
          writeXml = new XmlWriter(fileName,false);
        String[] xxx = parser.getWhereColumnNames();
        String[] yyy = (String[])writeXml.getTableProperties( parser.tableName ).get(0);
        boolean isOK = true;
        for(int i=0; i< xxx.length; i++) {
          if(!xxx[i].endsWith("*")) {
            out:
        for(int j=0; j< yyy.length; j++) {
          if(xxx[i].equalsIgnoreCase(yyy[j])) {
            isOK=true;
            break out;
          }
          else
            isOK=false;
        }
        if(!isOK)
          throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName());
          }
        }
        writeXml.delete( parser.getTableName() , parser.getWhereColumnNames() , parser.getWhereColumnValues() );
      }
      catch (Exception e)
      {
        throw new SQLException("Error reading data file. Message was: " + e);
      }

    }
    else if (parser.sqlType.equals(parser.DROP_TABLE)) {
    }
    return 0;


  }


  /**
   * Releases this <code>Statement</code> object's database
   * and JDBC resources immediately instead of waiting for
   * this to happen when it is automatically closed.
   * It is generally good practice to release resources as soon as
   * you are finished with them to avoid tying up database
   * resources.
   * <P>
   * Calling the method <code>close</code> on a <code>Statement</code>
   * object that is already closed has no effect.
   * <P>
   * <B>Note:</B> A <code>Statement</code> object is automatically closed
   * when it is garbage collected. When a <code>Statement</code> object is
   * closed, its current <code>ResultSet</code> object, if one exists, is
   * also closed.
   *
   * @exception SQLException if a database access error occurs
   */
  public void close() throws SQLException {

  }


  /**
   *Description of the Method
   *
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void cancel() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Description of the Method
   *
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void clearWarnings() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Description of the Method
   *
   * @param  sql               Description of Parameter
   * @return                   Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public boolean execute(String sql) throws SQLException
  {
    XmlWriter writeXml;
    if( this.connection.getAutoCommit() )
      writeXml = new XmlWriter(fileName,true);
    else
      writeXml = new XmlWriter(fileName,false);
    XmlSqlParser parser = new XmlSqlParser( this.fileName, this.connection.getAutoCommit() );
    try
    {
      parser.parse(sql);
    }
    catch (Exception e)
    {
      throw new SQLException("Syntax Error. " + e.getMessage());
    }
    if(parser.sqlType.equals(parser.SELECT))
      throw new SQLException("Use executeQuery() for SELECT statement");
    else if (parser.sqlType.equals(parser.UPDATE))
      executeUpdate(sql);
    else if (parser.sqlType.equals(parser.INSERT))
      executeUpdate(sql);
    else if (parser.sqlType.equals(parser.DELETE))
      executeUpdate(sql);
    else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
      String fileName = connection.getPath() + connection.getExtension();
      try
      {
        writeXml.createTable( parser.getSqlStatement() , parser.getTableName() );
      }
      catch (Exception e)
      {
        throw new SQLException("Error reading data file. Message was: " + e);
      }
    } else if (parser.sqlType.equals(parser.DROP_TABLE)) {
      String fileName = connection.getPath() + connection.getExtension();
      try
      {
        writeXml.dropTable( parser.getTableName() );
      }
      catch (Exception e)
      {
        throw new SQLException("Error reading data file. Message was: " + e);
      }
    }
    return true;

  }



  /**
   *Adds a feature to the Batch attribute of the XmlStatement object
   *
   * @param  p0                The feature to be added to the Batch attribute
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void addBatch(String p0) throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Description of the Method
   *
   * @exception  SQLException  Description of Exception
   * @since
   */
  public void clearBatch() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }


  /**
   *Description of the Method
   *
   * @return                   Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int[] executeBatch() throws SQLException
  {
    throw new SQLException("Not Supported !");
  }

  //---------------------------------------------------------------------
  // JDBC 3.0
  //---------------------------------------------------------------------

  public boolean getMoreResults(int current) throws SQLException {
    throw new UnsupportedOperationException("Statement.getMoreResults(int) unsupported");
  }

  public ResultSet getGeneratedKeys() throws SQLException {
    throw new UnsupportedOperationException("Statement.getGeneratedKeys() unsupported");
  }

  public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
    throw new UnsupportedOperationException("Statement.executeUpdate(String,int) unsupported");
  }

  public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
    throw new UnsupportedOperationException("Statement.executeUpdate(String,int[]) unsupported");
  }

  public int executeUpdate(String sql, String[] columnNames) throws SQLException {
    throw new UnsupportedOperationException("Statement.executeUpdate(String,String[]) unsupported");
  }

  public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
    throw new UnsupportedOperationException("Statement.execute(String,int) unsupported");
  }

  public boolean execute(String sql, int[] columnIndexes) throws SQLException {
    throw new UnsupportedOperationException("Statement.execute(String,int[]) unsupported");
  }

  public boolean execute(String sql, String[] columnNames) throws SQLException {
    throw new UnsupportedOperationException("Statement.execute(String,String[]) unsupported");
  }

  public int getResultSetHoldability() throws SQLException {
    throw new UnsupportedOperationException("Statement.getResultSetHoldability() unsupported");
  }


   public boolean isClosed() throws SQLException {
      // TODO Auto-generated method stub
      return false;
   }

   public boolean isPoolable() throws SQLException {
      // TODO Auto-generated method stub
      return false;
   }

   public void setPoolable(boolean poolable) throws SQLException {
      // TODO Auto-generated method stub
      throw new UnsupportedOperationException("Statement.setPoolable(boolean) unsupported");
   }

   public boolean isWrapperFor(Class<?> iface) throws SQLException {
      // TODO Auto-generated method stub
      return false;
   }

   public <T> T unwrap(Class<T> iface) throws SQLException {
      // TODO Auto-generated method stub
      return null;
   }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.