using System;
using System.Data;
using System.Data.OleDb;

namespace System.Data.AccessClient 
{
   /// <summary>
   /// Summary description for System.
   /// </summary>
   public class AccessCommand : IDbCommand
   {
      private OleDbCommand _cmd = null;

      #region construct/init/dispose

      public AccessCommand()
      {
         this._cmd = new OleDbCommand();
		 this._cmd.CommandType = CommandType.Text;
      }
      public AccessCommand ( AccessConnection Connection )
      {
         this._cmd = new OleDbCommand();
         this._cmd.Connection = Connection.OleDbConnection;
      }

      public void Dispose()
      {
         if ( this._cmd != null)
            this._cmd.Dispose();
      }

      public void Cancel()
      {
         this._cmd.Cancel();
      }


      #endregion

      #region IDbCommand implementation

      public string CommandText
      {
         get { return this._cmd.CommandText;  }
         set { this._cmd.CommandText = value; }
      }

      public int CommandTimeout
      {
         get { return this._cmd.CommandTimeout;  }
         set { this._cmd.CommandTimeout = value; }
      }

      public System.Data.CommandType CommandType
      {
         get { return CommandType.Text;  }
         set 
		 { 
			 if (value != CommandType.Text)
				throw new NotSupportedException ("AccessCommand supports only Text command type"); 
		 }
      }

      public IDbConnection Connection
      {
         get { return this._cmd.Connection;  }
         set 
         { 
            try
            {
               if ( typeof(OleDbConnection).Equals(value.GetType())				||
				    typeof(OleDbConnection).Equals(value.GetType().BaseType))
				   				   
                  this._cmd.Connection = (OleDbConnection) value; 

               else if 
				   (typeof(AccessConnection).Equals(value.GetType())			||
				    typeof(AccessConnection).Equals(value.GetType().BaseType))

				  this._cmd.Connection = ((AccessConnection)value).OleDbConnection; 
			   else
                  throw new NotSupportedException ("AccessCommand supports only OleDbConnection and AccessConnection types");
            }
            catch ( Exception ex )
            {
               throw ex;
            }
         }
      }

      public IDbDataParameter CreateParameter()
      {
         return this._cmd.CreateParameter();
      }

      public int ExecuteNonQuery() 
      {
         return this._cmd.ExecuteNonQuery();
      }

	   public IDataReader ExecuteReader()
	   {
		   try
		   {
			   return this._cmd.ExecuteReader();
		   }
		   catch
		   {
			   throw;
		   }
	   }
	   
      public IDataReader ExecuteReader(CommandBehavior behavior )
      {
         return this._cmd.ExecuteReader(behavior);
      }

      public object ExecuteScalar()
      {
         return this._cmd.ExecuteScalar();
      }

      public IDataParameterCollection Parameters
      {
         get
         {
            return this._cmd.Parameters;
         }
      }
   
      public void Prepare()
      {
         this._cmd.Prepare();
      }

      public IDbTransaction Transaction
      {
         get
         {
            return this._cmd.Transaction;
         }
         set
         {
            this._cmd.Transaction = (OleDbTransaction)value;
         }
      }

      public UpdateRowSource UpdatedRowSource
      {
         get
         {
            return this._cmd.UpdatedRowSource;
         }
         set
         {
            this._cmd.UpdatedRowSource = value;
         }
      }
   

      #endregion
   
   }
}