/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

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace NHibernate.SqlCommand
  7. {
  8. /// <summary>
  9. /// Helper methods for SqlString.
  10. /// </summary>
  11. public static class SqlStringHelper
  12. {
  13. public static SqlString Join(SqlString separator, IEnumerable objects)
  14. {
  15. SqlStringBuilder buf = new SqlStringBuilder();
  16. bool first = true;
  17. foreach (object obj in objects)
  18. {
  19. if (!first)
  20. {
  21. buf.Add(separator);
  22. }
  23. first = false;
  24. buf.AddObject(obj);
  25. }
  26. return buf.ToSqlString();
  27. }
  28. internal static SqlString JoinParts(object separator, IList<object> parts)
  29. {
  30. if (parts.Count == 0)
  31. return SqlString.Empty;
  32. if (parts.Count == 1)
  33. return parts[0] is SqlString sqlstring
  34. ? sqlstring
  35. : new SqlString(parts);
  36. var buf = new SqlStringBuilder();
  37. buf.AddObject(parts[0]);
  38. for (var index = 1; index < parts.Count; index++)
  39. {
  40. buf.AddObject(separator).AddObject(parts[index]);
  41. }
  42. return buf.ToSqlString();
  43. }
  44. internal static SqlString Join(string separator, IList<SqlString> strings)
  45. {
  46. if (strings.Count == 0)
  47. return SqlString.Empty;
  48. if (strings.Count == 1)
  49. return strings[0];
  50. var buf = new SqlStringBuilder();
  51. buf.Add(strings[0]);
  52. for (var index = 1; index < strings.Count; index++)
  53. {
  54. buf.Add(separator).Add(strings[index]);
  55. }
  56. return buf.ToSqlString();
  57. }
  58. public static SqlString[] Add(SqlString[] x, string sep, SqlString[] y)
  59. {
  60. SqlString[] result = new SqlString[x.Length];
  61. for (int i = 0; i < x.Length; i++)
  62. {
  63. result[i] = new SqlString(x[i], sep, y[i]);
  64. }
  65. return result;
  66. }
  67. /// <summary>
  68. /// Removes the <c>as someColumnAlias</c> clause from a <c>SqlString</c> representing a column expression.
  69. /// Consider using <c>CriterionUtil.GetColumn...</c> methods instead.
  70. /// </summary>
  71. /// <param name="sql">The <c>SqlString</c> representing a column expression which might be aliased.</param>
  72. /// <returns><paramref name="sql" /> if it was not aliased, otherwise an un-aliased <c>SqlString</c> representing the column.</returns>
  73. public static SqlString RemoveAsAliasesFromSql(SqlString sql)
  74. {
  75. int index = sql.LastIndexOfCaseInsensitive(" as ");
  76. if (index < 0) return sql;
  77. return sql.Substring(0, index);
  78. }
  79. public static bool IsNotEmpty(SqlString str)
  80. {
  81. return !IsEmpty(str);
  82. }
  83. public static bool IsEmpty(SqlString str)
  84. {
  85. return str == null || str.Count == 0;
  86. }
  87. internal static SqlString ParametersList(List<Parameter> parameters)
  88. {
  89. var parametersCount = parameters.Count;
  90. if (parametersCount == 0)
  91. {
  92. return SqlString.Empty;
  93. }
  94. if (parametersCount == 1)
  95. {
  96. return new SqlString(parameters[0]);
  97. }
  98. var builder = new SqlStringBuilder();
  99. builder.Add("(");
  100. builder.Add(parameters[0]);
  101. for (var index = 1; index < parametersCount; index++)
  102. {
  103. builder.Add(", ");
  104. builder.Add(parameters[index]);
  105. }
  106. builder.Add(")");
  107. return builder.ToSqlString();
  108. }
  109. internal static SqlString Repeat(SqlString placeholder, int count, string separator, bool wrapInParens)
  110. {
  111. if (count == 0)
  112. return SqlString.Empty;
  113. if (count == 1)
  114. return wrapInParens
  115. ? new SqlString("(", placeholder, ")")
  116. : placeholder;
  117. var builder = new SqlStringBuilder((placeholder.Count + 1) * count + 1);
  118. if (wrapInParens)
  119. {
  120. builder.Add("(");
  121. }
  122. builder.Add(placeholder);
  123. for (int i = 1; i < count; i++)
  124. {
  125. builder.Add(separator).Add(placeholder);
  126. }
  127. if (wrapInParens)
  128. {
  129. builder.Add(")");
  130. }
  131. return builder.ToSqlString();
  132. }
  133. }
  134. }