コード例 #1
0
 /// <summary>
 /// Gets a column value by the index
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="dataReader"></param>
 /// <returns></returns>
 public override object GetValueByIndex(ResultProperty mapping, IDataReader dataReader)
 {
     return(DBNull.Value);
 }
コード例 #2
0
 set => SetValue(ResultProperty, value);
コード例 #3
0
 /// <summary>
 /// Gets a column value by the index
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="dataReader"></param>
 /// <returns></returns>
 public abstract object GetValueByIndex(ResultProperty mapping, IDataReader dataReader);
コード例 #4
0
 /// <summary>
 /// Sets the value to the result property.
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="target"></param>
 /// <param name="dataBaseValue"></param>
 public override void SetData(ref object target, ResultProperty mapping, object dataBaseValue)
 {
     ((IDictionary)target).Add(mapping.PropertyName, dataBaseValue);
 }
コード例 #5
0
        /// <summary>
        /// Builds the the iBATIS core model (statement, alias, resultMap, parameterMap, dataSource)
        /// from an <see cref="IConfigurationStore"/> and store all the refrences in an <see cref="IModelStore"/> .
        /// </summary>
        /// <param name="configurationSetting">The configuration setting.</param>
        /// <param name="store">The configuration store.</param>
        /// <returns>The model store</returns>
        public virtual void BuildModel(ConfigurationSetting configurationSetting, IConfigurationStore store)
        {
            IObjectFactory      objectFactory      = null;
            IGetAccessorFactory getAccessorFactory = null;
            ISetAccessorFactory setAccessorFactory = null;
            ISessionFactory     sessionFactory     = null;
            ISessionStore       sessionStore       = null;

            if (configurationSetting != null)
            {
                objectFactory          = configurationSetting.ObjectFactory;
                setAccessorFactory     = configurationSetting.SetAccessorFactory;
                getAccessorFactory     = configurationSetting.GetAccessorFactory;
                dataSource             = configurationSetting.DataSource;
                sessionFactory         = configurationSetting.SessionFactory;
                sessionStore           = configurationSetting.SessionStore;
                dynamicSqlEngine       = configurationSetting.DynamicSqlEngine;
                isCacheModelsEnabled   = configurationSetting.IsCacheModelsEnabled;
                useStatementNamespaces = configurationSetting.UseStatementNamespaces;
                useReflectionOptimizer = configurationSetting.UseReflectionOptimizer;
                preserveWhitespace     = configurationSetting.PreserveWhitespace;
            }

            // Xml setting override code setting
            //取store中的数据初始化类成员 为下面做准备
            LoadSetting(store);

            if (objectFactory == null)
            {
                objectFactory = new ObjectFactory(useReflectionOptimizer);
            }
            if (setAccessorFactory == null)
            {
                setAccessorFactory = new SetAccessorFactory(useReflectionOptimizer);
            }
            if (getAccessorFactory == null)
            {
                getAccessorFactory = new GetAccessorFactory(useReflectionOptimizer);
            }
            AccessorFactory accessorFactory = new AccessorFactory(setAccessorFactory, getAccessorFactory);

            TypeHandlerFactory typeHandlerFactory = new TypeHandlerFactory();
            TypeAlias          alias = new TypeAlias("MEMORY", typeof(PerpetualCache));

            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("Perpetual", typeof(PerpetualCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("LRU", typeof(LruCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("Lru", typeof(LruCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("FIFO", typeof(FifoCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("Fifo", typeof(FifoCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("Weak", typeof(WeakCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("WEAK", typeof(WeakCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("MemCached", typeof(MemCached));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("MEMCACHED", typeof(MemCached));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("AnsiStringTypeHandler", typeof(AnsiStringTypeHandler));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            //将以上类信息存入到modelStore中 实质性工作
            modelStore.DataExchangeFactory = new DataExchangeFactory(typeHandlerFactory, objectFactory, accessorFactory);

            if (sessionStore == null)
            {
                sessionStore = SessionStoreFactory.GetSessionStore(modelStore.Id);
            }
            //将ISessionStore类对象存入modelStore中 准备数据库的连接 与 事物操作功能  实质性工作
            modelStore.SessionStore = sessionStore;

            //初始化DefaultModelBuilder的成员变量
            deSerializerFactory = new DeSerializerFactory(modelStore);

            //设置一个空类型的参数映射类
            ParameterMap emptyParameterMap = new ParameterMap(
                ConfigConstants.EMPTY_PARAMETER_MAP,
                string.Empty,
                string.Empty,
                typeof(string),
                modelStore.DataExchangeFactory.GetDataExchangeForClass(null), //获得ComplexDataExchange对象
                false);

            //向参数字典中添加一个空参数类
            modelStore.AddParameterMap(emptyParameterMap);

            //完成了对DefaultModelBuilder成员变量dbProviderFactory的初始化,其中完成了对provider使用中节点的类初始化
            BuildProviders(store);

            //完成了对DefaultModelBuilder成员变量IDataSource的初始化,包括数据库的名字 连接字符串 以及DbPrivder类
            BuildDataSource(store);

            if (sessionFactory == null)
            {
                sessionFactory = new DefaultSessionFactory(
                    dataSource,
                    modelStore.SessionStore,
                    new DefaultTransactionManager(new AdoTransactionFactory()));
            }
            //初始化modelStore中的ISessionFactory变量
            modelStore.SessionFactory = sessionFactory;

            //将具体SQLXML配置文件中的Alias节点对应的别名信息写入到modelStore中的TypeHandlerFactory的字典中
            BuildTypeAlias(store);

            //将TypeHanders下的子节点信息放入 modelStore.DataExchangeFactory.TypeHandlerFactory中的字典当中
            BuildTypeHandlers(store);

            //将cacheModels下子节点cacheModel加入到modelStore的CacheModel字典中
            BuildCacheModels(store);
            //将resultMap节点信息添加到modelStore的resultMaps中
            BuildResultMaps(store);

            //为resultMapping属性所在的节点信息类设置对应的ResultProperty
            for (int i = 0; i < nestedProperties.Count; i++)
            {
                ResultProperty property = nestedProperties[i];
                property.NestedResultMap = modelStore.GetResultMap(property.NestedResultMapName);
            }

            for (int i = 0; i < discriminators.Count; i++)
            {
                //完成对discriminator类中对字典的初始化
                discriminators[i].Initialize(modelStore);
            }
            //将parameter节点信息添加到modelStore类中的parameterMaps字典中
            BuildParameterMaps(store);

            //将statements下的节点添加到modelStore的statements的字典中
            BuildMappedStatements(store, configurationSetting);

            for (int i = 0; i < store.CacheModels.Length; i++)
            {
                CacheModel cacheModel = modelStore.GetCacheModel(store.CacheModels[i].Id);

                for (int j = 0; j < cacheModel.StatementFlushNames.Count; j++)
                {
                    string           statement       = cacheModel.StatementFlushNames[j];
                    IMappedStatement mappedStatement = modelStore.GetMappedStatement(statement);
                    if (mappedStatement != null)
                    {
                        //为IMappedStatement类的Executed制定委托事件  目的是清空缓存
                        cacheModel.RegisterTriggerStatement(mappedStatement);
                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("Registering trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]");
                        }
                    }
                    else
                    {
                        if (logger.IsWarnEnabled)
                        {
                            logger.Warn("Unable to register trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]. Statement does not exist.");
                        }
                    }
                }
            }

            if (logger.IsInfoEnabled)
            {
                logger.Info("Model Store");
                logger.Info(modelStore.ToString());
            }
        }
コード例 #6
0
 /// <summary>
 /// Sets value of the specified <see cref="ResultProperty"/> on the target object
 /// when a 'resultMapping' attribute exists
 /// on the <see cref="ResultProperty"/>.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="resultMap">The result map.</param>
 /// <param name="mapping">The ResultProperty.</param>
 /// <param name="target">The target.</param>
 /// <param name="reader">The reader.</param>
 /// <param name="keys">The keys</param>
 public void Set(RequestScope request, IResultMap resultMap,
                 ResultProperty mapping, ref object target, IDataReader reader, object keys)
 {
     Get(request, resultMap, mapping, ref target, reader);
 }
コード例 #7
0
ファイル: BaseDataExchange.cs プロジェクト: rasitha1/SqlBatis
 /// <summary>
 /// Sets the value to the result property.
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="target"></param>
 /// <param name="dataBaseValue"></param>
 public abstract void SetData(ref object target, ResultProperty mapping, object dataBaseValue);
コード例 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="dataReader"></param>
        /// <returns></returns>
        public override object GetValueByName(ResultProperty mapping, IDataReader dataReader)
        {
            int index = dataReader.GetOrdinal(mapping.ColumnName);

            return(dataReader.IsDBNull(index) ? (object)DBNull.Value : dataReader.GetDecimal(index));
        }
コード例 #9
0
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/>.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns></returns>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader);
            DataRow    dataRow   = (DataRow)resultObject;
            DataTable  dataTable = dataRow.Table;

            if (dataTable.Columns.Count == 0)
            {
                // Builds and adss the columns
                dataTable.TableName = resultMap.Id;
                if (resultMap is AutoResultMap)
                {
                    for (int index = 0; index < reader.FieldCount; index++)
                    {
                        string columnName = reader.GetName(index);
                        Type   type       = reader.GetFieldType(index);

                        DataColumn column = new DataColumn();
                        column.DataType   = type;
                        column.ColumnName = columnName;
                        dataTable.Columns.Add(column);
                    }
                }
                else
                {
                    for (int index = 0; index < resultMap.Properties.Count; index++)
                    {
                        ResultProperty property = resultMap.Properties[index];
                        DataColumn     column   = new DataColumn();
                        if (property.CLRType.Length > 0)
                        {
                            column.DataType = request.DataExchangeFactory.TypeHandlerFactory.GetType(property.CLRType);
                        }
                        else
                        {
                            object value = property.GetDataBaseValue(reader);
                            if (value == null)
                            {
                                int columnIndex = reader.GetOrdinal(property.ColumnName);
                                column.DataType = reader.GetFieldType(columnIndex);
                            }
                            else
                            {
                                column.DataType = value.GetType();
                            }
                        }
                        column.ColumnName = property.PropertyName;

                        dataTable.Columns.Add(column);
                    }
                }
            }


            if (resultMap is AutoResultMap)
            {
                for (int index = 0; index < reader.FieldCount; index++)
                {
                    string       propertyName = reader.GetName(index);
                    int          columnIndex  = index;
                    ITypeHandler typeHandler  = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(index));

                    ResultProperty property = new ResultProperty(
                        propertyName,
                        string.Empty,
                        columnIndex,
                        string.Empty,
                        string.Empty,
                        string.Empty,
                        false,
                        string.Empty,
                        null,
                        string.Empty,
                        typeof(DataRow),
                        request.DataExchangeFactory,
                        typeHandler);

                    object value = property.GetDataBaseValue(reader);
                    if (value == null)
                    {
                        value = DBNull.Value;
                    }
                    dataRow[property.PropertyName] = value;
                }
            }
            else
            {
                for (int index = 0; index < resultMap.Properties.Count; index++)
                {
                    ResultProperty property = resultMap.Properties[index];
                    object         value    = property.GetDataBaseValue(reader);
                    if (value == null)
                    {
                        value = DBNull.Value;
                    }
                    dataRow[property.PropertyName] = value;
                }
            }

            return(null);
        }
コード例 #10
0
 /// <summary>
 /// Sets the value to the result property.
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="target"></param>
 /// <param name="dataBaseValue"></param>
 public override void SetData(ref object target, ResultProperty mapping, object dataBaseValue)
 {
     target = dataBaseValue;
 }
コード例 #11
0
        public static ResultPropertyCollection Build(DataExchangeFactory dataExchangeFactory, IDataReader reader, ref object resultObject)
        {
            Type type = resultObject.GetType();
            ResultPropertyCollection propertys = new ResultPropertyCollection();

            try
            {
                string[]  writeableMemberNames = ReflectionInfo.GetInstance(type).GetWriteableMemberNames();
                Hashtable hashtable            = new Hashtable();
                int       length = writeableMemberNames.Length;
                for (int i = 0; i < length; i++)
                {
                    ISetAccessor accessor = dataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(type, writeableMemberNames[i]);
                    hashtable.Add(writeableMemberNames[i], accessor);
                }
                DataTable schemaTable = reader.GetSchemaTable();
                int       count       = schemaTable.Rows.Count;
                for (int j = 0; j < count; j++)
                {
                    string         memberName  = schemaTable.Rows[j][0].ToString();
                    ISetAccessor   setAccessor = hashtable[memberName] as ISetAccessor;
                    ResultProperty property    = new ResultProperty {
                        ColumnName  = memberName,
                        ColumnIndex = j
                    };
                    if (resultObject is Hashtable)
                    {
                        property.PropertyName = memberName;
                        propertys.Add(property);
                    }
                    Type memberTypeForSetter = null;
                    if (setAccessor == null)
                    {
                        try
                        {
                            memberTypeForSetter = ObjectProbe.GetMemberTypeForSetter(resultObject, memberName);
                        }
                        catch
                        {
                            _logger.Error("The column [" + memberName + "] could not be auto mapped to a property on [" + resultObject.ToString() + "]");
                        }
                    }
                    else
                    {
                        memberTypeForSetter = setAccessor.MemberType;
                    }
                    if ((memberTypeForSetter != null) || (setAccessor != null))
                    {
                        property.PropertyName = (setAccessor != null) ? setAccessor.Name : memberName;
                        if (setAccessor != null)
                        {
                            property.Initialize(dataExchangeFactory.TypeHandlerFactory, setAccessor);
                        }
                        else
                        {
                            property.TypeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(memberTypeForSetter);
                        }
                        property.PropertyStrategy = PropertyStrategyFactory.Get(property);
                        propertys.Add(property);
                    }
                }
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error automapping columns. Cause: " + exception.Message, exception);
            }
            return(propertys);
        }
コード例 #12
0
        public void Set(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader, object keys)
        {
            object dataBaseValue = this.Get(request, resultMap, mapping, ref target, reader);

            resultMap.SetValueOfProperty(ref target, mapping, dataBaseValue);
        }
コード例 #13
0
 /// <summary>
 /// Stores ResultProperty from which the NestedResultMap property must be resolved
 /// Delay resolution until all the ResultMap are processed.
 /// </summary>
 /// <param name="property">The property.</param>
 private void WaitResultPropertyResolution(ResultProperty property)
 {
     nestedProperties.Add(property);
 }
コード例 #14
0
        /// <summary>
        /// Builds the the iBATIS core model (statement, alias, resultMap, parameterMap, dataSource)
        /// from an <see cref="IConfigurationStore"/> and store all the refrences in an <see cref="IModelStore"/> .
        /// </summary>
        /// <param name="configurationSetting">The configuration setting.</param>
        /// <param name="store">The configuration store.</param>
        /// <returns>The model store</returns>
        public virtual void BuildModel(ConfigurationSetting configurationSetting, IConfigurationStore store)
        {
            IObjectFactory      objectFactory      = null;
            IGetAccessorFactory getAccessorFactory = null;
            ISetAccessorFactory setAccessorFactory = null;
            ISessionFactory     sessionFactory     = null;
            ISessionStore       sessionStore       = null;

            if (configurationSetting != null)
            {
                objectFactory          = configurationSetting.ObjectFactory;
                setAccessorFactory     = configurationSetting.SetAccessorFactory;
                getAccessorFactory     = configurationSetting.GetAccessorFactory;
                dataSource             = configurationSetting.DataSource;
                sessionFactory         = configurationSetting.SessionFactory;
                sessionStore           = configurationSetting.SessionStore;
                dynamicSqlEngine       = configurationSetting.DynamicSqlEngine;
                isCacheModelsEnabled   = configurationSetting.IsCacheModelsEnabled;
                useStatementNamespaces = configurationSetting.UseStatementNamespaces;
                useReflectionOptimizer = configurationSetting.UseReflectionOptimizer;
                preserveWhitespace     = configurationSetting.PreserveWhitespace;
            }

            // Xml setting override code setting
            LoadSetting(store);

            if (objectFactory == null)
            {
                objectFactory = new ObjectFactory(useReflectionOptimizer);
            }
            if (setAccessorFactory == null)
            {
                setAccessorFactory = new SetAccessorFactory(useReflectionOptimizer);
            }
            if (getAccessorFactory == null)
            {
                getAccessorFactory = new GetAccessorFactory(useReflectionOptimizer);
            }
            AccessorFactory accessorFactory = new AccessorFactory(setAccessorFactory, getAccessorFactory);

            TypeHandlerFactory typeHandlerFactory = new TypeHandlerFactory();
            TypeAlias          alias = new TypeAlias("MEMORY", typeof(PerpetualCache));

            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("Perpetual", typeof(PerpetualCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("LRU", typeof(LruCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("Lru", typeof(LruCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("FIFO", typeof(FifoCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("Fifo", typeof(FifoCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("Weak", typeof(WeakCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);
            alias = new TypeAlias("WEAK", typeof(WeakCache));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            alias = new TypeAlias("AnsiStringTypeHandler", typeof(AnsiStringTypeHandler));
            typeHandlerFactory.AddTypeAlias(alias.Id, alias);

            modelStore.DataExchangeFactory = new DataExchangeFactory(typeHandlerFactory, objectFactory, accessorFactory);

            if (sessionStore == null)
            {
                sessionStore = SessionStoreFactory.GetSessionStore(modelStore.Id);
            }
            modelStore.SessionStore = sessionStore;

            deSerializerFactory = new DeSerializerFactory(modelStore);

            ParameterMap emptyParameterMap = new ParameterMap(
                ConfigConstants.EMPTY_PARAMETER_MAP,
                string.Empty,
                string.Empty,
                typeof(string),
                modelStore.DataExchangeFactory.GetDataExchangeForClass(null),
                false);

            modelStore.AddParameterMap(emptyParameterMap);

            BuildProviders(store);
            BuildDataSource(store);

            if (sessionFactory == null)
            {
                sessionFactory = new DefaultSessionFactory(
                    dataSource,
                    modelStore.SessionStore,
                    new DefaultTransactionManager(new AdoTransactionFactory()));
            }
            modelStore.SessionFactory = sessionFactory;

            BuildTypeAlias(store);
            BuildTypeHandlers(store);
            BuildCacheModels(store);
            BuildResultMaps(store);

            for (int i = 0; i < nestedProperties.Count; i++)
            {
                ResultProperty property = nestedProperties[i];
                property.NestedResultMap = modelStore.GetResultMap(property.NestedResultMapName);
            }

            for (int i = 0; i < discriminators.Count; i++)
            {
                discriminators[i].Initialize(modelStore);
            }
            BuildParameterMaps(store);
            BuildMappedStatements(store, configurationSetting);

            for (int i = 0; i < store.CacheModels.Length; i++)
            {
                CacheModel cacheModel = modelStore.GetCacheModel(store.CacheModels[i].Id);

                for (int j = 0; j < cacheModel.StatementFlushNames.Count; j++)
                {
                    string           statement       = cacheModel.StatementFlushNames[j];
                    IMappedStatement mappedStatement = modelStore.GetMappedStatement(statement);
                    if (mappedStatement != null)
                    {
                        cacheModel.RegisterTriggerStatement(mappedStatement);
                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("Registering trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]");
                        }
                    }
                    else
                    {
                        if (logger.IsWarnEnabled)
                        {
                            logger.Warn("Unable to register trigger statement [" + statement + "] to cache model [" + cacheModel.Id + "]. Statement does not exist.");
                        }
                    }
                }
            }

            if (logger.IsInfoEnabled)
            {
                logger.Info("Model Store");
                logger.Info(modelStore.ToString());
            }
        }
コード例 #15
0
        /// <summary>
        /// Builds a <see cref="ResultPropertyCollection"/> for an <see cref="AutoResultMap"/>.
        /// </summary>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public static ResultPropertyCollection Build <T>(DataExchangeFactory dataExchangeFactory,
                                                         IDataReader reader,
                                                         ref T resultObject)
        {
            Type targetType = resultObject.GetType();
            ResultPropertyCollection properties = new ResultPropertyCollection();

            try
            {
                // Get all PropertyInfo from the resultObject properties
                var reflectionInfo = ReflectionInfo.GetInstance(targetType);
                var membersName    = reflectionInfo.GetWriteableMemberNames();

                var propertyMap = new Hashtable();
                int length      = membersName.Length;
                for (int i = 0; i < length; i++)
                {
                    ISetAccessorFactory setAccessorFactory = dataExchangeFactory.AccessorFactory.SetAccessorFactory;
                    ISetAccessor        setAccessor        = setAccessorFactory.CreateSetAccessor(targetType, membersName[i]);
                    propertyMap.Add(membersName[i], setAccessor);
                }

                // Get all column Name from the reader
                // and build a resultMap from with the help of the PropertyInfo[].
                var dataColumn = reader.GetSchemaTable();
                if (dataColumn == null)
                {
                    return(new ResultPropertyCollection());
                }
                int count = dataColumn.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    var columnName         = dataColumn.Rows[i][0].ToString();
                    var matchedSetAccessor = propertyMap[columnName] as ISetAccessor;

                    var property = new ResultProperty {
                        ColumnName = columnName, ColumnIndex = i
                    };

                    if (resultObject is Hashtable)
                    {
                        property.PropertyName = columnName;
                        properties.Add(property);
                    }

                    Type propertyType = null;

                    if (matchedSetAccessor == null)
                    {
                        try
                        {
                            propertyType = ObjectProbe.GetMemberTypeForSetter(resultObject, columnName);
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                    else
                    {
                        propertyType = matchedSetAccessor.MemberType;
                    }

                    if (propertyType != null || matchedSetAccessor != null)
                    {
                        property.PropertyName = (matchedSetAccessor != null ? matchedSetAccessor.Name : columnName);
                        if (matchedSetAccessor != null)
                        {
                            property.Initialize(dataExchangeFactory.TypeHandlerFactory, matchedSetAccessor);
                        }
                        else
                        {
                            property.TypeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType);
                        }

                        property.PropertyStrategy = PropertyStrategyFactory.Get(property);
                        properties.Add(property);
                    }
                }
            }
            catch (Exception e)
            {
                throw new DataMapperException("Error automapping columns. Cause: " + e.Message, e);
            }

            return(properties);
        }
コード例 #16
0
ファイル: ReaderAutoMapper.cs プロジェクト: yhh1234/web_test
        /// <summary>
        /// Builds a <see cref="ResultPropertyCollection"/> for an <see cref="AutoResultMap"/>.
        /// 根据reader的字段从resultObject类中获取对应的属性类集合
        /// </summary>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        public static ResultPropertyCollection Build(DataExchangeFactory dataExchangeFactory,
                                                     IDataReader reader,
                                                     ref object resultObject)
        {
            Type targetType = resultObject.GetType();
            ResultPropertyCollection properties = new ResultPropertyCollection();

            try
            {
                // Get all PropertyInfo from the resultObject properties
                ReflectionInfo reflectionInfo = ReflectionInfo.GetInstance(targetType);
                string[]       membersName    = reflectionInfo.GetWriteableMemberNames();

                //为结果类resultObject的属性设置对应的访问与获取的函数类
                IDictionary <string, ISetAccessor> propertyMap = new Dictionary <string, ISetAccessor>();
                int length = membersName.Length;
                for (int i = 0; i < length; i++)
                {
                    ISetAccessorFactory setAccessorFactory = dataExchangeFactory.AccessorFactory.SetAccessorFactory;
                    ISetAccessor        setAccessor        = setAccessorFactory.CreateSetAccessor(targetType, membersName[i]);
                    propertyMap.Add(membersName[i], setAccessor);
                }

                // Get all column Name from the reader
                // and build a resultMap from with the help of the PropertyInfo[].
                DataTable dataColumn = reader.GetSchemaTable();
                int       count      = dataColumn.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    string propertyName = string.Empty;
                    string columnName   = dataColumn.Rows[i][0].ToString();
                    //获取当前列字段对应的设置访问类
                    ISetAccessor matchedSetAccessor = null;
                    propertyMap.TryGetValue(columnName, out matchedSetAccessor);

                    int columnIndex = i;

                    if (resultObject is Hashtable)
                    {
                        propertyName = columnName;
                        //将当前列字段信息转化为ResultProperty类
                        ResultProperty property = new ResultProperty(
                            propertyName,
                            columnName,
                            columnIndex,
                            string.Empty,
                            string.Empty,
                            string.Empty,
                            false,
                            string.Empty,
                            null,
                            string.Empty,
                            targetType,
                            dataExchangeFactory,
                            null);
                        properties.Add(property);
                    }
                    //获取当前列字段的类型
                    Type propertyType = null;

                    if (matchedSetAccessor == null)
                    {
                        try
                        {
                            propertyType = ObjectProbe.GetMemberTypeForSetter(resultObject, columnName);
                        }
                        catch
                        {
                            _logger.Error("The column [" + columnName + "] could not be auto mapped to a property on [" + resultObject + "]");
                        }
                    }
                    else
                    {
                        propertyType = matchedSetAccessor.MemberType;
                    }
                    //获取当前列字段的名称及对应的处理类
                    if (propertyType != null || matchedSetAccessor != null)
                    {
                        propertyName = (matchedSetAccessor != null ? matchedSetAccessor.Name : columnName);
                        ITypeHandler typeHandler = null;

                        if (matchedSetAccessor != null)
                        {
                            //property.Initialize(dataExchangeFactory.TypeHandlerFactory, matchedSetAccessor);
                            typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(matchedSetAccessor.MemberType);
                        }
                        else
                        {
                            typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType);
                        }

                        //property.PropertyStrategy = PropertyStrategyFactory.Get(property);

                        ResultProperty property = new ResultProperty(
                            propertyName,
                            columnName,
                            columnIndex,
                            string.Empty,
                            string.Empty,
                            string.Empty,
                            false,
                            string.Empty,
                            null,
                            string.Empty,
                            targetType,
                            dataExchangeFactory,
                            typeHandler);

                        properties.Add(property);
                    }
                }
            }
            catch (Exception e)
            {
                throw new DataMapperException("Error automapping columns. Cause: " + e.Message, e);
            }

            return(properties);
        }
コード例 #17
0
 /// <summary>
 /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="resultMap">The result map.</param>
 /// <param name="mapping">The mapping.</param>
 /// <param name="reader">The reader.</param>
 /// <param name="target">The target object</param>
 public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
 {
     throw new NotSupportedException("Get method on ResultMapStrategy is not supported");
 }
コード例 #18
0
        /// <summary>
        /// Gets a column value by the name
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="dataReader"></param>
        /// <returns></returns>
        public override object GetValueByName(ResultProperty mapping, IDataReader dataReader)
        {
            int index = dataReader.GetOrdinal(mapping.ColumnName);

            return(GetValue(index, dataReader));
        }
コード例 #19
0
        /// <summary>
        /// Gets the value of the specified <see cref="ResultProperty"/> that must be set on the target object.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultMap">The result map.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="target">The target object</param>
        public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
        {
            // The property is a IList
            IList list = null;

            // Get the IList property
            object property = ObjectProbe.GetMemberValue(target, mapping.PropertyName,
                                                         request.DataExchangeFactory.AccessorFactory);

            if (property == null)// Create the list if need
            {
                property = mapping.ListFactory.CreateInstance(null);
                mapping.SetAccessor.Set(target, property);
            }
            list = (IList)property;

            object     result             = null;
            IResultMap propertyRresultMap = mapping.NestedResultMap.ResolveSubMap(reader);

            if (propertyRresultMap.GroupByProperties.Count > 0)
            {
                string uniqueKey = GetUniqueKey(propertyRresultMap, request, reader);

                // Gets the [key, result object] already build
                IDictionary buildObjects = request.GetUniqueKeys(propertyRresultMap);

                if (buildObjects != null && buildObjects.Contains(uniqueKey))
                {
                    // Unique key is already known, so get the existing result object and process additional results.
                    result = buildObjects[uniqueKey];

                    //In some cases (nested groupings) our object may be null, so there is
                    //no point in going on
                    if (result != null)
                    {
                        // process resulMapping attribute which point to a groupBy attribute
                        for (int index = 0; index < propertyRresultMap.Properties.Count; index++)
                        {
                            ResultProperty resultProperty = propertyRresultMap.Properties[index];
                            if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy)
                            {
                                resultProperty.PropertyStrategy.Set(request, propertyRresultMap, resultProperty, ref result, reader, null);
                            }
                        }
                    }
                    result = SKIP;
                }
                else if (uniqueKey == null || buildObjects == null || !buildObjects.Contains(uniqueKey))
                {
                    // Unique key is NOT known, so create a new result object and then process additional results.
                    result = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);

                    if (buildObjects == null)
                    {
                        buildObjects = new Hashtable();
                        request.SetUniqueKeys(propertyRresultMap, buildObjects);
                    }
                    buildObjects[uniqueKey] = result;
                }
            }
            else // Last resultMap have no groupBy attribute
            {
                result = _resultMapStrategy.Get(request, resultMap, mapping, ref target, reader);
            }


            if ((result != null) && (result != SKIP))
            {
                list.Add(result);
            }

            return(result);
        }
コード例 #20
0
 /// <summary>
 /// Gets a column value by the index
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="dataReader"></param>
 /// <returns></returns>
 public override object GetValueByIndex(ResultProperty mapping, IDataReader dataReader)
 {
     return(GetValue(mapping.ColumnIndex, dataReader));
 }
コード例 #21
0
 /// <summary>
 /// Sets the value to the result property.
 /// </summary>
 /// <param name="mapping"></param>
 /// <param name="target"></param>
 /// <param name="dataBaseValue"></param>
 public override void SetData(ref object target, ResultProperty mapping, object dataBaseValue)
 {
     ObjectProbe.SetMemberValue(target, mapping.PropertyName, dataBaseValue,
                                DataExchangeFactory.ObjectFactory,
                                DataExchangeFactory.AccessorFactory);
 }
コード例 #22
0
        /// <summary>
        /// Processes the specified <see cref="IDataReader"/>
        /// when no resultClass or resultMap attribute are specified.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <returns></returns>
        public object Process(RequestScope request, ref IDataReader reader, object resultObject)
        {
            object outObject = resultObject;

            if (reader.FieldCount == 1)
            {
                const string propertyName = "value";
                const int    columnIndex  = 0;
                ITypeHandler typeHandler  = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(0));

                ResultProperty property = new ResultProperty(
                    propertyName,
                    string.Empty,
                    columnIndex,
                    string.Empty,
                    string.Empty,
                    string.Empty,
                    false,
                    string.Empty,
                    null,
                    string.Empty,
                    null,
                    request.DataExchangeFactory,
                    typeHandler);

                //ResultProperty property = new ResultProperty();
                //property.PropertyName = "value";
                //property.ColumnIndex = 0;
                //property.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(0));
                outObject = property.GetDataBaseValue(reader);
            }
            else if (reader.FieldCount > 1)
            {
                object[] newOutObject = new object[reader.FieldCount];
                int      count        = reader.FieldCount;
                for (int i = 0; i < count; i++)
                {
                    const string propertyName = "value";
                    int          columnIndex  = i;
                    ITypeHandler typeHandler  = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(i));

                    ResultProperty property = new ResultProperty(
                        propertyName,
                        string.Empty,
                        columnIndex,
                        string.Empty,
                        string.Empty,
                        string.Empty,
                        false,
                        string.Empty,
                        null,
                        string.Empty,
                        null,
                        request.DataExchangeFactory,
                        typeHandler);

                    //ResultProperty property = new ResultProperty();
                    //property.PropertyName = "value";
                    //property.ColumnIndex = i;
                    //property.TypeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(i));

                    newOutObject[i] = property.GetDataBaseValue(reader);
                }

                outObject = newOutObject;
            }
            //else
            //{
            //    // do nothing if 0 fields
            //}

            return(outObject);
        }
コード例 #23
0
        /// <summary>
        /// Gets a column value by the index
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="dataReader"></param>
        /// <returns></returns>
        public override object GetValueByIndex(ResultProperty mapping, IDataReader dataReader)
        {
            IResultGetter getter = new ResultGetterImpl(dataReader, mapping.ColumnIndex);

            return(_callback.GetResult(getter));
        }
コード例 #24
0
        /// <summary>
        /// Builds a <see cref="ResultPropertyCollection"/> for an <see cref="AutoResultMap"/>.
        /// </summary>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="resultObject">The result object.</param>
        /// <param name="isUseAutoMapCompatibilityMode">The result object.</param>
        public static ResultPropertyCollection Build(DataExchangeFactory dataExchangeFactory,
                                                     IDataReader reader,
                                                     ref object resultObject,
                                                     bool isUseAutoMapCompatibilityMode)
        {
            Type targetType = resultObject.GetType();
            ResultPropertyCollection properties = new ResultPropertyCollection();

            try
            {
                // Get all PropertyInfo from the resultObject properties
                ReflectionInfo reflectionInfo = ReflectionInfo.GetInstance(targetType);
                string[]       membersName    = reflectionInfo.GetPublicWriteableMemberNames();
                Hashtable      propertyMap    = new Hashtable();
                int            length         = membersName.Length;
                for (int i = 0; i < length; i++)
                {
                    var memberName = membersName[i];
                    ISetAccessorFactory setAccessorFactory = dataExchangeFactory.AccessorFactory.SetAccessorFactory;
                    ISetAccessor        setAccessor        = setAccessorFactory.CreateSetAccessor(targetType, memberName);
                    var menberInfo = reflectionInfo.GetSetter(memberName);
                    //ʹ�ñ�ע������Ϊ�к����ԵĹ�ϵ
                    var attrs = menberInfo.GetCustomAttribute <AliasAttribute>();
                    if (attrs != null)
                    {
                        memberName = attrs.Name;
                    }
                    else if (isUseAutoMapCompatibilityMode)//���ü���ģʽ
                    {
                        memberName = memberName.Replace("_", "").ToLower();
                    }
                    propertyMap.Add(memberName, setAccessor);
                }

                // Get all column Name from the reader
                // and build a resultMap from with the help of the PropertyInfo[].
                DataTable dataColumn = reader.GetSchemaTable();
                int       count      = dataColumn.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    string       columnName         = dataColumn.Rows[i][0].ToString();
                    ISetAccessor matchedSetAccessor = propertyMap[columnName] as ISetAccessor;
                    if (matchedSetAccessor == null && isUseAutoMapCompatibilityMode)
                    {
                        columnName         = columnName.Replace("_", "").ToLower();
                        matchedSetAccessor = propertyMap[columnName] as ISetAccessor;
                    }

                    ResultProperty property = new ResultProperty();
                    property.ColumnName  = columnName;
                    property.ColumnIndex = i;

                    if (resultObject is Hashtable)
                    {
                        property.PropertyName = columnName;
                        properties.Add(property);
                    }

                    Type propertyType = null;

                    if (matchedSetAccessor == null)
                    {
                        try
                        {
                            propertyType = ObjectProbe.GetMemberTypeForSetter(resultObject, columnName);
                        }
                        catch
                        {
                            _logger.Error("The column [" + columnName + "] could not be auto mapped to a property on [" + resultObject.ToString() + "]");
                        }
                    }
                    else
                    {
                        propertyType = matchedSetAccessor.MemberType;
                    }

                    if (propertyType != null || matchedSetAccessor != null)
                    {
                        property.PropertyName = (matchedSetAccessor != null ? matchedSetAccessor.Name : columnName);
                        if (matchedSetAccessor != null)
                        {
                            property.Initialize(dataExchangeFactory.TypeHandlerFactory, matchedSetAccessor);
                        }
                        else
                        {
                            property.TypeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(propertyType);
                        }

                        property.PropertyStrategy = PropertyStrategyFactory.Get(property);
                        properties.Add(property);
                    }
                }
            }
            catch (Exception e)
            {
                throw new DataMapperException("Error automapping columns. Cause: " + e.Message, e);
            }

            return(properties);
        }