Skip to main content

limbo_sqlite3_parser/to_sql_string/
mod.rs

1//! ToSqlString trait definition and implementations
2
3mod expr;
4mod stmt;
5
6use crate::ast::TableInternalId;
7
8/// Context to be used in ToSqlString
9pub trait ToSqlContext {
10    /// Given an id, get the table name
11    ///
12    /// Currently not considering aliases
13    fn get_table_name(&self, id: TableInternalId) -> &str;
14    /// Given a table id and a column index, get the column name
15    fn get_column_name(&self, table_id: TableInternalId, col_idx: usize) -> &str;
16}
17
18/// Trait to convert an ast to a string
19pub trait ToSqlString {
20    /// Convert the given value to String
21    fn to_sql_string<C: ToSqlContext>(&self, context: &C) -> String;
22}
23
24impl<T: ToSqlString> ToSqlString for Box<T> {
25    fn to_sql_string<C: ToSqlContext>(&self, context: &C) -> String {
26        T::to_sql_string(&self, context)
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::ToSqlContext;
33
34    struct TestContext;
35
36    impl ToSqlContext for TestContext {
37        fn get_column_name(&self, _table_id: crate::ast::TableInternalId, _col_idx: usize) -> &str {
38            "placeholder_column"
39        }
40
41        fn get_table_name(&self, _id: crate::ast::TableInternalId) -> &str {
42            "placeholder_table"
43        }
44    }
45}