コード例 #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));
        }
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
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);
            }
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
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);
        }
コード例 #10
0
        /// <summary>
        /// 生成实体类型的SQL脚本。
        /// </summary>
        /// <param name="sql">SQL查询实例。</param>
        /// <returns>返回SQL脚本。</returns>
        public SqlIndentedStringBuilder Query(IQuerySql sql)
        {
            var builder = new SqlIndentedStringBuilder();

            if (sql.PageIndex != null)
            {
                PageQuery(sql, builder);
            }
            else if (sql.Size != null)
            {
                SizeQuery(sql, builder);
            }
            else
            {
                Query(sql, builder);
            }
            return(builder);
        }
コード例 #11
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(")").AppendLine(SqlHelper.StatementTerminator);
                if (entityType.Identity != null)
                {
                    builder.Append(SelectIdentity());
                }
                return(new CacheEntry(builder.ToString(), names));
            });

            return(new SqlIndentedStringBuilder(entry.Sql, entry.Parameters));
        }
コード例 #12
0
 /// <summary>
 /// 选项特定数量的记录数的查询脚本。
 /// </summary>
 /// <param name="sql">当前查询实例。</param>
 /// <param name="builder"><see cref="SqlIndentedStringBuilder"/>实例。</param>
 protected abstract void SizeQuery(IQuerySql sql, SqlIndentedStringBuilder builder);