int PrepareSimpleQuery(StringBuilder sb, string query, IList userParametersList, int userParametersListStart) { int queryCurrentPosition = 0; int userParametersListPosition = userParametersListStart; if (userParametersList.Count > 0) { for (SimpleMatch m = ParameterRegExp.Match(query); m.Success;m = m.NextMatch()) { SimpleCapture parameterCapture = m; sb.Append(query,queryCurrentPosition,parameterCapture.Index - queryCurrentPosition); // advance in query queryCurrentPosition = parameterCapture.Index + parameterCapture.Length; AbstractDbParameter userParameter = GetUserParameter(parameterCapture.Value, userParametersList, userParametersListPosition); if (userParameter != null) { if (IsNullParameter(userParameter)) { sb.Append("null"); NullParametersInPrepare = true; } else { sb.Append('?'); InternalParameters.Add(userParameter); } // advance in user parameters userParametersListPosition++; } else { sb.Append(parameterCapture.Value); } } } sb.Append(query,queryCurrentPosition,query.Length - queryCurrentPosition); int userParamsConsumed = userParametersListPosition - userParametersListStart; if ((Behavior & CommandBehavior.KeyInfo) == 0) return userParamsConsumed; AbstractDBConnection connection = (AbstractDBConnection)Connection; if (connection == null) return userParamsConsumed; string dbname = connection.JdbcConnection.getMetaData().getDatabaseProductName(); if (dbname == "Microsoft SQL Server") { //must add "FOR BROWSE" for selects #if USE_DOTNET_REGEX if (!SqlStatementsHelper.ForBrowseStatementReqExp.IsMatch(query)) sb.Append(" FOR BROWSE"); #else if (!SqlStatementsHelper.ForBrowseStatementReqExp.matcher ((java.lang.CharSequence)(object)query).find ()) sb.Append (" FOR BROWSE"); #endif } return userParamsConsumed; }
private string PrepareCommandTextAndParameters() { NullParametersInPrepare = false; switch (CommandType) { case CommandType.TableDirect : return CreateTableDirectCommandText(CommandText); case CommandType.StoredProcedure : return CreateStoredProcedureCommandTextSimple (InternalCommandText, Parameters, DeriveParameters (InternalCommandText, false)); case CommandType.Text : int userParametersPosition = 0; int charsConsumed = 0; StringBuilder sb = new StringBuilder(CommandText.Length); for (SimpleMatch match = SqlStatementsHelper.CompoundStatementSplitterReqExp.Match(CommandText); match.Success; match = match.NextMatch()) { int length = match.Length; if (length == 0) continue; int start = match.Index; string value = match.Value; sb.Append(CommandText, charsConsumed, start-charsConsumed); charsConsumed = start + length; #if USE_DOTNET_REGEX Match storedProcMatch = StoredProcedureRegExp.Match(value); // count parameters for all kinds of simple statements userParametersPosition += (storedProcMatch.Success) ? // statement is stored procedure call CreateStoredProcedureCommandText(sb, value, storedProcMatch, Parameters, userParametersPosition) : // statement is a simple SQL query PrepareSimpleQuery(sb, value, Parameters, userParametersPosition); #else Matcher storedProcMatch = StoredProcedureRegExp.matcher((java.lang.CharSequence)(object)value); userParametersPosition += (storedProcMatch.find()) ? // statement is stored procedure call CreateStoredProcedureCommandText(sb, value, storedProcMatch, Parameters, userParametersPosition) : // statement is a simple SQL query PrepareSimpleQuery(sb, value, Parameters, userParametersPosition); #endif } sb.Append(CommandText, charsConsumed, CommandText.Length-charsConsumed); return sb.ToString(); } return null; }
int CreateStoredProcedureCommandText(StringBuilder sb, string sql, Matcher match, IDataParameterCollection userParams, int userParamsStartPosition) #endif { int curUserPos = userParamsStartPosition; #if USE_DOTNET_REGEX Group procNameGroup = null; for (Match procNameMatch = match; procNameMatch.Success; procNameMatch = procNameMatch.NextMatch()){ procNameGroup = match.Groups["PROCNAME"]; if (!procNameGroup.Success) { continue; } } if (procNameGroup == null || !procNameGroup.Success) throw new ArgumentException("Not a stored procedure call: '{0}'", sql); ArrayList derivedParameters = DeriveParameters(procNameGroup.Value, false); #else ArrayList derivedParameters = DeriveParameters(match.group(2).Trim(), false); #endif int curDerivedPos = 0; AbstractDbParameter retValderivedParameter = curDerivedPos < derivedParameters.Count ? (AbstractDbParameter)derivedParameters[curDerivedPos] : null; if (retValderivedParameter != null && retValderivedParameter.Direction == ParameterDirection.ReturnValue) curDerivedPos++; int queryCurrentPosition = 0; #if USE_DOTNET_REGEX for (Match retValMatch = match; retValMatch.Success; retValMatch = retValMatch.NextMatch()){ Group retval = retValMatch.Groups["RETVAL"]; if (!retval.Success) { continue; } int retvalIndex = retval.Index; string retvalValue = retval.Value; int retvalLength = retval.Length; #else int retvalIndex = match.start(1); for (;retvalIndex >= 0;) { string retvalValue = match.group(1); int retvalLength = retvalValue.Length; #endif sb.Append(sql, queryCurrentPosition, retvalIndex); AbstractDbParameter userParameter = GetUserParameter(retvalValue, userParams, curUserPos); if (userParameter != null) { sb.Append('?'); InternalParameters.Add(userParameter); if (retValderivedParameter != null && !userParameter.IsDbTypeSet) { userParameter.JdbcType = retValderivedParameter.JdbcType; } curUserPos++; } else { sb.Append(retvalValue); } queryCurrentPosition = (retvalIndex + retvalLength); break; } #if USE_DOTNET_REGEX sb.Append(sql, queryCurrentPosition, procNameGroup.Index + procNameGroup.Length - queryCurrentPosition); queryCurrentPosition = procNameGroup.Index + procNameGroup.Length; #else sb.Append(sql, queryCurrentPosition, match.end(2) - queryCurrentPosition); queryCurrentPosition = match.end(2); #endif bool hasUserParams = false; #if USE_DOTNET_REGEX must rewrite the regex to not parse params to have single code with java regex #else int paramsStart = match.start(3); if (paramsStart >= 0) { #endif hasUserParams = true; sb.Append(sql,queryCurrentPosition,paramsStart - queryCurrentPosition); queryCurrentPosition = paramsStart; for (SimpleMatch m = SqlStatementsHelper.ProcedureParameterSplitterReqExp.Match(match.group(3)); m.Success;m = m.NextMatch()) { SimpleCapture parameterCapture = m; sb.Append(sql,queryCurrentPosition,paramsStart + parameterCapture.Index - queryCurrentPosition); // advance in query queryCurrentPosition = paramsStart + parameterCapture.Index + parameterCapture.Length; AbstractDbParameter derivedParameter = curDerivedPos < derivedParameters.Count ? (AbstractDbParameter)derivedParameters[curDerivedPos++] : null; //check for special params while (derivedParameter != null && derivedParameter.IsSpecial) { // derived parameter is special - never appears in user parameters or user values InternalParameters.Add((AbstractDbParameter)derivedParameter.Clone()); sb.Append('?'); sb.Append(','); derivedParameter = curDerivedPos < derivedParameters.Count ? (AbstractDbParameter)derivedParameters[curDerivedPos++] : null; } AbstractDbParameter userParameter = GetUserParameter(parameterCapture.Value.Trim(), userParams, curUserPos); if (userParameter != null) { sb.Append('?'); InternalParameters.Add(userParameter); if (derivedParameter != null && !userParameter.IsDbTypeSet) { userParameter.JdbcType = derivedParameter.JdbcType; } // advance in user parameters curUserPos++; } else { sb.Append(parameterCapture.Value); } } } bool addedSpecialParams = false; for (int i = curDerivedPos; i < derivedParameters.Count;) { AbstractDbParameter derivedParameter = (AbstractDbParameter)derivedParameters[i++]; if (derivedParameter.IsSpecial) { // derived parameter is special - never appears in user parameters or user values if (!hasUserParams && !addedSpecialParams) { addedSpecialParams = true; curDerivedPos++; sb.Append('('); } for (;curDerivedPos < i;curDerivedPos++) sb.Append(','); InternalParameters.Add((AbstractDbParameter)derivedParameter.Clone()); sb.Append('?'); } } if (!hasUserParams && addedSpecialParams) sb.Append(')'); sb.Append(sql,queryCurrentPosition,sql.Length - queryCurrentPosition); return curUserPos - userParamsStartPosition; }