Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        private string Process(RequestScope request, object parameterObject)
        {
            SqlTagContext ctx           = new SqlTagContext();
            IList         localChildren = _children;

            ProcessBodyChildren(request, ctx, parameterObject, localChildren);

            // Builds a 'dynamic' ParameterMap
            ParameterMap map = new ParameterMap(request.DataExchangeFactory);

            map.Id = _statement.Id + "-InlineParameterMap";
            map.Initialize(_usePositionalParameters, request);
            map.Class = _statement.ParameterClass;

            // Adds 'dynamic' ParameterProperty
            IList parameters = ctx.GetParameterMappings();
            int   count      = parameters.Count;

            for (int i = 0; i < count; i++)
            {
                map.AddParameterProperty((ParameterProperty)parameters[i]);
            }
            request.ParameterMap = map;

            string dynSql = ctx.BodyText;

            // Processes $substitutions$ after DynamicSql
            if (SimpleDynamicSql.IsSimpleDynamicSql(dynSql))
            {
                dynSql = new SimpleDynamicSql(request, dynSql, _statement).GetSql(parameterObject);
            }
            return(dynSql);
        }
Пример #2
0
        /// <summary>
        /// Builds a new <see cref="RequestScope"/> and the <see cref="IDbCommand"/> text to execute.
        /// </summary>
        /// <param name="mappedStatement">The <see cref="IMappedStatement"/>.</param>
        /// <param name="parameterObject">The parameter object (used by DynamicSql/SimpleDynamicSql).
        /// Use to complete the sql statement.</param>
        /// <param name="session">The current session</param>
        /// <returns>A new <see cref="RequestScope"/>.</returns>
        public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISession session)
        {
            RequestScope request = new RequestScope(dataExchangeFactory, session, statement);

            string sqlCommandText    = statement.SqlSource.GetSql(mappedStatement, parameterObject);
            string newSqlCommandText = string.Empty;

            if (request.ParameterMap == null)
            {
                request.ParameterMap = inlineParemeterMapBuilder.BuildInlineParemeterMap(statement, sqlCommandText, out newSqlCommandText);
            }

            // Processes $substitutions$ after DynamicSql
            if (SimpleDynamicSql.IsSimpleDynamicSql(newSqlCommandText))
            {
                newSqlCommandText = new SimpleDynamicSql(
                    dataExchangeFactory,
                    dbHelperParameterCache,
                    newSqlCommandText,
                    statement).GetSql(parameterObject);
            }

            request.PreparedStatement = BuildPreparedStatement(session, request, newSqlCommandText);

            return(request);
        }
Пример #3
0
        private string Process(RequestScope request, object parameterObject)
        {
            SqlTagContext ctx           = new SqlTagContext();
            IList         localChildren = this._children;

            this.ProcessBodyChildren(request, ctx, parameterObject, localChildren);
            ParameterMap map = new ParameterMap(request.DataExchangeFactory)
            {
                Id = this._statement.Id + "-InlineParameterMap"
            };

            map.Initialize(this._usePositionalParameters, request);
            map.Class = this._statement.ParameterClass;
            IList parameterMappings = ctx.GetParameterMappings();
            int   count             = parameterMappings.Count;

            for (int i = 0; i < count; i++)
            {
                map.AddParameterProperty((ParameterProperty)parameterMappings[i]);
            }
            request.ParameterMap = map;
            string bodyText = ctx.BodyText;

            if (SimpleDynamicSql.IsSimpleDynamicSql(bodyText))
            {
                bodyText = new SimpleDynamicSql(request, bodyText, this._statement).GetSql(parameterObject);
            }
            return(bodyText);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
        /// <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);
        }
Пример #6
0
        /// <summary>
        /// Process the Sql cpmmand text statement (Build ISql)
        /// </summary>
        /// <param name="statementConfiguration">The statement configuration.</param>
        /// <param name="statement">The statement.</param>
        private void ProcessSqlStatement(IConfiguration statementConfiguration, IStatement statement)
        {
            if (dynamicSqlEngine != null)
            {
                statement.SqlSource = dynamicSqlEngine;
            }

            if (statement.SqlSource != null)
            {
                #region sqlSource - external processor
                string commandText = string.Empty;

                if (statementConfiguration.Children.Count > 0)
                {
                    IConfiguration child = statementConfiguration.Children[0];
                    if (child.Type == ConfigConstants.ELEMENT_TEXT || child.Type == ConfigConstants.ELEMENT_CDATA)
                    {
                        // pass the unformated sql to the external processor
                        commandText = child.Value;
                    }
                }

                ExternalSql externalSql = new ExternalSql(modelStore, statement, commandText);
                statement.Sql = externalSql;
                #endregion
            }
            else
            {
                #region default - internal processor
                bool isDynamic = false;

                DynamicSql dynamic = new DynamicSql(
                    modelStore.SessionFactory.DataSource.DbProvider.UsePositionalParameters,
                    modelStore.DBHelperParameterCache,
                    modelStore.DataExchangeFactory,
                    statement);
                StringBuilder sqlBuffer = new StringBuilder();

                isDynamic = ParseDynamicTags(statementConfiguration, dynamic, sqlBuffer, isDynamic, false, statement);

                if (isDynamic)
                {
                    statement.Sql = dynamic;
                }
                else
                {
                    string sqlText           = sqlBuffer.ToString();
                    string newSqlCommandText = string.Empty;

                    ParameterMap map = inlineParemeterMapBuilder.BuildInlineParemeterMap(statement, sqlText, out newSqlCommandText);
                    if (map != null)
                    {
                        statement.ParameterMap = map;
                    }

                    if (SimpleDynamicSql.IsSimpleDynamicSql(newSqlCommandText))
                    {
                        statement.Sql = new SimpleDynamicSql(
                            modelStore.DataExchangeFactory,
                            modelStore.DBHelperParameterCache,
                            newSqlCommandText,
                            statement);
                    }
                    else
                    {
                        if (statement is Procedure)
                        {
                            statement.Sql = new ProcedureSql(
                                modelStore.DataExchangeFactory,
                                modelStore.DBHelperParameterCache,
                                newSqlCommandText,
                                statement);
                            // Could not call BuildPreparedStatement for procedure because when NUnit Test
                            // the database is not here (but in theory procedure must be prepared like statement)
                            // It's even better as we can then switch DataSource.
                        }
                        else if (statement is Statement)
                        {
                            statement.Sql = new StaticSql(
                                modelStore.DataExchangeFactory,
                                modelStore.DBHelperParameterCache,
                                statement);

                            // this does not open a connection to the database
                            ISession session = modelStore.SessionFactory.OpenSession();

                            ((StaticSql)statement.Sql).BuildPreparedStatement(session, newSqlCommandText);

                            session.Close();
                        }
                    }
                }
                #endregion
            }

            Contract.Ensure.That(statement.Sql, Is.Not.Null).When("process Sql statement.");
        }
