/src/NHibernate/SqlCommand/SqlStringHelper.cs
https://github.com/RogerKratz/nhibernate-core · C# · 167 lines · 125 code · 33 blank · 9 comment · 28 complexity · a8d9d40ec93252284d51c682ff4afd82 MD5 · raw file
- 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();
- }
- }
- }