Skip to content

Commit 894de33

Browse files
committed
1 parent be0ada0 commit 894de33

File tree

32 files changed

+2384
-1469
lines changed

32 files changed

+2384
-1469
lines changed

EntityFramework.Reverse.POCO.Generator/Database.cs

Lines changed: 39 additions & 39 deletions
Large diffs are not rendered by default.

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@
731731
return 7;
732732

733733
case TemplateType.Ef6:
734+
case TemplateType.FileBasedEf6:
734735
default:
735736
return 0;
736737
}
@@ -16028,49 +16029,68 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
1602816029
public string WriteStoredProcFunctionParams(bool includeProcResult, bool forInterface)
1602916030
{
1603016031
var sb = new StringBuilder(255);
16031-
var n = 1;
1603216032
var data = Parameters.Where(x => x.Mode != StoredProcedureParameterMode.Out).OrderBy(x => x.Ordinal).ToList();
16033-
var count = data.Count;
16034-
16035-
if (includeProcResult && ReturnModels.Count > 0 && ReturnModels.First().Count > 0)
16036-
{
16037-
sb.Append("out int procResult");
16038-
if (count > 0)
16039-
sb.Append(", ");
16040-
}
1604116033

16034+
var minNullableParameter = WhichTailEndParametersCanBeNullable(data, includeProcResult, forInterface);
16035+
if (minNullableParameter == 0)
16036+
minNullableParameter = 9999;
16037+
16038+
var count = 0;
1604216039
foreach (var p in data)
1604316040
{
16041+
++count;
1604416042
var notNullable = Column.StoredProcedureNotNullable.Contains(p.PropertyType.ToLower());
16043+
var isInParam = p.Mode == StoredProcedureParameterMode.In;
1604516044

16046-
sb.AppendFormat("{0}{1}{2} {3}{4}{5}",
16047-
p.Mode == StoredProcedureParameterMode.In ? string.Empty : "out ",
16045+
sb.AppendFormat("{0}{1}{2} {3}{4}, ",
16046+
isInParam ? string.Empty : "out ",
1604816047
p.PropertyType,
1604916048
notNullable ? string.Empty : "?",
1605016049
p.NameHumanCase,
16051-
notNullable || forInterface ? string.Empty : " = null",
16052-
n++ < count ? ", " : string.Empty);
16050+
(notNullable || forInterface || !isInParam || count < minNullableParameter) ? string.Empty : " = null");
16051+
}
16052+
16053+
if (includeProcResult && ReturnModels.Count > 0 && ReturnModels.First().Count > 0)
16054+
{
16055+
sb.Append("out int procResult, ");
1605316056
}
1605416057

16058+
if(sb.Length > 2)
16059+
sb.Length -= 2;
1605516060

1605616061
return sb.ToString();
1605716062
}
1605816063

16064+
// Returns the minimum nullable parameter, counting from 1. 0 Means no column is nullable.
16065+
public int WhichTailEndParametersCanBeNullable(List<StoredProcedureParameter> data, bool includeProcResult, bool forInterface)
16066+
{
16067+
if (forInterface || includeProcResult || !data.Any())
16068+
return 0;
16069+
16070+
var dataCount = data.Count;
16071+
var parameterNumber = dataCount;
16072+
foreach (var parameter in data.OrderByDescending(x => x.Ordinal))
16073+
{
16074+
if (parameter.Mode == StoredProcedureParameterMode.InOut)
16075+
return parameterNumber == dataCount ? 0 : parameterNumber + 1;
16076+
16077+
--parameterNumber;
16078+
}
16079+
16080+
return 1;
16081+
}
16082+
1605916083
public string WriteStoredProcFunctionOverloadCall()
1606016084
{
1606116085
var sb = new StringBuilder(255);
16062-
sb.Append("out procResult");
16063-
if (Parameters.Any())
16064-
sb.Append(", ");
16065-
1606616086
foreach (var p in Parameters.OrderBy(x => x.Ordinal))
1606716087
{
1606816088
sb.AppendFormat("{0}{1}, ",
1606916089
p.Mode == StoredProcedureParameterMode.In ? string.Empty : "out ",
1607016090
p.NameHumanCase);
1607116091
}
1607216092

16073-
sb.Length -= 2;
16093+
sb.Append("out procResult");
1607416094
return sb.ToString();
1607516095
}
1607616096

