/// <summary> /// Gets the SQL. /// </summary> /// <param name="mappedStatement">The mapped statement.</param> /// <param name="parameterObject">The parameter object.</param> /// <returns></returns> /// <remarks> /// Paremeters should be typeof IDictionary<string, object> /// </remarks> public string GetSql(IMappedStatement mappedStatement, object parameterObject) { Contract.Assert.That(parameterObject, Is.TypeOf <IDictionary <string, object> >()).When("Processing NVelocity source for statement :" + mappedStatement.Id); StringWriter sw = new StringWriter(); ExternalSql externalSql = (ExternalSql)mappedStatement.Statement.Sql; string commandText = externalSql.CommandText; if (commandText.Contains(VELOCITY_DIRECTIVE)) { VelocityContext velocityContext = new VelocityContext(); IDictionary <string, object> dico = (IDictionary <string, object>)parameterObject; foreach (string key in dico.Keys) { velocityContext.Put(key, dico[key]); } try { velocityEngine.Evaluate(velocityContext, sw, "error", commandText); } catch (Exception ex) { if (logger.IsDebugEnabled) { logger.Debug("Could not parse velocity string '" + commandText + "' for " + mappedStatement.Id); } throw new DataMapperException("Could not parse velocity string '" + commandText + "' for " + mappedStatement.Id); } commandText = sw.GetStringBuilder().ToString(); } if (logger.IsDebugEnabled) { logger.Debug("SQL command text parse by velocity: " + commandText); } return(commandText); }
/// <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."); }
/// <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."); }