Skip to content

Commit 34d938d

Browse files
committed
Merge branch 'line-ending-parsing'
2 parents c85d038 + 3c8a1e7 commit 34d938d

File tree

16 files changed

+160
-63
lines changed

16 files changed

+160
-63
lines changed

src/NHibernate.Test/ProjectionFixtures/Fixture.cs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using NHibernate.Criterion;
33
using NHibernate.Dialect;
44
using NHibernate.Driver;
5+
using NHibernate.Exceptions;
56
using NUnit.Framework;
67

78
namespace NHibernate.Test.ProjectionFixtures
@@ -64,47 +65,47 @@ protected override void OnTearDown()
6465
}
6566

6667

67-
[Test]
68-
public void ErrorFromDBWillGiveTheActualSQLExecuted()
69-
{
70-
if (!(Dialect is MsSql2000Dialect))
71-
Assert.Ignore("Test checks for exact sql and expects an error to occur in a case which is not erroneous on all databases.");
72-
73-
string pName = ((ISqlParameterFormatter) sessions.ConnectionProvider.Driver).GetParameterName(0);
74-
string expectedMessagePart0 =
75-
string.Format(
76-
@"could not execute query
77-
[ SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0} ]",
78-
pName);
79-
string expectedMessagePart1 = string.Format(@"[SQL: SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0}]",pName);
80-
81-
DetachedCriteria projection = DetachedCriteria.For<TreeNode>("child")
82-
.Add(Restrictions.Eq("child.Key.Id", 2))
83-
.SetProjection(
84-
Projections.ProjectionList()
85-
.Add(Projections.Property("child.Key.Id"))
86-
.Add(Projections.Count("child.Key.Area"))
87-
);
88-
try
89-
{
90-
using (var s = sessions.OpenSession())
91-
using (var tx = s.BeginTransaction())
92-
{
93-
var criteria = projection.GetExecutableCriteria(s);
94-
criteria.List();
95-
96-
tx.Commit();
97-
}
98-
Assert.Fail();
99-
}
100-
catch (Exception e)
101-
{
102-
if (!e.Message.Contains(expectedMessagePart0) || !e.Message.Contains(expectedMessagePart1))
103-
throw;
104-
}
105-
}
106-
107-
[Test]
68+
[Test]
69+
public void ErrorFromDBWillGiveTheActualSQLExecuted()
70+
{
71+
if (!(Dialect is MsSql2000Dialect))
72+
Assert.Ignore(
73+
"Test checks for exact sql and expects an error to occur in a case which is not erroneous on all databases.");
74+
75+
string pName = ((ISqlParameterFormatter) sessions.ConnectionProvider.Driver).GetParameterName(0);
76+
string expectedMessagePart0 =
77+
string.Format("could not execute query" + Environment.NewLine +
78+
"[ SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0} ]",
79+
pName);
80+
string expectedMessagePart1 =
81+
string.Format(
82+
@"[SQL: SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0}]", pName);
83+
84+
DetachedCriteria projection = DetachedCriteria.For<TreeNode>("child")
85+
.Add(Restrictions.Eq("child.Key.Id", 2))
86+
.SetProjection(
87+
Projections.ProjectionList()
88+
.Add(Projections.Property("child.Key.Id"))
89+
.Add(Projections.Count("child.Key.Area"))
90+
);
91+
92+
93+
var e = Assert.Throws<GenericADOException>(() =>
94+
{
95+
using (var s = sessions.OpenSession())
96+
using (var tx = s.BeginTransaction())
97+
{
98+
var criteria = projection.GetExecutableCriteria(s);
99+
criteria.List();
100+
101+
tx.Commit();
102+
}
103+
});
104+
105+
Assert.That(e.Message, Is.StringContaining(expectedMessagePart0).Or.StringContaining(expectedMessagePart1));
106+
}
107+
108+
[Test]
108109
public void AggregatingHirearchyWithCount()
109110
{
110111
var root = new Key {Id = 1, Area = 2};

src/NHibernate.Test/UtilityTest/StringHelperFixture.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,35 @@ public void PurgeBackticksEnclosing()
137137
Assert.That(StringHelper.PurgeBackticksEnclosing("something`"), Is.EqualTo("something`"));
138138
Assert.That(StringHelper.PurgeBackticksEnclosing("`something`"), Is.EqualTo("something"));
139139
}
140+
141+
142+
[TestCase("ab", 0, -1, 0)]
143+
[TestCase("a\r\nb", 0, 1, 2)]
144+
[TestCase("a\nb", 0, 1, 1)]
145+
[TestCase("ab\r\nfoo\r\n", 4, 7, 2)]
146+
public void IndexOfAnyNewLineReturnsIndexAndLength(string str, int startIndex, int expectedIndex,
147+
int expectedMatchLength)
148+
{
149+
int matchLength;
150+
var matchIndex = str.IndexOfAnyNewLine(startIndex, out matchLength);
151+
152+
Assert.That(matchIndex, Is.EqualTo(expectedIndex));
153+
Assert.That(matchLength, Is.EqualTo(expectedMatchLength));
154+
}
155+
156+
157+
[TestCase("ab", 0, false, 0)]
158+
[TestCase("a\r\nb", 0, false, 0)]
159+
[TestCase("a\nb", 1, true, 1)]
160+
[TestCase("a\r\nb", 1, true, 2)]
161+
public void IsAnyNewLineMatchAndLength(string str, int startIndex, bool expectNewLine,
162+
int expectedMatchLength)
163+
{
164+
int matchLength;
165+
var isNewLine = str.IsAnyNewLine(startIndex, out matchLength);
166+
167+
Assert.That(isNewLine, Is.EqualTo(expectNewLine));
168+
Assert.That(matchLength, Is.EqualTo(expectedMatchLength));
169+
}
140170
}
141171
}

