mz_deploy/project/syntax/input.rs
1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Source-owned data types for a single parsed database object.
11//!
12//! [`DatabaseObject`] and [`ObjectVariant`] carry the parsed statements and
13//! source locations for one `.sql` file (or a default + profile-override pair)
14//! before semantic validation. Loading and assembly into per-schema groupings
15//! live in [`crate::project::compiler`].
16
17use crate::project::syntax::parser::LocatedStatement;
18use std::path::PathBuf;
19
20/// A single file variant of a database object (default or profile-specific).
21#[derive(Debug, Clone)]
22pub struct ObjectVariant {
23 /// The full path to the file
24 pub path: PathBuf,
25 /// The profile name, or `None` for the default variant
26 pub profile: Option<String>,
27 /// The parsed SQL statements from the file, each with its byte offset.
28 pub statements: Vec<LocatedStatement>,
29}
30
31/// A database object that may have multiple profile variants.
32///
33/// Represents one logical object name in a schema directory. The object may have
34/// a default file and/or one or more profile-specific override files. All variants
35/// are loaded and parsed; cross-variant validation and active-variant resolution
36/// happen during object compilation.
37///
38/// # Contents
39///
40/// A typical object file contains:
41/// - One primary CREATE statement (table, view, source, etc.)
42/// - Zero or more supporting statements (indexes, grants, comments)
43///
44/// Example `users.sql`:
45/// ```sql
46/// CREATE TABLE users (
47/// id INT,
48/// name TEXT
49/// );
50///
51/// CREATE INDEX users_id_idx ON users (id);
52/// GRANT SELECT ON users TO analyst_role;
53/// COMMENT ON TABLE users IS 'User data';
54/// ```
55///
56/// All statements are parsed into `statements` field without validation of their
57/// relationships or correctness.
58#[derive(Debug, Clone)]
59pub struct DatabaseObject {
60 /// The name of the object (without extension or profile suffix)
61 pub name: String,
62 /// The directory-derived database name (no profile suffix applied).
63 /// Per-object validation uses this to match the declared database in the
64 /// user's SQL against the directory the file lives under; the suffix is
65 /// reapplied to the compiled statement after assembly.
66 pub database: String,
67 /// The schema name (directory name)
68 pub schema: String,
69 /// All profile variants for this object (at least one)
70 pub variants: Vec<ObjectVariant>,
71}