コード例 #1
0
        /// <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(")").Append(SqlHelper.fieldsTerminator);
                if (entityType.Identity != null)
                {
                    builder.Append(SelectIdentity()).Append(SqlHelper.fieldsTerminator);
                }

                return(new CacheEntry(builder.ToString(), names));
            });

            return(new SqlIndentedStringBuilder(entry.Sql, entry.Parameters));
        }
コード例 #2
0
        /// <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));
        }
コード例 #3
0
        /// <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();
                    var keys = new List <string>();
                    keys.AddRange(primaryKeys);
                    if (entityType.RowVersion != null)
                    {
                        keys.Add(entityType.RowVersion.Name);
                    }

                    if (entityType.ConcurrencyKey != null)
                    {
                        keys.AddRange(entityType.ConcurrencyKey.Properties.Select(x => x.Name));
                    }

                    names.AddRange(keys);
                    builder.Append("WHERE ")
                    .JoinAppend(
                        keys.Select(
                            name => $"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}"),
                        " AND ")
                    .Append(SqlHelper.fieldsTerminator);
                    if (entityType.RowVersion != null || entityType.ConcurrencyKey != null)
                    {
                        builder.Append(SelectAffectedRows()).Append(" FROM ")
                        .Append(SqlHelper.DelimitIdentifier(entityType.Table))
                        .Append(" WHERE ")
                        .JoinAppend(
                            keys.Select(
                                name => $"{SqlHelper.DelimitIdentifier(name)}={SqlHelper.Parameterized(name)}"),
                            " AND ")
                        .Append(SqlHelper.fieldsTerminator);
                    }
                }

                return(new CacheEntry(builder.ToString(), names));
            });

            return(new SqlIndentedStringBuilder(entry.Sql, entry.Parameters));
        }