Exemple #1
0
        /// <summary>
        /// Builds an <see cref="Procedure"/> for a procedure configuration.
        /// </summary>
        /// <param name="statementConfig">The statement config.</param>
        /// <param name="configurationSetting"></param>
        private IMappedStatement BuildProcedure(IConfiguration statementConfig, ConfigurationSetting configurationSetting)
        {
            BaseStatementDeSerializer procedureDeSerializer = new ProcedureDeSerializer();
            IStatement statement = procedureDeSerializer.Deserialize(modelStore, statementConfig, configurationSetting);

            ProcessSqlStatement(statementConfig, statement);
            MappedStatement mappedStatement = new MappedStatement(modelStore, statement);

            return(BuildCachingStatement(statement, mappedStatement));
        }
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);
        }
Exemple #3
0
 public Statement GetStatement(string fullSqlId)
 {
     if (MappedStatement.ContainsKey(fullSqlId))
     {
         return(MappedStatement[fullSqlId]);
     }
     if (_logger.IsEnabled(LogLevel.Error))
     {
         _logger.LogError($"StatementMap could not find statement:{fullSqlId}");
     }
     throw new SmartSqlException($"StatementMap could not find statement:{fullSqlId}");
 }
Exemple #4
0
 public Statement GetStatement(string fullSqlId)
 {
     if (!MappedStatement.TryGetValue(fullSqlId, out Statement statement))
     {
         if (_logger.IsEnabled(LogLevel.Error))
         {
             _logger.LogError($"StatementMap could not find statement:{fullSqlId}");
         }
         throw new SmartSqlException($"StatementMap could not find statement:{fullSqlId}");
     }
     return(statement);
 }
 private void InitSqlMapStatementMap(SmartSqlMap sqlmap)
 {
     foreach (var statementKV in sqlmap.Statements)
     {
         var statement = statementKV.Value;
         if (MappedStatement.ContainsKey(statement.FullSqlId))
         {
             throw new SmartSqlException($"SmartSqlMapConfig Load MappedStatements: StatementId:{statement.FullSqlId}  already exists!");
         }
         MappedStatement.Add(statement.FullSqlId, statement);
     }
 }
Exemple #6
0
        public override int executeNonEmptyUpdateStmt(string updateStmt, object parameter)
        {
            updateStmt = dbSqlSessionFactory.mapStatement(updateStmt);

            //if mapped statement is empty, which can happens for some databases, we have no need to execute it
            MappedStatement mappedStatement = sqlSession.Configuration.getMappedStatement(updateStmt);

            if (mappedStatement.getBoundSql(parameter).Sql.Empty)
            {
                return(0);
            }

            return(sqlSession.update(updateStmt, parameter));
        }
Exemple #7
0
 private void InitSqlMapStatementMap(SmartSqlMap sqlmap)
 {
     foreach (var statement in sqlmap.Statements)
     {
         if (MappedStatement.ContainsKey(statement.FullSqlId))
         {
             MappedStatement.Remove(statement.FullSqlId);
             if (_logger.IsEnabled(LogLevel.Warning))
             {
                 _logger.LogWarning($"SmartSqlMapConfig Load MappedStatements: StatementId:{statement.FullSqlId}  already exists!");
             }
         }
         MappedStatement.Add(statement.FullSqlId, statement);
     }
 }
Exemple #8
0
        /// <summary>
        /// Builds a <see cref="Statement"/> for a statement configuration.
        /// </summary>
        /// <param name="statementConfig">The statement config.</param>
        /// <param name="configurationSetting"></param>
        private IMappedStatement BuildStatement(IConfiguration statementConfig, ConfigurationSetting configurationSetting)
        {
            BaseStatementDeSerializer statementDeSerializer = new StatementDeSerializer();
            //解析statement节点属性到类中
            IStatement statement = statementDeSerializer.Deserialize(modelStore, statementConfig, configurationSetting);

            //估计是处理statement节点对应的文本内容  ----->确实如此 此处是个核心 一个statement节点的配置信息 和 对应的内存类
            //处理的结果放在了statement中 包括了要执行的sql语句和参数
            ProcessSqlStatement(statementConfig, statement);

            //具体封装了IDbcommand IDataReader的一些类
            MappedStatement mappedStatement = new MappedStatement(modelStore, statement);

            //放入缓存类中 并返回mappedStatement
            return(BuildCachingStatement(statement, mappedStatement));
        }
Exemple #9
0
        /// <summary>
        /// Builds an <see cref="Insert"/> for a insert configuration.
        /// </summary>
        /// <param name="statementConfig">The statement config.</param>
        /// <param name="configurationSetting"></param>
        private IMappedStatement BuildInsert(IConfiguration statementConfig, ConfigurationSetting configurationSetting)
        {
            BaseStatementDeSerializer insertDeSerializer = new InsertDeSerializer();
            IStatement statement = insertDeSerializer.Deserialize(modelStore, statementConfig, configurationSetting);

            ProcessSqlStatement(statementConfig, statement);
            MappedStatement mappedStatement = new InsertMappedStatement(modelStore, statement);
            Insert          insert          = (Insert)statement;

            if (insert.SelectKey != null)
            {
                ConfigurationCollection selectKeys      = statementConfig.Children.Find(ConfigConstants.ELEMENT_SELECTKEY);
                IConfiguration          selectKeyConfig = selectKeys[0];

                ProcessSqlStatement(selectKeyConfig, insert.SelectKey);
                MappedStatement mapStatement = new MappedStatement(modelStore, insert.SelectKey);
                modelStore.AddMappedStatement(mapStatement);
            }

            return(BuildCachingStatement(statement, mappedStatement));
        }
Exemple #10
0
#pragma warning restore 0067
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="statement"></param>
        public CachingStatement(MappedStatement statement)
        {
            _mappedStatement = statement;
        }
        /// <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();
        }