/// <summary>
 /// Builds a Mapped Statement for a statement.
 /// </summary>
 /// <param name="statement">The statement.</param>
 /// <param name="mappedStatement">The mapped statement.</param>
 /// <returns></returns>
 private IMappedStatement BuildCachingStatement(IStatement statement, MappedStatement mappedStatement)
 {
     IMappedStatement mapStatement = mappedStatement;
     if (statement.CacheModel != null && isCacheModelsEnabled)
     {
         mapStatement = new CachingStatement(mappedStatement);
     }
     return mapStatement;
 }
Exemple #2
0
        /// <summary>
        /// Builds a Mapped Statement for a statement.
        /// </summary>
        /// <param name="statement">The statement.</param>
        /// <param name="mappedStatement">The mapped statement.</param>
        /// <returns></returns>
        private IMappedStatement BuildCachingStatement(IStatement statement, MappedStatement mappedStatement)
        {
            IMappedStatement mapStatement = mappedStatement;

            if (statement.CacheModel != null && isCacheModelsEnabled)
            {
                mapStatement = new CachingStatement(mappedStatement);
            }
            return(mapStatement);
        }
        /// <summary>
        /// Load sqlMap statement.
        /// </summary>
        private void ConfigureSqlMap( )
        {
            XmlNode sqlMapNode = _configScope.NodeContext;

            _configScope.ErrorContext.Activity = "loading SqlMap";
            _configScope.ErrorContext.Resource = sqlMapNode.OuterXml.ToString();

            if (_configScope.UseConfigFileWatcher)
            {
                if (sqlMapNode.Attributes["resource"] != null || sqlMapNode.Attributes["url"] != null)
                {
                    ConfigWatcherHandler.AddFileToWatch( Resources.GetFileInfo( Resources.GetValueOfNodeResourceUrl(sqlMapNode, _configScope.Properties) ) );
                }
            }

            // Load the file
            _configScope.SqlMapDocument = Resources.GetAsXmlDocument(sqlMapNode, _configScope.Properties);

            if (_configScope.ValidateSqlMap)
            {
                ValidateSchema( _configScope.SqlMapDocument.ChildNodes[1], "SqlMap.xsd" );
            }

            _configScope.SqlMapNamespace = _configScope.SqlMapDocument.SelectSingleNode( ApplyMappingNamespacePrefix(XML_MAPPING_ROOT), _configScope.XmlNamespaceManager ).Attributes["namespace"].Value;

            #region Load TypeAlias

            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_TYPEALIAS), _configScope.XmlNamespaceManager))
            {
                TypeAliasDeSerializer.Deserialize(xmlNode, _configScope);
            }
            _configScope.ErrorContext.MoreInfo = string.Empty;
            _configScope.ErrorContext.ObjectId = string.Empty;

            #endregion

            #region Load resultMap

            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_RESULTMAP), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading ResultMap tag";
                _configScope.NodeContext = xmlNode; // A ResultMap node

                BuildResultMap();
            }

            #endregion

            #region Load parameterMaps

            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_PARAMETERMAP), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading ParameterMap tag";
                _configScope.NodeContext = xmlNode; // A ParameterMap node

                BuildParameterMap();
            }

            #endregion

            #region Load statements

            #region Sql tag
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes(ApplyMappingNamespacePrefix(SQL_STATEMENT), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading sql tag";
                _configScope.NodeContext = xmlNode; // A sql tag

                SqlDeSerializer.Deserialize(xmlNode, _configScope);
            }
            #endregion

            #region Statement tag
            Statement statement;
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_STATEMENT), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading statement tag";
                _configScope.NodeContext = xmlNode; // A statement tag

                statement = StatementDeSerializer.Deserialize(xmlNode, _configScope);
                statement.CacheModelName = _configScope.ApplyNamespace(statement.CacheModelName);
                statement.ParameterMapName = _configScope.ApplyNamespace(statement.ParameterMapName);
                //statement.ResultMapName = ApplyNamespace( statement.ResultMapName );

                if (_configScope.UseStatementNamespaces)
                {
                    statement.Id = _configScope.ApplyNamespace(statement.Id);
                }
                _configScope.ErrorContext.ObjectId = statement.Id;
                statement.Initialize( _configScope );

                // Build ISql (analyse sql statement)
                ProcessSqlStatement( statement  );

                // Build MappedStatement
                MappedStatement mappedStatement = new MappedStatement(_configScope.SqlMapper, statement);
                IMappedStatement mapStatement = mappedStatement;
                if (statement.CacheModelName != null && statement.CacheModelName.Length > 0 && _configScope.IsCacheModelsEnabled)
                {
                    mapStatement = new CachingStatement(mappedStatement);
                }

                _configScope.SqlMapper.AddMappedStatement(mapStatement.Id, mapStatement);
            }
            #endregion

            #region Select tag
            Select select;
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_SELECT), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading select tag";
                _configScope.NodeContext = xmlNode; // A select node

                select = SelectDeSerializer.Deserialize(xmlNode, _configScope);
                select.CacheModelName = _configScope.ApplyNamespace(select.CacheModelName);
                select.ParameterMapName = _configScope.ApplyNamespace(select.ParameterMapName);
                //select.ResultMapName = ApplyNamespace( select.ResultMapName );

                if (_configScope.UseStatementNamespaces)
                {
                    select.Id = _configScope.ApplyNamespace(select.Id);
                }
                _configScope.ErrorContext.ObjectId = select.Id;

                select.Initialize( _configScope );

                if (select.Generate != null)
                {
                    GenerateCommandText(_configScope, select);
                }
                else
                {
                    // Build ISql (analyse sql statement)
                    ProcessSqlStatement( select);
                }

                // Build MappedStatement
                MappedStatement mappedStatement = new SelectMappedStatement(_configScope.SqlMapper, select);
                IMappedStatement mapStatement = mappedStatement;
                if (select.CacheModelName != null && select.CacheModelName.Length> 0 && _configScope.IsCacheModelsEnabled)
                {
                    mapStatement = new CachingStatement(mappedStatement);
                }

                _configScope.SqlMapper.AddMappedStatement(mapStatement.Id, mapStatement);
            }
            #endregion

            #region Insert tag
            Insert insert;
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_INSERT), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading insert tag";
                _configScope.NodeContext = xmlNode; // A insert tag

                MappedStatement mappedStatement;

                insert = InsertDeSerializer.Deserialize(xmlNode, _configScope);
                insert.CacheModelName = _configScope.ApplyNamespace(insert.CacheModelName);
                insert.ParameterMapName = _configScope.ApplyNamespace(insert.ParameterMapName);
                //insert.ResultMapName = ApplyNamespace( insert.ResultMapName );

                if (_configScope.UseStatementNamespaces)
                {
                    insert.Id = _configScope.ApplyNamespace(insert.Id);
                }
                _configScope.ErrorContext.ObjectId = insert.Id;
                insert.Initialize( _configScope );

                // Build ISql (analyse sql command text)
                if (insert.Generate != null)
                {
                    GenerateCommandText(_configScope, insert);
                }
                else
                {
                    ProcessSqlStatement( insert);
                }

                // Build MappedStatement
                mappedStatement = new InsertMappedStatement( _configScope.SqlMapper, insert);

                _configScope.SqlMapper.AddMappedStatement(mappedStatement.Id, mappedStatement);

                #region statement SelectKey
                // Set sql statement SelectKey
                if (insert.SelectKey != null)
                {
                    _configScope.ErrorContext.MoreInfo = "loading selectKey tag";
                    _configScope.NodeContext = xmlNode.SelectSingleNode( ApplyMappingNamespacePrefix(XML_SELECTKEY), _configScope.XmlNamespaceManager);

                    insert.SelectKey.Id = insert.Id;
                    insert.SelectKey.Initialize( _configScope );
                    insert.SelectKey.Id += DOT + "SelectKey";

                    // Initialize can also use _configScope.ErrorContext.ObjectId to get the id
                    // of the parent <select> node
                    // insert.SelectKey.Initialize( _configScope );
                    // insert.SelectKey.Id = insert.Id + DOT + "SelectKey";

                    ProcessSqlStatement(insert.SelectKey);

                    // Build MappedStatement
                    mappedStatement = new MappedStatement( _configScope.SqlMapper, insert.SelectKey);

                    _configScope.SqlMapper.AddMappedStatement(mappedStatement.Id, mappedStatement);
                }
                #endregion
            }
            #endregion

            #region Update tag
            Update update;
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_UPDATE), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading update tag";
                _configScope.NodeContext = xmlNode; // A update tag

                MappedStatement mappedStatement;

                update = UpdateDeSerializer.Deserialize(xmlNode, _configScope);
                update.CacheModelName = _configScope.ApplyNamespace(update.CacheModelName);
                update.ParameterMapName = _configScope.ApplyNamespace(update.ParameterMapName);
                //update.ResultMapName = ApplyNamespace( update.ResultMapName );

                if (_configScope.UseStatementNamespaces)
                {
                    update.Id = _configScope.ApplyNamespace(update.Id);
                }
                _configScope.ErrorContext.ObjectId = update.Id;
                update.Initialize( _configScope );

                // Build ISql (analyse sql statement)
                if (update.Generate != null)
                {
                    GenerateCommandText(_configScope, update);
                }
                else
                {
                    // Build ISql (analyse sql statement)
                    ProcessSqlStatement(update);
                }

                // Build MappedStatement
                mappedStatement = new UpdateMappedStatement( _configScope.SqlMapper, update);

                _configScope.SqlMapper.AddMappedStatement(mappedStatement.Id, mappedStatement);
            }
            #endregion

            #region Delete tag
            Delete delete;
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_DELETE), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading delete tag";
                _configScope.NodeContext = xmlNode; // A delete tag
                MappedStatement mappedStatement;

                delete = DeleteDeSerializer.Deserialize(xmlNode, _configScope);
                delete.CacheModelName = _configScope.ApplyNamespace(delete.CacheModelName);
                delete.ParameterMapName = _configScope.ApplyNamespace(delete.ParameterMapName);
                //delete.ResultMapName = ApplyNamespace( delete.ResultMapName );

                if (_configScope.UseStatementNamespaces)
                {
                    delete.Id = _configScope.ApplyNamespace(delete.Id);
                }
                _configScope.ErrorContext.ObjectId = delete.Id;
                delete.Initialize( _configScope );

                // Build ISql (analyse sql statement)
                if (delete.Generate != null)
                {
                    GenerateCommandText(_configScope, delete);
                }
                else
                {
                    // Build ISql (analyse sql statement)
                    ProcessSqlStatement(delete);
                }

                // Build MappedStatement
                mappedStatement = new DeleteMappedStatement( _configScope.SqlMapper, delete);

                _configScope.SqlMapper.AddMappedStatement(mappedStatement.Id, mappedStatement);
            }
            #endregion

            #region Procedure tag
            Procedure procedure;
            foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_PROCEDURE), _configScope.XmlNamespaceManager))
            {
                _configScope.ErrorContext.MoreInfo = "loading procedure tag";
                _configScope.NodeContext = xmlNode; // A procedure tag

                procedure = ProcedureDeSerializer.Deserialize(xmlNode, _configScope);
                procedure.CacheModelName = _configScope.ApplyNamespace(procedure.CacheModelName);
                procedure.ParameterMapName = _configScope.ApplyNamespace(procedure.ParameterMapName);
                //procedure.ResultMapName = ApplyNamespace( procedure.ResultMapName );

                if (_configScope.UseStatementNamespaces)
                {
                    procedure.Id = _configScope.ApplyNamespace(procedure.Id);
                }
                _configScope.ErrorContext.ObjectId = procedure.Id;
                procedure.Initialize( _configScope );

                // Build ISql (analyse sql command text)
                ProcessSqlStatement( procedure );

                // Build MappedStatement
                MappedStatement mappedStatement = new MappedStatement(_configScope.SqlMapper, procedure);
                IMappedStatement mapStatement = mappedStatement;
                if (procedure.CacheModelName != null && procedure.CacheModelName.Length > 0 && _configScope.IsCacheModelsEnabled)
                {
                    mapStatement = new CachingStatement(mappedStatement);
                }

                _configScope.SqlMapper.AddMappedStatement(mapStatement.Id, mapStatement);
            }
            #endregion

            #endregion

            #region Load CacheModels

            if (_configScope.IsCacheModelsEnabled)
            {
                CacheModel cacheModel;
                foreach (XmlNode xmlNode in _configScope.SqlMapDocument.SelectNodes( ApplyMappingNamespacePrefix(XML_CACHE_MODEL), _configScope.XmlNamespaceManager))
                {
                    cacheModel = CacheModelDeSerializer.Deserialize(xmlNode, _configScope);
                    cacheModel.Id = _configScope.ApplyNamespace(cacheModel.Id);

                    // Attach ExecuteEventHandler
                    foreach(XmlNode flushOn in xmlNode.SelectNodes( ApplyMappingNamespacePrefix(XML_FLUSH_ON_EXECUTE), _configScope.XmlNamespaceManager  ))
                    {
                        string statementName = flushOn.Attributes["statement"].Value;
                        if (_configScope.UseStatementNamespaces)
                        {
                            statementName = _configScope.ApplyNamespace(statementName);
                        }

                        // delay registering statements to cache model until all sqlMap files have been processed
                        IList statementNames = (IList)_configScope.CacheModelFlushOnExecuteStatements[cacheModel.Id];
                        if (statementNames == null)
                        {
                            statementNames = new ArrayList();
                        }
                        statementNames.Add(statementName);
                        _configScope.CacheModelFlushOnExecuteStatements[cacheModel.Id] = statementNames;
                    }

                    // Get Properties
                    foreach(XmlNode propertie in xmlNode.SelectNodes( ApplyMappingNamespacePrefix(XML_PROPERTY), _configScope.XmlNamespaceManager))
                    {
                        string name = propertie.Attributes["name"].Value;
                        string value = propertie.Attributes["value"].Value;

                        cacheModel.AddProperty(name, value);
                    }

                    cacheModel.Initialize();

                    _configScope.SqlMapper.AddCache( cacheModel );
                }
            }

            #endregion

            _configScope.ErrorContext.Reset();
        }