/// <summary> /// Create a list of IDataParameter for the statement and build the sql string. /// </summary> /// <param name="isDynamic">if set to <c>true</c> this statement is dynamic.</param> /// <returns></returns> public PreparedStatement Prepare(bool isDynamic) { preparedStatement = new PreparedStatement(); if (statement.CommandType == CommandType.Text) { preparedStatement.PreparedSql = commandText; if (request.ParameterMap != null) { CreateParametersForTextCommand(); EvaluateParameterMap(); } } else if (statement.CommandType == CommandType.StoredProcedure) // StoredProcedure { // Necessary to prevent stored procedures with extra space around their name like // " ps_SelectAccount " to be sent to the database as "ps_SelectAccount". preparedStatement.PreparedSql = commandText.Trim(); if (request.ParameterMap == null) // No parameterMap --> error { throw new DataMapperException("A procedure statement tag must have a parameterMap attribute, which is not the case for the procedure '" + statement.Id + "."); } if (dbProvider.UseDeriveParameters) { if (isDynamic) { DiscoverParameter(request.ParameterMap); } else { DiscoverParameter(statement.ParameterMap); } } else { CreateParametersForProcedureCommand(); } #region Fix for Odbc // Although executing a parameterized stored procedure using the ODBC .NET Provider // is slightly different from executing the same procedure using the SQL or // the OLE DB Provider, there is one important difference // -- the stored procedure must be called using the ODBC CALL syntax rather than // the name of the stored procedure. // For additional information on this CALL syntax, // see the page entitled "Procedure Calls" in the ODBC Programmer's Reference // in the MSDN Library. //http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q309486 if (dbProvider.IsObdc) { StringBuilder commandTextBuilder = new StringBuilder("{ call "); commandTextBuilder.Append(commandText); if (preparedStatement.DbParameters.Length > 0) { commandTextBuilder.Append(" ("); int supIndex = preparedStatement.DbParameters.Length - 1; for (int i = 0; i < supIndex; i++) { commandTextBuilder.Append("?,"); } commandTextBuilder.Append("?) }"); } preparedStatement.PreparedSql = commandTextBuilder.ToString(); } #endregion } if (logger.IsDebugEnabled) { logger.Debug("Statement Id: [" + statement.Id + "] Prepared SQL: [" + preparedStatement.PreparedSql + "]"); } return(preparedStatement); }
/// <summary> /// Build the PreparedStatement /// </summary> /// <param name="session">The session.</param> /// <param name="sql">The SQL.</param> public void BuildPreparedStatement(ISession session, string sql) { RequestScope request = new RequestScope( dataExchangeFactory, session, statement); PreparedStatementFactory factory = new PreparedStatementFactory(session, dbHelperParameterCache, request, statement, sql); preparedStatement = factory.Prepare(false); }
/// <summary> /// Create a list of IDataParameter for the statement and build the sql string. /// </summary> /// <param name="isDynamic">if set to <c>true</c> this statement is dynamic.</param> /// <returns></returns> public PreparedStatement Prepare(bool isDynamic) { preparedStatement = new PreparedStatement(); if (statement.CommandType == CommandType.Text) { //获取SQL语句 preparedStatement.PreparedSql = commandText; if (request.ParameterMap != null) { //创建语句使用到的参数列表 CreateParametersForTextCommand(); //用具体的参数代替SQL语句中的"?" EvaluateParameterMap(); } } else if (statement.CommandType == CommandType.StoredProcedure) // StoredProcedure { // Necessary to prevent stored procedures with extra space around their name like // " ps_SelectAccount " to be sent to the database as "ps_SelectAccount". preparedStatement.PreparedSql = commandText.Trim(); if (request.ParameterMap == null) // No parameterMap --> error { throw new DataMapperException("A procedure statement tag must have a parameterMap attribute, which is not the case for the procedure '"+statement.Id+"."); } if (dbProvider.UseDeriveParameters) { if (isDynamic) { DiscoverParameter(request.ParameterMap); } else { DiscoverParameter(statement.ParameterMap); } } else { CreateParametersForProcedureCommand(); } #region Fix for Odbc // Although executing a parameterized stored procedure using the ODBC .NET Provider // is slightly different from executing the same procedure using the SQL or // the OLE DB Provider, there is one important difference // -- the stored procedure must be called using the ODBC CALL syntax rather than // the name of the stored procedure. // For additional information on this CALL syntax, // see the page entitled "Procedure Calls" in the ODBC Programmer's Reference // in the MSDN Library. //http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q309486 if (dbProvider.IsObdc) { StringBuilder commandTextBuilder = new StringBuilder("{ call "); commandTextBuilder.Append( commandText ); if (preparedStatement.DbParameters.Length >0) { commandTextBuilder.Append(" ("); int supIndex = preparedStatement.DbParameters.Length-1; for(int i=0;i<supIndex;i++) { commandTextBuilder.Append("?,"); } commandTextBuilder.Append("?) }"); } preparedStatement.PreparedSql = commandTextBuilder.ToString(); } #endregion } if (logger.IsDebugEnabled) { logger.Debug("Statement Id: [" + statement.Id + "] Prepared SQL: [" + preparedStatement.PreparedSql + "]"); } return preparedStatement; }
/// <summary> /// Build the PreparedStatement /// </summary> /// <param name="session">The session.</param> /// <param name="request">The request.</param> /// <param name="commandText">The command text.</param> /// <returns></returns> public PreparedStatement BuildPreparedStatement(ISession session, RequestScope request, string commandText) { if (preparedStatement == null ) { lock(syncLock) { if (preparedStatement==null) { PreparedStatementFactory factory = new PreparedStatementFactory(session, dbHelperParameterCache, request, statement, commandText); preparedStatement = factory.Prepare(false); } } } return preparedStatement; }