Esempio n. 1
0
        /// <summary>
        /// 将参数类型转 SqlParameter 集合
        /// </summary>
        /// <param name="parameters">参数</param>
        /// <param name="arguments">参数值</param>
        /// <returns>SqlParameter[]</returns>
        public static SqlParameter[] ToSqlParameters(this ParameterInfo[] parameters, object[] arguments)
        {
            var sqlParameters = new List <SqlParameter>();

            if (parameters == null || parameters.Length == 0)
            {
                return(sqlParameters.ToArray());
            }

            // 只支持要么全是基元类型,或全部都是类类型
            if (!parameters.All(u => u.ParameterType.IsRichPrimitive()) || !parameters.All(u => u.ParameterType.IsClass))
            {
                throw Oops.Oh("Invalid type cast", typeof(InvalidOperationException));
            }

            // 处理基元类型参数
            if (parameters.All(u => u.ParameterType.IsRichPrimitive()))
            {
                for (var i = 0; i < parameters.Length; i++)
                {
                    var parameter      = parameters[i];
                    var parameterValue = (arguments.Length > i ? arguments[i] : null) ?? DBNull.Value;

                    // 判断属性是否贴有 [DbParameter] 特性
                    if (parameter.IsDefined(typeof(DbParameterAttribute), true))
                    {
                        var dbParameterAttribute = parameter.GetCustomAttribute <DbParameterAttribute>(true);
                        sqlParameters.Add(DbHelpers.CreateSqlParameter(parameter.Name, parameterValue, dbParameterAttribute));
                        continue;
                    }
                    sqlParameters.Add(new SqlParameter(parameter.Name, parameterValue));
                }
            }
            // 处理类类型参数
            else
            {
                foreach (var argument in arguments)
                {
                    sqlParameters.AddRange(argument.ToSqlParameters());
                }
            }

            return(sqlParameters.ToArray());
        }
Esempio n. 2
0
        /// <summary>
        /// 生成函数执行 sql 语句
        /// </summary>
        ///<param name="providerName">ADO.NET 数据库对象</param>
        /// <param name="dbFunctionType">函数类型</param>
        /// <param name="funcName">函数名词</param>
        /// <param name="model">参数模型</param>
        /// <returns>(string sql, SqlParameter[] parameters)</returns>
        internal static (string sql, SqlParameter[] parameters) CombineFunctionSql(string providerName, DbFunctionType dbFunctionType, string funcName, object model)
        {
            // 检查是否支持函数
            DatabaseProvider.CheckFunctionSupported(providerName, dbFunctionType);

            // 获取模型所有公开的属性
            var properities = model == null
                ? Array.Empty <PropertyInfo>()
                : model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            var parameters    = new List <SqlParameter>();
            var stringBuilder = new StringBuilder();

            stringBuilder.Append($"SELECT{(dbFunctionType == DbFunctionType.Table ? " * FROM" : "")} {funcName}(");

            for (var i = 0; i < properities.Length; i++)
            {
                var property      = properities[i];
                var propertyValue = property.GetValue(model) ?? DBNull.Value;

                stringBuilder.Append($"@{property.Name}");

                // 处理最后一个参数逗号
                if (i != properities.Length - 1)
                {
                    stringBuilder.Append(", ");
                }

                // 判断属性是否贴有 [DbParameter] 特性
                if (property.IsDefined(typeof(DbParameterAttribute), true))
                {
                    var dbParameterAttribute = property.GetCustomAttribute <DbParameterAttribute>(true);
                    parameters.Add(DbHelpers.CreateSqlParameter(property.Name, propertyValue, dbParameterAttribute));
                    continue;
                }
                parameters.Add(new SqlParameter(property.Name, propertyValue));
            }

            stringBuilder.Append("); ");

            return(stringBuilder.ToString(), parameters.ToArray());
        }
Esempio n. 3
0
        /// <summary>
        /// 将模型转为 SqlParameter 集合
        /// </summary>
        /// <param name="model">模型</param>
        /// <returns>SqlParameter[]</returns>
        public static SqlParameter[] ToSqlParameters(this object model)
        {
            var sqlParameters = new List <SqlParameter>();

            var modelType = model?.GetType();

            if (model == null || !modelType.IsClass)
            {
                return(sqlParameters.ToArray());
            }

            // 获取所有公开实例属性
            var properties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (properties.Length == 0)
            {
                return(sqlParameters.ToArray());
            }

            // 遍历所有属性
            foreach (var property in properties)
            {
                var propertyValue = property.GetValue(model) ?? DBNull.Value;

                // 判断属性是否贴有 [DbParameter] 特性
                if (property.IsDefined(typeof(DbParameterAttribute), true))
                {
                    var dbParameterAttribute = property.GetCustomAttribute <DbParameterAttribute>(true);
                    sqlParameters.Add(DbHelpers.CreateSqlParameter(property.Name, propertyValue, dbParameterAttribute));
                    continue;
                }
                sqlParameters.Add(new SqlParameter(property.Name, propertyValue));
            }

            return(sqlParameters.ToArray());
        }