using ryCommon;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using static ryCommonDb.DataProvider;

namespace Itrycn_Project2
{
    public static class Itrycn_Db
    {
        public static string SQLConnStr = "<app>\\UserDb\\myDb.dat|";
        public static ryCommonDb.DataProvider.DataProviderType dataType = ryCommonDb.DataProvider.DataProviderType.SQLiteDataProvider;
        public static IDbInterface CreateDataProvider()
        {
            return CreateDataProvider(dataType);
        }

        public static 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.SQLiteDataProvider:
                    return new SQLiteDataProvider();
                default:
                    return null;
            }
        }
        private static void CreateTable(ryCommonDb.DataProvider.DataProviderType dataType, string ConnStr)
        {
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                #region 表1
                RyQuickSQL mySQL = new RyQuickSQL("test1");
                //mySQL.AddField("", "");
                mySQL.AddField("addTime", DateTime.Now);
                mySQL.AddField("editTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                //
                #region 设置表
                mySQL.Clear();
                mySQL.TableName = "Setting";
                mySQL.AddField("name", "");
                mySQL.AddField("info", "");
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        /// <returns>返回大于0的数,表示设置成功,否则表示失败</returns>
        private static int SetSetting(string name ,string info)
        {
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.SQLConnStr) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("Setting");
                mySQL.AddField("name", name);
                mySQL.AddField("info", info);
                var result = db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where name='" + name + "'", mySQL);
                if (result == 0)
                {
                   result=db.ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
                }
                return result;
            }
            db.Free();
            return -1000;
        }
        /// <summary>
        ///获取设置
        /// </summary>
        /// <returns></returns>
        private static string GetSetting(string name, string defValue)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            var result = defValue;
            if (db.ConnDb(Itrycn_Db.SQLConnStr) == 1)
            {
                DataSet ds = db.ReadData("select * from Setting where name='"+ name + "'");
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    #region 读取信息               
                    result = reader["Name"].ToString();
                    #endregion
                }
            }
            db.Free();
            return result;
        }
        /// <summary>
        /// 获取数据库版本
        /// </summary>
        /// <returns></returns>
        public static int GetDbVer()
        {
            return GetSetting("dbVer","1").ToInt();
        }
        /// <summary>
        /// 设置数据库版本
        /// </summary>
        /// <returns></returns>
        public static int SetDbVer(int DbVer)
        {
            return SetSetting("dbVer", DbVer.ToString());
        }
        public static void CreateTable()
        {
            CreateTable(dataType, SQLConnStr);
        }
    }
}
