forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlStringFormatter.cs
84 lines (72 loc) · 2.65 KB
/
SqlStringFormatter.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.SqlCommand;
using NHibernate.Engine.Query;
namespace NHibernate.Driver
{
public class SqlStringFormatter : ISqlStringVisitor
{
private readonly StringBuilder result = new StringBuilder();
private int parameterIndex = 0;
private readonly ISqlParameterFormatter formatter;
private readonly string multipleQueriesSeparator;
private bool hasReturnParameter;
private bool foundReturnParameter = false;
private readonly List<string> assignedParameterNames = new List<string>();
public SqlStringFormatter(ISqlParameterFormatter formatter, string multipleQueriesSeparator)
{
this.formatter = formatter;
this.multipleQueriesSeparator = multipleQueriesSeparator;
}
public void Format(SqlString text)
{
hasReturnParameter = DetermineIfSqlStringHasReturnParameter(text);
text.Visit(this);
}
public string GetFormattedText()
{
return result.ToString();
}
void ISqlStringVisitor.String(string text)
{
result.Append(text);
}
void ISqlStringVisitor.String(SqlString sqlString)
{
result.Append(sqlString.ToString());
}
void ISqlStringVisitor.Parameter(Parameter parameter)
{
if (hasReturnParameter && !foundReturnParameter)
{
result.Append(parameter);
assignedParameterNames.Add(String.Empty);
foundReturnParameter = true;
return;
}
// NH: even if using SqlType[] the final commad may have X parameters, with this line we will use Y parameters in the DbCommand
// for example the ParameterCollection may contains two parameters called @p0 and @p1 but the command contains just @p0.
// In this way the same parameter can be used in different places in the query without create a problem to the dear SQL-server (see NH1981)
// TODO: find a way to have exactly the same amount of parameters between the final DbCommand and its DbParameterCollection
// A candidateplace is making DriverBase.SetCommandParameters a little bit more intelligent... perhaps SqlString aware (see also DriverBase.SetCommandText, DriverBase.GenerateCommand)
string name = formatter.GetParameterName(parameter.ParameterPosition ?? parameterIndex);
assignedParameterNames.Add(name);
parameterIndex++;
result.Append(name);
}
private bool DetermineIfSqlStringHasReturnParameter(SqlString text)
{
CallableParser.Detail callableDetail = CallableParser.Parse(text.ToString());
return (callableDetail.IsCallable && callableDetail.HasReturn);
}
public bool HasReturnParameter
{
get { return foundReturnParameter; }
}
public string[] AssignedParameterNames
{
get { return assignedParameterNames.ToArray(); }
}
}
}