-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathSqlBaseBuilder.cs
112 lines (97 loc) · 3.38 KB
/
SqlBaseBuilder.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.SqlCommand
{
/// <summary>
/// The base class for all of the SqlBuilders.
/// </summary>
public abstract class SqlBaseBuilder
{
private readonly Dialect.Dialect dialect;
private readonly IMapping mapping;
protected SqlBaseBuilder(Dialect.Dialect dialect, IMapping mapping)
{
this.dialect = dialect;
this.mapping = mapping;
}
protected IMapping Mapping
{
get { return mapping; }
}
public Dialect.Dialect Dialect
{
get { return dialect; }
}
/// <summary>
/// Converts the ColumnNames and ColumnValues to a WhereFragment
/// </summary>
/// <param name="columnNames">The names of the Columns to Add to the WhereFragment</param>
/// <returns>A SqlString that contains the WhereFragment</returns>
/// <remarks>This just calls the overloaded ToWhereFragment() with the operator as " = " and the tableAlias null.</remarks>
protected SqlString ToWhereString(string[] columnNames)
{
return ToWhereString(columnNames, " = ");
}
/// <summary>
/// Converts the ColumnNames and ColumnValues to a WhereFragment
/// </summary>
/// <param name="tableAlias">The Alias for the Table.</param>
/// <param name="columnNames">The names of the Columns to Add to the WhereFragment</param>
/// <returns>A SqlString that contains the WhereFragment</returns>
/// <remarks>This defaults the op to " = "</remarks>
protected SqlString ToWhereString(string tableAlias, string[] columnNames)
{
return ToWhereString(tableAlias, columnNames, " = ");
}
/// <summary>
/// Converts the ColumnNames and ColumnValues to a WhereFragment
/// </summary>
/// <param name="columnNames">The names of the Columns to Add to the WhereFragment</param>
/// <param name="op">The operator to use between the names & values. For example " = " or "!="</param>
/// <returns>A SqlString that contains the WhereFragment</returns>
protected SqlString ToWhereString(string[] columnNames, string op)
{
return ToWhereString(null, columnNames, op);
}
/// <summary>
/// Converts the ColumnNames and ColumnValues to a WhereFragment
/// </summary>
/// <param name="tableAlias">The Alias for the Table.</param>
/// <param name="columnNames">The names of the Columns to Add to the WhereFragment</param>
/// <param name="op">The operator to use between the names & values. For example " = " or "!="</param>
/// <returns>A SqlString that contains the WhereFragment</returns>
protected SqlString ToWhereString(string tableAlias, string[] columnNames, string op)
{
SqlStringBuilder sqlBuilder = new SqlStringBuilder((columnNames.Length * 2) + 5);
bool andNeeded = false;
for (int i = 0; i < columnNames.Length; i++)
{
if (string.IsNullOrEmpty(columnNames[i])) continue; // prevent empty column name
if (andNeeded)
{
sqlBuilder.Add(" AND ");
}
andNeeded = true;
string columnName;
if (tableAlias != null && tableAlias.Length > 0)
{
columnName = tableAlias + StringHelper.Dot + columnNames[i];
}
else
{
columnName = columnNames[i];
}
sqlBuilder
.Add(columnName)
.Add(op)
.AddParameter();
}
return sqlBuilder.ToSqlString();
}
protected SqlString ToWhereString(string columnName, string op)
{
if (string.IsNullOrEmpty(columnName)) return null;
return new SqlString(columnName, op, Parameter.Placeholder);
}
}
}