// DONE : H3.2 Executable query (now can be supported for named SQL query/ storedProcedure) public int PerformExecuteUpdate(QueryParameters queryParameters, ISessionImplementor session) { CoordinateSharedCacheCleanup(session); if (queryParameters.Callable) { throw new ArgumentException("callable not yet supported for native queries"); } RowSelection selection = queryParameters.RowSelection; int result; try { var parametersSpecifications = customQuery.CollectedParametersSpecifications.ToList(); SqlString sql = ExpandDynamicFilterParameters(customQuery.SQL, parametersSpecifications, session); // After the last modification to the SqlString we can collect all parameters types. parametersSpecifications.ResetEffectiveExpectedType(queryParameters); var sqlParametersList = sql.GetParameters().ToList(); SqlType[] sqlTypes = parametersSpecifications.GetQueryParameterTypes(sqlParametersList, session.Factory); var ps = session.Batcher.PrepareCommand(CommandType.Text, sql, sqlTypes); try { if (selection != null && selection.Timeout != RowSelection.NoValue) { // NH Difference : set Timeout for native query ps.CommandTimeout = selection.Timeout; } foreach (IParameterSpecification parameterSpecification in parametersSpecifications) { parameterSpecification.Bind(ps, sqlParametersList, queryParameters, session); } result = session.Batcher.ExecuteNonQuery(ps); } finally { if (ps != null) { session.Batcher.CloseCommand(ps, null); } } } catch (HibernateException) { throw; } catch (Exception sqle) { throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not execute native bulk manipulation query:" + sourceQuery); } return(result); }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { var parameters = _sql.GetParameters().ToList(); var paramPos = 0; for (int i = 0; i < _typedValues.Length; i++) { var controlledParameters = criteriaQuery.NewQueryParameter(_typedValues[i]); foreach (Parameter parameter in controlledParameters) { parameters[paramPos++].BackTrack = parameter.BackTrack; } } return(criteriaQuery.RenderSQLAliases(_sql).Replace("{alias}", criteriaQuery.GetSQLAlias(criteria))); }
public override int Execute(QueryParameters parameters, ISessionImplementor session) { CoordinateSharedCacheCleanup(session); DbCommand st = null; RowSelection selection = parameters.RowSelection; try { try { CheckParametersExpectedType(parameters); // NH Different behavior (NH-1898) var sqlQueryParametersList = sql.GetParameters().ToList(); SqlType[] parameterTypes = Parameters.GetQueryParameterTypes(sqlQueryParametersList, session.Factory); st = session.Batcher.PrepareCommand(CommandType.Text, sql, parameterTypes); foreach (var parameterSpecification in Parameters) { parameterSpecification.Bind(st, sqlQueryParametersList, parameters, session); } if (selection != null) { if (selection.Timeout != RowSelection.NoValue) { st.CommandTimeout = selection.Timeout; } } return(session.Batcher.ExecuteNonQuery(st)); } finally { if (st != null) { session.Batcher.CloseCommand(st, null); } } } catch (DbException sqle) { throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not execute update query", sql); } }
public override SqlString OnPrepareStatement(SqlString sql) { var parameters = sql.GetParameters(); var paramCount = parameters.Count(); if (paramCount > 0) { return(sql); } string optionString = " OPTION (OPTIMIZE FOR ("; for (var i = 0; i < paramCount; i++) { var comma = i > 0 ? "," : string.Empty; optionString = optionString + comma + "@p" + i + " UNKNOWN"; } optionString = optionString + "))"; var builder = new SqlStringBuilder(sql); builder.Add(optionString); return(builder.ToSqlString()); }
protected override IEnumerable <IParameterSpecification> GetParameterSpecifications() { return(parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray())); }
public override int Execute(QueryParameters parameters, ISessionImplementor session) { CoordinateSharedCacheCleanup(session); CreateTemporaryTableIfNecessary(persister, session); try { // First, save off the pertinent ids, as the return value DbCommand ps = null; int resultCount; try { try { int parameterStart = Walker.NumberOfParametersInSetClause; IList <IParameterSpecification> allParams = Walker.Parameters; List <IParameterSpecification> whereParams = (new List <IParameterSpecification>(allParams)).GetRange( parameterStart, allParams.Count - parameterStart); var sqlQueryParametersList = idInsertSelect.GetParameters().ToList(); SqlType[] parameterTypes = whereParams.GetQueryParameterTypes(sqlQueryParametersList, session.Factory); ps = session.Batcher.PrepareCommand(CommandType.Text, idInsertSelect, parameterTypes); foreach (var parameterSpecification in whereParams) { parameterSpecification.Bind(ps, sqlQueryParametersList, parameters, session); } resultCount = session.Batcher.ExecuteNonQuery(ps); } finally { if (ps != null) { session.Batcher.CloseCommand(ps, null); } } } catch (DbException e) { throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not insert/select ids for bulk update", idInsertSelect); } // Start performing the updates for (int i = 0; i < updates.Length; i++) { if (updates[i] == null) { continue; } try { try { var sqlQueryParametersList = updates[i].GetParameters().ToList(); var paramsSpec = hqlParameters[i]; SqlType[] parameterTypes = paramsSpec.GetQueryParameterTypes(sqlQueryParametersList, session.Factory); ps = session.Batcher.PrepareCommand(CommandType.Text, updates[i], parameterTypes); foreach (var parameterSpecification in paramsSpec) { parameterSpecification.Bind(ps, sqlQueryParametersList, parameters, session); } session.Batcher.ExecuteNonQuery(ps); } finally { if (ps != null) { session.Batcher.CloseCommand(ps, null); } } } catch (DbException e) { throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "error performing bulk update", updates[i]); } } return(resultCount); } finally { DropTemporaryTableIfNecessary(persister, session); } }
public override int Execute(QueryParameters parameters, ISessionImplementor session) { CoordinateSharedCacheCleanup(session); CreateTemporaryTableIfNecessary(persister, session); try { // First, save off the pertinent ids, saving the number of pertinent ids for return DbCommand ps = null; int resultCount; try { try { var paramsSpec = Walker.Parameters; var sqlQueryParametersList = idInsertSelect.GetParameters().ToList(); SqlType[] parameterTypes = paramsSpec.GetQueryParameterTypes(sqlQueryParametersList, session.Factory); ps = session.Batcher.PrepareCommand(CommandType.Text, idInsertSelect, parameterTypes); foreach (var parameterSpecification in paramsSpec) { parameterSpecification.Bind(ps, sqlQueryParametersList, parameters, session); } resultCount = session.Batcher.ExecuteNonQuery(ps); } finally { if (ps != null) { session.Batcher.CloseCommand(ps, null); } } } catch (DbException e) { throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not insert/select ids for bulk delete", idInsertSelect); } // Start performing the deletes for (int i = 0; i < deletes.Length; i++) { try { try { ps = session.Batcher.PrepareCommand(CommandType.Text, deletes[i], new SqlType[0]); session.Batcher.ExecuteNonQuery(ps); } finally { if (ps != null) { session.Batcher.CloseCommand(ps, null); } } } catch (DbException e) { throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "error performing bulk delete", deletes[i]); } } return(resultCount); } finally { DropTemporaryTableIfNecessary(persister, session); } }