Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 删除实例。
        /// </summary>
        /// <param name="entityType">模型实例。</param>
        /// <param name="expression">条件表达式。</param>
        /// <returns>返回SQL构建实例。</returns>
        public virtual SqlIndentedStringBuilder Delete(IEntityType entityType, Expression expression)
        {
            var builder = new SqlIndentedStringBuilder();

            builder.Append("DELETE FROM ").Append(SqlHelper.DelimitIdentifier(entityType.Table));
            builder.AppendEx(Visit(expression), " WHERE {0}").Append(SqlHelper.fieldsTerminator);
            return(builder);
        }
Ejemplo n.º 8
0
        /// <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);
            }
        }
Ejemplo n.º 9
0
        /// <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);
        }
Ejemplo n.º 10
0
        /// <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);
        }
Ejemplo n.º 11
0
        /// <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);
        }
Ejemplo n.º 12
0
        /// <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);
        }