Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 'master' of http://git.newlifex.com/NewLife/NewLife.XCode:
  1、修复时间fech数据错误
  • Loading branch information
peksmart committed Feb 22, 2024
2 parents 6ad2fa9 + 5fd0643 commit c571c6b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
7 changes: 6 additions & 1 deletion XCode/DataAccessLayer/Database/SqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,12 @@ private List<IDataParameter[]> GetParametersList(IDataColumn[] columns, ICollect
var dt = val.ToDateTime();
if (dt.Year < 1970) val = new DateTime(1970, 1, 1);
}

//byte[]类型查询时候参数化异常
else if (dc.DataType == typeof(Byte[]))
{
if (val == null)
val = new Byte[0];
}
// 逐列创建参数对象
dps.Add(db.CreateParameter(dc.Name, val, dc));
}
Expand Down
19 changes: 15 additions & 4 deletions XCode/DataAccessLayer/MetaData/DbMetaData_Positive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Data.Common;
using System.Linq;
using NewLife;
using NewLife.Log;
using NewLife.Serialization;
using XCode.Common;
using XCode.Exceptions;

Expand Down Expand Up @@ -210,9 +212,17 @@ protected virtual List<IDataColumn> GetFields(IDataTable table, DataRow[] rows)

// 原始数据类型
field.RawType = GetDataRowValue<String>(dr, "DATA_TYPE", "DATATYPE", "COLUMN_DATA_TYPE");

// 长度
field.Length = GetDataRowValue<Int32>(dr, "CHARACTER_MAXIMUM_LENGTH", "LENGTH", "COLUMN_SIZE");

//修复sqlserver同步到sqlserver建表长度为1的bug
var rawType = field.RawType.ToLower();
if (rawType == "nvarchar" || rawType == "varchar" || rawType == "char" || rawType == "binary")
field.RawType += "(" + field.Length + ")";
if (rawType == "varbinary" && field.Length == -1)
field.RawType = "varbinary(max)";

if (field is XField fi)
{
// 精度 与 位数
Expand All @@ -221,6 +231,7 @@ protected virtual List<IDataColumn> GetFields(IDataTable table, DataRow[] rows)
if (field.Length == 0) field.Length = fi.Precision;
}


// 允许空
if (TryGetDataRowValue(dr, "IS_NULLABLE", out b))
field.Nullable = b;
Expand Down Expand Up @@ -333,7 +344,7 @@ protected virtual List<IDataIndex> GetIndexes(IDataTable table, DataTable indexe
protected IDictionary<Type, String[]> Types { get; set; } = null!;

protected List<KeyValuePair<Type, Type>>? _FieldTypeMaps;
/// <summary>字段类型映射(数据库-实体类)</summary>
/// <summary>字段类型映射</summary>
protected virtual List<KeyValuePair<Type, Type>> FieldTypeMaps
{
get
Expand All @@ -348,8 +359,6 @@ protected virtual List<IDataIndex> GetIndexes(IDataTable table, DataTable indexe
// 因为等价,字节需要能够互相映射
new(typeof(Byte), typeof(SByte)),
new(typeof(Byte), typeof(Boolean)),
new(typeof(Boolean), typeof(Byte)),
new(typeof(Byte), typeof(Int32)),

new(typeof(UInt16), typeof(Int16)),
new(typeof(Int16), typeof(UInt16)),
Expand All @@ -368,7 +377,6 @@ protected virtual List<IDataIndex> GetIndexes(IDataTable table, DataTable indexe
new(typeof(Int64), typeof(UInt64)),
//list.Add(new KeyValuePair<Type, Type>(typeof(UInt64), typeof(Int32)));
//list.Add(new KeyValuePair<Type, Type>(typeof(Int64), typeof(Int32)));
new(typeof(Int64), typeof(Int32)),

// 数据库使用Double,实体类使用Decimal
new(typeof(Double), typeof(Decimal)),
Expand Down Expand Up @@ -411,6 +419,9 @@ protected virtual List<IDataIndex> GetIndexes(IDataTable table, DataTable indexe
var typeName = ns.FirstOrDefault();
// 大文本选第二个类型
if (ns.Length > 1 && type == typeof(String) && (field.Length <= 0 || field.Length >= Database.LongTextLength)) typeName = ns[1];
//mysql 转sqlserver
if (field.RawType.ToLower() == "longblob")
typeName = "varbinary(max)";
if (typeName.Contains("{0}"))
{
if (typeName.Contains("{1}"))
Expand Down
7 changes: 5 additions & 2 deletions XCode/Transform/TimeExtracter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public virtual IEnumerable<DbTable> Fetch()
// 分割数据页
var sb = Builder.Clone();
if (!sb.Where.IsNullOrEmpty()) sb.Where += " And ";
sb.Where += $"{name}.{db.FormatValue(field, StartTime)}";
//修复拼接sql错误
if (StartTime != null && StartTime > DateTime.MinValue)
sb.Where += $"{name}>{db.FormatValue(field, StartTime)}";

// 查询数据
var dt = Dal.Query(sb, 0, BatchSize);
Expand All @@ -86,7 +88,8 @@ public virtual IEnumerable<DbTable> Fetch()
{
sb = Builder.Clone();
if (!sb.Where.IsNullOrEmpty()) sb.Where += " And ";
sb.Where += $"{name}>={db.FormatValue(field, StartTime)} And {name}<{db.FormatValue(field, StartTime.AddSeconds(1))}";
//避免分页插入重复的一条数据
sb.Where += $"{name}>{db.FormatValue(field, StartTime)} And {name}<{db.FormatValue(field, StartTime.AddSeconds(1))}";

// 查询数据,该时间点数据也可能有多页
var startRow = 0;
Expand Down

0 comments on commit c571c6b

Please sign in to comment.