Skip to content
24 changes: 21 additions & 3 deletions Source/Core/Core/ParameterNameMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public const string NullOnlyForReturn
public const string ProcedureDeclares
= "The procedure declares";

/// <summary>
/// The words before the names that a command or a query declares, when
/// it is not said to be a procedure.
/// </summary>
public const string ItDeclares
= "It declares";

/// <summary>
/// The words before the names that a command declares, by its type: a
/// text command is not a procedure.
/// </summary>
/// <param name="type">The command's type.</param>
/// <returns>The words.</returns>
public static string DeclaresWords(CommandType type)
=> type == CommandType.Text ? ItDeclares : ProcedureDeclares;

/// <summary>
/// The call that names a parameter, as the fix of a message writes it.
/// </summary>
Expand Down Expand Up @@ -189,10 +205,11 @@ public static string NameWithColumn(string subject)
/// <param name="subject">What was written, as the message starts.</param>
/// <param name="first">The first parameter it fits.</param>
/// <param name="second">The second parameter it fits.</param>
/// <param name="procedure">The procedure.</param>
/// <param name="procedure">The procedure or the command text.</param>
/// <param name="candidate">The candidate name that fits both.</param>
/// <param name="tier">How the candidate fits them.</param>
/// <param name="declared">The parameters the procedure declares.</param>
/// <param name="declares">The words before the declared names.</param>
/// <param name="declared">The parameters the owner declares.</param>
/// <param name="fix">What to do.</param>
/// <returns>The message.</returns>
public static string Ambiguous(
Expand All @@ -202,11 +219,12 @@ public static string Ambiguous(
string procedure,
string candidate,
string tier,
string declares,
string declared,
string fix)
=> $"{subject} matches both '{first}' and '{second}' of '{procedure}'. "
+ $"Its candidate name '{candidate}' fits each of them {tier}. "
+ $"The procedure declares: {declared}. {fix}";
+ $"{declares}: {declared}. {fix}";

/// <summary>An argument fits none of the parameters.</summary>
/// <param name="expression">The text of the argument.</param>
Expand Down
17 changes: 13 additions & 4 deletions Source/Core/DbQueryBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ public static string ResolveDerived(
ParameterNames.DerivedExpression.Resolution? last = derived.Last;
if (last is null || !ReferenceEquals(last.Names, names))
{
string real = names!.Match(derived.Expression, definition.CommandText)
string real = names!.Match(
derived.Expression,
definition.CommandText,
declares: ParameterNameMessages.ItDeclares)
?? throw NoMatch(
derived.Expression, definition.CommandText,
"It declares", names!.Names);
ParameterNameMessages.ItDeclares, names!.Names);
last = new(names!, real, names!.Find(real));
derived.Last = last;
}
Expand Down Expand Up @@ -119,7 +122,10 @@ var members
for (int i = 0; i < members.Names.Length; i++)
{
string? name = known
? names!.Match(members.Names[i], definition.CommandText)
? names!.Match(
members.Names[i],
definition.CommandText,
declares: ParameterNameMessages.ItDeclares)
: members.Names[i];
if (name is null || whole.Claims(name, names, definition)) continue;

Expand Down Expand Up @@ -182,7 +188,10 @@ static DbQuery.MemberMatch Resolved<T>(
var entries = new NameSet.Entry?[members.Length];
for (int i = 0; i < members.Length; i++)
{
resolved[i] = names.Match(members[i], definition.CommandText);
resolved[i] = names.Match(
members[i],
definition.CommandText,
declares: ParameterNameMessages.ItDeclares);
if (resolved[i] is { } real) entries[i] = names.Find(real);
}

Expand Down
16 changes: 12 additions & 4 deletions Source/Core/ExpressiveCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,10 @@ void Apply(
? p.Name
: scan
? TextNameCache.Scan(Command, expression)
: real!.Match(expression, Command);
: real!.Match(
expression,
Command,
declares: DeclaresWords);
if (name is null)
{
// A member of an object that the procedure does not take is simply not sent; an argument named by the caller must match.
Expand Down Expand Up @@ -1222,12 +1225,17 @@ InvalidOperationException Collision(string name, Param first, Param second, Immu
name,
Written(first),
Written(second),
$"The procedure '{Command}'",
Type == CommandType.Text
? $"'{Command}'"
: $"The procedure '{Command}'",
real));

InvalidOperationException NoMatch(string expression, ImmutableArray<string> real)
=> DbQueryBinding.NoMatch(
expression, Command, ParameterNameMessages.ProcedureDeclares, real);
=> DbQueryBinding.NoMatch(expression, Command, DeclaresWords, real);

// What a message says before the names the command declares: a text
// command is not a procedure.
string DeclaresWords => ParameterNameMessages.DeclaresWords(Type);

#endregion

Expand Down
22 changes: 18 additions & 4 deletions Source/Core/NameSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,23 @@ public void KeepMetadata(ParameterMetadata metadata)
/// <param name="procedure">The procedure's name or the command text, for the message of an ambiguity.</param>
/// <param name="subject">What matches, for the message of an ambiguity.</param>
/// <param name="fix">What to do about an ambiguity.</param>
/// <param name="declares">The words before the declared names in the message of an ambiguity.</param>
/// <returns>The real name, or <see langword="null"/> when the name matches none.</returns>
/// <exception cref="InvalidOperationException">The name fits more than one parameter equally well.</exception>
public string? MatchName(string name, string procedure, string subject, string fix)
public string? MatchName(
string name,
string procedure,
string subject,
string fix,
string declares)
{
if (_nameMatches.TryGetValue(name, out string? remembered)) return remembered;

string bare = name.Length > 0 && name[0] == '@' ? name[1..] : name;

// Two threads may both match and store: the answer is the same.
string? found = ParameterNames.MatchBareAs([bare], Names, BareNames, name, procedure, subject, fix);
string? found = ParameterNames.MatchBareAs(
[bare], Names, BareNames, name, procedure, subject, fix, declares);
if (_nameMatches.Count < MaxMatches) _nameMatches[name] = found;

return found;
Expand Down Expand Up @@ -185,18 +192,25 @@ Dictionary<string, Entry> BuildEntries()
/// <param name="expression">The text of the argument, or the name of a member of an <c>AddParams</c> object.</param>
/// <param name="procedure">The procedure's name or the command text, for the message of an ambiguity.</param>
/// <param name="subject">What matches, for the message of an ambiguity; <see langword="null"/> names the argument.</param>
/// <param name="declares">The words before the declared names in the message of an ambiguity.</param>
/// <returns>The real name, or <see langword="null"/> when the expression matches none.</returns>
/// <exception cref="ArgumentException">The expression is not derivable.</exception>
/// <exception cref="InvalidOperationException">The expression fits more than one name equally well.</exception>
public string? Match(string expression, string procedure, string? subject = null)
public string? Match(
string expression,
string procedure,
string? subject = null,
string declares = ParameterNameMessages.ProcedureDeclares)
{
if (_matches.TryGetValue(expression, out string? remembered)) return remembered;

// An argument of AddParam(value) was described when it was added; the member of an AddParams object is not derivable if it is a verbatim keyword, which Candidates reports.
ImmutableArray<string> candidates = ParameterNames.Describe(expression)?.Candidates ?? ParameterNames.Candidates(expression);

// Two threads may both match and store: the answer is the same.
string? found = ParameterNames.MatchBareAs(candidates, Names, BareNames, expression, procedure, subject);
string? found = ParameterNames.MatchBareAs(
candidates, Names, BareNames, expression, procedure, subject,
declares: declares);
if (_matches.Count < MaxMatches) _matches[expression] = found;

return found;
Expand Down
20 changes: 15 additions & 5 deletions Source/Core/OutputBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void Resolve(
continue;
}

resolved[i] = Find(member, real!, commandText)
resolved[i] = Find(member, real!, type, commandText)
?? throw NoOutputMatch(member, real!.Names, where);
}

Expand Down Expand Up @@ -229,18 +229,28 @@ static void CheckMetadata(
// An exact member ([Column]) names its parameter; the others go through
// the tiers of AddParam's names: a named output over the name it was given
// (or the candidates it carries), a model member over the member's name.
static string? Find(OutputMember member, NameSet real, string commandText)
static string? Find(
OutputMember member,
NameSet real,
CommandType type,
string commandText)
{
string declares = ParameterNameMessages.DeclaresWords(type);
if (member.Exact) return real.Find(member.Name)?.Real;
if (!member.Named) return real.Match(member.Name, commandText, member.Display);
if (!member.Named)
{
return real.Match(
member.Name, commandText, member.Display, declares);
}

string fix = OutputArguments.ExactNameFix(member.Form);

// A named output or definition: one candidate, the name as given,
// remembered by that name.
if (member.Form != OutputForm.Capture)
{
return real.MatchName(member.Name, commandText, member.Display, fix);
return real.MatchName(
member.Name, commandText, member.Display, fix, declares);
}

ImmutableArray<string> candidates = member.Candidates.IsDefault
Expand All @@ -253,7 +263,7 @@ static void CheckMetadata(
// by the name, answers for one candidate.
return ParameterNames.MatchBareAs(
candidates, real.Names, real.BareNames,
member.Name, commandText, member.Display, fix);
member.Name, commandText, member.Display, fix, declares);
}

/// <summary>
Expand Down
11 changes: 7 additions & 4 deletions Source/Core/ParameterNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,20 @@ static bool ClosesAt(string s, int open, int close)
/// <param name="procedure">The procedure's name, for the message of an ambiguity.</param>
/// <param name="subject">What matches, for the message of an ambiguity; <see langword="null"/> names the argument.</param>
/// <param name="fix">What to do about an ambiguity, ending its message; <see langword="null"/> advises <c>[Column]</c> on the subject, or an explicit <c>AddParam</c> name.</param>
/// <param name="declares">The words before the declared names in the message of an ambiguity.</param>
internal static string? MatchBareAs(
ImmutableArray<string> candidates,
ImmutableArray<string> parameterNames,
ImmutableArray<string> bareNames,
string expression,
string procedure,
string? subject,
string? fix = null)
string? fix = null,
string declares = ParameterNameMessages.ProcedureDeclares)
{
foreach (string candidate in candidates)
{
if (Find(expression, procedure, candidate, parameterNames, bareNames, subject, fix) is { } found) return found;
if (Find(expression, procedure, candidate, parameterNames, bareNames, subject, fix, declares) is { } found) return found;
}

return null;
Expand All @@ -335,7 +337,7 @@ static bool ClosesAt(string s, int open, int close)
_ => ParameterNameMessages.TierWithoutPrefix,
};

static string? Find(string expression, string procedure, string candidate, ImmutableArray<string> parameterNames, ImmutableArray<string> bareNames, string? subject, string? fix = null)
static string? Find(string expression, string procedure, string candidate, ImmutableArray<string> parameterNames, ImmutableArray<string> bareNames, string? subject, string? fix, string declares)
{
// Tier 1: exact. Tier 2: ignoring case. Tier 3: ignoring case and underscores. Tier 4: the same without one common parameter prefix.
for (int tier = 1; tier <= 4; tier++)
Expand All @@ -351,7 +353,8 @@ static bool ClosesAt(string s, int open, int close)
ParameterNameMessages.Ambiguous(
subject ?? ParameterNameMessages.TheArgument(expression),
found, real, procedure, candidate,
TierText(tier), string.Join(", ", parameterNames),
TierText(tier), declares,
string.Join(", ", parameterNames),
fix ?? (subject is null
? ParameterNameMessages.NameParameterExplicitly
: ParameterNameMessages.NameWithColumn(subject))));
Expand Down
Loading