essential_builder_db/
sql.rs

1//! Provides the SQL statements used by `essential-builder-db` via `const` `str`s.
2
3/// Short-hand for including an SQL string from the `sql/` subdir at compile time.
4macro_rules! include_sql_str {
5    ($subpath:expr) => {
6        include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/sql/", $subpath))
7    };
8}
9
10/// Short-hand for declaring a `const` SQL str and presenting the SQL via the doc comment.
11macro_rules! decl_const_sql_str {
12    ($name:ident, $subpath:expr) => {
13        /// ```sql
14        #[doc = include_sql_str!($subpath)]
15        /// ```
16        pub const $name: &str = include_sql_str!($subpath);
17    };
18}
19
20/// Table creation statements.
21pub mod create {
22    decl_const_sql_str!(SOLUTION_SET, "create/solution_set.sql");
23    decl_const_sql_str!(SUBMISSION, "create/submission.sql");
24    decl_const_sql_str!(SOLUTION_SET_FAILURE, "create/solution_set_failure.sql");
25}
26
27/// Statements for deleting rows from tables.
28pub mod delete {
29    decl_const_sql_str!(SOLUTION_SET, "delete/solution_set.sql");
30    decl_const_sql_str!(
31        OLDEST_SOLUTION_SET_FAILURES,
32        "delete/oldest_solution_set_failures.sql"
33    );
34}
35
36/// Statements for inserting rows into the tables.
37pub mod insert {
38    decl_const_sql_str!(SOLUTION_SET, "insert/solution_set.sql");
39    decl_const_sql_str!(SUBMISSION, "insert/submission.sql");
40    decl_const_sql_str!(SOLUTION_SET_FAILURE, "insert/solution_set_failure.sql");
41}
42
43/// Statements for making queries.
44pub mod query {
45    decl_const_sql_str!(GET_SOLUTION_SET, "query/get_solution_set.sql");
46    decl_const_sql_str!(LIST_SOLUTION_SETS, "query/list_solution_sets.sql");
47    decl_const_sql_str!(LIST_SUBMISSIONS, "query/list_submissions.sql");
48    decl_const_sql_str!(
49        LATEST_SOLUTION_SET_FAILURES,
50        "query/latest_solution_set_failures.sql"
51    );
52    decl_const_sql_str!(
53        LIST_SOLUTION_SET_FAILURES,
54        "query/list_solution_set_failures.sql"
55    );
56}
57
58pub mod table {
59    use super::create;
60
61    /// A table's name along with its create statement.
62    #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
63    pub struct Table {
64        /// The name of the table as declared in the create statement.
65        pub name: &'static str,
66        /// The table's create statement.
67        pub create: &'static str,
68    }
69
70    impl Table {
71        const fn new(name: &'static str, create: &'static str) -> Self {
72            Self { name, create }
73        }
74    }
75
76    pub const SOLUTION_SET: Table = Table::new("solution_set", create::SOLUTION_SET);
77    pub const SUBMISSION: Table = Table::new("submission", create::SUBMISSION);
78    pub const SOLUTION_SET_FAILURE: Table =
79        Table::new("solution_set_failure", create::SOLUTION_SET_FAILURE);
80
81    /// All tables in a list. Useful for initialisation and testing.
82    pub const ALL: &[Table] = &[SOLUTION_SET, SUBMISSION, SOLUTION_SET_FAILURE];
83}