[feat] MapTo支持参数覆盖。close: https://github.com/NewLifeX/NewLife.XCode/issues/51
This commit is contained in:
parent
6d8becc490
commit
379ce47b61
|
@ -70,7 +70,9 @@ public partial class DAL
|
|||
if (db is DbBase dbBase) dbBase.Tracer = Tracer;
|
||||
|
||||
// 设置连接字符串时,可能触发内部的一系列动作,因此放在最后
|
||||
if (!ConnStr.IsNullOrEmpty()) db.ConnectionString = DecodeConnStr(ConnStr);
|
||||
var connStr = ConnStr;
|
||||
if (!_mapConnStr.IsNullOrEmpty()) connStr = _mapConnStr;
|
||||
if (!connStr.IsNullOrEmpty()) db.ConnectionString = DecodeConnStr(connStr);
|
||||
|
||||
_Db = db;
|
||||
|
||||
|
@ -86,6 +88,7 @@ public partial class DAL
|
|||
public IAsyncDbSession AsyncSession => (Db as DbBase)!.CreateSessionForAsync();
|
||||
|
||||
private String? _mapTo;
|
||||
private String? _mapConnStr;
|
||||
private static ICache _cache = new MemoryCache();
|
||||
#endregion
|
||||
|
||||
|
@ -122,12 +125,31 @@ public partial class DAL
|
|||
AddConnStr(connName, connstr, null, "SQLite");
|
||||
}
|
||||
|
||||
ConnStr = css[connName];
|
||||
if (ConnStr.IsNullOrEmpty()) throw new XCodeException("请在使用数据库前设置[" + connName + "]连接字符串");
|
||||
var connStr = ConnStr = css[connName];
|
||||
if (connStr.IsNullOrEmpty()) throw new XCodeException("请在使用数据库前设置[" + connName + "]连接字符串");
|
||||
|
||||
// 连接映射
|
||||
var vs = ConnStr.SplitAsDictionary("=", ",", true);
|
||||
if (vs.TryGetValue("MapTo", out var map) && !map.IsNullOrEmpty()) _mapTo = map;
|
||||
var vs = connStr.SplitAsDictionary("=", ";", true);
|
||||
if (vs.TryGetValue("MapTo", out var map) && !map.IsNullOrEmpty() && !map.EqualIgnoreCase(connName))
|
||||
{
|
||||
// 如果连接字符串除了MapTo还有其它设置,则需要复制后覆盖
|
||||
if (vs.Count <= 1)
|
||||
_mapTo = map;
|
||||
else
|
||||
{
|
||||
var dal = Create(map);
|
||||
var builder = new ConnectionStringBuilder(dal.ConnStr);
|
||||
foreach (var item in vs)
|
||||
{
|
||||
if (!item.Key.EqualIgnoreCase("MapTo"))
|
||||
builder[item.Key] = item.Value;
|
||||
}
|
||||
|
||||
_mapConnStr = builder.ToString();
|
||||
ProviderType = dal.ProviderType;
|
||||
DbType = dal.DbType;
|
||||
}
|
||||
}
|
||||
|
||||
if (_infos!.TryGetValue(connName, out var t) && t.Type != null)
|
||||
{
|
||||
|
|
|
@ -95,6 +95,27 @@ public class SQLiteTests
|
|||
Assert.Equal(dal1, dal2);
|
||||
Assert.Equal("sysSQLite", dal2.ConnName);
|
||||
Assert.Equal(DatabaseType.SQLite, dal2.DbType);
|
||||
Assert.Equal($"Data Source={db}", dal2.ConnStr);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MapToTest2()
|
||||
{
|
||||
var db = "Data\\Membership.db";
|
||||
var dbf = db.GetFullPath();
|
||||
|
||||
DAL.AddConnStr("sysSQLite", $"Data Source={db}", null, "SQLite");
|
||||
DAL.AddConnStr("mapTest", "MapTo=sysSQLite;TablePrefix=xcwl_", null, null);
|
||||
|
||||
var dal1 = DAL.Create("sysSQLite");
|
||||
var dal2 = DAL.Create("mapTest");
|
||||
Assert.NotNull(dal2);
|
||||
Assert.NotEqual(dal1, dal2);
|
||||
Assert.Equal("mapTest", dal2.ConnName);
|
||||
Assert.Equal(DatabaseType.SQLite, dal2.DbType);
|
||||
Assert.NotEqual($"Data Source={db};TablePrefix=xcwl_", dal2.ConnStr);
|
||||
//Assert.EndsWith(";TablePrefix=xcwl_", dal2.Db.ConnectionString);
|
||||
Assert.Equal(dbf, (dal2.Db as FileDbBase).DatabaseName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
Loading…
Reference in New Issue