essential_node_db_sql/
lib.rs

1//! Provides the SQL statements used by `essential-node-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!(BLOCK, "create/block.sql");
23    decl_const_sql_str!(BLOCK_SOLUTION_SET, "create/block_solution_set.sql");
24    decl_const_sql_str!(FAILED_BLOCK, "create/failed_block.sql");
25    decl_const_sql_str!(FINALIZED_BLOCK, "create/finalized_block.sql");
26    decl_const_sql_str!(MUTATION, "create/mutation.sql");
27    decl_const_sql_str!(PRED_DATA, "create/pred_data.sql");
28    decl_const_sql_str!(SOLUTION, "create/solution.sql");
29    decl_const_sql_str!(SOLUTION_SET, "create/solution_set.sql");
30    decl_const_sql_str!(STATE, "create/state.sql");
31    decl_const_sql_str!(VALIDATION_PROGRESS, "create/validation_progress.sql");
32}
33
34/// Statements for inserting rows into the tables.
35pub mod insert {
36    decl_const_sql_str!(BLOCK, "insert/block.sql");
37    decl_const_sql_str!(BLOCK_SOLUTION_SET, "insert/block_solution_set.sql");
38    decl_const_sql_str!(FAILED_BLOCK, "insert/failed_block.sql");
39    decl_const_sql_str!(FINALIZE_BLOCK, "insert/finalize_block.sql");
40    decl_const_sql_str!(MUTATION, "insert/mutation.sql");
41    decl_const_sql_str!(PRED_DATA, "insert/pred_data.sql");
42    decl_const_sql_str!(SOLUTION, "insert/solution.sql");
43    decl_const_sql_str!(SOLUTION_SET, "insert/solution_set.sql");
44    decl_const_sql_str!(VALIDATION_PROGRESS, "insert/validation_progress.sql");
45}
46
47/// Statements for making queries.
48pub mod query {
49    decl_const_sql_str!(GET_BLOCK_HEADER, "query/get_block_header.sql");
50    decl_const_sql_str!(GET_BLOCK, "query/get_block.sql");
51    decl_const_sql_str!(GET_LATEST_BLOCK_NUMBER, "query/get_latest_block_number.sql");
52    decl_const_sql_str!(
53        GET_LATEST_FINALIZED_BLOCK_ADDRESS,
54        "query/get_latest_finalized_block_address.sql"
55    );
56    decl_const_sql_str!(
57        GET_NEXT_BLOCK_ADDRESSES,
58        "query/get_next_block_addresses.sql"
59    );
60    decl_const_sql_str!(
61        GET_PARENT_BLOCK_ADDRESS,
62        "query/get_parent_block_address.sql"
63    );
64    decl_const_sql_str!(GET_SOLUTION, "query/get_solution.sql");
65    decl_const_sql_str!(GET_SOLUTION_MUTATIONS, "query/get_solution_mutations.sql");
66    decl_const_sql_str!(GET_SOLUTION_PRED_DATA, "query/get_solution_pred_data.sql");
67    decl_const_sql_str!(GET_STATE, "query/get_state.sql");
68    decl_const_sql_str!(GET_VALIDATION_PROGRESS, "query/get_validation_progress.sql");
69    decl_const_sql_str!(LIST_BLOCKS, "query/list_blocks.sql");
70    decl_const_sql_str!(LIST_BLOCKS_BY_TIME, "query/list_blocks_by_time.sql");
71    decl_const_sql_str!(LIST_FAILED_BLOCKS, "query/list_failed_blocks.sql");
72    decl_const_sql_str!(LIST_UNCHECKED_BLOCKS, "query/list_unchecked_blocks.sql");
73    decl_const_sql_str!(
74        QUERY_STATE_AT_BLOCK_FINALIZED,
75        "query/query_state_at_block_finalized.sql"
76    );
77    decl_const_sql_str!(
78        QUERY_STATE_AT_SOLUTION_SET_FINALIZED,
79        "query/query_state_at_solution_set_finalized.sql"
80    );
81    decl_const_sql_str!(
82        QUERY_STATE_BLOCK_ADDRESS,
83        "query/query_state_block_address.sql"
84    );
85}
86
87/// Statements for updating and deleting state.
88pub mod update {
89    decl_const_sql_str!(STATE, "update/state.sql");
90    decl_const_sql_str!(DELETE_STATE, "update/delete_state.sql");
91}
92
93pub mod table {
94    use crate::create;
95
96    /// A table's name along with its create statement.
97    #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
98    pub struct Table {
99        /// The name of the table as declared in the create statement.
100        pub name: &'static str,
101        /// The table's create statement.
102        pub create: &'static str,
103    }
104
105    impl Table {
106        const fn new(name: &'static str, create: &'static str) -> Self {
107            Self { name, create }
108        }
109    }
110
111    pub const BLOCK: Table = Table::new("block", create::BLOCK);
112    pub const BLOCK_SOLUTION_SET: Table =
113        Table::new("block_solution_set", create::BLOCK_SOLUTION_SET);
114    pub const FAILED_BLOCK: Table = Table::new("failed_block", create::FAILED_BLOCK);
115    pub const FINALIZED_BLOCK: Table = Table::new("finalized_block", create::FINALIZED_BLOCK);
116    pub const MUTATION: Table = Table::new("mutation", create::MUTATION);
117    pub const PRED_DATA: Table = Table::new("pred_data", create::PRED_DATA);
118    pub const SOLUTION: Table = Table::new("solution", create::SOLUTION);
119    pub const SOLUTION_SET: Table = Table::new("solution_set", create::SOLUTION_SET);
120    pub const STATE: Table = Table::new("state", create::STATE);
121    pub const VALIDATION_PROGRESS: Table =
122        Table::new("validation_progress", create::VALIDATION_PROGRESS);
123
124    /// All tables in a list. Useful for initialisation and testing.
125    pub const ALL: &[Table] = &[
126        BLOCK,
127        FINALIZED_BLOCK,
128        MUTATION,
129        PRED_DATA,
130        SOLUTION,
131        SOLUTION_SET,
132        BLOCK_SOLUTION_SET,
133        FAILED_BLOCK,
134        STATE,
135        VALIDATION_PROGRESS,
136    ];
137}