private ArgumentListSyntax GetArgumentList(string sourceName, MappingTypeInfo typeInfo, ImmutableDictionary <string, ExpressionSyntax> existingArguments)
        {
            var arguments = typeInfo
                            .MemberPairs
                            .Where(arg => arg.Target != null)
                            .Select(m => GetArgument(sourceName, m, existingArguments))
                            .Where(x => x != null)
                            .ToImmutableList();

            var sb = new StringBuilder();

            sb.Append("\r\n (\r\n");

            for (int i = 0; i < arguments.Count; i++)
            {
                sb.Append("     " + arguments[i].ToString());
                if (i < arguments.Count - 1)
                {
                    sb.Append(",");
                }
                sb.Append("\r\n");
            }

            sb.Append(" )");
            var tet = sb.ToString();

            return(SyntaxFactory.ParseArgumentList(sb.ToString()));
        }
Exemplo n.º 2
0
 public static IMRM CreateMRM(MemberInfo member, MappingTypeInfo mappingTypeInfo)
 {
     if (mappingTypeInfo.DbValueConverter == null || member.GetMemberType().GetUnderlyingType().IsEnum /* 枚举比较特殊 */)
     {
         Type type = ClassGenerator.CreateMRMType(member);
         IMRM obj  = (IMRM)type.GetConstructor(Type.EmptyTypes).Invoke(null);
         return(obj);
     }
     else
     {
         return(new MRM2(member, mappingTypeInfo.DbValueConverter));
     }
 }
Exemplo n.º 3
0
        IDbDataParameter CreateDataParameter(IDbCommand cmd, DbParam param, MappingTypeInfo mappingTypeInfo)
        {
            IDbDataParameter parameter = cmd.CreateParameter();

            parameter.ParameterName = param.Name;

            Type parameterType = null;

            if (param.Value == null || param.Value == DBNull.Value)
            {
                parameter.Value = DBNull.Value;
                parameterType   = param.Type ?? typeof(object);
            }
            else
            {
                parameterType = param.Value.GetType();
                if (parameterType.IsEnum)
                {
                    parameterType   = Enum.GetUnderlyingType(parameterType);
                    parameter.Value = Convert.ChangeType(param.Value, parameterType);
                }
                else
                {
                    parameter.Value = param.Value;
                }
            }

            if (param.Precision != null)
            {
                parameter.Precision = param.Precision.Value;
            }

            if (param.Scale != null)
            {
                parameter.Scale = param.Scale.Value;
            }

            if (param.Size != null)
            {
                parameter.Size = param.Size.Value;
            }

            if (param.DbType != null)
            {
                parameter.DbType = param.DbType.Value;
            }
            else if (mappingTypeInfo != null)
            {
                parameter.DbType = mappingTypeInfo.MapDbType;
            }

            const int defaultSizeOfStringOutputParameter = 4000;/* 当一个 string 类型输出参数未显示指定 Size 时使用的默认大小。如果有需要更大或者该值不足以满足需求,需显示指定 DbParam.Size 值 */

            if (param.Direction == ParamDirection.Input)
            {
                parameter.Direction = ParameterDirection.Input;
            }
            else if (param.Direction == ParamDirection.Output)
            {
                parameter.Direction = ParameterDirection.Output;
                param.Value         = null;
                if (param.Size == null && param.Type == UtilConstants.TypeOfString)
                {
                    parameter.Size = defaultSizeOfStringOutputParameter;
                }
            }
            else if (param.Direction == ParamDirection.InputOutput)
            {
                parameter.Direction = ParameterDirection.InputOutput;
                if (param.Size == null && param.Type == UtilConstants.TypeOfString)
                {
                    parameter.Size = defaultSizeOfStringOutputParameter;
                }
            }
            else
            {
                throw new NotSupportedException(string.Format("ParamDirection '{0}' is not supported.", param.Direction));
            }

            return(parameter);
        }
Exemplo n.º 4
0
        IDbCommand PrepareCommand(string cmdText, DbParam[] parameters, CommandType cmdType, out List <OutputParameter> outputParameters)
        {
            outputParameters = null;

            IDbCommand cmd = this._dbConnection.CreateCommand();

            cmd.CommandText    = cmdText;
            cmd.CommandType    = cmdType;
            cmd.CommandTimeout = this._commandTimeout;
            if (this.IsInTransaction)
            {
                cmd.Transaction = this._dbTransaction;
            }

            if (parameters != null)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    DbParam param = parameters[i];
                    if (param == null)
                    {
                        continue;
                    }

                    if (param.ExplicitParameter != null)/* 如果存在创建好了的 IDbDataParameter,则直接用它。同时也忽视了 DbParam 的其他属性 */
                    {
                        cmd.Parameters.Add(param.ExplicitParameter);
                        continue;
                    }

                    Type parameterType;
                    if (param.Value == null || param.Value == DBNull.Value)
                    {
                        parameterType = param.Type ?? typeof(object);
                    }
                    else
                    {
                        parameterType = param.Value.GetType();
                        if (parameterType.IsEnum)
                        {
                            parameterType = Enum.GetUnderlyingType(parameterType);
                        }
                    }

                    IDbDataParameter parameter       = null;
                    MappingTypeInfo  mappingTypeInfo = MappingTypeSystem.GetMappingTypeInfo(parameterType);
                    if (mappingTypeInfo != null && mappingTypeInfo.MappingType != null)
                    {
                        parameter = mappingTypeInfo.MappingType.CreateDataParameter(cmd, param);
                    }
                    else
                    {
                        parameter = this.CreateDataParameter(cmd, param, mappingTypeInfo);
                    }

                    cmd.Parameters.Add(parameter);

                    OutputParameter outputParameter = null;
                    if (param.Direction == ParamDirection.Output || param.Direction == ParamDirection.InputOutput)
                    {
                        outputParameter = new OutputParameter(param, parameter);
                        if (outputParameters == null)
                        {
                            outputParameters = new List <OutputParameter>();
                        }
                        outputParameters.Add(outputParameter);
                    }
                }
            }

            return(cmd);
        }