Compare commits

...

5 Commits

Author SHA1 Message Date
sunkaixuan
4603be72fd Update nav query by array 2025-10-18 13:25:48 +08:00
sunkaixuan
022e1d60cf Update db.Reportable 2025-10-16 17:14:19 +08:00
sunkaixuan
3db6aed1dc Update Update.InnerJoin(queryable,exp) 2025-10-16 15:50:02 +08:00
sunkaixuan
8784e62499 Add demo 2025-10-15 16:55:53 +08:00
sunkaixuan
262fe19f76 Update exp to sql 2025-10-15 16:52:27 +08:00
8 changed files with 204 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ namespace OrmTest
}
public static void Init()
{
Unitsadsfasdfys.Init();
Unitsdfyasfs3lsss.Init();
Unitadsfasyss.Init();
Unitsadfasysss.Init();

View File

@@ -0,0 +1,104 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace OrmTest
{
internal class Unitsadsfasdfys
{
public static void Init()
{
var _db = DbHelper.GetNewDb();
// 执行你的查询(已修复 WithNoLock
var brandIds = new List<int> { 1 }; // 可变条件
_db.CodeFirst.InitTables<Category, SKU, Product>();
var result = _db.Queryable<Category>()
.Where((c ) =>
SqlFunc.Subqueryable<SKU>()
.WithNoLock()
.InnerJoin<Product>(
(s, p) => s.ProductId == p.ProductId
)
.Where((s, p) => s.IsCanPurchase == true && s.IsSalesmanBuy == false && s.Valid == false)
.Any()
)
.With(SqlWith.NoLock) // Category 表加 NOLOCK
.ToList();
var sqlObj = _db.Queryable<Category>()
.Where((c) =>
SqlFunc.Subqueryable<SKU>()
.WithNoLock()
.InnerJoin<Product>(
(s, p) => s.ProductId == p.ProductId
)
.Where((s, p) => s.IsCanPurchase == true && s.IsSalesmanBuy == false && s.Valid == false)
.Any()
)
.With(SqlWith.NoLock) // Category 表加 NOLOCK
.ToSqlString();
if (!sqlObj.Contains(" [UnitsdsdfaProduct] [p] WITH(NOLOCK) ")) { throw new Exception("unit error"); }
}
// ======================
// 实体类定义
// ======================
[SugarTable("UnitdafaCategory")]
public class Category
{
[SugarColumn(IsPrimaryKey = true)]
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public int BrandId { get; set; }
public bool IsEnable { get; set; }
public bool Valid { get; set; }
}
[SugarTable("UnitsdgssBrand")]
public class Brand
{
[SugarColumn(IsPrimaryKey = true)]
public int BrandId { get; set; }
public string BrandName { get; set; }
public bool IsEnable { get; set; }
public bool Valid { get; set; }
}
[SugarTable("UnitsdsdfaProduct")]
public class Product
{
[SugarColumn(IsPrimaryKey = true)]
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int BrandId { get; set; }
public string ProductName { get; set; }
public bool Valid { get; set; }
}
[SugarTable("UnitaadfafaSKU")]
public class SKU
{
[SugarColumn(IsPrimaryKey = true)]
public int SkuId { get; set; }
public int ProductId { get; set; }
public bool IsCanPurchase { get; set; }
public bool IsSalesmanBuy { get; set; }
public bool Valid { get; set; }
}
// ======================
// 输出 DTO
// ======================
public class SaleCategoryOutputDto
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public int BrandId { get; set; }
}
}
}

View File