src/NHibernate/Engine/Query/CallableParser.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class Detail
1616
}
1717

1818
private static readonly Regex functionNameFinder = new Regex(@"\{[\S\s]*call[\s]+([\w\.]+)[^\w]");
19-
private static readonly int NewLineLength = Environment.NewLine.Length;
2019

2120
public static Detail Parse(string sqlString)
2221
{

src/NHibernate/Engine/Query/ParameterParser.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace NHibernate.Engine.Query
1212
/// </summary>
1313
public class ParameterParser
1414
{
15-
private static readonly int NewLineLength = Environment.NewLine.Length;
16-
1715
public interface IRecognizer
1816
{
1917
void OutParameter(int position);
@@ -56,6 +54,8 @@ public static void Parse(string sqlString, IRecognizer recognizer)
5654
bool afterNewLine = false;
5755
for (int indx = 0; indx < stringLength; indx++)
5856
{
57+
int currentNewLineLength;
58+
5959
// check comments
6060
if (indx + 1 < stringLength && sqlString.Substring(indx,2) == "/*")
6161
{
@@ -64,9 +64,11 @@ public static void Parse(string sqlString, IRecognizer recognizer)
6464
indx = closeCommentIdx + 1;
6565
continue;
6666
}
67+
6768
if (afterNewLine && (indx + 1 < stringLength) && sqlString.Substring(indx, 2) == "--")
6869
{
69-
var closeCommentIdx = sqlString.IndexOf(Environment.NewLine, indx + 2);
70+
var closeCommentIdx = sqlString.IndexOfAnyNewLine(indx + 2, out currentNewLineLength);
71+
7072
string comment;
7173
if (closeCommentIdx == -1)
7274
{
@@ -75,16 +77,17 @@ public static void Parse(string sqlString, IRecognizer recognizer)
7577
}
7678
else
7779
{
78-
comment = sqlString.Substring(indx, closeCommentIdx - indx + Environment.NewLine.Length);
80+
comment = sqlString.Substring(indx, closeCommentIdx - indx + currentNewLineLength);
7981
}
8082
recognizer.Other(comment);
81-
indx = closeCommentIdx + NewLineLength - 1;
83+
indx = closeCommentIdx + currentNewLineLength - 1;
8284
continue;
8385
}
84-
if (indx + NewLineLength -1 < stringLength && sqlString.Substring(indx, NewLineLength) == Environment.NewLine)
86+
87+
if (sqlString.IsAnyNewLine(indx, out currentNewLineLength))
8588
{
8689
afterNewLine = true;
87-
indx += NewLineLength - 1;
90+
indx += currentNewLineLength - 1;
8891
recognizer.Other(Environment.NewLine);
8992
continue;
9093
}

src/NHibernate/Exceptions/ADOExceptionHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ public static string ExtendMessage(string message, string sql, object[] paramete
8383
IDictionary<string, TypedValue> namedParameters)
8484
{
8585
var sb = new StringBuilder();
86-
sb.Append(message).Append(Environment.NewLine).Append("[ ").Append(sql ?? SQLNotAvailable).Append(" ]");
86+
sb.Append(message).AppendLine().Append("[ ").Append(sql ?? SQLNotAvailable).Append(" ]");
8787
if (parameterValues != null && parameterValues.Length > 0)
8888
{
89-
sb.Append(Environment.NewLine).Append("Positional parameters: ");
89+
sb.AppendLine().Append("Positional parameters: ");
9090
for (int index = 0; index < parameterValues.Length; index++)
9191
{
9292
object value = parameterValues[index] ?? "null";
@@ -95,14 +95,14 @@ public static string ExtendMessage(string message, string sql, object[] paramete
9595
}
9696
if (namedParameters != null && namedParameters.Count > 0)
9797
{
98-
sb.Append(Environment.NewLine);
98+
sb.AppendLine();
9999
foreach (var namedParameter in namedParameters)
100100
{
101101
object value = namedParameter.Value.Value ?? "null";
102102
sb.Append(" ").Append("Name:").Append(namedParameter.Key).Append(" - Value:").Append(value);
103103
}
104104
}
105-
sb.Append(Environment.NewLine);
105+
sb.AppendLine();
106106
return sb.ToString();
107107
}
108108

src/NHibernate/Impl/CriteriaImpl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public override string ToString()
325325
}
326326
if (orderEntries.Count != 0)
327327
{
328-
builder.Append(Environment.NewLine);
328+
builder.AppendLine();
329329
}
330330
first = true;
331331
foreach (OrderEntry orderEntry in orderEntries)

src/NHibernate/Mapping/ByCode/Impl/DiscriminatorMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using NHibernate.Cfg.MappingSchema;
44
using NHibernate.Type;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Mapping.ByCode.Impl
78
{
@@ -94,7 +95,7 @@ public void Formula(string formula)
9495

9596
ResetColumnPlainValues();
9697
discriminatorMapping.Item = null;
97-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
98+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
9899
if (formulaLines.Length > 1)
99100
{
100101
discriminatorMapping.Item = new HbmFormula {Text = formulaLines};

src/NHibernate/Mapping/ByCode/Impl/ElementMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NHibernate.Cfg.MappingSchema;
55
using NHibernate.Type;
66
using NHibernate.UserTypes;
7+
using NHibernate.Util;
78

89
namespace NHibernate.Mapping.ByCode.Impl
910
{
@@ -192,7 +193,7 @@ public void Formula(string formula)
192193

193194
ResetColumnPlainValues();
194195
elementMapping.Items = null;
195-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
196+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
196197
if (formulaLines.Length > 1)
197198
{
198199
elementMapping.Items = new[] {new HbmFormula {Text = formulaLines}};

src/NHibernate/Mapping/ByCode/Impl/FilterMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Util;
34

45
namespace NHibernate.Mapping.ByCode.Impl
56
{
@@ -35,7 +36,7 @@ public void Condition(string sqlCondition)
3536
filter.Text = null;
3637
return;
3738
}
38-
string[] conditionLines = sqlCondition.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
39+
string[] conditionLines = sqlCondition.Split(StringHelper.LineSeparators, StringSplitOptions.None);
3940
if (conditionLines.Length > 1)
4041
{
4142
filter.Text = conditionLines;

src/NHibernate/Mapping/ByCode/Impl/ManyToManyMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using NHibernate.Cfg.MappingSchema;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Mapping.ByCode.Impl
78
{
@@ -128,7 +129,7 @@ public void Formula(string formula)
128129

129130
ResetColumnPlainValues();
130131
manyToMany.Items = null;
131-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
132+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
132133
if (formulaLines.Length > 1)
133134
{
134135
manyToMany.Items = new[] {new HbmFormula {Text = formulaLines}};

src/NHibernate/Mapping/ByCode/Impl/ManyToOneMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using NHibernate.Cfg.MappingSchema;
6+
using NHibernate.Util;
67

78
namespace NHibernate.Mapping.ByCode.Impl
89
{
@@ -82,7 +83,7 @@ public void Formula(string formula)
8283

8384
ResetColumnPlainValues();
8485
_manyToOne.Items = null;
85-
string[] formulaLines = formula.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
86+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
8687
if (formulaLines.Length > 1)
8788
{
8889
_manyToOne.Items = new[] { new HbmFormula { Text = formulaLines } };

src/NHibernate/Mapping/ByCode/Impl/MapKeyManyToManyMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using NHibernate.Cfg.MappingSchema;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Mapping.ByCode.Impl
78
{
@@ -36,7 +37,7 @@ public void Formula(string formula)
3637

3738
ResetColumnPlainValues();
3839
mapping.Items = null;
39-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
40+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
4041
if (formulaLines.Length > 1)
4142
{
4243
mapping.Items = new[] {new HbmFormula {Text = formulaLines}};

src/NHibernate/Mapping/ByCode/Impl/MapKeyMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NHibernate.Cfg.MappingSchema;
55
using NHibernate.Type;
66
using NHibernate.UserTypes;
7+
using NHibernate.Util;
78

89
namespace NHibernate.Mapping.ByCode.Impl
910
{
@@ -112,7 +113,7 @@ public void Formula(string formula)
112113

113114
ResetColumnPlainValues();
114115
hbmMapKey.Items = null;
115-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
116+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
116117
if (formulaLines.Length > 1)
117118
{
118119
hbmMapKey.Items = new[] {new HbmFormula {Text = formulaLines}};

src/NHibernate/Mapping/ByCode/Impl/OneToOneMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq.Expressions;
33
using System.Reflection;
44
using NHibernate.Cfg.MappingSchema;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Mapping.ByCode.Impl
78
{
@@ -101,7 +102,7 @@ public void Formula(string formula)
101102
return;
102103
}
103104

104-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
105+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
105106
if (formulaLines.Length > 1)
106107
{
107108
_oneToOne.formula = new[] {new HbmFormula {Text = formulaLines}};

src/NHibernate/Mapping/ByCode/Impl/PropertyMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NHibernate.Cfg.MappingSchema;
66
using NHibernate.Type;
77
using NHibernate.UserTypes;
8+
using NHibernate.Util;
89

910
namespace NHibernate.Mapping.ByCode.Impl
1011
{
@@ -231,7 +232,7 @@ public void Formula(string formula)
231232

232233
ResetColumnPlainValues();
233234
propertyMapping.Items = null;
234-
string[] formulaLines = formula.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
235+
string[] formulaLines = formula.Split(StringHelper.LineSeparators, StringSplitOptions.None);
235236
if (formulaLines.Length > 1)
236237
{
237238
propertyMapping.Items = new[] {new HbmFormula {Text = formulaLines}};

0 commit comments

Comments
 (0)