using System;
using System.Diagnostics;

namespace Txgy.EWS.Client.Common
{
    public class FreeSqlTencent
    {
        private static Lazy<IFreeSql> mySQLLazy = CreateLazy();

        public static IFreeSql tencentRemoteMySQL => mySQLLazy.Value;

        public static void Reset()
        {
            if (mySQLLazy.IsValueCreated)
            {
                mySQLLazy.Value.Dispose();
            }

            mySQLLazy = CreateLazy();
        }

        private static Lazy<IFreeSql> CreateLazy()
        {
            return new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
             .UseMonitorCommand(cmd => Trace.WriteLine($"Sql:{cmd.CommandText}"))
             .UseConnectionString(FreeSql.DataType.MySql, BusinessConfigManager.GetConnectionString("TencetnMySQL"))
             .Build());
        }
    }
}
