
using System;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;

namespace STMes.DataProvider
{
	/// <summary>
	/// Implements access to the Data Provider for ODBC.
	/// </summary>
	/// <remarks>
	/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
	/// </remarks>
	/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
	public class OdbcDataProvider: IDataProvider
	{
		/// <summary>
		/// Creates the database connection object.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <returns>The database connection object.</returns>
		IDbConnection IDataProvider.CreateConnectionObject()
		{
			return new OdbcConnection();
		}
		IDbConnection IDataProvider.CreateConnectionObject(string connectionString )
		{
			OdbcConnection conn = new OdbcConnection(connectionString);
			
			return conn;
		}

		/// <summary>
		/// Creates the data adapter object.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <returns>A data adapter object.</returns>
		DbDataAdapter IDataProvider.CreateDataAdapterObject()
		{
			return new OdbcDataAdapter();
		}

		/// <summary>
		/// Populates the specified IDbCommand object's Parameters collection with 
		/// parameter information for the stored procedure specified in the IDbCommand.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <param name="command">The IDbCommand referencing the stored procedure for which the parameter information is to be derived. The derived parameters will be populated into the Parameters of this command.</param>
		void IDataProvider.DeriveParameters(IDbCommand command)
		{
			OdbcCommandBuilder.DeriveParameters((OdbcCommand)command);
		}

		/// <summary>
		/// Returns connection type.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <value>An instance of the <see cref="Type"/> class.</value>
		public Type ConnectionType
		{
			get
			{
				return typeof(OdbcConnection);
			}
		}

		/// <summary>
		/// Returns the data provider name.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataProvider Method</seealso>
		/// <value>Data provider name.</value>
		public string Name
		{
			get
			{
				return "Odbc";
			}
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public IDbDataParameter CreateDataParameter()
		{
			// TODO:  添加 OdbcDataProvider.CreateDataParameter 实现
			return new OdbcParameter();
		}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="da"></param>
	/// <returns></returns>
		public Object CreateCommandBuilder(DbDataAdapter da)
		{
			return new OdbcCommandBuilder(da as OdbcDataAdapter) as object;
		}

		/// <summary>
		/// Test Connection Info(ODBC)
		/// </summary>
		/// <param name="db">Test Connection Object</param> 
		/// <returns>Test Result</returns>
		public bool Ping(ref IDbConnection db)
		{
			// TODO:  添加 OdbcDataProvider.Ping 实现
			return true;
        }
        public int GetSID(ref IDbConnection db)
        {
            return -1;
        }
	}
}
