-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSqlStatement.cs
More file actions
90 lines (78 loc) · 4.12 KB
/
Copy pathSqlStatement.cs
File metadata and controls
90 lines (78 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
namespace CoreEx.Database;
/// <summary>
/// Represents a database SQL statement, including its command type and text, for use with data access operations.
/// </summary>
public record class SqlStatement
{
private string? _commandText;
/// <summary>
/// Gets an indeterminate <see cref="SqlStatement"/>.
/// </summary>
public static SqlStatement Indeterminate { get; } = new SqlStatement();
/// <summary>
/// Creates a stored procedure <see cref="SqlStatement"/>.
/// </summary>
/// <param name="storedProcedure">The stored procedure name.</param>
/// <returns>The <see cref="SqlStatement"/>.</returns>
public static SqlStatement StoredProcedure(string storedProcedure) => new(CommandType.StoredProcedure, storedProcedure);
/// <summary>
/// Creates a SQL text <see cref="SqlStatement"/> from the <paramref name="text"/>.
/// </summary>
/// <param name="text">The SQL statement text.</param>
/// <returns>The <see cref="SqlStatement"/>.</returns>
public static SqlStatement FromText(string text) => new(CommandType.Text, text);
/// <summary>
/// Creates a SQL text <see cref="SqlStatement"/> from the named embedded resource within the specified <paramref name="assembly"/>.
/// </summary>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualifed resource name).</param>
/// <param name="assembly">The <see cref="Assembly"/> that contains the embedded resource; defaults to <see cref="Assembly.GetCallingAssembly"/>.</param>
/// <returns>The <see cref="SqlStatement"/>.</returns>
public static SqlStatement FromResource(string resourceName, Assembly? assembly = null)
{
using var s = Resource.GetStream(resourceName, assembly ?? Assembly.GetCallingAssembly());
using var sr = new StreamReader(s);
return FromText(sr.ReadToEnd());
}
/// <summary>
/// Creates a SQL text <see cref="SqlStatement"/> from the named embedded resource within the <see name="Assembly"/> inferred from the <typeparamref name="TResource"/> <see cref="Type"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to infer the <see cref="Assembly"/> that contains the embedded resource.</typeparam>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualifed resource name).</param>
/// <returns>The <see cref="SqlStatement"/>.</returns>
public static SqlStatement FromResource<TResource>(string resourceName) => FromResource(resourceName, typeof(TResource).Assembly);
/// <summary>
/// Initializes a new instance of the <see cref="SqlStatement"/> class.
/// </summary>
/// <param name="commandType">The <see cref="System.Data.CommandType"/>.</param>
/// <param name="commandText">The command text.</param>
public SqlStatement(CommandType commandType, string commandText)
{
CommandType = commandType;
CommandText = commandText.ThrowIfNullOrEmpty();
}
/// <summary>
/// Initializes a new <see cref="Indeterminate"/> instance of the <see cref="SqlStatement"/> struct.
/// </summary>
private SqlStatement() { }
/// <summary>
/// Gets the <see cref="System.Data.CommandType"/>.
/// </summary>
public CommandType CommandType { get; }
/// <summary>
/// Gets the command text.
/// </summary>
public string CommandText
{
get => _commandText ?? throw new InvalidOperationException($"{nameof(CommandText)} is not initialized; the {nameof(SqlStatement)} is indeterminate.");
private init => _commandText = value;
}
/// <summary>
/// Indicates whether the <see cref="SqlStatement"/> is indeterminate (i.e. has not been initialized with a <see cref="CommandText"/>).
/// </summary>
public bool IsIndeterminate => _commandText is null;
/// <summary>
/// An implicit cast from a text <see cref="string"/> to a <see cref="SqlStatement"/> (<see cref="System.Data.CommandType.Text"/>).
/// </summary>
/// <param name="text">The SQL statement text.</param>
public static implicit operator SqlStatement(string text) => FromText(text);
}