Skip to main content

main_write

Function main_write 

Source
pub fn main_write(
    mutate_plan: &MutatePlan,
    _read_plan: &ReadPlanTree,
    return_representation: bool,
    handler: Option<&MediaHandler>,
    dialect: &dyn SqlDialect,
) -> SqlBuilder
Expand description

Build the final mutation statement with CTE wrapper.

Wraps a MutatePlan query in a CTE, optionally adding a read sub-select for Prefer: return=representation.

§Behaviour

  • The mutation CTE (dbrst_source) contains the INSERT/UPDATE/DELETE
  • If return_representation is true, the response body includes the returned rows as JSON
  • The location header expression is included for INSERT operations

§SQL Example

WITH dbrst_source AS (
  INSERT INTO "public"."users"("name") VALUES ($1) RETURNING "id", "name"
)
SELECT
  '' AS total_result_set,
  pg_catalog.count(_dbrst_t) AS page_total,
  coalesce(json_agg(_dbrst_t), '[]')::text AS body,
  nullif(current_setting('response.headers', true), '') AS response_headers,
  nullif(current_setting('response.status', true), '') AS response_status
FROM (SELECT * FROM dbrst_source) AS _dbrst_t