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

namespace System.Data.AccessClient
{
   /// <summary>
   /// Summary description for AccessConnection.
   /// </summary>
   public class AccessConnection : IDbConnection
   {
		private OleDbConnection						_cnn        = null;

      #region construct/init/dispose
      
      public AccessConnection()
      {
          this._cnn = new OleDbConnection();
		  
      }
      public AccessConnection(string ConnectString)
      {
		  this._cnn = new OleDbConnection(ConnectString);
      }

	   public void Dispose()
	   {
		   if (this._cnn.State != ConnectionState.Closed)
			   this._cnn.Close();
		   this._cnn.Dispose();
	   }

      #endregion

      internal OleDbConnection OleDbConnection
      {
         get { return _cnn; }
      }

      #region IDbConnection implementation

      public IDbTransaction BeginTransaction() 
      {
		  try
		  {
			  return (OleDbTransaction)this._cnn.BeginTransaction();
		  }
		  catch (Exception ex)
		  {
			  throw ex;
		  }
      }

      public IDbTransaction BeginTransaction(IsolationLevel il ) 
      {
		  try
		  {
			  return this._cnn.BeginTransaction(il);
		  }
		  catch (Exception ex)
		  {
			  throw ex;
		  }
      }

      public void ChangeDatabase(string databaseName) 
      {
		  throw new NotSupportedException("AccessConnection does not support ChangeDatabase");
      }
      
      public int ConnectionTimeout
      {
         get 
         {
            try
            {
               return this._cnn.ConnectionTimeout;
            }
            catch ( Exception ex )
            {
               throw ex;
            }
         }
      }

      public IDbCommand CreateCommand()
      {
         try
         {
            return this._cnn.CreateCommand();
         }
         catch ( Exception ex )
         {
            throw ex;
         }        
      }

      public string Database
      {
         get
         {
            try
            {
               return this._cnn.Database;
            }
            catch ( Exception ex )
            {
               throw ex;
            }
         }
      }

      public string ConnectionString
      {
         get 
         {
			 return _cnn.ConnectionString;
         }
         set
         {
            _cnn.ConnectionString = value;
         }
      }

      public void Close()
      {
		  lock (this._cnn)
		  {
			  switch (this._cnn.State)
			  {
				  case ConnectionState.Open:
				  case ConnectionState.Broken:
					  _cnn.Close();
					  break;
			  }
		  }
      }

      public virtual void Open()
      {
		  try
		  {
			  if (null == this._cnn)
				  this._cnn = new OleDbConnection(this.ConnectionString);
			  switch (this._cnn.State)
			  {
				  case ConnectionState.Broken:
					  _cnn.Close();
					  _cnn.Open();
					  break;
				  case ConnectionState.Closed:
					  _cnn.Open();
					  break;
			  }
		  }
		  catch (Exception ex)
		  {
			  throw ex;
		  }
	  }

      public ConnectionState State
      {
         get
         {
            try
            {
               return this._cnn.State;
            }
            catch ( Exception ex )
            {
               throw ex;
            }
         }
      }
 
      #endregion
   }
}
