mirror of
https://github.com/dotnetcore/Util.git
synced 2026-10-26 01:32:30 +08:00
添加Sql查询条件
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化And连接条件
|
||||
/// 初始化Sql相等查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
/// <summary>
|
||||
/// Sql大于查询条件
|
||||
/// </summary>
|
||||
public class GreaterCondition : ICondition {
|
||||
/// <summary>
|
||||
/// 列名
|
||||
/// </summary>
|
||||
private readonly string _column;
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Sql大于查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
public GreaterCondition( string column, string parameter ) {
|
||||
_column = column;
|
||||
_parameter = parameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询条件
|
||||
/// </summary>
|
||||
public string GetCondition() {
|
||||
return $"{_column}>{_parameter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
/// <summary>
|
||||
/// Sql大于等于查询条件
|
||||
/// </summary>
|
||||
public class GreaterEqualCondition : ICondition {
|
||||
/// <summary>
|
||||
/// 列名
|
||||
/// </summary>
|
||||
private readonly string _column;
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Sql大于等于查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
public GreaterEqualCondition( string column, string parameter ) {
|
||||
_column = column;
|
||||
_parameter = parameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询条件
|
||||
/// </summary>
|
||||
public string GetCondition() {
|
||||
return $"{_column}>={_parameter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
/// <summary>
|
||||
/// Sql小于查询条件
|
||||
/// </summary>
|
||||
public class LessCondition : ICondition {
|
||||
/// <summary>
|
||||
/// 列名
|
||||
/// </summary>
|
||||
private readonly string _column;
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Sql小于查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
public LessCondition( string column, string parameter ) {
|
||||
_column = column;
|
||||
_parameter = parameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询条件
|
||||
/// </summary>
|
||||
public string GetCondition() {
|
||||
return $"{_column}<{_parameter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
/// <summary>
|
||||
/// Sql小于等于查询条件
|
||||
/// </summary>
|
||||
public class LessEqualCondition : ICondition {
|
||||
/// <summary>
|
||||
/// 列名
|
||||
/// </summary>
|
||||
private readonly string _column;
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Sql小于等于查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
public LessEqualCondition( string column, string parameter ) {
|
||||
_column = column;
|
||||
_parameter = parameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询条件
|
||||
/// </summary>
|
||||
public string GetCondition() {
|
||||
return $"{_column}<={_parameter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
/// <summary>
|
||||
/// Sql模糊查询条件
|
||||
/// </summary>
|
||||
public class LikeCondition : ICondition {
|
||||
/// <summary>
|
||||
/// 列名
|
||||
/// </summary>
|
||||
private readonly string _column;
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Sql模糊查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
public LikeCondition( string column, string parameter ) {
|
||||
_column = column;
|
||||
_parameter = parameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询条件
|
||||
/// </summary>
|
||||
public string GetCondition() {
|
||||
return $"{_column} Like {_parameter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
/// <summary>
|
||||
/// Sql不相等查询条件
|
||||
/// </summary>
|
||||
public class NotEqualCondition : ICondition {
|
||||
/// <summary>
|
||||
/// 列名
|
||||
/// </summary>
|
||||
private readonly string _column;
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
private readonly string _parameter;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Sql不相等查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="parameter">参数</param>
|
||||
public NotEqualCondition( string column, string parameter ) {
|
||||
_column = column;
|
||||
_parameter = parameter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询条件
|
||||
/// </summary>
|
||||
public string GetCondition() {
|
||||
return $"{_column}!={_parameter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,22 +16,22 @@ namespace Util.Datas.Sql.Queries.Builders.Conditions {
|
||||
switch( @operator ) {
|
||||
case Operator.Equal:
|
||||
return new EqualCondition( name, parameter );
|
||||
//case Operator.NotEqual:
|
||||
// return new NotEqualCondition( name, prefix );
|
||||
//case Operator.Greater:
|
||||
// return new GreaterCondition( name, prefix );
|
||||
//case Operator.Less:
|
||||
// return new LessCondition( name, prefix );
|
||||
//case Operator.GreaterEqual:
|
||||
// return new GreaterEqualCondition( name, prefix );
|
||||
//case Operator.LessEqual:
|
||||
// return new LessEqualCondition( name, prefix );
|
||||
//case Operator.Contains:
|
||||
// return new LikeCondition( name, prefix );
|
||||
//case Operator.Starts:
|
||||
// return new LikeCondition( name, prefix );
|
||||
//case Operator.Ends:
|
||||
// return new LikeCondition( name, prefix );
|
||||
case Operator.NotEqual:
|
||||
return new NotEqualCondition( name, parameter );
|
||||
case Operator.Greater:
|
||||
return new GreaterCondition( name, parameter );
|
||||
case Operator.Less:
|
||||
return new LessCondition( name, parameter );
|
||||
case Operator.GreaterEqual:
|
||||
return new GreaterEqualCondition( name, parameter );
|
||||
case Operator.LessEqual:
|
||||
return new LessEqualCondition( name, parameter );
|
||||
case Operator.Contains:
|
||||
return new LikeCondition( name, parameter );
|
||||
case Operator.Starts:
|
||||
return new LikeCondition( name, parameter );
|
||||
case Operator.Ends:
|
||||
return new LikeCondition( name, parameter );
|
||||
}
|
||||
throw new NotImplementedException( $"运算符 {@operator.Description()} 未实现" );
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
/// </summary>
|
||||
/// <param name="columns">列名</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder Select<TEntity>( Expression<Func<TEntity, object[]>> columns,string tableAlias = null );
|
||||
ISqlBuilder Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置表名
|
||||
/// </summary>
|
||||
@@ -41,17 +41,62 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
/// <param name="schema">架构名</param>
|
||||
ISqlBuilder From( string table, string alias = null, string schema = null );
|
||||
/// <summary>
|
||||
/// 设置表名
|
||||
/// </summary>
|
||||
/// <param name="alias">别名</param>
|
||||
/// <param name="schema">架构名</param>
|
||||
ISqlBuilder From<TEntity>( string alias = null, string schema = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// And连接条件
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
ISqlBuilder And( ICondition condition );
|
||||
/// <summary>
|
||||
/// 设置条件
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder Where<TEntity>( Expression<Func<TEntity, bool>> expression, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder Where<TEntity>( Expression<Func<TEntity, object>> expression, object value, Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder Where( string column, object value, Operator @operator = Operator.Equal, string tableAlias = null );
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder WhereIf<TEntity>( Expression<Func<TEntity, bool>> expression, bool condition, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder WhereIf<TEntity>( Expression<Func<TEntity, object>> expression, object value, bool condition, Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlBuilder WhereIf( string column, object value, bool condition, Operator @operator = Operator.Equal, string tableAlias = null );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
|
||||
#region 字段
|
||||
|
||||
/// <summary>
|
||||
/// 表别名集合
|
||||
/// </summary>
|
||||
private readonly IDictionary<Type, string> _tableAlias;
|
||||
/// <summary>
|
||||
/// 参数集合
|
||||
/// </summary>
|
||||
@@ -33,6 +37,7 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
/// 初始化Sql生成器
|
||||
/// </summary>
|
||||
protected SqlBuilderBase() {
|
||||
_tableAlias = new Dictionary<Type, string>();
|
||||
_params = new Dictionary<string, object>();
|
||||
Columns = new List<SqlItem>();
|
||||
}
|
||||
@@ -147,7 +152,7 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
/// </summary>
|
||||
/// <param name="columns">列名</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public virtual ISqlBuilder Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string tableAlias = null ) {
|
||||
public virtual ISqlBuilder Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string tableAlias = null ) where TEntity : class {
|
||||
if( columns == null )
|
||||
return this;
|
||||
return Select( Lambda.GetNames( columns ).Join(), tableAlias );
|
||||
@@ -170,6 +175,17 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名
|
||||
/// </summary>
|
||||
/// <param name="alias">别名</param>
|
||||
/// <param name="schema">架构名</param>
|
||||
public ISqlBuilder From<TEntity>( string alias = null, string schema = null ) where TEntity : class {
|
||||
if( string.IsNullOrWhiteSpace( alias ) == false )
|
||||
_tableAlias.Add( typeof( TEntity ), alias );
|
||||
return From( typeof( TEntity ).Name, alias, schema );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region And(And连接条件)
|
||||
@@ -185,10 +201,43 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
|
||||
#endregion
|
||||
|
||||
#region Where(设置条件)
|
||||
#region Where(设置查询条件)
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlBuilder Where<TEntity>( Expression<Func<TEntity, bool>> expression, string tableAlias = null ) where TEntity : class {
|
||||
return Where( Lambda.GetLastName( expression ), Lambda.GetValue( expression ), Lambda.GetOperator( expression ).SafeValue(), GetTableAlias<TEntity>( tableAlias ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlBuilder Where<TEntity>( Expression<Func<TEntity, object>> expression, object value,
|
||||
Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class {
|
||||
return Where( Lambda.GetLastName( expression ), value, @operator, GetTableAlias<TEntity>( tableAlias ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取表别名
|
||||
/// </summary>
|
||||
protected string GetTableAlias<TEntity>( string tableAlias ) {
|
||||
if( string.IsNullOrWhiteSpace( tableAlias ) == false )
|
||||
return tableAlias;
|
||||
var type = typeof( TEntity );
|
||||
if( _tableAlias.ContainsKey( type ) )
|
||||
return _tableAlias[type];
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
@@ -241,6 +290,47 @@ namespace Util.Datas.Sql.Queries.Builders {
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlBuilder WhereIf<TEntity>( Expression<Func<TEntity, bool>> expression, bool condition, string tableAlias = null ) where TEntity : class {
|
||||
if( condition )
|
||||
return Where( expression, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlBuilder WhereIf<TEntity>( Expression<Func<TEntity, object>> expression, object value, bool condition,
|
||||
Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class {
|
||||
if( condition )
|
||||
return Where( expression, value, @operator, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlBuilder WhereIf( string column, object value, bool condition, Operator @operator = Operator.Equal, string tableAlias = null ) {
|
||||
if( condition )
|
||||
return Where( column, value, @operator, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
#region 获取子句
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace Util.Datas.Sql.Queries {
|
||||
/// </summary>
|
||||
/// <param name="columns">列名,范例:t => new object[] { t.A, t.B }</param>
|
||||
/// <param name="alias">别名</param>
|
||||
ISqlQuery Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string alias = null );
|
||||
ISqlQuery Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string alias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置表名
|
||||
/// </summary>
|
||||
@@ -47,17 +47,62 @@ namespace Util.Datas.Sql.Queries {
|
||||
/// <param name="schema">架构名</param>
|
||||
ISqlQuery From( string table, string alias = null, string schema = null );
|
||||
/// <summary>
|
||||
/// 设置表名
|
||||
/// </summary>
|
||||
/// <param name="alias">别名</param>
|
||||
/// <param name="schema">架构名</param>
|
||||
ISqlQuery From<TEntity>( string alias = null, string schema = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// And连接条件
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
ISqlQuery And( ICondition condition );
|
||||
/// <summary>
|
||||
/// 设置条件
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlQuery Where<TEntity>( Expression<Func<TEntity, bool>> expression, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlQuery Where<TEntity>( Expression<Func<TEntity, object>> expression, object value, Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlQuery Where( string column, object value, Operator @operator = Operator.Equal, string tableAlias = null );
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlQuery WhereIf<TEntity>( Expression<Func<TEntity, bool>> expression, bool condition, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlQuery WhereIf<TEntity>( Expression<Func<TEntity, object>> expression, object value, bool condition, Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class;
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
ISqlQuery WhereIf( string column, object value, bool condition, Operator @operator = Operator.Equal, string tableAlias = null );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace Util.Datas.Sql.Queries {
|
||||
/// </summary>
|
||||
/// <param name="columns">列名</param>
|
||||
/// <param name="alias">别名</param>
|
||||
public ISqlQuery Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string alias = null ) {
|
||||
public ISqlQuery Select<TEntity>( Expression<Func<TEntity, object[]>> columns, string alias = null ) where TEntity : class {
|
||||
GetBuilder().Select( columns, alias );
|
||||
return this;
|
||||
}
|
||||
@@ -105,6 +105,16 @@ namespace Util.Datas.Sql.Queries {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名
|
||||
/// </summary>
|
||||
/// <param name="alias">别名</param>
|
||||
/// <param name="schema">架构名</param>
|
||||
public ISqlQuery From<TEntity>( string alias = null, string schema = null ) where TEntity : class {
|
||||
GetBuilder().From<TEntity>( alias, schema );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// And连接条件
|
||||
/// </summary>
|
||||
@@ -115,15 +125,76 @@ namespace Util.Datas.Sql.Queries {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlQuery Where<TEntity>( Expression<Func<TEntity, bool>> expression, string tableAlias = null ) where TEntity : class {
|
||||
GetBuilder().Where( expression, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlQuery Where<TEntity>( Expression<Func<TEntity, object>> expression, object value,
|
||||
Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class {
|
||||
GetBuilder().Where( expression, value, @operator, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlQuery Where( string column, object value, Operator @operator = Operator.Equal,string tableAlias = null ) {
|
||||
public ISqlQuery Where( string column, object value, Operator @operator = Operator.Equal, string tableAlias = null ) {
|
||||
GetBuilder().Where( column, value, @operator, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">查询条件表达式</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlQuery WhereIf<TEntity>( Expression<Func<TEntity, bool>> expression, bool condition, string tableAlias = null ) where TEntity : class {
|
||||
GetBuilder().WhereIf( expression, condition, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="expression">列名表达式</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlQuery WhereIf<TEntity>( Expression<Func<TEntity, object>> expression, object value, bool condition,
|
||||
Operator @operator = Operator.Equal, string tableAlias = null ) where TEntity : class {
|
||||
GetBuilder().WhereIf( expression, value, condition, @operator, tableAlias );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置查询条件
|
||||
/// </summary>
|
||||
/// <param name="column">列名</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
|
||||
/// <param name="operator">运算符</param>
|
||||
/// <param name="tableAlias">表别名</param>
|
||||
public ISqlQuery WhereIf( string column, object value, bool condition, Operator @operator = Operator.Equal, string tableAlias = null ) {
|
||||
GetBuilder().WhereIf( column, value, condition, @operator, tableAlias );
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,16 +36,36 @@ namespace Util.Helpers {
|
||||
return GetMemberExpression( ( (UnaryExpression)expression ).Operand );
|
||||
case ExpressionType.MemberAccess:
|
||||
return (MemberExpression)expression;
|
||||
case ExpressionType.Equal:
|
||||
case ExpressionType.NotEqual:
|
||||
case ExpressionType.GreaterThan:
|
||||
case ExpressionType.LessThan:
|
||||
case ExpressionType.GreaterThanOrEqual:
|
||||
case ExpressionType.LessThanOrEqual:
|
||||
return GetMemberExpression( ( (BinaryExpression)expression ).Left );
|
||||
case ExpressionType.Call:
|
||||
return GetMethodCallExpressionName( expression );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取方法调用表达式的成员名称
|
||||
/// </summary>
|
||||
private static MemberExpression GetMethodCallExpressionName( Expression expression ) {
|
||||
var methodCallExpression = (MethodCallExpression)expression;
|
||||
var argumentExpression = methodCallExpression.Arguments.FirstOrDefault();
|
||||
if ( argumentExpression != null && argumentExpression.NodeType == ExpressionType.MemberAccess )
|
||||
return (MemberExpression)argumentExpression;
|
||||
return (MemberExpression)methodCallExpression.Object;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetName(获取成员名称)
|
||||
|
||||
/// <summary>
|
||||
/// 获取成员名称,范例:t => t.Name,返回 Name
|
||||
/// 获取成员名称,范例:t => t.A.Name,返回 A.Name
|
||||
/// </summary>
|
||||
/// <param name="expression">表达式,范例:t => t.Name</param>
|
||||
public static string GetName( Expression expression ) {
|
||||
@@ -96,6 +116,22 @@ namespace Util.Helpers {
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetLastName(获取最后一级成员名称)
|
||||
|
||||
/// <summary>
|
||||
/// 获取最后一级成员名称,范例:t => t.A.Name,返回 Name
|
||||
/// </summary>
|
||||
/// <param name="expression">表达式,范例:t => t.Name</param>
|
||||
public static string GetLastName( Expression expression ) {
|
||||
var memberExpression = GetMemberExpression( expression );
|
||||
if( memberExpression == null )
|
||||
return string.Empty;
|
||||
string result = memberExpression.ToString();
|
||||
return result.Substring( result.LastIndexOf( ".", StringComparison.Ordinal ) + 1 );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetValue(获取值)
|
||||
|
||||
/// <summary>
|
||||
@@ -170,6 +206,56 @@ namespace Util.Helpers {
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetOperator(获取查询操作符)
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符,范例:t => t.Name == "A",返回 Operator.Equal
|
||||
/// </summary>
|
||||
/// <param name="expression">表达式,范例:t => t.Name == "A"</param>
|
||||
public static Operator? GetOperator( Expression expression ) {
|
||||
if( expression == null )
|
||||
return null;
|
||||
switch( expression.NodeType ) {
|
||||
case ExpressionType.Lambda:
|
||||
return GetOperator( ( (LambdaExpression)expression ).Body );
|
||||
case ExpressionType.Convert:
|
||||
return GetOperator( ( (UnaryExpression)expression ).Operand );
|
||||
case ExpressionType.Equal:
|
||||
return Operator.Equal;
|
||||
case ExpressionType.NotEqual:
|
||||
return Operator.NotEqual;
|
||||
case ExpressionType.GreaterThan:
|
||||
return Operator.Greater;
|
||||
case ExpressionType.LessThan:
|
||||
return Operator.Less;
|
||||
case ExpressionType.GreaterThanOrEqual:
|
||||
return Operator.GreaterEqual;
|
||||
case ExpressionType.LessThanOrEqual:
|
||||
return Operator.LessEqual;
|
||||
case ExpressionType.Call:
|
||||
return GetMethodCallExpressionOperator( expression );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取方法调用表达式的值
|
||||
/// </summary>
|
||||
private static Operator? GetMethodCallExpressionOperator( Expression expression ) {
|
||||
var methodCallExpression = (MethodCallExpression)expression;
|
||||
switch ( methodCallExpression?.Method?.Name?.ToLower() ) {
|
||||
case "contains":
|
||||
return Operator.Contains;
|
||||
case "endswith":
|
||||
return Operator.Ends;
|
||||
case "startswith":
|
||||
return Operator.Starts;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetParameter(获取参数)
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -157,8 +157,8 @@ namespace Util.Datas.Tests.Dapper.SqlServer {
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestSelect_12() {
|
||||
_builder.Select( "a As e,[b]" ).From( "c", "d" );
|
||||
Assert.Equal( $"Select [d].[a] As [e],[d].[b] {Common.Line}From [c] As [d]", _builder.ToSql() );
|
||||
_builder.Select( "t.a As e,[b],f.g", "k" ).Select( "n" ).From( "c", "d" );
|
||||
Assert.Equal( $"Select [t].[a] As [e],[k].[b],[f].[g],[d].[n] {Common.Line}From [c] As [d]", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -233,10 +233,37 @@ namespace Util.Datas.Tests.Dapper.SqlServer {
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestFrom_7() {
|
||||
_builder.From( "a.b as t","e","f" );
|
||||
_builder.From( "a.b as t", "e", "f" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [a].[b] As [t]", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置表 - 泛型实体
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestFrom_8() {
|
||||
_builder.From<Sample>();
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t]", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置表 - 泛型实体 - 别名
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestFrom_9() {
|
||||
_builder.From<Sample>( "a" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [a]", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置表 - 泛型实体 - 别名 - 架构
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestFrom_10() {
|
||||
_builder.From<Sample>( "a", "b" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [b].[Sample] As [a]", _builder.ToSql() );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Where(设置条件)
|
||||
@@ -275,8 +302,209 @@ namespace Util.Datas.Tests.Dapper.SqlServer {
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_4() {
|
||||
_builder.From( "Test" ).Where( "f.Name", "a", tableAlias: "d" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Test] As [t] {Common.Line}Where [f].[Name]=@_p__0", _builder.ToSql() );
|
||||
_builder.From( "Test as test", "test1", "test2" ).Where( "f.Name", "a", tableAlias: "d" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [test2].[Test] As [test] {Common.Line}Where [f].[Name]=@_p__0", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_5() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.Email, "a" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[Email]=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "a", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名 - 设置表别名
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_6() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.Email, "a", tableAlias: "d" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [d].[Email]=@_p__0", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名 - 通过From设置表别名
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_7() {
|
||||
_builder.From<Sample>( "k" ).Where<Sample>( t => t.Email, "a" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k] {Common.Line}Where [k].[Email]=@_p__0", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - Where覆盖From设置的表别名
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_8() {
|
||||
_builder.From<Sample>( "k" ).Where<Sample>( t => t.Email, "a", tableAlias: "n" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k] {Common.Line}Where [n].[Email]=@_p__0", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - 相等
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_9() {
|
||||
_builder.From<Sample>( "k" ).Where<Sample>( t => t.Email == "a" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k] {Common.Line}Where [k].[Email]=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "a", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - 不相等
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_10() {
|
||||
_builder.From<Sample>( "k" ).Where<Sample>( t => t.Email != "a" );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k] {Common.Line}Where [k].[Email]!=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "a", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - 大于
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_11() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.IntValue > 1 );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[IntValue]>@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( 1, _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - 小于
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_12() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.IntValue < 1 );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[IntValue]<@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( 1, _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - 大于等于
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_13() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.IntValue >= 1 );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[IntValue]>=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( 1, _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - 小于等于
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_14() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.IntValue <= 1 );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[IntValue]<=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( 1, _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - Contains
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_15() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.Email.Contains( "a" ) );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[Email] Like @_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "%a%", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - StartsWith
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_16() {
|
||||
_builder.From<Sample>().Where<Sample>( t => t.Email.StartsWith( "a" ) );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[Email] Like @_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "a%", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置条件 - EndsWith
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhere_17() {
|
||||
_builder.From<Sample>( "k" ).Where<Sample>( t => t.Email.EndsWith( "a" ) );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k] {Common.Line}Where [k].[Email] Like @_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "%a", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WhereIf(设置条件)
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 添加条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhereIf_1() {
|
||||
_builder.From( "Test" ).WhereIf( "Name", "a", true );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Test] As [t] {Common.Line}Where [t].[Name]=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "a", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 忽略条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhereIf_2() {
|
||||
_builder.From( "Test" ).WhereIf( "Name", "a", false );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Test] As [t]", _builder.ToSql() );
|
||||
Assert.Empty( _builder.GetParams() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名 - 添加条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhereIf_3() {
|
||||
_builder.From<Sample>().WhereIf<Sample>( t => t.Email, "a", true );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t] {Common.Line}Where [t].[Email]=@_p__0", _builder.ToSql() );
|
||||
Assert.Single( _builder.GetParams() );
|
||||
Assert.Equal( "a", _builder.GetParams()["@_p__0"] );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名 - 忽略条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhereIf_4() {
|
||||
_builder.From<Sample>().WhereIf<Sample>( t => t.Email, "a", false );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [t]", _builder.ToSql() );
|
||||
Assert.Empty( _builder.GetParams() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名 - 添加条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhereIf_5() {
|
||||
_builder.From<Sample>( "k" ).WhereIf<Sample>( t => t.Email == "a", true );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k] {Common.Line}Where [k].[Email]=@_p__0", _builder.ToSql() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置条件 - 通过lambda设置列名 - 忽略条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestWhereIf_6() {
|
||||
_builder.From<Sample>( "k" ).WhereIf<Sample>( t => t.Email == "a", false );
|
||||
Assert.Equal( $"Select * {Common.Line}From [Sample] As [k]", _builder.ToSql() );
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -343,4 +571,4 @@ namespace Util.Datas.Tests.Dapper.SqlServer {
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Datas.Sql.Queries.Builders.Conditions;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Datas.Sql.Conditions {
|
||||
/// <summary>
|
||||
/// Sql大于查询条件测试
|
||||
/// </summary>
|
||||
public class GreaterConditionTest {
|
||||
/// <summary>
|
||||
/// 获取条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Test() {
|
||||
var condition = new GreaterCondition( "Age", "@Age" );
|
||||
Assert.Equal( "Age>@Age", condition.GetCondition() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Datas.Sql.Queries.Builders.Conditions;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Datas.Sql.Conditions {
|
||||
/// <summary>
|
||||
/// Sql大于等于查询条件测试
|
||||
/// </summary>
|
||||
public class GreaterEqualConditionTest {
|
||||
/// <summary>
|
||||
/// 获取条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Test() {
|
||||
var condition = new GreaterEqualCondition( "Age", "@Age" );
|
||||
Assert.Equal( "Age>=@Age", condition.GetCondition() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Datas.Sql.Queries.Builders.Conditions;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Datas.Sql.Conditions {
|
||||
/// <summary>
|
||||
/// Sql小于查询条件测试
|
||||
/// </summary>
|
||||
public class LessConditionTest {
|
||||
/// <summary>
|
||||
/// 获取条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Test() {
|
||||
var condition = new LessCondition( "Age", "@Age" );
|
||||
Assert.Equal( "Age<@Age", condition.GetCondition() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Datas.Sql.Queries.Builders.Conditions;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Datas.Sql.Conditions {
|
||||
/// <summary>
|
||||
/// Sql小于等于查询条件测试
|
||||
/// </summary>
|
||||
public class LessEqualConditionTest {
|
||||
/// <summary>
|
||||
/// 获取条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Test() {
|
||||
var condition = new LessEqualCondition( "Age", "@Age" );
|
||||
Assert.Equal( "Age<=@Age", condition.GetCondition() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Datas.Sql.Queries.Builders.Conditions;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Datas.Sql.Conditions {
|
||||
/// <summary>
|
||||
/// Sql模糊查询条件测试
|
||||
/// </summary>
|
||||
public class LikeConditionTest {
|
||||
/// <summary>
|
||||
/// 获取条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Test() {
|
||||
var condition = new LikeCondition( "Name", "@Name" );
|
||||
Assert.Equal( "Name Like @Name", condition.GetCondition() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Datas.Sql.Queries.Builders.Conditions;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Datas.Sql.Conditions {
|
||||
/// <summary>
|
||||
/// Sql不相等查询条件测试
|
||||
/// </summary>
|
||||
public class NotEqualConditionTest {
|
||||
/// <summary>
|
||||
/// 获取条件
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Test() {
|
||||
var condition = new NotEqualCondition( "Name", "@Name" );
|
||||
Assert.Equal( "Name!=@Name", condition.GetCondition() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,174 @@ namespace Util.Tests.Helpers {
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetLastName(获取最后一级成员名称)
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 返回类型为Object
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Object() {
|
||||
Expression<Func<Sample, object>> expression = t => t.StringValue == "A";
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 返回类型为bool
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Bool() {
|
||||
Assert.Empty( Lambda.GetLastName( null ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue == "A";
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression2 = t => t.Test2.IntValue == 1;
|
||||
Assert.Equal( "IntValue", Lambda.GetLastName( expression2 ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression3 = t => t.Test2.Test3.StringValue == "B";
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression3 ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 运算符
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Operation() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.Test2.IntValue != 1;
|
||||
Assert.Equal( "IntValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue > 1;
|
||||
Assert.Equal( "IntValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue < 1;
|
||||
Assert.Equal( "IntValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue >= 1;
|
||||
Assert.Equal( "IntValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue <= 1;
|
||||
Assert.Equal( "IntValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 可空类型
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Nullable() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.NullableIntValue == 1;
|
||||
Assert.Equal( "NullableIntValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.NullableDecimalValue == 1.5M;
|
||||
Assert.Equal( "NullableDecimalValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 方法
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Method() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue.Contains( "A" );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue.Contains( "B" );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.Test3.StringValue.StartsWith( "C" );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 实例
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Instance() {
|
||||
var test = new Sample() { StringValue = "a", Test2 = new Sample2() { StringValue = "b", Test3 = new Sample3() { StringValue = "c" } } };
|
||||
|
||||
Expression<Func<string>> expression = () => test.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
Expression<Func<string>> expression2 = () => test.Test2.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression2 ) );
|
||||
|
||||
Expression<Func<string>> expression3 = () => test.Test2.Test3.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression3 ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 复杂类型
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Complex() {
|
||||
var test = new Sample() { StringValue = "a", Test2 = new Sample2() { StringValue = "b" } };
|
||||
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue == test.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
Expression<Func<Sample, bool>> expression2 = t => t.StringValue == test.Test2.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression2 ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression3 = t => t.StringValue.Contains( test.StringValue );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression3 ) );
|
||||
Expression<Func<Sample, bool>> expression4 = t => t.StringValue.Contains( test.Test2.StringValue );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression4 ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 枚举
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Enum() {
|
||||
var test1 = new Sample { NullableEnumValue = EnumSample.C };
|
||||
|
||||
Expression<Func<Sample, bool>> expression = test => test.EnumValue == EnumSample.D;
|
||||
Assert.Equal( "EnumValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = test => test.EnumValue == test1.NullableEnumValue;
|
||||
Assert.Equal( "EnumValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = test => test.NullableEnumValue == EnumSample.E;
|
||||
Assert.Equal( "NullableEnumValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = test => test.NullableEnumValue == test1.NullableEnumValue;
|
||||
Assert.Equal( "NullableEnumValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
test1.NullableEnumValue = null;
|
||||
expression = test => test.NullableEnumValue == test1.NullableEnumValue;
|
||||
Assert.Equal( "NullableEnumValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 集合
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_List() {
|
||||
var list = new List<string> { "a", "b" };
|
||||
Expression<Func<Sample, bool>> expression = t => list.Contains( t.Test2.StringValue );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取成员名称 - 静态成员
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetLastName_Static() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue == Sample.StaticString;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.StringValue == Sample.StaticSample.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue == Sample.StaticString;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue == Sample.StaticSample.StringValue;
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue.Contains( Sample.StaticSample.StringValue );
|
||||
Assert.Equal( "StringValue", Lambda.GetLastName( expression ) );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetValue(获取成员值)
|
||||
|
||||
/// <summary>
|
||||
@@ -243,6 +411,153 @@ namespace Util.Tests.Helpers {
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetOperator(获取查询操作符)
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 返回类型为Object
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Object() {
|
||||
Expression<Func<Sample, object>> expression = t => t.StringValue == "A";
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 返回类型为bool
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator() {
|
||||
Assert.Null( Lambda.GetOperator( null ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue == "A";
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression2 = t => t.Test2.IntValue == 1;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression2 ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression3 = t => t.Test2.Test3.StringValue == "B";
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression3 ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 运算符
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Operation() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.Test2.IntValue != 1;
|
||||
Assert.Equal( Operator.NotEqual, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue > 1;
|
||||
Assert.Equal( Operator.Greater, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue < 1;
|
||||
Assert.Equal( Operator.Less, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue >= 1;
|
||||
Assert.Equal( Operator.GreaterEqual, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.IntValue <= 1;
|
||||
Assert.Equal( Operator.LessEqual, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 可空类型
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Nullable() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.NullableIntValue == 1;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.NullableDecimalValue == 1.5M;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 方法
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Method() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue.Contains( "A" );
|
||||
Assert.Equal( Operator.Contains, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue.EndsWith( "B" );
|
||||
Assert.Equal( Operator.Ends, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.Test3.StringValue.StartsWith( "C" );
|
||||
Assert.Equal( Operator.Starts, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 复杂类型
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Complex() {
|
||||
var test = new Sample() { StringValue = "a", Test2 = new Sample2() { StringValue = "b" } };
|
||||
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue == test.StringValue;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
Expression<Func<Sample, bool>> expression2 = t => t.StringValue != test.Test2.StringValue;
|
||||
Assert.Equal( Operator.NotEqual, Lambda.GetOperator( expression2 ) );
|
||||
|
||||
Expression<Func<Sample, bool>> expression3 = t => t.StringValue.Contains( test.StringValue );
|
||||
Assert.Equal( Operator.Contains, Lambda.GetOperator( expression3 ) );
|
||||
Expression<Func<Sample, bool>> expression4 = t => t.StringValue.EndsWith( test.Test2.StringValue );
|
||||
Assert.Equal( Operator.Ends, Lambda.GetOperator( expression4 ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 枚举
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Enum() {
|
||||
var test1 = new Sample { NullableEnumValue = EnumSample.C };
|
||||
|
||||
Expression<Func<Sample, bool>> expression = test => test.EnumValue == EnumSample.D;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = test => test.EnumValue == test1.NullableEnumValue;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = test => test.NullableEnumValue == EnumSample.E;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = test => test.NullableEnumValue == test1.NullableEnumValue;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 集合
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_List() {
|
||||
var list = new List<string> { "a", "b" };
|
||||
Expression<Func<Sample, bool>> expression = t => list.Contains( t.StringValue );
|
||||
Assert.Equal( Operator.Contains, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询操作符 - 静态成员
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestGetOperator_Static() {
|
||||
Expression<Func<Sample, bool>> expression = t => t.StringValue == Sample.StaticString;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.StringValue == Sample.StaticSample.StringValue;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue == Sample.StaticString;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue == Sample.StaticSample.StringValue;
|
||||
Assert.Equal( Operator.Equal, Lambda.GetOperator( expression ) );
|
||||
|
||||
expression = t => t.Test2.StringValue.Contains( Sample.StaticSample.StringValue );
|
||||
Assert.Equal( Operator.Contains, Lambda.GetOperator( expression ) );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetParameter(获取参数)
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user