Skip to content

🔎 SQL 3.2.0

Latest

Choose a tag to compare

@MihaelIsaev MihaelIsaev released this 30 Nov 15:07

The new way to inspect a query while it is being prepared.

This was built mostly for SwiftStream/Duck and other database integrations. They can now get structured information about a prepared query without parsing the generated SQL back into a query structure.

.package(
    url: "https://github.com/SwiftStream/SQL",
    from: "3.2.0"
)

Inspect a prepared query

let query = SQL
    .select(DB.Column("id"))
    .from(DB.Table("events"))
    .where(DB.Column("tenant_id") == 41)

let observed = query.prepareObserving(.psql)
let split = observed.prepared.splitted

if case .complete(let trace) = observed.preparationTrace,
   case .directSelect(let select) = trace.statement,
   case .andEqualities(let whereClause) = select.predicate,
   let tenantFilter = whereClause.equalities.first {
    print(select.relation.table) // events
    print(split.values[tenantFilter.bindingIndex]) // 41
}

The trace and the prepared query come from the same preparation pass, so a bindingIndex always points to the corresponding value in observed.prepared.splitted.values.

What can be inspected

For supported direct SELECT, INSERT, UPDATE, and DELETE statements, the preparation trace can expose things such as:

  • the target table
  • supported conditions
  • statement-specific details
  • exact bind positions

This makes the API useful for database drivers and integration layers that need to understand a query before executing it.

Conservative by design

SQL does not try to reverse-engineer or guess the meaning of generated SQL.

If a query contains raw SQL, custom components, or another shape that cannot be described safely, the query is still prepared normally, but its semantic trace is unavailable.

Existing custom SQLDialect implementations remain source-compatible. Preparation semantics are currently provided by the built-in PostgreSQL, MySQL, and Duck dialects.

Why this exists

Most applications will probably never need to call this API directly.

It was introduced primarily for SwiftStream/Duck, where the driver needs more information about a query than just the final SQL string and bind values.

SQL now gives that information directly from the same preparation process that produces the query it will actually execute.