Skip to content

Commit

Permalink
C# codegen making state relative to a DbConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
lcodes authored and John Detter committed Oct 22, 2024
1 parent c5ab7f1 commit 856926f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
14 changes: 11 additions & 3 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,22 @@ fn autogen_csharp_access_funcs_for_struct(
"public readonly ref struct {csharp_field_name_pascal}UniqueIndex"
);
indented_block(output, |output| {
writeln!(output, "readonly {csharp_table_name}Handle Handle;");
write!(
output,
"internal {csharp_field_name_pascal}UniqueIndex({csharp_table_name}Handle handle) => "
);
writeln!(output, "Handle = handle;");
writeln!(output);

writeln!(
output,
"public {struct_name_pascal_case}? Find({csharp_field_type} value)"
);
indented_block(output, |output| {
writeln!(
output,
"{csharp_field_name_pascal}_Index.TryGetValue(value, out var r);"
"Handle.{csharp_field_name_pascal}_Index.TryGetValue(value, out var r);"
);
writeln!(output, "return r;");
});
Expand All @@ -438,7 +446,7 @@ fn autogen_csharp_access_funcs_for_struct(
writeln!(output);
writeln!(
output,
"public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} => new();"
"public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} => new(this);"
);
writeln!(output);
}
Expand Down Expand Up @@ -602,7 +610,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
let type_name = ty_fmt(ctx, &col.col_type, namespace);
writeln!(
output,
"private static Dictionary<{type_name}, {table_type}> {field_name}_Index = new(16);"
"private Dictionary<{type_name}, {table_type}> {field_name}_Index = new(16);"
);
unique_indexes.push(field_name);
}
Expand Down
36 changes: 24 additions & 12 deletions crates/cli/tests/snapshots/codegen__codegen_csharp.snap
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ namespace SpacetimeDB

public class PkMultiIdentityHandle : RemoteTableHandle<EventContext, PkMultiIdentity>
{
private static Dictionary<uint, PkMultiIdentity> Id_Index = new(16);
private static Dictionary<uint, PkMultiIdentity> Other_Index = new(16);
private Dictionary<uint, PkMultiIdentity> Id_Index = new(16);
private Dictionary<uint, PkMultiIdentity> Other_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
Expand All @@ -672,27 +672,33 @@ namespace SpacetimeDB

public readonly ref struct IdUniqueIndex
{
readonly PkMultiIdentityHandle Handle;
internal IdUniqueIndex(PkMultiIdentityHandle handle) => Handle = handle;

public PkMultiIdentity? Find(uint value)
{
Id_Index.TryGetValue(value, out var r);
Handle.Id_Index.TryGetValue(value, out var r);
return r;
}

}

public IdUniqueIndex Id => new();
public IdUniqueIndex Id => new(this);

public readonly ref struct OtherUniqueIndex
{
readonly PkMultiIdentityHandle Handle;
internal OtherUniqueIndex(PkMultiIdentityHandle handle) => Handle = handle;

public PkMultiIdentity? Find(uint value)
{
Other_Index.TryGetValue(value, out var r);
Handle.Other_Index.TryGetValue(value, out var r);
return r;
}

}

public OtherUniqueIndex Other => new();
public OtherUniqueIndex Other => new(this);

internal PkMultiIdentityHandle()
{
Expand Down Expand Up @@ -736,7 +742,7 @@ namespace SpacetimeDB

public class RepeatingTestArgHandle : RemoteTableHandle<EventContext, RepeatingTestArg>
{
private static Dictionary<ulong, RepeatingTestArg> ScheduledId_Index = new(16);
private Dictionary<ulong, RepeatingTestArg> ScheduledId_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
Expand All @@ -751,15 +757,18 @@ namespace SpacetimeDB

public readonly ref struct ScheduledIdUniqueIndex
{
readonly RepeatingTestArgHandle Handle;
internal ScheduledIdUniqueIndex(RepeatingTestArgHandle handle) => Handle = handle;

public RepeatingTestArg? Find(ulong value)
{
ScheduledId_Index.TryGetValue(value, out var r);
Handle.ScheduledId_Index.TryGetValue(value, out var r);
return r;
}

}

public ScheduledIdUniqueIndex ScheduledId => new();
public ScheduledIdUniqueIndex ScheduledId => new(this);

internal RepeatingTestArgHandle()
{
Expand Down Expand Up @@ -803,7 +812,7 @@ namespace SpacetimeDB

public class TestEHandle : RemoteTableHandle<EventContext, TestE>
{
private static Dictionary<ulong, TestE> Id_Index = new(16);
private Dictionary<ulong, TestE> Id_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
Expand All @@ -818,15 +827,18 @@ namespace SpacetimeDB

public readonly ref struct IdUniqueIndex
{
readonly TestEHandle Handle;
internal IdUniqueIndex(TestEHandle handle) => Handle = handle;

public TestE? Find(ulong value)
{
Id_Index.TryGetValue(value, out var r);
Handle.Id_Index.TryGetValue(value, out var r);
return r;
}

}

public IdUniqueIndex Id => new();
public IdUniqueIndex Id => new(this);

public class NameIndex
{
Expand Down

0 comments on commit 856926f

Please sign in to comment.