forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlStringHelper.cs
167 lines (134 loc) · 3.59 KB
/
SqlStringHelper.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate.SqlCommand
{
/// <summary>
/// Helper methods for SqlString.
/// </summary>
public static class SqlStringHelper
{
public static SqlString Join(SqlString separator, IEnumerable objects)
{
SqlStringBuilder buf = new SqlStringBuilder();
bool first = true;
foreach (object obj in objects)
{
if (!first)
{
buf.Add(separator);
}
first = false;
buf.AddObject(obj);
}
return buf.ToSqlString();
}
internal static SqlString JoinParts(object separator, IList<object> parts)
{
if (parts.Count == 0)
return SqlString.Empty;
if (parts.Count == 1)
return parts[0] is SqlString sqlstring
? sqlstring
: new SqlString(parts);
var buf = new SqlStringBuilder();
buf.AddObject(parts[0]);
for (var index = 1; index < parts.Count; index++)
{
buf.AddObject(separator).AddObject(parts[index]);
}
return buf.ToSqlString();
}
internal static SqlString Join(string separator, IList<SqlString> strings)
{
if (strings.Count == 0)
return SqlString.Empty;
if (strings.Count == 1)
return strings[0];
var buf = new SqlStringBuilder();
buf.Add(strings[0]);
for (var index = 1; index < strings.Count; index++)
{
buf.Add(separator).Add(strings[index]);
}
return buf.ToSqlString();
}
public static SqlString[] Add(SqlString[] x, string sep, SqlString[] y)
{
SqlString[] result = new SqlString[x.Length];
for (int i = 0; i < x.Length; i++)
{
result[i] = new SqlString(x[i], sep, y[i]);
}
return result;
}
/// <summary>
/// Removes the <c>as someColumnAlias</c> clause from a <c>SqlString</c> representing a column expression.
/// Consider using <c>CriterionUtil.GetColumn...</c> methods instead.
/// </summary>
/// <param name="sql">The <c>SqlString</c> representing a column expression which might be aliased.</param>
/// <returns><paramref name="sql" /> if it was not aliased, otherwise an un-aliased <c>SqlString</c> representing the column.</returns>
public static SqlString RemoveAsAliasesFromSql(SqlString sql)
{
int index = sql.LastIndexOfCaseInsensitive(" as ");
if (index < 0) return sql;
return sql.Substring(0, index);
}
public static bool IsNotEmpty(SqlString str)
{
return !IsEmpty(str);
}
public static bool IsEmpty(SqlString str)
{
return str == null || str.Count == 0;
}
internal static SqlString ParametersList(List<Parameter> parameters)
{
var parametersCount = parameters.Count;
if (parametersCount == 0)
{
return SqlString.Empty;
}
if (parametersCount == 1)
{
return new SqlString(parameters[0]);
}
var builder = new SqlStringBuilder();
builder.Add("(");
builder.Add(parameters[0]);
for (var index = 1; index < parametersCount; index++)
{
builder.Add(", ");
builder.Add(parameters[index]);
}
builder.Add(")");
return builder.ToSqlString();
}
internal static SqlString Repeat(SqlString placeholder, int count, string separator, bool wrapInParens)
{
if (count == 0)
return SqlString.Empty;
if (count == 1)
return wrapInParens
? new SqlString("(", placeholder, ")")
: placeholder;
var builder = new SqlStringBuilder((placeholder.Count + 1) * count + 1);
if (wrapInParens)
{
builder.Add("(");
}
builder.Add(placeholder);
for (int i = 1; i < count; i++)
{
builder.Add(separator).Add(placeholder);
}
if (wrapInParens)
{
builder.Add(")");
}
return builder.ToSqlString();
}
}
}