pub trait CodegenBackend: Send + Sync {
Show 16 methods
// Required methods
fn name(&self) -> &str;
fn manifest(&self) -> &BackendManifest;
fn generate_row_struct(
&self,
query_name: &str,
columns: &[ResolvedColumn],
) -> Result<String, ScytheError>;
fn generate_model_struct(
&self,
table_name: &str,
columns: &[ResolvedColumn],
) -> Result<String, ScytheError>;
fn generate_query_fn(
&self,
analyzed: &AnalyzedQuery,
struct_name: &str,
columns: &[ResolvedColumn],
params: &[ResolvedParam],
) -> Result<String, ScytheError>;
fn generate_enum_def(
&self,
enum_info: &EnumInfo,
) -> Result<String, ScytheError>;
fn generate_composite_def(
&self,
composite: &CompositeInfo,
) -> Result<String, ScytheError>;
// Provided methods
fn file_header(&self) -> String { ... }
fn file_footer(&self) -> String { ... }
fn query_class_header(&self) -> String { ... }
fn post_footer(&self) -> String { ... }
fn generate_rbs_file(
&self,
_context: &RbsGenerationContext,
) -> Option<String> { ... }
fn generate_grouped_structs(
&self,
_parent_struct_name: &str,
_child_struct_name: &str,
_parent_columns: &[ResolvedColumn],
_child_columns: &[ResolvedColumn],
_key_column: &str,
) -> Result<String, ScytheError> { ... }
fn generate_grouped_query_fn(
&self,
_request: &GroupedQueryFn<'_>,
) -> Result<String, ScytheError> { ... }
fn apply_options(
&mut self,
_options: &HashMap<String, String>,
) -> Result<(), ScytheError> { ... }
fn supported_engines(&self) -> &[&str] { ... }
}Expand description
Trait that all codegen backends must implement.
Required Methods§
Sourcefn manifest(&self) -> &BackendManifest
fn manifest(&self) -> &BackendManifest
The backend’s manifest (type mappings, naming conventions, etc).
Sourcefn generate_row_struct(
&self,
query_name: &str,
columns: &[ResolvedColumn],
) -> Result<String, ScytheError>
fn generate_row_struct( &self, query_name: &str, columns: &[ResolvedColumn], ) -> Result<String, ScytheError>
Generate a row struct for a query result.
Sourcefn generate_model_struct(
&self,
table_name: &str,
columns: &[ResolvedColumn],
) -> Result<String, ScytheError>
fn generate_model_struct( &self, table_name: &str, columns: &[ResolvedColumn], ) -> Result<String, ScytheError>
Generate a model struct for a table.
Sourcefn generate_query_fn(
&self,
analyzed: &AnalyzedQuery,
struct_name: &str,
columns: &[ResolvedColumn],
params: &[ResolvedParam],
) -> Result<String, ScytheError>
fn generate_query_fn( &self, analyzed: &AnalyzedQuery, struct_name: &str, columns: &[ResolvedColumn], params: &[ResolvedParam], ) -> Result<String, ScytheError>
Generate a query function.
Sourcefn generate_enum_def(&self, enum_info: &EnumInfo) -> Result<String, ScytheError>
fn generate_enum_def(&self, enum_info: &EnumInfo) -> Result<String, ScytheError>
Generate an enum definition.
Sourcefn generate_composite_def(
&self,
composite: &CompositeInfo,
) -> Result<String, ScytheError>
fn generate_composite_def( &self, composite: &CompositeInfo, ) -> Result<String, ScytheError>
Generate a composite type definition.
Provided Methods§
Sourcefn file_header(&self) -> String
fn file_header(&self) -> String
Generate a file-level header (imports, docstring, etc). Returns an empty string by default; backends may override.
Generate a file-level footer (closing braces, etc). Returns an empty string by default; backends may override.
Sourcefn query_class_header(&self) -> String
fn query_class_header(&self) -> String
Generate a class header that wraps query functions only. When non-empty, the assembly will emit all type definitions (enums, row structs, model structs) first, then this class header, then all query functions, then the file footer. Returns an empty string by default (no class wrapper).
Generate code that should be emitted after the file footer. This is useful for backends that need top-level code after a class wrapper. For example, C# extension methods must be top-level, not nested. Returns an empty string by default.
Sourcefn generate_rbs_file(&self, _context: &RbsGenerationContext) -> Option<String>
fn generate_rbs_file(&self, _context: &RbsGenerationContext) -> Option<String>
Generate an RBS type signature file for Ruby backends.
Returns None by default; Ruby backends override this.
Sourcefn generate_grouped_structs(
&self,
_parent_struct_name: &str,
_child_struct_name: &str,
_parent_columns: &[ResolvedColumn],
_child_columns: &[ResolvedColumn],
_key_column: &str,
) -> Result<String, ScytheError>
fn generate_grouped_structs( &self, _parent_struct_name: &str, _child_struct_name: &str, _parent_columns: &[ResolvedColumn], _child_columns: &[ResolvedColumn], _key_column: &str, ) -> Result<String, ScytheError>
Generate parent and child structs for a :grouped query.
A :grouped query folds flat rows from a normal SQL SELECT into a nested
parent/child structure entirely on the client side — the SQL itself is
unchanged from a regular :many query.
§Struct layout
- Child struct (
child_struct_name): contains allchild_columns. Defined first in the output to avoid forward references. - Parent struct (
parent_struct_name): contains allparent_columnsplus one extra fieldchildren: Vec<child_struct_name>(or the language-native equivalent collection type).
§Grouping semantics
The generated query function fetches flat rows and folds them into an
order-preserving list of parent structs, appending each row’s child
fields to the matching parent’s collection. Equality on key_column is
the fold predicate.
§Parameters
parent_struct_name– fully qualified struct/class name for the parent (e.g."GetUsersWithOrdersRow").child_struct_name– fully qualified struct/class name for the child (e.g."GetUsersWithOrdersChildRow").parent_columns– resolved columns belonging to the parent table.child_columns– resolved columns belonging to child table(s).key_column– SQL column name used as the grouping key; identifies the boundary between parent groups (matches [GroupByConfig::key_column]).
§Return value
A string containing both struct definitions (child first, parent second),
stored in [GeneratedCode::row_struct].
§Default implementation
Returns an error: “grouped queries are not yet supported by the ‘<name>’ backend”. Backends opt in by overriding this method.
Sourcefn generate_grouped_query_fn(
&self,
_request: &GroupedQueryFn<'_>,
) -> Result<String, ScytheError>
fn generate_grouped_query_fn( &self, _request: &GroupedQueryFn<'_>, ) -> Result<String, ScytheError>
Generate the query function for a :grouped query.
The function runs the flat SQL from analyzed.sql, decodes each row,
and folds the rows into an order-preserving Vec<parent_struct_name>
(or the language-native equivalent), grouping by key_column and
appending each row’s child fields to the matching parent’s children
collection.
§Parameters
All inputs are bundled in GroupedQueryFn: the analyzed query, both
generated struct names, the flat and split (parent/child) column sets,
the resolved params, and the grouping key column.
§Return value
A string containing the full query function definition, stored in
[GeneratedCode::query_fn].
§Default implementation
Returns an error: “grouped queries are not yet supported by the ‘<name>’ backend”. Backends opt in by overriding this method.
Sourcefn apply_options(
&mut self,
_options: &HashMap<String, String>,
) -> Result<(), ScytheError>
fn apply_options( &mut self, _options: &HashMap<String, String>, ) -> Result<(), ScytheError>
Apply per-backend configuration options from [[sql.gen]].
Backends override this to handle options like row_type = "pydantic".
Sourcefn supported_engines(&self) -> &[&str]
fn supported_engines(&self) -> &[&str]
Database engines this backend supports. Defaults to PostgreSQL only. Multi-DB backends override this.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".