
using System;
using System.Data;
using System.Data.Common;
using System.Data.OracleClient;

namespace STMes.DataProvider
{
    /// <summary>
    /// Implements access to the Data Provider for Oracle.
    /// </summary>
    /// <remarks>
    /// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
    /// </remarks>
    /// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
    public class OracleDataProvider : IDataProvider
    {
        private CoreWriteLogFile cwl = new CoreWriteLogFile();
        /// <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()
        {
            OracleConnection conn = new OracleConnection();

            return conn;
        }
        IDbConnection IDataProvider.CreateConnectionObject(string connectionString)
        {
            OracleConnection conn = new OracleConnection(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 OracleDataAdapter();
        }

        /// <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)
        {
            OracleCommandBuilder.DeriveParameters((OracleCommand)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(OracleConnection);
            }
        }

        /// <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 "Oracle";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IDbDataParameter CreateDataParameter()
        {
            // TODO:  Ìí¼Ó OracleDataProvider.CreateDataParameter ÊµÏÖ
            return new OracleParameter();
        }

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

        /// <summary>
        /// Test Connection Info(ORACLE)
        /// </summary>
        /// <param name="db">Test Connection Object</param> 
        /// <returns>Test Result</returns>
        public bool Ping(ref IDbConnection db)
        {
            OracleDataReader read = null;
            try
            {
                using (OracleCommand cmd = new OracleCommand("SELECT 1 FROM DUAL", (OracleConnection)db))
                {
                    read = cmd.ExecuteReader();
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (read != null) read.Dispose();
            }
            return false;
        }

        public int GetSID(ref IDbConnection db)
        {
            //OracleDataReader read = null;
            //try
            //{
            //    int sid = -1;
            //    using (OracleCommand cmd = new OracleCommand("SELECT SID FROM SYS.V_$MYSTAT@XGCXMES WHERE ROWNUM =1", (OracleConnection)db))
            //    {
            //        read = cmd.ExecuteReader();

            //        while (read.Read())
            //        {
            //            sid = Convert.ToInt32(read.GetValue(0));
            //            read.Close();
            //            return sid;
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    cwl.WriteLog(ex.Message, LogInfoLevel.Error, "GetSID", "ORACLE");
            //}
            //finally
            //{
            //    if (read != null) read.Dispose();
            //}
            return -1;
        }
    }
}