@@ -21515,7 +21535,7 @@ using {{this}};{{#newline}}
2151521535
{{ReturnType}} {{FunctionName}}({{WriteStoredProcFunctionParamsTrueTrue}});{{#newline}}
2151621536
{{/if}}
2151721537
{{#else}}
21518-
int {{FunctionName}}({{WriteStoredProcFunctionParamsTrue}});{{#newline}}
21538+
int {{FunctionName}}({{WriteStoredProcFunctionParamsTrueTrue}});{{#newline}}
2151921539
{{/if}}
2152021540

2152121541
{{#if AsyncFunctionCannotBeCreated}}
@@ -26293,6 +26313,7 @@ public enum {{EnumName}}{{#newline}}
2629326313
case TemplateType.EfCore7:
2629426314
return new TemplateEfCore7();
2629526315

26316+
case TemplateType.FileBasedEf6:
2629626317
case TemplateType.FileBasedCore2:
2629726318
case TemplateType.FileBasedCore3:
2629826319
case TemplateType.FileBasedCore5:
@@ -26474,6 +26495,7 @@ public enum {{EnumName}}{{#newline}}
2647426495
EfCore5,
2647526496
EfCore6,
2647626497
EfCore7,
26498+
FileBasedEf6,
2647726499
FileBasedCore2,
2647826500
FileBasedCore3,
2647926501
FileBasedCore5,
Lines changed: 217 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Efrpg;
1+
using System.Collections.Generic;
2+
using Efrpg;
23
using NUnit.Framework;
34
using System.Data;
5+
using System.Linq;
46
using Generator.Tests.Common;
57

68
namespace Generator.Tests.Unit
@@ -9,34 +11,242 @@ namespace Generator.Tests.Unit
911
[Category(Constants.CI)]
1012
public class StoredProcedureTests
1113
{
12-
public StoredProcedure Sut { get; private set; }
14+
private StoredProcedure _sut;
1315

1416
[SetUp]
1517
public void Init()
1618
{
17-
Sut = new StoredProcedure { Schema = new Schema("dbo"), DbName = "name", NameHumanCase = "some_sp", IsStoredProcedure = true };
18-
19+
_sut = new StoredProcedure
20+
{
21+
Schema = new Schema("dbo"),
22+
DbName = "name",
23+
NameHumanCase = "some_sp",
24+
IsStoredProcedure = true,
25+
ReturnModels = new List<List<DataColumn>>
26+
{
27+
new List<DataColumn> { new DataColumn { AllowDBNull = true } }
28+
},
29+
Parameters = new List<StoredProcedureParameter>
30+
{
31+
new StoredProcedureParameter
32+
{
33+
Mode = StoredProcedureParameterMode.In,
34+
PropertyType = "DateTime",
35+
NameHumanCase = "A",
36+
Ordinal = 1
37+
},
38+
new StoredProcedureParameter
39+
{
40+
Mode = StoredProcedureParameterMode.InOut,
41+
PropertyType = "DateTime",
42+
NameHumanCase = "B",
43+
Ordinal = 2
44+
},
45+
new StoredProcedureParameter
46+
{
47+
Mode = StoredProcedureParameterMode.In,
48+
PropertyType = "DateTime",
49+
NameHumanCase = "C",
50+
Ordinal = 3
51+
},
52+
new StoredProcedureParameter
53+
{
54+
Mode = StoredProcedureParameterMode.Out, // Ignored
55+
PropertyType = "DateTime",
56+
NameHumanCase = "X",
57+
Ordinal = 4
58+
},
59+
new StoredProcedureParameter
60+
{
61+
Mode = StoredProcedureParameterMode.In,
62+
PropertyType = "DateTime",
63+
NameHumanCase = "D",
64+
Ordinal = 5
65+
}
66+
}
67+
};
1968
}
2069

2170
[Description("Issue #286")]
22-
[Test]
2371
[TestCase("JSON_F52E2B61-18A1-11d1-B105-00805F49916B", "JSON_Value")]
2472
[TestCase("XML_F52E2B61-18A1-11d1-B105-00805F49916B", "XML_Value")]
2573
public void ColumnNameForXmlOrJsonReturnType(string exampleServerGenerated, string expected)
2674
{
2775
// Arrange
28-
2976
var col = new DataColumn
3077
{
3178
DataType = typeof(string),
3279
ColumnName = exampleServerGenerated,
3380
};
3481

3582
// Act
36-
var result = Sut.WriteStoredProcReturnColumn(col);
83+
var result = _sut.WriteStoredProcReturnColumn(col);
3784

3885
// Assert
3986
Assert.AreEqual($"public string {expected} {{ get; set; }}", result);
4087
}
88+
89+
[TestCase(false, false, "DateTime? A, out DateTime? B, DateTime? C = null, DateTime? D = null")]
90+
[TestCase(false, true, "DateTime? A, out DateTime? B, DateTime? C, DateTime? D")]
91+
[TestCase(true, false, "DateTime? A, out DateTime? B, DateTime? C, DateTime? D, out int procResult")]
92+
[TestCase(true, true, "DateTime? A, out DateTime? B, DateTime? C, DateTime? D, out int procResult")]
93+
public void WriteStoredProcFunctionParams_HasTailNullable(bool includeProcResult, bool forInterface, string expected)
94+
{
95+
// Act
96+
var result = _sut.WriteStoredProcFunctionParams(includeProcResult, forInterface);
97+
98+
// Assert
99+
Assert.IsNotNull(result);
100+
Assert.AreEqual(expected, result);
101+
}
102+
103+
[TestCase(false, false, "DateTime? A, out DateTime? B, DateTime? C, out DateTime? D")]
104+
[TestCase(false, true, "DateTime? A, out DateTime? B, DateTime? C, out DateTime? D")]
105+
[TestCase(true, false, "DateTime? A, out DateTime? B, DateTime? C, out DateTime? D, out int procResult")]
106+
[TestCase(true, true, "DateTime? A, out DateTime? B, DateTime? C, out DateTime? D, out int procResult")]
107+
public void WriteStoredProcFunctionParams_NoTailNullable(bool includeProcResult, bool forInterface, string expected)
108+
{
109+
// Arrange - Set last to be an 'out' parameter.
110+
_sut.Parameters.Single(x => x.NameHumanCase == "D").Mode = StoredProcedureParameterMode.InOut;
111+
112+
// Act
113+
var result = _sut.WriteStoredProcFunctionParams(includeProcResult, forInterface);
114+
115+
// Assert
116+
Assert.IsNotNull(result);
117+
Assert.AreEqual(expected, result);
118+
}
119+
120+
[Test]
121+
[TestCase(false, false, false, 3)]
122+
[TestCase(false, true, false, 0)]
123+
[TestCase(true, false, false, 0)]
124+
[TestCase(true, true, false, 0)]
125+
[TestCase(false, false, true, 0)]
126+
[TestCase(false, true, true, 0)]
127+
[TestCase(true, false, true, 0)]
128+
[TestCase(true, true, true, 0)]
129+
public void WhichTailEndParametersCanBeNullable(bool includeProcResult, bool forInterface, bool noTailNullable, int expected)
130+
{
131+
if (noTailNullable)
132+
{
133+
// Arrange - Set last to be an 'out' parameter.
134+
_sut.Parameters.Single(x => x.NameHumanCase == "D").Mode = StoredProcedureParameterMode.InOut;
135+
}
136+
137+
// Act
138+
var result = _sut.WhichTailEndParametersCanBeNullable(GetParams(), includeProcResult, forInterface);
139+
140+
// Assert
141+
Assert.IsNotNull(result);
142+
Assert.AreEqual(expected, result);
143+
}
144+
145+
[Test]
146+
public void WhichTailEndParametersCanBeNullable_AllOutParameters()
147+
{
148+
foreach (var parameter in _sut.Parameters)
149+
{
150+
parameter.Mode = StoredProcedureParameterMode.InOut;
151+
}
152+
153+
// Act
154+
var result = _sut.WhichTailEndParametersCanBeNullable(GetParams(), false, false);
155+
156+
// Assert
157+
Assert.IsNotNull(result);
158+
Assert.AreEqual(0, result);
159+
}
160+
161+
[Test]
162+
public void WhichTailEndParametersCanBeNullable_AllInParameters()
163+
{
164+
foreach (var parameter in _sut.Parameters)
165+
{
166+
parameter.Mode = StoredProcedureParameterMode.In;
167+
}
168+
169+
// Act
170+
var result = _sut.WhichTailEndParametersCanBeNullable(GetParams(), false, false);
171+
172+
// Assert
173+
Assert.IsNotNull(result);
174+
Assert.AreEqual(1, result);
175+
}
176+
177+
[Test]
178+
public void WhichTailEndParametersCanBeNullable_AllInOutParameters()
179+
{
180+
foreach (var parameter in _sut.Parameters)
181+
{
182+
parameter.Mode = StoredProcedureParameterMode.InOut;
183+
}
184+
185+
// Act
186+
var result = _sut.WhichTailEndParametersCanBeNullable(GetParams(), false, false);
187+
188+
// Assert
189+
Assert.IsNotNull(result);
190+
Assert.AreEqual(0, result);
191+
}
192+
193+
[Test]
194+
public void ThisHasMixedOutParameters()
195+
{
196+
// Arrange
197+
_sut.Parameters = new List<StoredProcedureParameter>
198+
{
199+
new StoredProcedureParameter
200+
{
201+
Mode = StoredProcedureParameterMode.In,
202+
NameHumanCase = "foo",
203+
PropertyType = "DateTime",
204+
Ordinal = 1
205+
},
206+
new StoredProcedureParameter
207+
{
208+
Mode = StoredProcedureParameterMode.InOut,
209+
NameHumanCase = "firstOutParam",
210+
PropertyType = "int",
211+
Ordinal = 2
212+
},
213+
new StoredProcedureParameter
214+
{
215+
Mode = StoredProcedureParameterMode.In,
216+
NameHumanCase = "bar",
217+
PropertyType = "DateTime",
218+
Ordinal = 3
219+
},
220+
new StoredProcedureParameter
221+
{
222+
Mode = StoredProcedureParameterMode.InOut,
223+
NameHumanCase = "secondOutParam",
224+
PropertyType = "int",
225+
Ordinal = 4
226+
},
227+
new StoredProcedureParameter
228+
{
229+
Mode = StoredProcedureParameterMode.In,
230+
NameHumanCase = "baz",
231+
PropertyType = "DateTime",
232+
Ordinal = 5
233+
}
234+
};
235+
236+
// Act
237+
var resultString = _sut.WriteStoredProcFunctionParams(false, false);
238+
var resultTail = _sut.WhichTailEndParametersCanBeNullable(GetParams(), false, false);
239+
240+
// Assert
241+
Assert.IsNotNull(resultString);
242+
Assert.IsNotNull(resultTail);
243+
Assert.AreEqual(5, resultTail);
244+
Assert.AreEqual("DateTime? foo, out int? firstOutParam, DateTime? bar, out int? secondOutParam, DateTime? baz = null", resultString);
245+
}
246+
247+
private List<StoredProcedureParameter> GetParams()
248+
{
249+
return _sut.Parameters.Where(x => x.Mode != StoredProcedureParameterMode.Out).OrderBy(x => x.Ordinal).ToList();
250+
}
41251
}
42252
}

0 commit comments

Comments
 (0)