using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

using System.Data.SqlClient;
using System.Collections;
using System.Data.SqlServerCe;
using Sdaleo.Systems;
using Devart.Data.PostgreSql;

namespace Sdaleo
{    
    /// <summary>
    /// This class is a Object factory wrapper class around our DBMS
    /// </summary>
    internal static class DBMS
    {
        /// <summary>
        /// Creates a Connection Object depending on the type of DBMS we are using
        /// </summary>
        /// <param name="dbsystem">the DBSystem you want parameters for</param>
        /// <param name="ConnectionStr">Must pass in a valid Connection String</param>
        /// <returns>a valid connection * Caller Must Dispose of the Object *, or null if system is not available</returns>
        internal static DbConnection CreateDbConnection(DBSystem dbsystem, string ConnectionStr)
        {
            DbConnection connection = null;
            if (!String.IsNullOrEmpty(ConnectionStr))
            {
                switch (dbsystem)
                {
                    case DBSystem.SQL_SERVER:
                        if(SystemAvailable.Check(dbsystem))
                           connection = (DbConnection)new SqlConnection(ConnectionStr);
                        break;

                    case DBSystem.SQL_CE:
                        if (SystemAvailable.Check(dbsystem))
                            connection = (DbConnection)new SqlCeConnection(ConnectionStr);
                        break;

                    case DBSystem.ADVANTAGE:
                        if (SystemAvailable.Check(dbsystem))
                            connection = (DbConnection)new SqlCeConnection(ConnectionStr);
                        break;

                    case DBSystem.CTREE:
                        if (SystemAvailable.Check(dbsystem))
                            connection = (DbConnection)new SqlCeConnection(ConnectionStr);
                        break;

                    case DBSystem.MYSQL:
                        if (SystemAvailable.Check(dbsystem))
                            connection = (DbConnection)new SqlCeConnection(ConnectionStr);
                        break;

                    case DBSystem.POSTGRES:
                        if (SystemAvailable.Check(dbsystem))
                            connection = (DbConnection)new PgSqlConnection(ConnectionStr);
                        break;
                }
            }
            return connection;
        }

        /// <summary>
        /// Creates a DataAdapter depending on the type of DBMS we are using
        /// </summary>
        /// <param name="dbsystem">the DBSystem you want a DataAdapter for</param>
        /// <param name="dbcommand">pass in a valid Command Object for the DBMS</param>
        /// <returns>a valid DataAdapter * Caller Must Dispose of the Object *, or null if system is not available</returns>
        internal static DbDataAdapter CreateDbDataAdapter(DBSystem dbsystem, DbCommand dbcommand)
        {
            DbDataAdapter dbDataAdapter = null;
            switch (dbsystem)
            {
                case DBSystem.SQL_SERVER:
                    if (SystemAvailable.Check(dbsystem))
                        dbDataAdapter = new SqlDataAdapter((SqlCommand) dbcommand);
                    break;

                case DBSystem.SQL_CE:
                    if (SystemAvailable.Check(dbsystem))
                        dbDataAdapter = new SqlCeDataAdapter((SqlCeCommand) dbcommand);
                    break;

                case DBSystem.ADVANTAGE:
                    if (SystemAvailable.Check(dbsystem))
                        dbDataAdapter = new SqlCeDataAdapter((SqlCeCommand)dbcommand);
                    break;

                case DBSystem.CTREE:
                    if (SystemAvailable.Check(dbsystem))
                        dbDataAdapter = new SqlCeDataAdapter((SqlCeCommand)dbcommand);
                    break;

                case DBSystem.MYSQL:
                    if (SystemAvailable.Check(dbsystem))
                        dbDataAdapter = new SqlCeDataAdapter((SqlCeCommand)dbcommand);
                    break;

                case DBSystem.POSTGRES:
                    if (SystemAvailable.Check(dbsystem))
                        dbDataAdapter = new PgSqlDataAdapter((PgSqlCommand)dbcommand);
                    break;
            }
            return dbDataAdapter;
        }
        
