/// <summary> /// Initializes a new instance of the <see cref="Update"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="parameterClass">The parameter class.</param> /// <param name="parameterMap">The parameter map.</param> /// <param name="remapResults">if set to <c>true</c> [remap results].</param> /// <param name="extends">The extends.</param> /// <param name="sqlSource">The SQL source.</param> /// <param name="preserveWhitespace">Preserve whitespace.</param> public Update( string id, Type parameterClass, ParameterMap parameterMap, bool remapResults, string extends, ISqlSource sqlSource, bool preserveWhitespace) : base(id, parameterClass, parameterMap, null, new ResultMapCollection(), null, null, null, remapResults, extends, sqlSource, preserveWhitespace) {}
/// <summary> /// Initializes a new instance of the <see cref="Delete"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="parameterClass">The parameter class.</param> /// <param name="parameterMap">The parameter map.</param> /// <param name="resultClass">The result class.</param> /// <param name="resultMaps">The result maps.</param> /// <param name="listClass">The list class.</param> /// <param name="listClassFactory">The list class factory.</param> /// <param name="cacheModel">The cache model.</param> /// <param name="remapResults">if set to <c>true</c> [remap results].</param> /// <param name="extends">The extends.</param> /// <param name="sqlSource">The SQL source.</param> /// <param name="preserveWhitespace">Preserve whitespace.</param> public Delete( string id, Type parameterClass, ParameterMap parameterMap, Type resultClass, ResultMapCollection resultMaps, Type listClass, IFactory listClassFactory, CacheModel cacheModel, bool remapResults, string extends, ISqlSource sqlSource, bool preserveWhitespace ) : base(id, parameterClass, parameterMap, resultClass, resultMaps, listClass, listClassFactory, cacheModel, remapResults, extends, sqlSource, preserveWhitespace) {}
/// <summary> /// Builds the parameter properties. /// </summary> /// <param name="parameterMap">The parameter map.</param> /// <param name="parameterMapConfig">The parameter map config.</param> private void BuildParameterProperties(ParameterMap parameterMap, IConfiguration parameterMapConfig) { ConfigurationCollection parametersConfig = parameterMapConfig.Children.Find(ConfigConstants.ELEMENT_PARAMETER); for (int i = 0; i < parametersConfig.Count; i++) { IConfiguration parameterConfig = parametersConfig[i]; ParameterProperty property = null; try { property = ParameterPropertyDeSerializer.Deserialize(modelStore.DataExchangeFactory, parameterMap.Class, parameterConfig); } catch (Exception e) { throw new DataMapperException("In ParameterMap (" + parameterMap.Id + ") can't build the parameter property: " + ConfigurationUtils.GetStringAttribute(parameterConfig.Attributes, ConfigConstants.ATTRIBUTE_PROPERTY) + ". Cause " + e.Message, e); } parameterMap.AddParameterProperty(property); } }
/// <summary> /// Adds a (named) ParameterMap. /// </summary> /// <param name="parameterMap">the ParameterMap to add</param> public void AddParameterMap(ParameterMap parameterMap) { if (parameterMaps.ContainsKey(parameterMap.Id)) { throw new DataMapperException("The DataMapper already contains an ParameterMap named " + parameterMap.Id); } parameterMaps.Add(parameterMap.Id, parameterMap); }
/// <summary> /// For store procedure, auto discover IDataParameters for stored procedures at run-time. /// </summary> private void DiscoverParameter(ParameterMap parameterMap) { // pull the parameters for this stored procedure from the parameter cache // (or discover them & populate the cache) IDataParameter[] commandParameters = dbHelperParameterCache.GetSpParameterSet(session, commandText); preparedStatement.DbParameters = new IDbDataParameter[commandParameters.Length]; int start = dbProvider.ParameterPrefix.Length; for(int i=0; i< commandParameters.Length;i++) { IDbDataParameter dataParameter = (IDbDataParameter)commandParameters[i]; if (dbProvider.UseParameterPrefixInParameter == false) { if (dataParameter.ParameterName.StartsWith(dbProvider.ParameterPrefix)) { dataParameter.ParameterName = dataParameter.ParameterName.Substring(start); } } preparedStatement.DbParameters[i] = dataParameter; if (dataParameter.Direction == ParameterDirection.Output || dataParameter.Direction == ParameterDirection.InputOutput) { parameterMap.HasOutputParameter = true; } } // Re-sort DbParameters to match order used in the parameterMap IDbDataParameter[] sortedDbParameters = new IDbDataParameter[parameterMap.Properties.Count]; for (int i = 0; i < parameterMap.Properties.Count; i++) { sortedDbParameters[i] = Search(preparedStatement.DbParameters, parameterMap.Properties[i], i); preparedStatement.DbParametersName.Add(sortedDbParameters[i].ParameterName); } preparedStatement.DbParameters = sortedDbParameters; }
/// <summary> /// Build inline paremeterMap /// </summary> /// <param name="statement">The statement.</param> /// <param name="sqlCommandText">The SQL command text.</param> /// <param name="newSqlCommandText">The newsql command text.</param> /// <returns></returns> public ParameterMap BuildInlineParemeterMap(IStatement statement, string sqlCommandText, out string newSqlCommandText) { newSqlCommandText = sqlCommandText; ParameterMap map = null; // Check the inline parameter if (statement.ParameterMap == null) { // Build a Parametermap with the inline parameters. // if they exist. Then delete inline infos from sqltext. SqlText sqlText = InlineParameterMapParser.ParseInlineParameterMap(modelStore.DataExchangeFactory, statement.Id, statement, newSqlCommandText); if (sqlText.Parameters.Length > 0) { string id = statement.Id + "-InLineParameterMap"; string className = string.Empty; Type classType = null; IDataExchange dataExchange = null; if (statement.ParameterClass != null) { className = statement.ParameterClass.Name; classType = statement.ParameterClass; //dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(classType); } if (statement.ParameterClass == null && sqlText.Parameters.Length == 1 && sqlText.Parameters[0].PropertyName == "value") //#value# parameter with no parameterClass attribut { dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(typeof(int)); //Get the primitiveDataExchange } else { dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(statement.ParameterClass); } map = new ParameterMap( id, className, string.Empty, classType, dataExchange, modelStore.SessionFactory.DataSource.DbProvider.UsePositionalParameters ) ; int lenght = sqlText.Parameters.Length; for (int index = 0; index < lenght; index++) { map.AddParameterProperty(sqlText.Parameters[index]); } } newSqlCommandText = sqlText.Text; } if (statement is Procedure) { newSqlCommandText = newSqlCommandText.Replace(MARK_TOKEN, string.Empty).Replace(COMMA_TOKEN, string.Empty); } // newSqlCommandText = newSqlCommandText.Trim(); return(map); }
/// <summary> /// Initializes a new instance of the <see cref="RequestScope"/> class. /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="session">The session.</param> /// <param name="statement">The statement</param> public RequestScope( DataExchangeFactory dataExchangeFactory, ISession session, IStatement statement ) { errorContext = new ErrorContext(); this.statement = statement; parameterMap = statement.ParameterMap; this.session = session; this.dataExchangeFactory = dataExchangeFactory; id = GetNextId(); }
/// <summary> /// 完成动态SQL语句中子语句的拼接 和 参数信息的取出 /// </summary> /// <param name="request"></param> /// <param name="parameterObject"></param> /// <returns></returns> private string Process(RequestScope request, object parameterObject) { SqlTagContext ctx = new SqlTagContext(); IList<ISqlChild> localChildren = children; ProcessBodyChildren(request, ctx, parameterObject, localChildren); #region 所有参数对应的属性类的集合信息放入到ParameterMap中 // Builds a 'dynamic' ParameterMap ParameterMap parameterMap = new ParameterMap( statement.Id + "-InlineParameterMap", statement.ParameterClass.FullName, string.Empty, statement.ParameterClass, dataExchangeFactory.GetDataExchangeForClass(null), usePositionalParameters); // Adds 'dynamic' ParameterProperty IList parameters = ctx.GetParameterMappings(); int count = parameters.Count; for (int i = 0; i < count; i++) { parameterMap.AddParameterProperty((ParameterProperty)parameters[i]); } request.ParameterMap = parameterMap; #endregion #region 完整的SQL语句 string dynSql = ctx.BodyText; if (statement is Procedure) { dynSql = dynSql.Replace(MARK_TOKEN, string.Empty).Replace(COMMA_TOKEN, string.Empty).Trim(); } // Processes $substitutions$ after DynamicSql if (SimpleDynamicSql.IsSimpleDynamicSql(dynSql)) { dynSql = new SimpleDynamicSql( dataExchangeFactory, dbHelperParameterCache, dynSql, statement).GetSql(parameterObject); } #endregion return dynSql; }
/// <summary> /// Build inline paremeterMap /// </summary> /// <param name="statement">The statement.</param> /// <param name="sqlCommandText">The SQL command text.</param> /// <param name="newSqlCommandText">The newsql command text.</param> /// <returns></returns> public ParameterMap BuildInlineParemeterMap(IStatement statement, string sqlCommandText, out string newSqlCommandText) { newSqlCommandText = sqlCommandText; ParameterMap map = null; // Check the inline parameter if (statement.ParameterMap == null) { // Build a Parametermap with the inline parameters. // if they exist. Then delete inline infos from sqltext. //感觉此处代码似乎有点多余 重新分析了语句-------->这个是针对静态语句的分析 而且是对一个完整语句的分析 SqlText sqlText = InlineParameterMapParser.ParseInlineParameterMap(modelStore.DataExchangeFactory, statement.Id, statement, newSqlCommandText); if (sqlText.Parameters.Length > 0) { string id = statement.Id + "-InLineParameterMap"; string className = string.Empty; Type classType = null; IDataExchange dataExchange = null; if (statement.ParameterClass != null) { //获取参数类的信息 className = statement.ParameterClass.Name; classType = statement.ParameterClass; //dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(classType); } //无参数类 只是一个变量的情况 if (statement.ParameterClass == null && sqlText.Parameters.Length == 1 && sqlText.Parameters[0].PropertyName == "value")//#value# parameter with no parameterClass attribut { dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(typeof(int));//Get the primitiveDataExchange } else { dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(statement.ParameterClass); } map = new ParameterMap( id, className, string.Empty, classType, dataExchange, modelStore.SessionFactory.DataSource.DbProvider.UsePositionalParameters ) ; //将SqlText类中的参数列表信息 转移到ParameterMap中 int lenght = sqlText.Parameters.Length; for (int index = 0; index < lenght; index++) { map.AddParameterProperty(sqlText.Parameters[index]); } } newSqlCommandText = sqlText.Text;//再将SqlText中分析得来的字符串转移到newSqlCommandText中 最近数据 } if (statement is Procedure) { newSqlCommandText = newSqlCommandText.Replace(MARK_TOKEN, string.Empty).Replace(COMMA_TOKEN, string.Empty); } // newSqlCommandText = newSqlCommandText.Trim(); return map; }
/// <summary> /// Deserializes the specified configuration in a Statement object. /// </summary> /// <param name="modelStore"></param> /// <param name="config">The config.</param> /// <param name="configurationSetting">Default settings.</param> /// <returns></returns> protected void BaseDeserialize(IModelStore modelStore, IConfiguration config, ConfigurationSetting configurationSetting) { nameSpace = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_NAMESPACE); id = configurationSetting.UseStatementNamespaces ? ApplyNamespace(nameSpace, config.Id) : config.Id; cacheModelName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_CACHEMODEL); extendsName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_EXTENDS); listClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_LISTCLASS); parameterClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_PARAMETERCLASS); parameterMapName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_PARAMETERMAP); resultClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_RESULTCLASS); resultMapName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_RESULTMAP); remapResults = ConfigurationUtils.GetBooleanAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_REMAPRESULTS, false); sqlSourceClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_SQLSOURCE); preserveWhitespace = ConfigurationUtils.GetBooleanAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_PRESERVEWHITSPACE, configurationSetting.PreserveWhitespace); // Gets the results Map if (resultMapName.Length > 0) { string[] ids = resultMapName.Split(','); for (int i = 0; i < ids.Length; i++) { string name = ApplyNamespace(nameSpace, ids[i].Trim()); resultsMap.Add(modelStore.GetResultMap(name)); } } // Gets the results class if (resultClassName.Length > 0) { string[] classNames = resultClassName.Split(','); for (int i = 0; i < classNames.Length; i++) { resultClass = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(classNames[i].Trim()); IFactory resultClassFactory = null; if (Type.GetTypeCode(resultClass) == TypeCode.Object && (resultClass.IsValueType == false) && resultClass != typeof(DataRow)) { resultClassFactory = modelStore.DataExchangeFactory.ObjectFactory.CreateFactory(resultClass, Type.EmptyTypes); } IDataExchange dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(resultClass); bool isSimpleType = modelStore.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(resultClass); IResultMap autoMap = new AutoResultMap(resultClass, resultClassFactory, dataExchange, isSimpleType); resultsMap.Add(autoMap); } } // Gets the ParameterMap if (parameterMapName.Length > 0) { parameterMap = modelStore.GetParameterMap(parameterMapName); } // Gets the ParameterClass if (parameterClassName.Length > 0) { parameterClass = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(parameterClassName); } // Gets the listClass if (listClassName.Length > 0) { listClass = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(listClassName); listClassFactory = modelStore.DataExchangeFactory.ObjectFactory.CreateFactory(listClass, Type.EmptyTypes); } // Gets the CacheModel if (cacheModelName.Length > 0) { cacheModel = modelStore.GetCacheModel(cacheModelName); } // Gets the SqlSource if (sqlSourceClassName.Length > 0) { Type sqlSourceType = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(sqlSourceClassName); IFactory factory = modelStore.DataExchangeFactory.ObjectFactory.CreateFactory(sqlSourceType, Type.EmptyTypes); sqlSource = (ISqlSource)factory.CreateInstance(null); } }
/// <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()); } }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="parameterObject"></param> /// <returns></returns> private string Process(RequestScope request, object parameterObject) { SqlTagContext ctx = new SqlTagContext(); IList<ISqlChild> localChildren = children; ProcessBodyChildren(request, ctx, parameterObject, localChildren); // Builds a 'dynamic' ParameterMap ParameterMap parameterMap = new ParameterMap( statement.Id + "-InlineParameterMap", statement.ParameterClass.FullName, string.Empty, statement.ParameterClass, dataExchangeFactory.GetDataExchangeForClass(null), usePositionalParameters); // Adds 'dynamic' ParameterProperty var parameters = ctx.GetParameterMappings(); parameterMap.AddParameterProperties(parameters); request.ParameterMap = parameterMap; string dynSql = ctx.BodyText; if (statement is Procedure) { dynSql = dynSql.Replace(MARK_TOKEN, string.Empty).Replace(COMMA_TOKEN, string.Empty).Trim(); } // Processes $substitutions$ after DynamicSql if (SimpleDynamicSql.IsSimpleDynamicSql(dynSql)) { dynSql = new SimpleDynamicSql( dataExchangeFactory, dbHelperParameterCache, dynSql, statement).GetSql(parameterObject); } return dynSql; }
/// <summary> /// Initializes a new instance of the <see cref="RequestScope"/> class. /// </summary> /// <param name="dataExchangeFactory">The data exchange factory.</param> /// <param name="session">The session.</param> /// <param name="statement">The statement</param> public RequestScope( DataExchangeFactory dataExchangeFactory, ISession session, IStatement statement ) { errorContext = new ErrorContext(); this.statement = statement;//statement则包含了sql语句字符串和参数类信息 parameterMap = statement.ParameterMap; this.session = session; //ISession对象是DataMapperSession类对象,其中包含了数据库的连接与事物类对象 this.dataExchangeFactory = dataExchangeFactory; id = GetNextId(); }
/// <summary> /// Deserializes the specified configuration in a Statement object. /// </summary> /// <param name="modelStore"></param> /// <param name="config">The config.</param> /// <param name="configurationSetting">Default settings.</param> /// <returns></returns> /// <remarks> /// Updated By: Richard Beacroft /// Updated Date: 11\10\2013 /// Description: configurationSetting can be null and therefore references to it have to assume that it could be null. /// </remarks> protected void BaseDeserialize(IModelStore modelStore, IConfiguration config, ConfigurationSetting configurationSetting) { // DefaultModelBuilderTest.Test_DefaultModelBuilder assumes that configurationSetting can be null - added code accordingly. // Typically, no public method should allow null to be passed-in, best handled by overloading method to exclude parameter, or in .NET 4, use optional parameter. var preserveWhitespace = (configurationSetting == null) ? false : configurationSetting.PreserveWhitespace; bool useStatementNamespaces = (configurationSetting == null) ? false : configurationSetting.UseStatementNamespaces; nameSpace = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_NAMESPACE); id = useStatementNamespaces ? ApplyNamespace(nameSpace, config.Id) : config.Id; cacheModelName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_CACHEMODEL); extendsName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_EXTENDS); listClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_LISTCLASS); parameterClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_PARAMETERCLASS); parameterMapName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_PARAMETERMAP); resultClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_RESULTCLASS); resultMapName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_RESULTMAP); remapResults = ConfigurationUtils.GetBooleanAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_REMAPRESULTS, false); sqlSourceClassName = ConfigurationUtils.GetStringAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_SQLSOURCE); preserveWhitespace = ConfigurationUtils.GetBooleanAttribute(config.Attributes, ConfigConstants.ATTRIBUTE_PRESERVEWHITSPACE, preserveWhitespace); // Gets the results Map if (resultMapName.Length > 0) { string[] ids = resultMapName.Split(','); for (int i = 0; i < ids.Length; i++) { string name = ApplyNamespace(nameSpace, ids[i].Trim()); resultsMap.Add(modelStore.GetResultMap(name)); } } // Gets the results class if (resultClassName.Length > 0) { string[] classNames = resultClassName.Split(','); for (int i = 0; i < classNames.Length; i++) { resultClass = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(classNames[i].Trim()); IFactory resultClassFactory = null; if (Type.GetTypeCode(resultClass) == TypeCode.Object && (resultClass.IsValueType == false) && resultClass != typeof(DataRow)) { resultClassFactory = modelStore.DataExchangeFactory.ObjectFactory.CreateFactory(resultClass, Type.EmptyTypes); } IDataExchange dataExchange = modelStore.DataExchangeFactory.GetDataExchangeForClass(resultClass); bool isSimpleType = modelStore.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(resultClass); IResultMap autoMap = new AutoResultMap(resultClass, resultClassFactory, dataExchange, isSimpleType); resultsMap.Add(autoMap); } } // Gets the ParameterMap if (parameterMapName.Length > 0) { parameterMap = modelStore.GetParameterMap(parameterMapName); } // Gets the ParameterClass if (parameterClassName.Length > 0) { parameterClass = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(parameterClassName); } // Gets the listClass if (listClassName.Length > 0) { listClass = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(listClassName); listClassFactory = modelStore.DataExchangeFactory.ObjectFactory.CreateFactory(listClass, Type.EmptyTypes); } // Gets the CacheModel if (cacheModelName.Length > 0) { cacheModel = modelStore.GetCacheModel(cacheModelName); } // Gets the SqlSource if (sqlSourceClassName.Length > 0) { Type sqlSourceType = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(sqlSourceClassName); IFactory factory = modelStore.DataExchangeFactory.ObjectFactory.CreateFactory(sqlSourceType, Type.EmptyTypes); sqlSource = (ISqlSource)factory.CreateInstance(null); } }
/// <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()); } }