forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplatedViolatedConstraintNameExtracter.cs
43 lines (39 loc) · 1.86 KB
/
TemplatedViolatedConstraintNameExtracter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Data.Common;
namespace NHibernate.Exceptions
{
/// <summary>
/// Knows how to extract a violated constraint name from an error message based on the
/// fact that the constraint name is templated within the message.
/// </summary>
public abstract class TemplatedViolatedConstraintNameExtracter : IViolatedConstraintNameExtracter
{
/// <summary>
/// Extracts the constraint name based on a template (i.e., <i>templateStart</i><b>constraintName</b><i>templateEnd</i>).
/// </summary>
/// <param name="templateStart">The pattern denoting the start of the constraint name within the message.</param>
/// <param name="templateEnd">The pattern denoting the end of the constraint name within the message.</param>
/// <param name="message">The templated error message containing the constraint name.</param>
/// <returns>The found constraint name, or null.</returns>
protected string ExtractUsingTemplate(string templateStart, string templateEnd, string message)
{
int templateStartPosition = message.IndexOf(templateStart);
if (templateStartPosition < 0)
{
return null;
}
int start = templateStartPosition + templateStart.Length;
int end = message.IndexOf(templateEnd, start);
if (end < 0)
{
end = message.Length;
}
return message.Substring(start, end);
}
/// <summary>
/// Extract the name of the violated constraint from the given SQLException.
/// </summary>
/// <param name="sqle">The exception that was the result of the constraint violation. </param>
/// <returns> The extracted constraint name. </returns>
public abstract string ExtractConstraintName(DbException sqle);
}
}