/// <summary> /// 更新实例。 /// </summary> /// <param name="entityType">模型实例。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Update(IEntityType entityType) { var entry = _updates.GetOrAdd(entityType.ClrType, key => { var names = entityType.GetProperties() .Where(property => property.IsUpdatable()) .Select(property => property.Name) .ToList(); var builder = new SqlIndentedStringBuilder(); builder.Append("UPDATE ").Append(SqlHelper.DelimitIdentifier(entityType.Table)).Append(" SET "); builder.JoinAppend(names.Select(name => $"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}")).AppendLine(); if (entityType.PrimaryKey != null) { var primaryKeys = entityType.PrimaryKey.Properties .Select(p => p.Name) .ToList(); builder.Append("WHERE ") .JoinAppend( primaryKeys.Select( name => $"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}"), " AND ") .Append(SqlHelper.StatementTerminator); names.AddRange(primaryKeys); } return(new CacheEntry(builder.ToString(), names)); }); return(new SqlIndentedStringBuilder(entry.Sql, entry.Parameters)); }
/// <summary> /// 更新实例。 /// </summary> /// <param name="entityType">模型实例。</param> /// <param name="expression">条件表达式。</param> /// <param name="parameters">匿名对象。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Update(IEntityType entityType, Expression expression, LambdaExpression parameters) { var builder = new SqlIndentedStringBuilder(); builder.Append("UPDATE ").Append(SqlHelper.DelimitIdentifier(entityType.Table)).Append(" SET "); builder.Append(VisitUpdateExpression(parameters)); builder.AppendEx(Visit(expression), " WHERE {0}").Append(SqlHelper.StatementTerminator); return(builder); }
/// <summary> /// 判断是否存在。 /// </summary> /// <param name="entityType">模型实例。</param> /// <param name="expression">条件表达式。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Any(IEntityType entityType, Expression expression) { var builder = new SqlIndentedStringBuilder(); builder.Append("SELECT 1 FROM ").Append(SqlHelper.DelimitIdentifier(entityType.Table)); builder.AppendEx(Visit(expression), " WHERE {0}").Append(SqlHelper.StatementTerminator); return(builder); }
/// <summary> /// 判断唯一主键关联是否存在。 /// </summary> /// <param name="entityType">模型实例。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Any(IEntityType entityType) { var builder = new SqlIndentedStringBuilder(); builder.Append("SELECT 1 FROM ").Append(SqlHelper.DelimitIdentifier(entityType.Table)); AppendWherePrimaryKey(builder, entityType); return(builder); }
/// <summary> /// 将唯一主键附加到当前语句构建实例中。 /// </summary> /// <param name="builder">SQL语句构建实例。</param> /// <param name="entityType">当前实体类。</param> /// <param name="terminated">是否结束语句。</param> protected void AppendWherePrimaryKey(SqlIndentedStringBuilder builder, IEntityType entityType, bool terminated = true) { var primaryKey = SqlHelper.DelimitIdentifier(entityType.SingleKey().Name); builder.Append($" WHERE {primaryKey} = {PrimaryKeyParameter}"); if (terminated) { builder.AppendLine(SqlHelper.StatementTerminator); } }
/// <summary> /// 快速构建唯一主键SQL语句。 /// </summary> /// <param name="entityType">模型实例。</param> /// <param name="sqlHeader">SQL语句头,如:DELETE FROM等。</param> /// <param name="key">主键值。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder PrimaryKeySql(IEntityType entityType, string sqlHeader, object key) { var builder = new SqlIndentedStringBuilder(); builder.Append(sqlHeader).Append(" ") .Append(SqlHelper.DelimitIdentifier(entityType.Table)); AppendWherePrimaryKey(builder, entityType); builder.AddPrimaryKey(key); return(builder); }
/// <summary> /// 更新实例。 /// </summary> /// <param name="entityType">模型实例。</param> /// <param name="expression">条件表达式。</param> /// <param name="parameters">匿名对象。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Update(IEntityType entityType, Expression expression, object parameters) { var builder = new SqlIndentedStringBuilder(); builder.Append("UPDATE ").Append(SqlHelper.DelimitIdentifier(entityType.Table)).Append(" SET "); builder.CreateObjectParameters(parameters); builder.JoinAppend(builder.Parameters.Keys.Select( name => $"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}")); builder.AppendEx(Visit(expression), " WHERE {0}").Append(SqlHelper.StatementTerminator); return(builder); }
/// <summary> /// 通过唯一主键更新实例。 /// </summary> /// <param name="entityType">模型实例。</param> /// <param name="parameters">匿名对象。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Update(IEntityType entityType, object parameters) { var builder = new SqlIndentedStringBuilder(); builder.Append("UPDATE ").Append(SqlHelper.DelimitIdentifier(entityType.Table)).Append(" SET "); builder.CreateObjectParameters(parameters); builder.JoinAppend(builder.Parameters.Keys.Select( name => $"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}")); AppendWherePrimaryKey(builder, entityType); return(builder); }
/// <summary> /// 聚合函数。 /// </summary> /// <param name="entityType">模型实例。</param> /// <param name="method">聚合函数。</param> /// <param name="column">聚合列。</param> /// <param name="expression">条件表达式。</param> /// <param name="nullColumn">当<paramref name="column"/>为空的时候,使用的值。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Scalar(IEntityType entityType, string method, LambdaExpression column, Expression expression, string nullColumn = null) { if (column != null) { nullColumn = SqlHelper.DelimitIdentifier(column.GetPropertyAccess().Name); } var builder = new SqlIndentedStringBuilder(); builder.Append($"SELECT {method}({nullColumn}) FROM {SqlHelper.DelimitIdentifier(entityType.Table)}"); builder.AppendEx(Visit(expression), " WHERE {0}") .AppendLine(SqlHelper.StatementTerminator); return(builder); }
/// <summary> /// 新建实例。 /// </summary> /// <param name="entityType">模型实例。</param> /// <returns>返回SQL构建实例。</returns> public virtual SqlIndentedStringBuilder Create(IEntityType entityType) { var entry = _creations.GetOrAdd(entityType.ClrType, key => { var names = entityType.GetProperties() .Where(property => property.IsCreatable()) .Select(property => property.Name) .ToList(); var builder = new SqlIndentedStringBuilder(); builder.Append("INSERT INTO"); builder.Append(" ").Append(SqlHelper.DelimitIdentifier(entityType.Table)); builder.Append("(").JoinAppend(names.Select(SqlHelper.DelimitIdentifier)).Append(")"); builder.Append("VALUES(") .JoinAppend(names.Select(SqlHelper.Parameterized)) .Append(")").AppendLine(SqlHelper.StatementTerminator); if (entityType.Identity != null) { builder.Append(SelectIdentity()); } return(new CacheEntry(builder.ToString(), names)); }); return(new SqlIndentedStringBuilder(entry.Sql, entry.Parameters)); }