Simplifies working with SQL views on a connection by composing the appropriate CREATE and DROP statements, including optional database qualifiers and temporary views.

Use cases

  • Encapsulates quoting rules so developers can treat view names as plain strings.
  • Supports temporary views, making it easy to stage pipeline-friendly projections during ETL steps.
  • Pairs well with migrations: wrap view creation/deletion in transactions for atomic schema updates.

Public API

MemberDescription
view(connection& con)Binds the helper to a connection.
~view()Destructor; no special behavior.
void create(bool temporary, std::string const& alias, std::string const& sql_query)Creates a view in the default schema.
void create(bool temporary, std::string const& database, std::string const& alias, std::string const& sql_query)Creates a view within a named database (e.g., temp or an attached DB).
void drop(std::string const& alias)Drops a view by name in the default schema.
void drop(std::string const& database, std::string const& alias)Drops a view within the specified database.

Example usage

sqlite::view v{db};

v.create(false, "active_orders", R\"SQL(
    SELECT o.id, o.customer_id, o.total
    FROM orders o
    WHERE o.state = 'active'
)SQL\");

// Later...
v.drop("active_orders");

Need a view inside an attached database? just pass the database name: v.create(true, "analytics", "recent_events", ...);