@@ -13,7 +13,7 @@ using System.Collections.ObjectModel;
using NetTaste;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
namespace SqlSugar
@@ -866,6 +866,15 @@ namespace SqlSugar
{
propertyInfo.SetValue(addItem,UtilMethods.To( kv.Value, propertyInfo.PropertyType));
}
else if (kv.Value is string s&&
s!=null&&
s?.StartsWith("[")==true&&
s?.EndsWith("]") == true&&
UtilMethods.IsArrayOrList(propertyInfo.PropertyType)
)
{
propertyInfo.SetValue(addItem, UtilMethods.ConvertToArray(kv.Value?.ToString(), propertyInfo.PropertyType));
}
else
{
propertyInfo.SetValue(addItem, kv.Value);

View File

@@ -215,6 +215,10 @@ namespace SqlSugar
{
return $" CAST( NULL AS timestamp) ";
}
else if (entityColumnInfo != null && entityColumnInfo.IsJson && value!=null)
{
return $"'{this.Context.Utilities.SerializeObject(value).ToSqlFilter()}'";
}
if (value == null)
return "null";
var type =UtilMethods.GetUnderType(value.GetType());

View File

@@ -275,7 +275,18 @@ namespace SqlSugar
public IUpdateable<T, T2> InnerJoin<T2>(ISugarQueryable<T2> queryable, Expression<Func<T, T2, bool>> joinExpress)
{
var tableName = $" ({queryable.Clone().ToSqlString()}) "; ;
return this.InnerJoin(joinExpress, tableName);
return this.InnerJoin<T2>(tableName, joinExpress,this.UpdateBuilder.TableName);
}
public IUpdateable<T, T2> InnerJoin<T2>(string joinTable, Expression<Func<T, T2, bool>> joinExpress, string tableName)
{
UpdateableProvider<T, T2> result = new UpdateableProvider<T, T2>();
result.updateableObj = this;
var querybale = this.Context.Queryable<T>().LeftJoin<T2>(joinExpress);
result.updateableObj.UpdateBuilder.JoinInfos = querybale.QueryBuilder.JoinQueryInfos;
result.updateableObj.UpdateBuilder.JoinInfos.Last().TableName = joinTable;
result.updateableObj.UpdateBuilder.ShortName = joinExpress.Parameters.FirstOrDefault()?.Name;
result.updateableObj.UpdateBuilder.TableName = tableName;
return result;
}
public IUpdateable<T, T2> InnerJoin<T2>(Expression<Func<T, T2, bool>> joinExpress,string TableName)
{

View File

@@ -304,10 +304,22 @@ namespace SqlSugar
{
this.context.CurrentShortName=ExpressionTool.GetParameters(allMethods.FirstOrDefault()).FirstOrDefault().Name;
}
var hasNolock = false;
List<string> result = isubList.Select(it =>
{
it.HasWhere = isHasWhere;
return it.GetValue(it.Expression);
if (it is SubWithNolock)
{
hasNolock = true;
}
var result = it.GetValue(it.Expression);
var isJoin = (it is SubLeftJoin || it is SubInnerJoin);
var isSqlServer =UtilMethods.GetDatabaseType(this.context) == DbType.SqlServer;
if (hasNolock && isJoin&& isSqlServer)
{
result = result.Replace("] ON (", "] " + SqlWith.NoLock + " ON (");
}
return result;
}).ToList();
if (this.context?.SugarContext?.Context?.CurrentConnectionConfig?.DbType == DbType.Oracle && isubList.Any(s => s is SubSelect) && isubList.Any(s => s is SubOrderBy || s is SubOrderByDesc))
{
@@ -319,6 +331,7 @@ namespace SqlSugar
return result;
}
private static void SetOrderByIndex(List<ISubOperation> isubList)
{
var orderByIndex = 0;

View File

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>SqlSugarCore</id>
<version>5.1.4.206-preview06</version>
<version>5.1.4.206-preview19</version>
<authors>sunkaixuan</authors>
<owners>果糖大数据科技</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>

View File

@@ -18,7 +18,65 @@ namespace SqlSugar
{
public class UtilMethods
{
public static bool IsArrayOrList(Type propertyType)
{
if (propertyType==null||propertyType == typeof(string))
return false;
var isList = propertyType.FullName.IsCollectionsList();
var isArray = propertyType.IsArray;
return isList || isArray;
}
public static object ConvertToArray(string input, Type targetType)
{
// 获取元素类型和集合类型
Type elementType;
bool isArray = targetType.IsArray;
bool isList = targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List<>);
if (!isArray && !isList)
throw new ArgumentException("目标类型必须是数组或List类型");
elementType = isArray ? targetType.GetElementType() : targetType.GetGenericArguments()[0];
// 处理空输入
if (string.IsNullOrEmpty(input))
{
if (isArray)
return Array.CreateInstance(elementType, 0);
else
return Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
}
// 解析输入字符串
var elements = input.Trim('[', ']').Split(',');
if (isArray)
{
// 处理数组
Array array = Array.CreateInstance(elementType, elements.Length);
for (int i = 0; i < elements.Length; i++)
{
string element = elements[i]?.Trim()?.TrimStart('"')?.TrimEnd('"');
array.SetValue(UtilMethods.ChangeType2(element, elementType), i);
}
return array;
}
else
{
// 处理List
var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
for (int i = 0; i < elements.Length; i++)
{
string element = elements[i]?.Trim()?.TrimStart('"')?.TrimEnd('"');
list.Add(UtilMethods.ChangeType2(element, elementType));
}
return list;
}
}
internal static DbType? GetDatabaseType(ExpressionContext context)
{
return context?.SugarContext?.Context?.CurrentConnectionConfig?.DbType;
}
internal static void SetDefaultValueForBoolean(EntityColumnInfo item, Type propertyType)
{
if (propertyType == UtilConstants.BoolType && item.DefaultValue != null && item.DefaultValue.EqualCase("true"))