pub struct PostgresProjectionGenerator { /* private fields */ }Expand description
PostgreSQL SQL projection generator using jsonb_build_object.
Generates efficient PostgreSQL SQL that projects only requested JSONB fields, reducing payload size and JSON deserialization time.
Implementations§
Source§impl PostgresProjectionGenerator
impl PostgresProjectionGenerator
Sourcepub fn new() -> Self
pub fn new() -> Self
Create new PostgreSQL projection generator with default JSONB column name.
Default JSONB column: “data”
Sourcepub fn with_column(jsonb_column: &str) -> Self
pub fn with_column(jsonb_column: &str) -> Self
Create projection generator with custom JSONB column name.
§Arguments
jsonb_column- Name of the JSONB column in the database table
Sourcepub fn generate_projection_sql(&self, fields: &[String]) -> Result<String>
pub fn generate_projection_sql(&self, fields: &[String]) -> Result<String>
Generate PostgreSQL projection SQL for specified fields.
Generates a jsonb_build_object() call that selects only the requested fields
from the JSONB column, drastically reducing payload size.
§Arguments
fields- GraphQL field names to project from JSONB
§Returns
SQL fragment that can be used in a SELECT clause, e.g.:
jsonb_build_object('id', data->>'id', 'email', data->>'email')
§Example
use fraiseql_db::projection_generator::PostgresProjectionGenerator;
let generator = PostgresProjectionGenerator::new();
let fields = vec!["id".to_string(), "email".to_string()];
let sql = generator.generate_projection_sql(&fields)?;
// Returns:
// jsonb_build_object('id', data->>'id', 'email', data->>'email')
assert!(sql.contains("jsonb_build_object"));§Errors
Returns FraiseQLError::Validation if any field name contains characters
that cannot be safely included in a SQL projection.
Sourcepub fn generate_typed_projection_sql(
&self,
fields: &[ProjectionField],
) -> Result<String>
pub fn generate_typed_projection_sql( &self, fields: &[ProjectionField], ) -> Result<String>
Generate type-aware PostgreSQL projection SQL.
Uses -> (JSONB extraction) for composite fields (objects, lists) and
->> (text extraction) for scalar fields. This avoids the unnecessary
text→JSON round-trip that occurs when ->> is used for nested objects.
When a composite field carries sub_fields, the generator recurses and
emits a nested jsonb_build_object(...) that selects only the requested
sub-fields rather than returning the entire blob. Recursion is capped at
[MAX_PROJECTION_DEPTH] levels; deeper fields fall back to data->'field'.
§Arguments
fields- Projection fields with type information
§Errors
Returns FraiseQLError::Validation if any field name contains characters
that cannot be safely included in a SQL projection.
Sourcepub fn generate_select_clause(
&self,
table_alias: &str,
fields: &[String],
) -> Result<String>
pub fn generate_select_clause( &self, table_alias: &str, fields: &[String], ) -> Result<String>
Generate complete SELECT clause with projection for a table.
§Arguments
table_alias- Table alias or name in the FROM clausefields- Fields to project
§Returns
Complete SELECT clause, e.g.: SELECT jsonb_build_object(...) as data
§Example
use fraiseql_db::projection_generator::PostgresProjectionGenerator;
let generator = PostgresProjectionGenerator::new();
let fields = vec!["id".to_string(), "name".to_string()];
let sql = generator.generate_select_clause("t", &fields).unwrap();
assert!(sql.contains("SELECT"));§Errors
Propagates any error from Self::generate_projection_sql.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PostgresProjectionGenerator
impl RefUnwindSafe for PostgresProjectionGenerator
impl Send for PostgresProjectionGenerator
impl Sync for PostgresProjectionGenerator
impl Unpin for PostgresProjectionGenerator
impl UnsafeUnpin for PostgresProjectionGenerator
impl UnwindSafe for PostgresProjectionGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more