Esempio n. 1
0
 /// <summary>
 /// 生成更新实体类型的SQL脚本。
 /// </summary>
 /// <param name="entity">实体类型实例。</param>
 /// <returns>返回SQL脚本。</returns>
 public virtual SqlScript Update(IEntityType entity)
 {
     return(Cached(entity, nameof(Update), () =>
     {
         var builder = NewBuilder(entity, Ignore.Update);
         var names = entity.FindProperties(Ignore.Update)
                     .Select(name => name.Name);
         var list = new List <string>();
         foreach (var name in names)
         {
             list.Add($"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}");
         }
         builder.JoinAppend(list).AppendLine();
         if (entity.PrimaryKey != null)
         {
             builder.Append("WHERE ")
             .JoinAppend(
                 entity.PrimaryKey.Properties.Select(
                     name => $"{SqlHelper.DelimitIdentifier(name.Name)}={SqlHelper.Parameterized(name.Name)}"))
             .Append(SqlHelper.StatementTerminator);
             names = names.Concat(entity.PrimaryKey.Properties.Select(p => p.Name));
         }
         return new SqlScript(builder, names.Distinct(StringComparer.OrdinalIgnoreCase), entity);
     }));
 }
Esempio n. 2
0
        /// <summary>
        /// 递归的查询脚本。
        /// </summary>
        /// <param name="entityType">当前查询类型实例。</param>
        /// <param name="expression">表达式。</param>
        /// <param name="isParent">父级。</param>
        /// <returns>返回脚本实例。</returns>
        public override SqlScript Recurse(IEntityType entityType, Expression expression, bool isParent = false)
        {
            var table   = Model.GetTable(entityType.ClrType);
            var columns = entityType.FindProperties(Ignore.List).Select(p => SqlHelper.DelimitIdentifier(p.Name));
            var fields  = string.Join(",", columns);
            var builder = new IndentedStringBuilder();

            builder.Append("WITH _recursive(").Append(fields).Append(")as(");
            builder.Append("SELECT ").Append(fields)
            .Append(" FROM ").Append(table).AppendEx(Visit(expression), "WHERE {0}");
            builder.Append(" UNION ALL (");
            fields = string.Join(",", columns.Select(x => $"a.{x}"));
            builder.Append("SELECT ")
            .Append(fields)
            .Append(" FROM ");
            builder.Append(table)
            .Append(" a INNER JOIN _recursive b ON ");
            if (isParent)
            {
                builder.Append("a.Id = b.ParentId)");
            }
            else
            {
                builder.Append("a.ParentId = b.Id)");
            }
            builder.Append(") SELECT * FROM _recursive;");
            return(new SqlScript(builder));
        }
Esempio n. 3
0
 /// <summary>
 /// 生成新建实体类型的SQL脚本。
 /// </summary>
 /// <param name="entity">实体类型实例。</param>
 /// <returns>返回SQL脚本。</returns>
 public virtual SqlScript Create(IEntityType entity)
 {
     return(Cached(entity, nameof(Create), () =>
     {
         var builder = NewBuilder(entity, Ignore.Insert);
         var names = entity.FindProperties(Ignore.Insert)
                     .Select(name => name.Name);
         builder.Append("(")
         .JoinAppend(names.Select(SqlHelper.DelimitIdentifier))
         .AppendLine(")");
         builder.Append("VALUES(")
         .JoinAppend(names.Select(SqlHelper.Parameterized))
         .Append(")")
         .Append(SqlHelper.StatementTerminator);
         if (entity.Identity != null)
         {
             builder.Append(SelectIdentity());
         }
         return new SqlScript(builder, names, entity);
     }));
 }