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

namespace ryCommonDb
{
    /// <summary>
    /// 数据库引擎
    /// </summary>
    public class DataProvider
    {
        /// <summary>
        /// 数据库枚举类型
        /// </summary>
        public enum DataProviderType
        {
            /// <summary>
            /// Odbc引擎
            /// </summary>
            OdbcDataProvider = 0,
            /// <summary>
            /// OleDb引擎
            /// </summary>
            OleDbDataProvider = 1,
            /// <summary>
            /// Oracle引擎
            /// </summary>
            OracleDataProvider = 2,
            /// <summary>
            /// MSSQL引擎
            /// </summary>
            SqlDataProvider = 3,
            /// <summary>
            /// SQLite引擎
            /// </summary>
            SQLiteDataProvider = 4,
            /// <summary>
            /// MySql引擎
            /// </summary>
            MySqlDataProvider =5
        }
        /// <summary>
        /// 判断DataSet是否包含数据
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public bool HaveData(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取首行数据
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public DataRow GetData(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            return null;
        }
        /// <summary>
        /// 获取第一行第一列的值
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public int GetValue(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToInt(0);
            }
            return 0;
        }
        /// <summary>
        /// 将DataSet类型转换为RyQuickSQL列表类型
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public List<RyQuickSQL> GetInsert(System.Data.DataSet ds)
        {
            return GetInsert(ds,";id;");
        }
        /// <summary>
        /// 将DataSet类型转换为RyQuickSQL列表类型
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="no_insert_list"></param>
        /// <returns></returns>
        public List<RyQuickSQL> GetInsert(System.Data.DataSet ds,string no_insert_list)
        {
            List<RyQuickSQL> list = new List<RyQuickSQL>();
            if (HaveData(ds))
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    RyQuickSQL mySQL = new RyQuickSQL(ds.Tables[0].TableName);
                    for (int m = 0; m < ds.Tables[0].Columns.Count; m++)
                    {
                        DataColumn col = ds.Tables[0].Columns[m];
                        if (col.AutoIncrement) { continue; }
                        if((";"+no_insert_list.ToLower()+";").IndexOf(";"+ col.ColumnName.ToLower()+";")>=0) { continue; }
                        #region 转换
                        if (col.DataType == typeof(string))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToString());
                        }
                        else if (col.DataType == typeof(int) || col.DataType == typeof(Int32))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToInt(0));
                        }
                        else if (col.DataType == typeof(Int64))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToInt64(0));
                        }
                        else if (col.DataType == typeof(decimal))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToDecimal(0));
                        }
                        else if (col.DataType == typeof(DateTime))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToDateTime(Convert.ToDateTime("2000-1-1")));
                        }
                        else if (col.DataType == typeof(double))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToDouble(0));
                        }
                        else if (col.DataType == typeof(bool))
                        {
                            mySQL.AddField(col.ColumnName, ds.Tables[0].Rows[i][col].ToBool());
                        }
                        else if (col.DataType == typeof(byte[]))
                        {
                            mySQL.AddField(col.ColumnName, (byte[])ds.Tables[0].Rows[i][col]);
                        }
                        #endregion
                    }
                    list.Add(mySQL);
                }
            }
            return list;
        }
        ///// <summary>
        ///// 建立访问数据库的实例
        ///// </summary>
        ///// <param name="DataProviderType">数据库枚举类型</param>
        ///// <returns></returns>
        //public IDbInterface CreateDataProvider(DataProviderType dataProviderType)
        //{
        //    switch (dataProviderType)
        //    {
        //        //case DataProviderType.OdbcDataProvider:
        //        //    return new OdbcDataProvider();
        //        //case DataProviderType.OleDbDataProvider:
        //        //    return new OleDbDataProvider();
        //        //case DataProviderType.OracleDataProvider:
        //        //    return new OracleDataProvider();
        //        case DataProviderType.SqlDataProvider:
        //            return new SqlDataProvider();
        //        case DataProviderType.SQLiteDataProvider:
        //            return new SQLiteDataProvider();
        //        default:
        //            return null;
        //    }
        //}
    }
}
