/// <summary> /// 过滤 /// </summary> /// <param name="dialect">Sql方言</param> /// <param name="matedata">实体元数据解析器</param> /// <param name="register">实体别名注册器</param> /// <param name="join">Join子句</param> /// <param name="where">Where子句</param> /// <param name="type">类型</param> /// <param name="alias">表别名</param> private void Filter(IDialect dialect, IEntityMatedata matedata, IEntityAliasRegister register, IJoinClause join, IWhereClause where, Type type, string alias) { if (type == null) { return; } if (string.IsNullOrWhiteSpace(alias)) { return; } if (typeof(IDelete).IsAssignableFrom(type) == false) { return; } var isDeleted = $"{dialect.SafeName(alias)}.{dialect.SafeName(matedata.GetColumn(type, "IsDeleted"))}"; if (register.FromType == type) { where.Where(isDeleted, false); return; } join.Find(type)?.On(isDeleted, false); }
/// <summary> /// 求总行数 /// </summary> /// <param name="columnAlias">列别名</param> public void Count( string columnAlias = null ) { if( string.IsNullOrWhiteSpace( columnAlias ) ) { Aggregate( "Count(*)" ); return; } Aggregate( $"Count(*) As {_dialect.SafeName( columnAlias )}" ); }
/// <summary> /// 过滤 /// </summary> private void Filter(IDialect dialect, IEntityMatedata matedata, IWhereClause where, Type type, string alias) { if (type == null) { return; } if (string.IsNullOrWhiteSpace(alias)) { return; } if (typeof(IDelete).IsAssignableFrom(type)) { where.Where($"{dialect.SafeName( alias )}.{dialect.SafeName( matedata.GetColumn( type, "IsDeleted" ) )}", false); } }
/// <summary> /// 获取安全名称 /// </summary> private string GetSafeName(IDialect dialect, string name) { if (dialect == null) { return(name); } return(dialect.SafeName(name)); }
/// <summary> /// 获取安全名称 /// </summary> /// <param name="source">源</param> /// <param name="name">名称</param> public static string GetSafeName(this IDialect source, string name) { if (source == null) { return(name); } return(source.SafeName(name)); }
/// <summary> /// 设置子查询表 /// </summary> /// <param name="builder">Sql生成器</param> /// <param name="alias">表别名</param> public void From(ISqlBuilder builder, string alias) { if (builder == null) { return; } var result = builder.ToSql(); if (string.IsNullOrWhiteSpace(alias) == false) { result = $"({result}) As {Dialect.SafeName( alias )}"; } AppendSql(result); }
/// <summary> /// 添加到连接子句 /// </summary> private void AppendJoin(string joinType, ISqlBuilder builder, string alias) { AppendJoin(joinType, $"({builder.ToSql()}) As {_dialect.SafeName(alias)}"); }
/// <summary> /// 获取安全名称 /// </summary> /// <param name="dialect">Sql方言</param> /// <param name="name">名称</param> public static string GetSafeName(this IDialect dialect, string name) => dialect == null ? name : dialect.SafeName(name);
/// <summary> /// 聚合 /// </summary> private void Aggregate(string fun, string column, string columnAlias) { Aggregate($"{fun}({_dialect.SafeName( column )})", columnAlias); }
/// <summary> /// 聚合 /// </summary> /// <param name="func">函数名</param> /// <param name="column">列名</param> /// <param name="columnAlias">列别名</param> private void Aggregate(string func, string column, string columnAlias) => Aggregate( $"{func}({_dialect.SafeName(column)})", string.IsNullOrWhiteSpace(columnAlias) ? column : columnAlias);
/// <summary> /// 获取安全名称 /// </summary> /// <param name="dialect">Sql方言</param> /// <param name="name">名称</param> protected string GetSafeName(IDialect dialect, string name) => dialect == null ? name : dialect.SafeName(name);