using FreeSql.Internal; using FreeSql.Internal.CommonProvider; using FreeSql.Odbc.Default; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading; namespace FreeSql.Odbc.Default { public class OdbcProvider : IFreeSql { public ISelect Select() where T1 : class => new OdbcSelect(this, this.InternalCommonUtils, this.InternalCommonExpression, null); public ISelect Select(object dywhere) where T1 : class => new OdbcSelect(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere); public IInsert Insert() where T1 : class => new OdbcInsert(this, this.InternalCommonUtils, this.InternalCommonExpression); public IInsert Insert(T1 source) where T1 : class => this.Insert().AppendData(source); public IInsert Insert(T1[] source) where T1 : class => this.Insert().AppendData(source); public IInsert Insert(List source) where T1 : class => this.Insert().AppendData(source); public IInsert Insert(IEnumerable source) where T1 : class => this.Insert().AppendData(source); public IUpdate Update() where T1 : class => new OdbcUpdate(this, this.InternalCommonUtils, this.InternalCommonExpression, null); public IUpdate Update(object dywhere) where T1 : class => new OdbcUpdate(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere); public IDelete Delete() where T1 : class => new OdbcDelete(this, this.InternalCommonUtils, this.InternalCommonExpression, null); public IDelete Delete(object dywhere) where T1 : class => new OdbcDelete(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere); public IAdo Ado { get; } public IAop Aop { get; } public ICodeFirst CodeFirst { get; } public IDbFirst DbFirst => throw new NotImplementedException("FreeSql.Odbc.Default 未实现该功能"); /// /// 生成一个普通访问功能的 IFreeSql 对象,用来访问 odbc /// /// /// /// 适配器 public OdbcProvider(string masterConnectionString, string[] slaveConnectionString) { this.InternalCommonUtils = new OdbcUtils(this); this.InternalCommonExpression = new OdbcExpression(this.InternalCommonUtils); this.Ado = new OdbcAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString); this.Aop = new AopProvider(); this.CodeFirst = new OdbcCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression); var _utils = InternalCommonUtils as OdbcUtils; //处理 MaxLength this.Aop.ConfigEntityProperty += new EventHandler((s, e) => { object[] attrs = null; try { attrs = e.Property.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常 } catch { } var maxlenAttr = attrs.Where(a => { return ((a as Attribute)?.TypeId as Type)?.Name == "MaxLengthAttribute"; }).FirstOrDefault(); if (maxlenAttr != null) { var lenProp = maxlenAttr.GetType().GetProperties().Where(a => a.PropertyType.IsNumberType()).FirstOrDefault(); if (lenProp != null && int.TryParse(string.Concat(lenProp.GetValue(maxlenAttr, null)), out var tryval)) { if (tryval != 0) { switch (this.Ado.DataType) { case DataType.Sqlite: e.ModifyResult.DbType = tryval > 0 ? $"{_utils.Adapter.MappingOdbcTypeVarChar}({tryval})" : _utils.Adapter.MappingOdbcTypeText; break; } } } } }); } internal CommonUtils InternalCommonUtils { get; } internal CommonExpression InternalCommonExpression { get; } public void Transaction(Action handler) => Ado.Transaction(handler); public void Transaction(TimeSpan timeout, Action handler) => Ado.Transaction(timeout, handler); public void Transaction(IsolationLevel isolationLevel, TimeSpan timeout, Action handler) => Ado.Transaction(isolationLevel, timeout, handler); public GlobalFilter GlobalFilter { get; } = new GlobalFilter(); ~OdbcProvider() => this.Dispose(); int _disposeCounter; public void Dispose() { if (Interlocked.Increment(ref _disposeCounter) != 1) return; try { (this.Ado as AdoProvider)?.Dispose(); } finally { FreeSqlOdbcGlobalExtensions._dicOdbcAdater.TryRemove(this as IFreeSql, out var tryada); } } } } partial class FreeSqlOdbcGlobalExtensions { internal static OdbcAdapter DefaultOdbcAdapter = new OdbcAdapter(); internal static ConcurrentDictionary _dicOdbcAdater = new ConcurrentDictionary(); public static void SetOdbcAdapter(this IFreeSql that, OdbcAdapter adapter) => _dicOdbcAdater.AddOrUpdate(that, adapter, (fsql, old) => adapter); internal static OdbcAdapter GetOdbcAdapter(this IFreeSql that) => _dicOdbcAdater.TryGetValue(that, out var tryada) ? tryada : DefaultOdbcAdapter; }