コード例 #1
0
        private TranslateResult TranslateColumnValue(object instance, Type type, List <bool> fieldsUsable, List <Parameter> parameters)
        {
            bool isFirst = true;

            TableSchema tableSchema = TableSchemaResolver.GetTableSchema(type);

            for (int i = 0, count = tableSchema.FieldsSchema.Count; i < count; i++)
            {
                var field = tableSchema.FieldsSchema[i];
                if (fieldsUsable[i] && field.IsPrimaryKey == false)
                {
                    object value = ExpressionFunc.GetPropertyValue(instance, type, field.PropertyInfo);
                    if (value == null && field.PropertyInfo.PropertyType.Name == "String")
                    {
                        value = "";
                    }
                    if (value == null)
                    {
                        throw new NotSupportedException(string.Format("The field '{0}' is not supported", field.FieldName));
                    }
                    if (!isFirst)
                    {
                        clauseBuilder.AppendClause(",");
                    }
                    clauseBuilder.AppendClause(field.FieldName);
                    clauseBuilder.AppendClause("=");
                    if (field.IsUpdCount)
                    {
                        int iValue = Convert.ToInt32(value);
                        if (iValue == short.MaxValue)
                        {
                            iValue = 1;
                        }
                        else
                        {
                            iValue++;
                        }
                        value = Convert.ChangeType(iValue, field.PropertyInfo.PropertyType);
                        ExpressionFunc.SetPropertyValue(instance, type, field.PropertyInfo, value);
                    }
                    clauseBuilder.AppendClause(parameterBuilder.GenerateParameterName(), value);
                    isFirst = false;
                }
            }

            return(new TranslateResult
            {
                CommandText = clauseBuilder.GetExpressionText(),                    // UPDATE ... column1=@value1,column2=@value2,...
                Parameters = clauseBuilder.GetExpressionParameters(),
            });
        }