Skip to content

Commit

Permalink
支持加载多种数据库驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 30, 2018
1 parent 63dd500 commit 9e9881a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
96 changes: 53 additions & 43 deletions XCode/DataAccessLayer/Common/DbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,64 +306,74 @@ public IMetaData CreateMetaData()
/// <summary>获取提供者工厂</summary>
/// <param name="assemblyFile"></param>
/// <param name="className"></param>
/// <param name="ignoreError"></param>
/// <returns></returns>
protected static DbProviderFactory GetProviderFactory(String assemblyFile, String className)
public static DbProviderFactory GetProviderFactory(String assemblyFile, String className, Boolean ignoreError = false)
{
var name = Path.GetFileNameWithoutExtension(assemblyFile);
var linkName = name;
if (Runtime.Is64BitProcess) linkName += "64";
var ver = Environment.Version;
if (ver.Major >= 4) linkName += "Fx" + ver.Major + ver.Minor;
// 有些数据库驱动不区分x86/x64,并且逐步以Fx4为主,所以来一个默认
linkName += ";" + name;
try
{
var name = Path.GetFileNameWithoutExtension(assemblyFile);
var linkName = name;
if (Runtime.Is64BitProcess) linkName += "64";
var ver = Environment.Version;
if (ver.Major >= 4) linkName += "Fx" + ver.Major + ver.Minor;
// 有些数据库驱动不区分x86/x64,并且逐步以Fx4为主,所以来一个默认
linkName += ";" + name;

#if __CORE__
linkName = "st_" + name;
if (!name.IsNullOrEmpty())
{
className = className + "," + name;//指定完全类型名可获取项目中添加了引用的类型,否则dll文件需要放在根目录
}
linkName = "st_" + name;
if (!name.IsNullOrEmpty())
{
className = className + "," + name;//指定完全类型名可获取项目中添加了引用的类型,否则dll文件需要放在根目录
}
#endif

var type = PluginHelper.LoadPlugin(className, null, assemblyFile, linkName);

// 反射实现获取数据库工厂
var file = assemblyFile;
var plugin = NewLife.Setting.Current.GetPluginPath();
file = plugin.CombinePath(file);
var type = PluginHelper.LoadPlugin(className, null, assemblyFile, linkName);

// 如果还没有,就写异常
if (type == null)
{
if (assemblyFile.IsNullOrEmpty()) return null;
if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file);
}

if (type == null)
{
XTrace.WriteLine("驱动文件{0}无效或不适用于当前环境,准备删除后重新下载!", assemblyFile);
// 反射实现获取数据库工厂
var file = assemblyFile;
var plugin = NewLife.Setting.Current.GetPluginPath();
file = plugin.CombinePath(file);

try
// 如果还没有,就写异常
if (type == null)
{
File.Delete(file);
if (assemblyFile.IsNullOrEmpty()) return null;
if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file);
}
catch (UnauthorizedAccessException) { }
catch (Exception ex) { XTrace.Log.Error(ex.ToString()); }

type = PluginHelper.LoadPlugin(className, null, file, linkName);
if (type == null)
{
XTrace.WriteLine("驱动文件{0}无效或不适用于当前环境,准备删除后重新下载!", assemblyFile);

// 如果还没有,就写异常
if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file);
}
if (type == null) return null;
try
{
File.Delete(file);
}
catch (UnauthorizedAccessException) { }
catch (Exception ex) { XTrace.Log.Error(ex.ToString()); }

var asm = type.Assembly;
if (DAL.Debug) DAL.WriteLog("{2}驱动{0} 版本v{1}", asm.Location, asm.GetName().Version, name ?? className.TrimEnd("Client", "Factory"));
type = PluginHelper.LoadPlugin(className, null, file, linkName);

var field = type.GetFieldEx("Instance");
if (field == null) return Activator.CreateInstance(type) as DbProviderFactory;
// 如果还没有,就写异常
if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file);
}
if (type == null) return null;

return Reflect.GetValue(null, field) as DbProviderFactory;
var asm = type.Assembly;
if (DAL.Debug) DAL.WriteLog("{2}驱动{0} 版本v{1}", asm.Location, asm.GetName().Version, name ?? className.TrimEnd("Client", "Factory"));

var field = type.GetFieldEx("Instance");
if (field == null) return Activator.CreateInstance(type) as DbProviderFactory;

return Reflect.GetValue(null, field) as DbProviderFactory;
}
catch
{
if (ignoreError) return null;

throw;
}
}

[DllImport("kernel32.dll")]
Expand Down
4 changes: 2 additions & 2 deletions XCode/DataAccessLayer/Database/MySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override DbProviderFactory Factory
//_Factory = GetProviderFactory("NewLife.MySql.dll", "NewLife.MySql.MySqlClientFactory") ??
// GetProviderFactory("MySql.Data.dll", "MySql.Data.MySqlClient.MySqlClientFactory");
// MewLife.MySql 在开发过程中,数据驱动下载站点没有它的包,暂时不支持下载
_Factory = GetProviderFactory(null, "NewLife.MySql.MySqlClientFactory") ??
_Factory = GetProviderFactory(null, "NewLife.MySql.MySqlClientFactory", true) ??
GetProviderFactory("MySql.Data.dll", "MySql.Data.MySqlClient.MySqlClientFactory");
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ protected override Boolean DatabaseExist(String databaseName)

public override String CreateDatabaseSQL(String dbname, String file)
{
return base.CreateDatabaseSQL(dbname, file)+ " DEFAULT CHARACTER SET utf8mb4";
return base.CreateDatabaseSQL(dbname, file) + " DEFAULT CHARACTER SET utf8mb4";
}

public override String DropDatabaseSQL(String dbname) => String.Format("Drop Database If Exists {0}", FormatName(dbname));
Expand Down
6 changes: 5 additions & 1 deletion XCode/DataAccessLayer/Database/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public override DbProviderFactory Factory
_Factory = GetProviderFactory("Mono.Data.Sqlite.dll", "Mono.Data.Sqlite.SqliteFactory");
else
{
_Factory = GetProviderFactory(null, "Microsoft.Data.Sqlite.SqliteFactory");
#if __CORE__
_Factory = GetProviderFactory("Microsoft.Data.Sqlite.dll", "Microsoft.Data.Sqlite.SqliteFactory", true);
#else
//_Factory = GetProviderFactory(null, "Microsoft.Data.Sqlite.SqliteFactory");
#endif
if (_Factory == null) _Factory = GetProviderFactory("System.Data.SQLite.dll", "System.Data.SQLite.SQLiteFactory");
}
}
Expand Down

0 comments on commit 9e9881a

Please sign in to comment.