public override SqlString ApplyLocksToSql(SqlString sql, IDictionary <string, LockMode> aliasedLockModes, IDictionary <string, string[]> keyColumnNames) { bool doWork = false; foreach (KeyValuePair <string, LockMode> de in aliasedLockModes) { if (NeedsLockHint(de.Value)) { doWork = true; break; } } if (!doWork) { return(sql); } // Regex matching any alias out of those given. Aliases should contain // no dangerous characters (they are identifiers) so they are not escaped. string aliasesPattern = StringHelper.Join("|", aliasedLockModes.Keys); // Match < alias >, < alias,>, or < alias$>, the intent is to capture alias names // in various kinds of "FROM table1 alias1, table2 alias2". Regex matchRegex = new Regex(" (" + aliasesPattern + ")([, ]|$)"); SqlStringBuilder result = new SqlStringBuilder(); MatchEvaluator evaluator = new LockHintAppender(this, aliasedLockModes).ReplaceMatch; foreach (object part in sql) { var parameter = part as Parameter; if (parameter != null) { result.Add(parameter); } else { result.Add(matchRegex.Replace((string)part, evaluator)); } } return(result.ToSqlString()); }
public override SqlString ApplyLocksToSql(SqlString sql, IDictionary<string, LockMode> aliasedLockModes, IDictionary<string, string[]> keyColumnNames) { bool doWork = false; foreach (KeyValuePair<string, LockMode> de in aliasedLockModes) { if (NeedsLockHint(de.Value)) { doWork = true; break; } } if (!doWork) { return sql; } // Regex matching any alias out of those given. Aliases should contain // no dangerous characters (they are identifiers) so they are not escaped. string aliasesPattern = StringHelper.Join("|", aliasedLockModes.Keys); // Match < alias >, < alias,>, or < alias$>, the intent is to capture alias names // in various kinds of "FROM table1 alias1, table2 alias2". Regex matchRegex = new Regex(" (" + aliasesPattern + ")([, ]|$)"); SqlStringBuilder result = new SqlStringBuilder(); MatchEvaluator evaluator = new LockHintAppender(this, aliasedLockModes).ReplaceMatch; foreach (object part in sql.Parts) { if (part == Parameter.Placeholder) { result.Add((Parameter)part); continue; } result.Add(matchRegex.Replace((string)part, evaluator)); } return result.ToSqlString(); }