        /// <summary>
        /// Creates an Error Object to be returned to the Caller, fills values depending on DBMS
        /// </summary>
        /// <param name="dbsystem">the DBSystem you want an Error for</param>
        /// <param name="e">the exception being thrown by the DBMS</param>
        /// <returns>a DBRetVal Object filled with the Error Data, or null if system is not available</returns>
        internal static DBRetVal CreateErrorDBRetVal(DBSystem dbsystem, Exception e)
        {
            DBRetVal retVal = null;
            switch (dbsystem)
            {
                case DBSystem.SQL_SERVER:
                    {
                        if (SystemAvailable.Check(dbsystem))
                        {
                            SqlException ex = e as SqlException;
                            if (ex != null)
                                retVal = new DBRetVal(ex.Number, ex.Message, ex.Errors);
                        }
                    }
                    break;

                case DBSystem.SQL_CE:
                    {
                        if (SystemAvailable.Check(dbsystem))
                        {
                            SqlCeException ex = e as SqlCeException;
                            if (ex != null)
                                retVal = new DBRetVal(ex.NativeError, ex.Message, ex.Errors);
                        }
                    }
                    break;

                case DBSystem.ADVANTAGE:
                    {
                        if (SystemAvailable.Check(dbsystem))
                        {
                            SqlCeException ex = e as SqlCeException;
                            if (ex != null)
                                retVal = new DBRetVal(ex.NativeError, ex.Message, ex.Errors);
                        }
                    }
                    break;

                case DBSystem.CTREE:
                    {
                        if (SystemAvailable.Check(dbsystem))
                        {
                            SqlCeException ex = e as SqlCeException;
                            if (ex != null)
                                retVal = new DBRetVal(ex.NativeError, ex.Message, ex.Errors);
                        }
                    }
                    break;

                case DBSystem.MYSQL:
                    {
                        if (SystemAvailable.Check(dbsystem))
                        {
                            SqlCeException ex = e as SqlCeException;
                            if (ex != null)
                                retVal = new DBRetVal(ex.NativeError, ex.Message, ex.Errors);
                        }
                    }
                    break;

                case DBSystem.POSTGRES:
                    {
                        if (SystemAvailable.Check(dbsystem))
                        {
                            PgSqlException ex = e as PgSqlException;
                            if (ex != null)
                                retVal = new DBRetVal(0, ex.Message, null);
                        }
                    }
                    break;
            }
            return retVal;
        }

        /// <summary>
        /// Creates a DBMS Specific Parameter Array from a DMBS Independent Parameter Array
        /// </summary>
        /// <param name="dbsystem">the DBSystem you want parameters for</param>
        /// <param name="parameters">a valid DBMSIndependent Param array</param>
        /// <returns>a DBMS Specific Parameter Array</returns>
        internal static DbParameter[] CreateDbParameter(DBSystem dbsystem, DBMSIndParameter[] parameters)
        {
            List<DbParameter> param_s = new List<DbParameter>();

            // Convert the Parameters
            foreach (DBMSIndParameter parameter in parameters)
            {
                switch (dbsystem)
                {
                    case DBSystem.SQL_SERVER:
                        {
                            if (SystemAvailable.Check(dbsystem))
                            {
                                param_s.Add(new SqlParameter(parameter.parameterName, parameter.Value));                                
                            }
                        }
                        break;

                    case DBSystem.SQL_CE:
                        {
                            if (SystemAvailable.Check(dbsystem))
                            {
                                param_s.Add(new SqlCeParameter(parameter.parameterName, parameter.Value));
                            }
                        }
                        break;
                    
                    case DBSystem.ADVANTAGE:
                        {
                            if (SystemAvailable.Check(dbsystem))
                            {
                                param_s.Add(new SqlCeParameter(parameter.parameterName, parameter.Value));
                            }
                        }
                        break;

                    case DBSystem.CTREE:
                        {
                            if (SystemAvailable.Check(dbsystem))
                            {
                                param_s.Add(new SqlCeParameter(parameter.parameterName, parameter.Value));                            
                            }
                        }
                        break;

                    case DBSystem.MYSQL:
                        {
                            if (SystemAvailable.Check(dbsystem))
                            {
                                param_s.Add(new SqlCeParameter(parameter.parameterName, parameter.Value));
                            }
                        }
                        break;

                    case DBSystem.POSTGRES:
                        {
                            if (SystemAvailable.Check(dbsystem))
                            {
                                param_s.Add(new PgSqlParameter(parameter.parameterName, parameter.Value));
                            }
                        }
                        break;
                }
            }

            // Return the Parameter
            return param_s.ToArray();                            
        }
    }
}
