use crate::diagnostic::DiagnosticBag;
use crate::synthetic::model::GeneratorConfig;
use crate::synthetic::schema::SqlTypeFamily;
use super::super::registry::{
ArgumentSpec, Buffering, ColumnScope, CompileContext, CompiledGenerator, Determinism,
GeneratorDescriptor, GeneratorFactory, RowContext, Verification,
};
use super::super::value::{GenerateError, GeneratedValue};
const FK_FAMILIES: &[SqlTypeFamily] = &[
SqlTypeFamily::Integer,
SqlTypeFamily::BigInteger,
SqlTypeFamily::Text,
SqlTypeFamily::Uuid,
SqlTypeFamily::Other,
];
const FK_ARGUMENTS: &[ArgumentSpec] = &[
ArgumentSpec {
name: "relationship",
required: false,
summary: "Name of the declared relationship this key follows.",
},
ArgumentSpec {
name: "distribution",
required: false,
summary: "Parent-assignment distribution: uniform, sequential, weighted, or observed.",
},
ArgumentSpec {
name: "null_rate",
required: false,
summary: "Fraction (0.0..=1.0) of children whose key is NULL (nullable columns only).",
},
];
pub struct ForeignKeyFactory;
static FOREIGN_KEY_DESCRIPTOR: GeneratorDescriptor = GeneratorDescriptor {
kind: "relation.foreign_key",
aliases: &["foreign_key"],
summary: "References a parent row; assigned by the generation engine.",
arguments: FK_ARGUMENTS,
accepts: FK_FAMILIES,
writes: ColumnScope::OwnColumn,
reads: ColumnScope::None,
determinism: Determinism::Deterministic,
buffering: Buffering::Streaming,
verification: Verification::Unsupported,
};
impl GeneratorFactory for ForeignKeyFactory {
fn descriptor(&self) -> &'static GeneratorDescriptor {
&FOREIGN_KEY_DESCRIPTOR
}
fn compile(
&self,
_config: &GeneratorConfig,
_context: &CompileContext<'_>,
) -> Result<Box<dyn CompiledGenerator>, DiagnosticBag> {
Ok(Box::new(ForeignKeyMarker {
kind: FOREIGN_KEY_DESCRIPTOR.kind,
}))
}
}
pub struct CompositeKeyFactory;
static COMPOSITE_KEY_DESCRIPTOR: GeneratorDescriptor = GeneratorDescriptor {
kind: "relation.composite_key",
aliases: &["composite_key"],
summary: "References a parent row via a composite key; assigned by the engine.",
arguments: FK_ARGUMENTS,
accepts: FK_FAMILIES,
writes: ColumnScope::OwnColumn,
reads: ColumnScope::None,
determinism: Determinism::Deterministic,
buffering: Buffering::Streaming,
verification: Verification::Unsupported,
};
impl GeneratorFactory for CompositeKeyFactory {
fn descriptor(&self) -> &'static GeneratorDescriptor {
&COMPOSITE_KEY_DESCRIPTOR
}
fn compile(
&self,
_config: &GeneratorConfig,
_context: &CompileContext<'_>,
) -> Result<Box<dyn CompiledGenerator>, DiagnosticBag> {
Ok(Box::new(ForeignKeyMarker {
kind: COMPOSITE_KEY_DESCRIPTOR.kind,
}))
}
}
struct ForeignKeyMarker {
kind: &'static str,
}
impl CompiledGenerator for ForeignKeyMarker {
fn generate(
&mut self,
_context: &RowContext<'_>,
_output: &mut GeneratedValue,
) -> Result<(), GenerateError> {
Err(GenerateError::InvalidInput(format!(
"`{}` is assigned by the generation engine, not executed as a column generator",
self.kind
)))
}
}