示例#1
0
        private PropertyValue GetSupportedValue(bool isNull, PropertyMapping mapper, IDataReader reader)
        {
            try
            {
                if (mapper.Property.Type == typeof(Guid))
                {
                    return(isNull ? new Guid?() : new Guid(reader.GetValue(mapper.Index).ToString()));
                }

                var typeCode = Type.GetTypeCode(mapper.Property.Type.GetNonNullableType());
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(isNull ? new bool?() : RecordWrapper.GetBoolean(reader, mapper.Index));

                case TypeCode.Char:
                    return(isNull ? new char?() : RecordWrapper.GetChar(reader, mapper.Index));

                case TypeCode.Byte:
                    return(isNull ? new byte?() : RecordWrapper.GetByte(reader, mapper.Index));

                case TypeCode.Int16:
                    return(isNull ? new short?() : RecordWrapper.GetInt16(reader, mapper.Index));

                case TypeCode.Int32:
                    return(isNull ? new int?() : RecordWrapper.GetInt32(reader, mapper.Index));

                case TypeCode.Int64:
                    return(isNull ? new long?() : RecordWrapper.GetInt64(reader, mapper.Index));

                case TypeCode.Decimal:
                    return(isNull ? new decimal?() : RecordWrapper.GetDecimal(reader, mapper.Index));

                case TypeCode.Double:
                    return(isNull ? new double?() : RecordWrapper.GetDouble(reader, mapper.Index));

                case TypeCode.String:
                    return(isNull ? string.Empty : RecordWrapper.GetString(reader, mapper.Index));

                case TypeCode.DateTime:
                    return(isNull ? new DateTime?() : RecordWrapper.GetDateTime(reader, mapper.Index));

                case TypeCode.Single:
                    return(isNull ? new float?() : RecordWrapper.GetFloat(reader, mapper.Index));

                default:
                    return(PropertyValueHelper.NewValue(RecordWrapper.GetValue(reader, mapper.Index)));
                }
            }
            catch (Exception ex)
            {
                throw new RowMapperCastException(mapper.Property.Name, mapper.Property.Type, ex);
            }
        }
示例#2
0
        private bool InternalCreate(IEntity entity)
        {
            var parameters = new ParameterCollection();
            var context    = CreateContext(parameters);
            var sql        = EntityPersistentQueryBuilder.BuidCreateQuery(context, entity);

            if (!sql.IsNullOrEmpty())
            {
                var rootType = entity.EntityType.GetRootType();

                //找出自增长序列的属性
                var incProperty = PropertyUnity.GetPersistentProperties(rootType).FirstOrDefault(s => s.Info.GenerateType == IdentityGenerateType.AutoIncrement);

                if (incProperty != null)
                {
                    if (!string.IsNullOrEmpty(context.Syntax.IdentitySelect))
                    {
                        //获得当前插入的自增长序列值
                        var identitySelect = context.Syntax.IdentitySelect;
                        if (!identitySelect.StartsWith(";"))
                        {
                            identitySelect = ";" + identitySelect;
                        }

                        var incValue = PropertyValueHelper.NewValue(context.Database.ExecuteScalar <int>(sql + identitySelect, context.Parameters));
                        entity.InternalSetValue(incProperty, incValue);
                        return(!incValue.IsNullOrEmpty());
                    }
                    else
                    {
                        //使用生成器生成值
                        var generator = context.Database.Provider.GetService <IGeneratorProvider>();
                        if (generator != null)
                        {
                            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);
                            var inc      = generator.GenerateValue(context.Database, context.Environment == null ? metadata.TableName : context.Environment.GetVariableTableName(metadata), incProperty.Info.FieldName);
                            entity.InternalSetValue(incProperty, inc);

                            parameters.Clear();
                            sql = EntityPersistentQueryBuilder.BuidCreateQuery(context, entity);

                            return(context.Database.ExecuteNonQuery(sql, context.Parameters) > 0);
                        }
                    }
                }

                return(database.ExecuteNonQuery(sql, parameters) > 0);
            }

            return(false);
        }
示例#3
0
        private PropertyValue GetSupportedValue(bool isNull, PropertyMapping mapper, DataRow row)
        {
            if (mapper.Property.Type == typeof(Guid))
            {
                return(isNull ? new Guid?() : new Guid(row[mapper.Index].ToString()));
            }

            var typeCode = Type.GetTypeCode(mapper.Property.Type.GetNonNullableType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return(isNull ? new bool?() : Convert.ToBoolean(row[mapper.Index]));

            case TypeCode.Char:
                return(isNull ? new char?() : Convert.ToChar(row[mapper.Index]));

            case TypeCode.Byte:
                return(isNull ? new byte?() : Convert.ToByte(row[mapper.Index]));

            case TypeCode.Int16:
                return(isNull ? new short?() : Convert.ToInt16(row[mapper.Index]));

            case TypeCode.Int32:
                return(isNull ? new int?() : Convert.ToInt32(row[mapper.Index]));

            case TypeCode.Int64:
                return(isNull ? new long?() : Convert.ToInt64(row[mapper.Index]));

            case TypeCode.Decimal:
                return(isNull ? new decimal?() : Convert.ToDecimal(row[mapper.Index]));

            case TypeCode.Double:
                return(isNull ? new double?() : Convert.ToDouble(row[mapper.Index]));

            case TypeCode.String:
                return(isNull ? string.Empty : Convert.ToString(row[mapper.Index]));

            case TypeCode.DateTime:
                return(isNull ? new DateTime?() : Convert.ToDateTime(row[mapper.Index]));

            case TypeCode.Single:
                return(isNull ? new float?() : Convert.ToSingle(row[mapper.Index]));

            default:
                return(PropertyValueHelper.NewValue(row[mapper.Index]));
            }
        }
示例#4
0
 /// <summary>
 /// 设置指定属性的值。
 /// </summary>
 /// <typeparam name="T">值的类型。</typeparam>
 /// <param name="property">实体属性。</param>
 /// <param name="value">要设置的值。</param>
 public virtual void SetValue <T>(IProperty property, T value)
 {
     SetValue(property, PropertyValueHelper.NewValue(value, property.Type));
 }