Skip to main content

PostgresProjectionGenerator

Struct PostgresProjectionGenerator 

Source
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

Source

pub fn new() -> Self

Create new PostgreSQL projection generator with default JSONB column name.

Default JSONB column: “data”

Source

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
Source

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.

Source

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.

§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.

Source

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 clause
  • fields - 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§

Source§

impl Default for PostgresProjectionGenerator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more