Skip to content

Commit 7ca6c0e

Browse files
committed
Merge branch 'refactoring/OnTopic.Data.Sql' into develop
2 parents 402301f + 2dd8beb commit 7ca6c0e

File tree

9 files changed

+893
-812
lines changed

9 files changed

+893
-812
lines changed

OnTopic.AspNetCore.Mvc.Host/Views/Shared/Components/PageLevelNavigation/Default.cshtml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
<h2>PageLevelNavigation</h2>
44
<ul>
5-
@foreach (var topic in Model.NavigationRoot.Children) {
6-
<li><a href="@topic.WebPath">@(topic.ShortTitle?? topic.Title)</a></li>
5+
@if (Model.NavigationRoot != null) {
6+
foreach (var topic in Model.NavigationRoot.Children) {
7+
<li><a href="@topic.WebPath">@(topic.ShortTitle?? topic.Title)</a></li>
8+
}
79
}
810
</ul>
911

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
using System;
7+
using System.Data;
8+
using System.Globalization;
9+
using System.Text;
10+
using Microsoft.Data.SqlClient;
11+
using OnTopic.Internal.Diagnostics;
12+
13+
namespace OnTopic.Data.Sql {
14+
15+
/*============================================================================================================================
16+
| CLASS: SQL COMMAND EXTENSIONS
17+
\---------------------------------------------------------------------------------------------------------------------------*/
18+
/// <summary>
19+
/// Extension methods for the <see cref="SqlCommand"/> class.
20+
/// </summary>
21+
internal static class SqlCommandExtensions {
22+
23+
/*==========================================================================================================================
24+
| METHOD: GET RETURN CODE
25+
\-------------------------------------------------------------------------------------------------------------------------*/
26+
/// <summary>
27+
/// Retrieves the return code from a stored procedure.
28+
/// </summary>
29+
/// <param name="command">The SQL command object.</param>
30+
internal static int GetReturnCode(this SqlCommand command, string sqlParameter = "ReturnCode") {
31+
Contract.Assume<InvalidOperationException>(
32+
command.Parameters.Contains($"@{sqlParameter}"),
33+
$"The call to the {command.CommandText} stored procedure did not return the expected 'ReturnCode' parameter."
34+
);
35+
var returnCode = command.Parameters[$"@{sqlParameter}"].Value.ToString();
36+
return Int32.Parse(returnCode, CultureInfo.InvariantCulture);
37+
}
38+
39+
/*==========================================================================================================================
40+
| METHOD: ADD OUTPUT PARAMETER
41+
\-------------------------------------------------------------------------------------------------------------------------*/
42+
/// <summary>
43+
/// Adds a SQL parameter to a command object, additionally setting the specified parameter direction.
44+
/// </summary>
45+
/// <param name="command">The SQL command object.</param>
46+
/// <param name="sqlParameter">The SQL parameter.</param>
47+
/// <param name="paramDirection">The SQL parameter's directional setting (input-only, output-only, etc.).</param>
48+
/// <param name="sqlDbType">The SQL field data type.</param>
49+
internal static void AddOutputParameter(this SqlCommand command, string sqlParameter = "ReturnCode") =>
50+
AddParameter(command, sqlParameter, null, SqlDbType.Int, ParameterDirection.ReturnValue);
51+
52+
/*==========================================================================================================================
53+
| METHOD: ADD PARAMETER
54+
\-------------------------------------------------------------------------------------------------------------------------*/
55+
/// <summary>
56+
/// Wrapper function that adds a SQL parameter to a command object.
57+
/// </summary>
58+
/// <param name="command">The SQL command object.</param>
59+
/// <param name="sqlParameter">The SQL parameter.</param>
60+
/// <param name="fieldValue">The SQL field value.</param>
61+
internal static void AddParameter(this SqlCommand command, string sqlParameter, int fieldValue)
62+
=> AddParameter(command, sqlParameter, fieldValue, SqlDbType.Int);
63+
64+
/// <summary>
65+
/// Wrapper function that adds a SQL parameter to a command object.
66+
/// </summary>
67+
/// <param name="command">The SQL command object.</param>
68+
/// <param name="sqlParameter">The SQL parameter.</param>
69+
/// <param name="fieldValue">The SQL field value.</param>
70+
internal static void AddParameter(this SqlCommand command, string sqlParameter, bool fieldValue)
71+
=> AddParameter(command, sqlParameter, fieldValue, SqlDbType.Bit);
72+
73+
/// <summary>
74+
/// Wrapper function that adds a SQL parameter to a command object.
75+
/// </summary>
76+
/// <param name="command">The SQL command object.</param>
77+
/// <param name="sqlParameter">The SQL parameter.</param>
78+
/// <param name="fieldValue">The SQL field value.</param>
79+
internal static void AddParameter(this SqlCommand command, string sqlParameter, DateTime fieldValue)
80+
=> AddParameter(command, sqlParameter, fieldValue, SqlDbType.DateTime);
81+
82+
/// <summary>
83+
/// Wrapper function that adds a SQL parameter to a command object.
84+
/// </summary>
85+
/// <param name="command">The SQL command object.</param>
86+
/// <param name="sqlParameter">The SQL parameter.</param>
87+
/// <param name="fieldValue">The SQL field value.</param>
88+
internal static void AddParameter(this SqlCommand command, string sqlParameter, StringBuilder fieldValue)
89+
=> AddParameter(command, sqlParameter, fieldValue.ToString(), SqlDbType.Xml);
90+
91+
/// <summary>
92+
/// Wrapper function that adds a SQL parameter to a command object.
93+
/// </summary>
94+
/// <param name="command">The SQL command object.</param>
95+
/// <param name="sqlParameter">The SQL parameter.</param>
96+
/// <param name="fieldValue">The SQL field value.</param>
97+
/// <param name="sqlDbType">The SQL field data type.</param>
98+
internal static void AddParameter(this SqlCommand command, string sqlParameter, string? fieldValue)
99+
=> AddParameter(command, sqlParameter, String.IsNullOrEmpty(fieldValue)? null : fieldValue, SqlDbType.VarChar);
100+
101+
/// <summary>
102+
/// Adds a SQL parameter to a command object, additionally setting the specified SQL data length for the field.
103+
/// </summary>
104+
/// <param name="command">The SQL command object.</param>
105+
/// <param name="sqlParameter">The SQL parameter.</param>
106+
/// <param name="fieldValue">The SQL field value.</param>
107+
/// <param name="sqlDbType">The SQL field data type.</param>
108+
/// <param name="paramDirection">The SQL parameter's directional setting (input-only, output-only, etc.).</param>
109+
/// <param name="sqlLength">Length limit for the SQL field.</param>
110+
/// <requires description="The SQL command object must be specified." exception="T:System.ArgumentNullException">
111+
/// command != null
112+
/// </requires>
113+
private static void AddParameter(
114+
SqlCommand command,
115+
string sqlParameter,
116+
object? fieldValue,
117+
SqlDbType sqlDbType,
118+
ParameterDirection paramDirection = ParameterDirection.Input
119+
) {
120+
121+
/*------------------------------------------------------------------------------------------------------------------------
122+
| Validate input
123+
\-----------------------------------------------------------------------------------------------------------------------*/
124+
Contract.Requires(command, "The SQL command object must be specified.");
125+
Contract.Requires(command.Parameters, "The SQL command object's parameters collection must be available");
126+
127+
/*------------------------------------------------------------------------------------------------------------------------
128+
| Establish basic parameter
129+
\-----------------------------------------------------------------------------------------------------------------------*/
130+
var parameter = new SqlParameter("@" + sqlParameter, sqlDbType) {
131+
Direction = paramDirection
132+
};
133+
134+
/*------------------------------------------------------------------------------------------------------------------------
135+
| Set parameter value
136+
\-----------------------------------------------------------------------------------------------------------------------*/
137+
if (fieldValue is null) {
138+
parameter.Value = (string?)null;
139+
}
140+
else if (paramDirection.Equals(ParameterDirection.Input)) {
141+
parameter.Value = sqlDbType switch {
142+
SqlDbType.Bit => (bool)fieldValue,
143+
SqlDbType.DateTime => (DateTime)fieldValue,
144+
SqlDbType.Int => (int)fieldValue,
145+
SqlDbType.Xml => (string)fieldValue,
146+
_ => (string)fieldValue,
147+
};
148+
}
149+
150+
/*------------------------------------------------------------------------------------------------------------------------
151+
| Add parameter to command
152+
\-----------------------------------------------------------------------------------------------------------------------*/
153+
command.Parameters.Add(parameter);
154+
155+
}
156+
157+
} //Class
158+
} //Namespace

0 commit comments

Comments
 (0)