-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Irwin
committed
Apr 22, 2024
1 parent
12bb71b
commit 1688d1a
Showing
16 changed files
with
769 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...ryParamsAndReturns.ObjectPropertyNullable#ObjectPropertyNullableDataContext.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//HintName: ObjectPropertyNullableDataContext.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
using Microsoft.Data.SqlClient; | ||
|
||
using SQuiL; | ||
|
||
namespace TestCase; | ||
|
||
partial class TestQueryParamsAndReturnsDataContext : SQuiLBaseDataContext | ||
{ | ||
public async Task<ObjectPropertyNullableResponse> ProcessObjectPropertyNullableAsync( | ||
ObjectPropertyNullableRequest request, | ||
CancellationToken cancellationToken = default!) | ||
{ | ||
var builder = ConnectionStringBuilder("SQuiLDatabase"); | ||
using SqlConnection connection = new(builder.ConnectionString); | ||
var command = connection.CreateCommand(); | ||
|
||
List<SqlParameter> parameters = new() | ||
{ | ||
new("@EnvironmentName", System.Data.SqlDbType.VarChar, EnvironmentName.Length) { Value = EnvironmentName }, | ||
new("@Debug", System.Data.SqlDbType.Bit) { Value = EnvironmentName != "Production" }, | ||
}; | ||
|
||
command.CommandText = Query(parameters); | ||
command.Parameters.AddRange(parameters.ToArray()); | ||
|
||
await connection.OpenAsync(cancellationToken); | ||
|
||
ObjectPropertyNullableResponse response = new(); | ||
|
||
var isStudent = false; | ||
var isParents = false; | ||
|
||
using var reader = await command.ExecuteReaderAsync(cancellationToken); | ||
|
||
do | ||
{ | ||
var tableTag = reader.GetName(0); | ||
if(tableTag.StartsWith("__SQuiL__Table__Type__")) | ||
{ | ||
switch (tableTag) | ||
{ | ||
case "__SQuiL__Table__Type__Error__": | ||
{ | ||
if (!await reader.ReadAsync(cancellationToken)) break; | ||
|
||
break; | ||
} | ||
case "__SQuiL__Table__Type__Return_Student__": | ||
{ | ||
if (isStudent) throw new Exception( | ||
"Already returned value for `Student`"); | ||
|
||
isStudent = true; | ||
|
||
if (!await reader.ReadAsync(cancellationToken)) break; | ||
|
||
if (response.Student is not null) | ||
throw new Exception("Student was already set."); | ||
|
||
if (reader.GetString(0) == "Return_Student") | ||
{ | ||
response.Student = new( | ||
reader.GetInt32(reader.GetOrdinal("ID")), | ||
reader.IsDBNull(reader.GetOrdinal("FirstName")) ? default! : reader.GetString(reader.GetOrdinal("FirstName")), | ||
reader.GetString(reader.GetOrdinal("LastName")), | ||
reader.IsDBNull(reader.GetOrdinal("Age")) ? default! : reader.GetInt32(reader.GetOrdinal("Age"))); | ||
} | ||
else | ||
{ | ||
continue; | ||
} | ||
|
||
if (await reader.ReadAsync(cancellationToken)) | ||
throw new Exception( | ||
"Return object results in more than one object. Consider using a return table instead."); | ||
|
||
break; | ||
} | ||
case "__SQuiL__Table__Type__Returns_Parents__": | ||
{ | ||
isParents = true; | ||
|
||
if (!await reader.ReadAsync(cancellationToken)) break; | ||
|
||
var indexID = reader.GetOrdinal("ID"); | ||
var indexFirstName = reader.GetOrdinal("FirstName"); | ||
var indexLastName = reader.GetOrdinal("LastName"); | ||
var indexAge = reader.GetOrdinal("Age"); | ||
|
||
do | ||
{ | ||
if (reader.GetString(0) == "Returns_Parents") | ||
{ | ||
response.Parents.Add(new( | ||
reader.GetInt32(indexID), | ||
reader.IsDBNull(indexFirstName) ? default! : reader.GetString(indexFirstName), | ||
reader.GetString(indexLastName), | ||
reader.IsDBNull(indexAge) ? default! : reader.GetInt32(indexAge))); | ||
} | ||
} | ||
while (await reader.ReadAsync(cancellationToken)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
while (await reader.NextResultAsync(cancellationToken)); | ||
|
||
if (!isStudent) throw new Exception("Expected return object `Student`)"); | ||
if (!isParents) throw new Exception("Expected return table `Parents`)"); | ||
|
||
return response; | ||
|
||
string Query(List<SqlParameter> parameters) => $""" | ||
Declare @Return_Student table( | ||
[__SQuiL__Table__Type__Return_Student__] varchar(max) default('Return_Student'), | ||
[ID] int, | ||
[FirstName] varchar(100) Null, | ||
[LastName] varchar(100), | ||
[Age] int Null); | ||
|
||
Declare @Returns_Parents table( | ||
[__SQuiL__Table__Type__Returns_Parents__] varchar(max) default('Returns_Parents'), | ||
[ID] int, | ||
[FirstName] varchar(100) Null, | ||
[LastName] varchar(100), | ||
[Age] int Null); | ||
|
||
Use [{builder.InitialCatalog}]; | ||
|
||
|
||
|
||
"""; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...tQueryParamsAndReturns.ObjectPropertyNullable#ObjectPropertyNullableRequest.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//HintName: ObjectPropertyNullableRequest.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace TestCase; | ||
|
||
public partial record ObjectPropertyNullableRequest | ||
{ | ||
public bool Debug { get; set; } | ||
} |
13 changes: 13 additions & 0 deletions
13
...QueryParamsAndReturns.ObjectPropertyNullable#ObjectPropertyNullableResponse.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//HintName: ObjectPropertyNullableResponse.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace TestCase; | ||
|
||
public partial record ObjectPropertyNullableResponse | ||
{ | ||
public StudentObject? Student { get; set; } = default!; | ||
|
||
public System.Collections.Generic.List<ParentsTable> Parents { get; set; } = []; | ||
} |
12 changes: 12 additions & 0 deletions
12
...amsAndReturns/TestQueryParamsAndReturns.ObjectPropertyNullable#ParentsTable.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//HintName: ParentsTable.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace TestCase; | ||
|
||
public partial record ParentsTable( | ||
int ID, | ||
string? FirstName, | ||
string LastName, | ||
int? Age); |
48 changes: 48 additions & 0 deletions
48
...turns/TestQueryParamsAndReturns.ObjectPropertyNullable#SQuiLBaseDataContext.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//HintName: SQuiLBaseDataContext.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace SQuiL; | ||
|
||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
using System.Collections.Generic; | ||
using System; | ||
|
||
public abstract partial class SQuiLBaseDataContext(IConfiguration Configuration) | ||
{ | ||
//public virtual string SettingName { get; } = "SQuiLDatabase"; | ||
|
||
protected string EnvironmentName { get; } = Configuration.GetSection("EnvironmentName")?.Value | ||
?? Environment.GetEnvironmentVariable(Configuration.GetSection("EnvironmentVariable")?.Value ?? "ASPNETCORE_ENVIRONMENT") | ||
?? "Development"; | ||
|
||
protected SqlConnectionStringBuilder ConnectionStringBuilder(string settingName) | ||
{ | ||
return new SqlConnectionStringBuilder(Configuration.GetConnectionString(settingName) | ||
?? throw new Exception($"Cannot find a connection string in the appsettings for {settingName}.")); | ||
} | ||
|
||
protected void AddParams(System.Text.StringBuilder query, List<SqlParameter> parameters, int index, string table, string name, System.Data.SqlDbType type, object value, int size = 0) | ||
{ | ||
var parameter = $"@{table}_{index}_{name}"; | ||
query.Append(parameter); | ||
|
||
if (size == 0) | ||
{ | ||
parameters.Add(new(parameter, type) { Value = value }); | ||
return; | ||
} | ||
|
||
parameters.Add(new(parameter, type, size) { | ||
Value = value is null || ((string)value).Length <= size | ||
? (value ?? "Null") | ||
: throw new Exception($""" | ||
ParamsTable model table property at index [{index}] has a string property [{name}] | ||
with more than {size} characters. | ||
""") | ||
}); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...AndReturns/TestQueryParamsAndReturns.ObjectPropertyNullable#SQuiLExtensions.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//HintName: SQuiLExtensions.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class SQuiLExtensions | ||
{ | ||
public static bool IsLoaded => true; | ||
|
||
public static IServiceCollection AddSQuiL( | ||
this IServiceCollection services) | ||
{ | ||
services.AddSingleton<TestCase.TestQueryParamsAndReturnsDataContext>(); | ||
|
||
return services; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...eturns/TestQueryParamsAndReturns.ObjectPropertyNullable#SQuiLQueryAttribute.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//HintName: SQuiLQueryAttribute.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace SQuiL; | ||
|
||
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)] | ||
public class SQuiLQueryAttribute : System.Attribute | ||
{ | ||
public QueryFiles Type { get; } | ||
|
||
public string Setting { get; } | ||
|
||
public SQuiLQueryAttribute( | ||
QueryFiles type, | ||
string setting = "SQuiLDatabase") | ||
{ | ||
Type = type; | ||
Setting = setting; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...eturns/TestQueryParamsAndReturns.ObjectPropertyNullable#SQuiLQueryFilesEnum.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//HintName: SQuiLQueryFilesEnum.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace SQuiL; | ||
|
||
public enum QueryFiles | ||
{ | ||
ObjectPropertyNullable | ||
} |
17 changes: 17 additions & 0 deletions
17
...dReturns/TestQueryParamsAndReturns.ObjectPropertyNullable#SQuiLTableAttribute.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//HintName: SQuiLTableAttribute.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace SQuiL; | ||
|
||
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)] | ||
public class SQuiLTableAttribute : System.Attribute | ||
{ | ||
public TableType Type { get; } | ||
|
||
public SQuiLTableAttribute(TableType type) | ||
{ | ||
Type = type; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...Returns/TestQueryParamsAndReturns.ObjectPropertyNullable#SQuiLTableTypeEnum.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//HintName: SQuiLTableTypeEnum.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace SQuiL; | ||
|
||
public enum TableType | ||
{ | ||
Parents, | ||
Student | ||
} |
12 changes: 12 additions & 0 deletions
12
...msAndReturns/TestQueryParamsAndReturns.ObjectPropertyNullable#StudentObject.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//HintName: StudentObject.g.cs | ||
// <auto-generated /> | ||
|
||
#nullable enable | ||
|
||
namespace TestCase; | ||
|
||
public partial record StudentObject( | ||
int ID, | ||
string? FirstName, | ||
string LastName, | ||
int? Age); |
Oops, something went wrong.