MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / generate_table_file_from_schema

Method generate_table_file_from_schema

crates/codegen/src/csharp.rs:506–737  ·  view source on GitHub ↗
(&self, module: &ModuleDef, table: &TableDef, schema: TableSchema)

Source from the content-addressed store, hash-verified

504
505impl Lang for Csharp<'_> {
506 fn generate_table_file_from_schema(&self, module: &ModuleDef, table: &TableDef, schema: TableSchema) -> OutputFile {
507 let mut output = CsharpAutogen::new(
508 self.namespace,
509 &[
510 "SpacetimeDB.BSATN",
511 "SpacetimeDB.ClientApi",
512 "System.Collections.Generic",
513 "System.Runtime.Serialization",
514 ],
515 false,
516 );
517
518 writeln!(output, "public sealed partial class RemoteTables");
519 indented_block(&mut output, |output| {
520 let csharp_table_name = table.accessor_name.deref().to_case(Case::Pascal);
521 let csharp_table_class_name = csharp_table_name.clone() + "Handle";
522 let table_type = type_ref_name(module, table.product_type_ref);
523
524 let base_class = if table.is_event {
525 "RemoteEventTableHandle"
526 } else {
527 "RemoteTableHandle"
528 };
529 writeln!(
530 output,
531 "public sealed class {csharp_table_class_name} : {base_class}<EventContext, {table_type}>"
532 );
533 indented_block(output, |output| {
534 writeln!(
535 output,
536 "protected override string RemoteTableName => \"{}\";",
537 table.name
538 );
539 writeln!(output);
540
541 // If this is a table, we want to generate event accessor and indexes
542 let product_type: &ProductTypeDef = module.typespace_for_generate()[table.product_type_ref]
543 .as_product()
544 .unwrap();
545
546 let mut index_names = Vec::new();
547
548 for idx in iter_indexes(table) {
549 let Some(accessor_name) = idx.accessor_name.as_ref() else {
550 // If there is no accessor name, we shouldn't generate a client-side index accessor.
551 continue;
552 };
553
554 // Whatever the index algorithm on the host,
555 // the client can still use btrees.
556 let columns = idx.algorithm.columns();
557 let get_csharp_field_name_and_type = |col_pos: ColId| {
558 let (field_name, field_type) = &product_type.elements[col_pos.idx()];
559 let csharp_field_name_pascal = field_name.deref().to_case(Case::Pascal);
560 let csharp_field_type = ty_fmt(module, field_type);
561 (csharp_field_name_pascal, csharp_field_type)
562 };
563

Callers

nothing calls this directly

Calls 15

indented_blockFunction · 0.85
iter_indexesFunction · 0.85
ty_fmtFunction · 0.85
to_caseMethod · 0.80
as_singletonMethod · 0.80
pkMethod · 0.80
has_indexedMethod · 0.80
has_uniqueMethod · 0.80
has_primary_keyMethod · 0.80
type_ref_nameFunction · 0.70

Tested by

no test coverage detected