Compare commits

...

3 Commits

Author SHA1 Message Date
猿人易 5ff79628e6 Merge branch 'master' of https://github.com/NewLifeX/NewLife.XCode
* 'master' of https://github.com/NewLifeX/NewLife.XCode:
  修复sqlite在更新已有数据表时,会运未修改列的修改sql语句的问题
2025-07-25 13:23:53 +08:00
大石头 67734969d6 升级Binary,支持从压缩流中读取实体对象,自动探测数据流已到末尾 2025-07-24 23:45:35 +08:00
xueshaoyu fda0a8c3be
修复sqlite在更新已有数据表时,会运未修改列的修改sql语句的问题 2025-07-24 13:39:30 +08:00
6 changed files with 36 additions and 28 deletions

View File

@ -549,12 +549,8 @@ internal class SQLiteMetaData : FileDbMetaData
return list;
}
static readonly Regex _reg = new("""
(?:^|,)\s*(\[\w+\]|\w+)
\s*(\w+(?:\(\d+(?:,\s*\d+)?\))?)
\s*([^,]*)?
""",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase);
static readonly Regex _reg = new("""(?:^|,)\s*("[^"]+"|\[\w+\]|\w+)\s*(\w+(?:\(\d+(?:,\s*\d+)?\))?)\s*([^,]*)?""",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase);
public void ParseColumns(IDataTable table, String sqlCreateTable)
{
if (sqlCreateTable.StartsWithIgnoreCase("create table"))
@ -579,10 +575,17 @@ internal class SQLiteMetaData : FileDbMetaData
var str = m.Groups[3].Value;
if (str.Contains("AUTOINCREMENT")) field.Identity = true;
if (str.Contains("Primary Key")) field.PrimaryKey = true;
//增加PRIMARY KEY大写判断
if (str.Contains("Primary Key") || str.Contains("PRIMARY KEY")) field.PrimaryKey = true;
if (str.Contains("NOT NULL"))
{
field.Nullable = false;
if (str.Contains("DEFAULT"))
{//增加默认值读取
field.DefaultValue = str.Split("DEFAULT")[1].Split(" ")[1].Trim('\'');
}
}
else if (str.Contains("NULL"))
field.Nullable = true;

View File

@ -361,7 +361,7 @@ public class DbPackage
try
{
// 二进制读写器
var bn = new Binary
var binary = new Binary
{
FullTime = true,
EncodeInt = true,
@ -369,7 +369,7 @@ public class DbPackage
};
var dt = new DbTable();
dt.ReadHeader(bn);
dt.ReadHeader(binary);
WriteLog("恢复[{0}/{1}]开始,共[{2:n0}]行", table.Name, connName, dt.Total);
// 输出日志
@ -394,7 +394,7 @@ public class DbPackage
//修复总行数是pageSize的倍数无法退出循环的情况
if (dt.Total == row) break;
// 读取数据
dt.ReadData(bn, Math.Min(dt.Total - row, pageSize));
dt.ReadData(binary, Math.Min(dt.Total - row, pageSize));
var rs = dt.Rows;
if (rs == null || rs.Count == 0) break;

View File

@ -1942,13 +1942,16 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
/// <param name="extend">是否序列化扩展属性</param>
protected virtual Boolean OnRead(Stream stream, Object? context, Boolean extend)
{
if (context is not Binary bn) bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
if (context is not Binary binary) binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
var fs = extend ? Meta.AllFields : Meta.Fields;
foreach (var fi in fs)
{
Object? value = null;
if (!binary.TryRead(fi.Type, ref value)) return false;
// 顺序要求很高
SetItem(fi.Name, bn.Read(fi.Type));
SetItem(fi.Name, value);
}
return true;
@ -1960,12 +1963,12 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
/// <param name="extend">是否序列化扩展属性</param>
protected virtual Boolean OnWrite(Stream stream, Object? context, Boolean extend)
{
if (context is not Binary bn) bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
if (context is not Binary binary) binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
var fs = extend ? Meta.AllFields : Meta.Fields;
foreach (var fi in fs)
{
bn.Write(this[fi.Name], fi.Type);
binary.Write(this[fi.Name], fi.Type);
}
return true;

View File

@ -1228,15 +1228,14 @@ public static class EntityExtension
{
if (list == null) return 0;
//todo Binary需要字段记录已经写入多少字节部分数据流不支持Position
var bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
var p = stream.Position;
// Binary需要字段记录已经写入多少字节部分数据流不支持Position
var binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
foreach (var entity in list)
{
if (entity is IAccessor acc) acc.Write(stream, bn);
if (entity is IAccessor acc) acc.Write(stream, binary);
}
return stream.Position - p;
return binary.Total;
}
/// <summary>写入文件,二进制格式</summary>
@ -1250,10 +1249,10 @@ public static class EntityExtension
var compressed = file.EndsWithIgnoreCase(".gz");
return file.AsFile().OpenWrite(compressed, fs =>
{
var bn = new Binary { Stream = fs, EncodeInt = true, FullTime = true };
var binary = new Binary { Stream = fs, EncodeInt = true, FullTime = true };
foreach (var entity in list)
{
if (entity is IAccessor acc) acc.Write(fs, bn);
if (entity is IAccessor acc) acc.Write(fs, binary);
}
});
}
@ -1331,11 +1330,14 @@ public static class EntityExtension
{
if (factory == null || stream == null) yield break;
var bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
while (stream.Position < stream.Length)
var binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
while (!binary.EndOfStream)
{
var entity = factory.Create();
if (entity is IAccessor acc) acc.Read(stream, bn);
if (entity is IAccessor acc)
{
if (!acc.Read(stream, binary)) yield break;
}
yield return entity;
}

View File

@ -46,7 +46,7 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.5.2025.701" />
<PackageReference Include="NewLife.Core" Version="11.5.2025.724-beta1230" />
</ItemGroup>
<ItemGroup>
<Using Include="NewLife" />

View File

@ -85,12 +85,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NewLife.Core" Version="11.5.2025.701" />
<PackageReference Include="NewLife.Core" Version="11.5.2025.724-beta1230" />
<PackageReference Include="NewLife.IP" Version="2.3.2025.601" />
<PackageReference Include="NewLife.UnitTest" Version="1.0.2025.101" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.6" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.7" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>