Пример #7
0
        /// <summary>
        /// Process the Sql cpmmand text statement (Build ISql)
        /// </summary>
        /// <param name="statementConfiguration">The statement configuration.</param>
        /// <param name="statement">The statement.</param>
        private void ProcessSqlStatement(IConfiguration statementConfiguration, IStatement statement)
        {
            //判断sqlSource属性值的存在与否 对应不同的处理方法
            if (dynamicSqlEngine != null)
            {
                statement.SqlSource = dynamicSqlEngine;
            }

            if (statement.SqlSource != null)//外部处理SQL语句方式
            {
                #region sqlSource - external processor
                string commandText = string.Empty;

                if (statementConfiguration.Children.Count > 0)
                {
                    IConfiguration child = statementConfiguration.Children[0];
                    if (child.Type == ConfigConstants.ELEMENT_TEXT || child.Type == ConfigConstants.ELEMENT_CDATA)
                    {
                        // pass the unformated sql to the external processor
                        commandText = child.Value;
                    }
                }

                ExternalSql externalSql = new ExternalSql(modelStore, statement, commandText);
                //最终的处理结果
                statement.Sql = externalSql; //(赋值到ISql类对象)
                #endregion
            }
            else//当前默认处理SQL语句的方式
            {
                #region default - internal processor
                bool isDynamic = false;

                //初始化DynamicSql的成员变量而已
                DynamicSql dynamic = new DynamicSql(
                    modelStore.SessionFactory.DataSource.DbProvider.UsePositionalParameters,
                    modelStore.DBHelperParameterCache,
                    modelStore.DataExchangeFactory,
                    statement);
                StringBuilder sqlBuffer = new StringBuilder();
                //解析SQL文本语句  这个函数使用了递归处理 结果保存到了 sqlBuffer和dynamic中
                //注意:sqlBuffer中应该是一个完整的SQL语句,dynamic最后将当前的select update insert等节点信息保存在了内部类SqlText和SqlTag类中了
                isDynamic = ParseDynamicTags(statementConfiguration, dynamic, sqlBuffer, isDynamic, false, statement);

                if (isDynamic)//如果是动态语句
                {
                    statement.Sql = dynamic;
                }
                else
                {
                    //此处的sqlText得到的是一个查询语句的完整状态 但包含参数符号信息
                    string sqlText           = sqlBuffer.ToString();
                    string newSqlCommandText = string.Empty;
                    //估计是实现将参数属性类 合并到 参数集合中去----->根据完整的语句中的参数符号标志分析到一个参数ParameterMap类中
                    //确实如此 参数放入ParameterMap中  参数类没有细分 都放到了map中 当前节点完整SQL放入到newSqlCommandText中
                    ParameterMap map = inlineParemeterMapBuilder.BuildInlineParemeterMap(statement, sqlText, out newSqlCommandText);
                    if (map != null)
                    {
                        statement.ParameterMap = map;
                    }

                    //如果包含$字符 就是简单SQL
                    if (SimpleDynamicSql.IsSimpleDynamicSql(newSqlCommandText))
                    {
                        statement.Sql = new SimpleDynamicSql(
                            modelStore.DataExchangeFactory,
                            modelStore.DBHelperParameterCache,
                            newSqlCommandText,
                            statement);
                    }
                    else
                    {
                        //如果是存储过程
                        if (statement is Procedure)
                        {
                            statement.Sql = new ProcedureSql(
                                modelStore.DataExchangeFactory,
                                modelStore.DBHelperParameterCache,
                                newSqlCommandText,
                                statement);
                            // Could not call BuildPreparedStatement for procedure because when NUnit Test
                            // the database is not here (but in theory procedure must be prepared like statement)
                            // It's even better as we can then switch DataSource.
                        }
                        else if (statement is Statement)//如果是update delete insert select等类型
                        {
                            statement.Sql = new StaticSql(
                                modelStore.DataExchangeFactory,
                                modelStore.DBHelperParameterCache,
                                statement);

                            // this does not open a connection to the database
                            //获取的是DefaultSessionFactory中的DataMapperSession对象
                            ISession session = modelStore.SessionFactory.OpenSession();

                            //完成对StaticSql对象中的PreparedStatement对SQL语句参数到数据库参数的对应解析
                            ((StaticSql)statement.Sql).BuildPreparedStatement(session, newSqlCommandText);

                            session.Close();
                        }
                    }
                }
                #endregion
            }

            Contract.Ensure.That(statement.Sql, Is.Not.Null).When("process Sql statement.");
        }