From 4ddfdb2a62e7c69470a7b37a7b7e5f319f2889ed Mon Sep 17 00:00:00 2001 From: TcMits Date: Wed, 27 Aug 2025 14:58:35 +0700 Subject: [PATCH] finish --- core/incremental/view.rs | 9 +- core/translate/display.rs | 8 +- core/translate/main_loop.rs | 8 +- core/translate/optimizer/mod.rs | 9 +- core/translate/schema.rs | 10 +- core/translate/view.rs | 10 +- core/util.rs | 2 +- core/vdbe/execute.rs | 13 +-- parser/src/ast.rs | 8 -- parser/src/ast/fmt.rs | 137 ++++++++++++++---------- parser/src/parser.rs | 4 +- simulator/model/mod.rs | 6 +- sql_generation/model/query/mod.rs | 18 ---- sql_generation/model/query/predicate.rs | 7 +- sql_generation/model/query/select.rs | 13 +-- 15 files changed, 117 insertions(+), 145 deletions(-) diff --git a/core/incremental/view.rs b/core/incremental/view.rs index 4f4d4c6e6..c40c8ef40 100644 --- a/core/incremental/view.rs +++ b/core/incremental/view.rs @@ -124,14 +124,7 @@ impl IncrementalView { Parser::new(sql.as_bytes()).next_cmd() { // Compare the SELECT statements as SQL strings - use turso_parser::ast::fmt::ToTokens; - - // Format both SELECT statements and compare - if let (Ok(current_sql), Ok(provided_sql)) = - (self.select_stmt.format(), select.format()) - { - return current_sql == provided_sql; - } + return self.select_stmt == select; } false } diff --git a/core/translate/display.rs b/core/translate/display.rs index 1bbb976f5..f183bc3e8 100644 --- a/core/translate/display.rs +++ b/core/translate/display.rs @@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter}; use turso_parser::{ ast::{ self, - fmt::{ToSqlContext, ToTokens, TokenStream}, + fmt::{BlankContext, ToSqlContext, ToTokens, TokenStream}, SortOrder, TableInternalId, }, token::TokenType, @@ -323,6 +323,12 @@ impl ToTokens for Plan { } } +impl Display for JoinedTable { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.displayer(&BlankContext).fmt(f) + } +} + impl ToTokens for JoinedTable { fn to_tokens( &self, diff --git a/core/translate/main_loop.rs b/core/translate/main_loop.rs index f190f3424..c326c97c0 100644 --- a/core/translate/main_loop.rs +++ b/core/translate/main_loop.rs @@ -83,8 +83,7 @@ pub fn init_distinct(program: &mut ProgramBuilder, plan: &SelectPlan) -> Distinc name: col .expr .displayer(&PlanContext(&[&plan.table_references])) - .to_string() - .unwrap(), + .to_string(), order: SortOrder::Asc, pos_in_table: i, collation: None, // FIXME: this should be determined based on the result column expression! @@ -159,10 +158,7 @@ pub fn init_loop( ephemeral: true, root_page: 0, columns: vec![IndexColumn { - name: agg.args[0] - .displayer(&PlanContext(&[tables])) - .to_string() - .unwrap(), + name: agg.args[0].displayer(&PlanContext(&[tables])).to_string(), order: SortOrder::Asc, pos_in_table: 0, collation: None, // FIXME: this should be inferred from the expression diff --git a/core/translate/optimizer/mod.rs b/core/translate/optimizer/mod.rs index 7fb16b81b..49b88c402 100644 --- a/core/translate/optimizer/mod.rs +++ b/core/translate/optimizer/mod.rs @@ -8,7 +8,7 @@ use join::{compute_best_join_order, BestJoinOrderResult}; use lift_common_subexpressions::lift_common_subexpressions_from_binary_or_terms; use order::{compute_order_target, plan_satisfies_order_target, EliminatesSortBy}; use turso_ext::{ConstraintInfo, ConstraintUsage}; -use turso_parser::ast::{self, fmt::ToTokens as _, Expr, SortOrder}; +use turso_parser::ast::{self, Expr, SortOrder}; use crate::{ parameters::PARAM_PREFIX, @@ -52,12 +52,7 @@ pub fn optimize_plan(plan: &mut Plan, schema: &Schema) -> Result<()> { } } // When debug tracing is enabled, print the optimized plan as a SQL string for debugging - tracing::debug!( - plan_sql = plan - .displayer(&crate::translate::display::PlanContext(&[])) - .to_string() - .unwrap() - ); + tracing::debug!(plan_sql = plan.to_string()); Ok(()) } diff --git a/core/translate/schema.rs b/core/translate/schema.rs index b40e19216..83002dfba 100644 --- a/core/translate/schema.rs +++ b/core/translate/schema.rs @@ -28,7 +28,6 @@ use crate::SymbolTable; use crate::{bail_parse_error, Result}; use turso_ext::VTabKind; -use turso_parser::ast::fmt::ToTokens; pub fn translate_create_table( tbl_name: ast::QualifiedName, @@ -497,14 +496,7 @@ enum PrimaryKeyDefinitionType<'a> { fn create_table_body_to_str(tbl_name: &ast::QualifiedName, body: &ast::CreateTableBody) -> String { let mut sql = String::new(); - sql.push_str( - format!( - "CREATE TABLE {} {}", - tbl_name.name.as_str(), - body.format().unwrap() - ) - .as_str(), - ); + sql.push_str(format!("CREATE TABLE {} {}", tbl_name.name.as_str(), body).as_str()); match body { ast::CreateTableBody::ColumnsAndConstraints { columns: _, diff --git a/core/translate/view.rs b/core/translate/view.rs index b339e8961..c8dbe47d6 100644 --- a/core/translate/view.rs +++ b/core/translate/view.rs @@ -6,7 +6,7 @@ use crate::vdbe::builder::{CursorType, ProgramBuilder}; use crate::vdbe::insn::{CmpInsFlags, Cookie, Insn}; use crate::{Connection, Result, SymbolTable}; use std::sync::Arc; -use turso_parser::ast::{self, fmt::ToTokens}; +use turso_parser::ast; /// Common logic for creating views (both regular and materialized) fn emit_create_view_program( @@ -112,7 +112,7 @@ fn create_materialized_view_to_str(view_name: &str, select_stmt: &ast::Select) - format!( "CREATE MATERIALIZED VIEW {} AS {}", view_name, - select_stmt.format().unwrap() + select_stmt, ) } @@ -148,11 +148,7 @@ pub fn translate_create_view( } fn create_view_to_str(view_name: &str, select_stmt: &ast::Select) -> String { - format!( - "CREATE VIEW {} AS {}", - view_name, - select_stmt.format().unwrap() - ) + format!("CREATE VIEW {} AS {}", view_name, select_stmt) } pub fn translate_drop_view( diff --git a/core/util.rs b/core/util.rs index 9b7d9e2c8..621552fe1 100644 --- a/core/util.rs +++ b/core/util.rs @@ -1233,7 +1233,7 @@ pub fn extract_view_columns(select_stmt: &ast::Select, schema: &Schema) -> Vec todo!(), @@ -4975,8 +4972,7 @@ pub fn op_function( columns, where_clause, } - .format() - .unwrap(), + .to_string(), ) } ast::Stmt::CreateTable { @@ -5016,8 +5012,7 @@ pub fn op_function( options, }, } - .format() - .unwrap(), + .to_string(), ) } _ => todo!(), diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 144a8be93..db8e6c7c0 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -3,8 +3,6 @@ pub mod fmt; use strum_macros::{EnumIter, EnumString}; -use crate::ast::fmt::ToTokens; - /// `?` or `$` Prepared statement arg placeholder(s) #[derive(Default)] pub struct ParameterInfo { @@ -1146,12 +1144,6 @@ pub enum SortOrder { Desc, } -impl core::fmt::Display for SortOrder { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.to_fmt(f) - } -} - /// `NULLS FIRST` or `NULLS LAST` #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/parser/src/ast/fmt.rs b/parser/src/ast/fmt.rs index 9499ac07a..ee2ddea89 100644 --- a/parser/src/ast/fmt.rs +++ b/parser/src/ast/fmt.rs @@ -144,27 +144,18 @@ impl<'a, 'b, C: ToSqlContext, T: ToTokens> SqlDisplayer<'a, 'b, C, T> { pub fn new(ctx: &'a C, start_node: &'b T) -> Self { Self { ctx, start_node } } - - // Return string representation with context - pub fn to_string(&self) -> Result { - let mut s = String::new(); - let mut stream = WriteTokenStream::new(&mut s); - self.start_node.to_tokens(&mut stream, self.ctx)?; - Ok(s) - } } impl<'a, 'b, C: ToSqlContext, T: ToTokens> Display for SqlDisplayer<'a, 'b, C, T> { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { let mut stream = WriteTokenStream::new(f); - self.start_node - .to_tokens(&mut stream, self.ctx) - .map_err(|_| fmt::Error) + self.start_node.to_tokens(&mut stream, self.ctx) } } /// Generate token(s) from AST node -pub trait ToTokens { +/// Also implements Display to make sure devs won't forget Display +pub trait ToTokens: Display { /// Send token(s) to the specified stream with context fn to_tokens( &self, @@ -179,14 +170,16 @@ pub trait ToTokens { { SqlDisplayer::new(ctx, self) } +} - // Return string representation with blank context - fn format(&self) -> Result - where - Self: Sized, - { - self.displayer(&BlankContext).to_string() - } +macro_rules! impl_display_for_to_tokens { + ($struct:ty) => { + impl Display for $struct { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + self.displayer(&BlankContext).fmt(f) + } + } + }; } impl ToTokens for &T { @@ -219,6 +212,7 @@ impl ToTokens for String { } } +impl_display_for_to_tokens!(Cmd); impl ToTokens for Cmd { fn to_tokens( &self, @@ -244,12 +238,7 @@ impl ToTokens for Cmd { } } -impl Display for Cmd { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.displayer(&BlankContext).fmt(f) - } -} - +impl_display_for_to_tokens!(Stmt); impl ToTokens for Stmt { fn to_tokens( &self, @@ -689,6 +678,7 @@ impl ToTokens for Stmt { } } +impl_display_for_to_tokens!(Expr); impl ToTokens for Expr { fn to_tokens( &self, @@ -804,15 +794,10 @@ impl ToTokens for Expr { } Self::Id(id) => id.to_tokens(s, context), Self::Column { table, column, .. } => { - s.append(TK_ID, context.get_table_name(*table))?; + let (tbl_name, col_name) = context.get_table_and_column_names(*table, *column); + s.append(TK_ID, Some(tbl_name.as_ref()))?; s.append(TK_DOT, None)?; - s.append( - TK_ID, - context - .get_flat_column_name(*table, *column) - .as_ref() - .map(|s| s.as_ref()), - ) + s.append(TK_ID, Some(col_name.as_ref())) } Self::InList { lhs, not, rhs } => { lhs.to_tokens(s, context)?; @@ -925,12 +910,7 @@ impl ToTokens for Expr { } } -impl Display for Expr { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.displayer(&BlankContext).fmt(f) - } -} - +impl_display_for_to_tokens!(Literal); impl ToTokens for Literal { fn to_tokens( &self, @@ -950,6 +930,7 @@ impl ToTokens for Literal { } } +impl_display_for_to_tokens!(LikeOperator); impl ToTokens for LikeOperator { fn to_tokens( &self, @@ -968,6 +949,7 @@ impl ToTokens for LikeOperator { } } +impl_display_for_to_tokens!(Operator); impl ToTokens for Operator { fn to_tokens( &self, @@ -1005,6 +987,7 @@ impl ToTokens for Operator { } } +impl_display_for_to_tokens!(UnaryOperator); impl ToTokens for UnaryOperator { fn to_tokens( &self, @@ -1023,6 +1006,7 @@ impl ToTokens for UnaryOperator { } } +impl_display_for_to_tokens!(Select); impl ToTokens for Select { fn to_tokens( &self, @@ -1045,6 +1029,7 @@ impl ToTokens for Select { } } +impl_display_for_to_tokens!(SelectBody); impl ToTokens for SelectBody { fn to_tokens( &self, @@ -1059,6 +1044,7 @@ impl ToTokens for SelectBody { } } +impl_display_for_to_tokens!(CompoundSelect); impl ToTokens for CompoundSelect { fn to_tokens( &self, @@ -1070,6 +1056,7 @@ impl ToTokens for CompoundSelect { } } +impl_display_for_to_tokens!(CompoundOperator); impl ToTokens for CompoundOperator { fn to_tokens( &self, @@ -1088,12 +1075,7 @@ impl ToTokens for CompoundOperator { } } -impl Display for CompoundOperator { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.displayer(&BlankContext).fmt(f) - } -} - +impl_display_for_to_tokens!(OneSelect); impl ToTokens for OneSelect { fn to_tokens( &self, @@ -1148,6 +1130,7 @@ impl ToTokens for OneSelect { } } +impl_display_for_to_tokens!(FromClause); impl ToTokens for FromClause { fn to_tokens( &self, @@ -1163,6 +1146,7 @@ impl ToTokens for FromClause { } } +impl_display_for_to_tokens!(Distinctness); impl ToTokens for Distinctness { fn to_tokens( &self, @@ -1179,6 +1163,7 @@ impl ToTokens for Distinctness { } } +impl_display_for_to_tokens!(ResultColumn); impl ToTokens for ResultColumn { fn to_tokens( &self, @@ -1203,6 +1188,7 @@ impl ToTokens for ResultColumn { } } +impl_display_for_to_tokens!(As); impl ToTokens for As { fn to_tokens( &self, @@ -1219,6 +1205,7 @@ impl ToTokens for As { } } +impl_display_for_to_tokens!(JoinedSelectTable); impl ToTokens for JoinedSelectTable { fn to_tokens( &self, @@ -1234,6 +1221,7 @@ impl ToTokens for JoinedSelectTable { } } +impl_display_for_to_tokens!(SelectTable); impl ToTokens for SelectTable { fn to_tokens( &self, @@ -1283,6 +1271,7 @@ impl ToTokens for SelectTable { } } +impl_display_for_to_tokens!(JoinOperator); impl ToTokens for JoinOperator { fn to_tokens( &self, @@ -1301,6 +1290,7 @@ impl ToTokens for JoinOperator { } } +impl_display_for_to_tokens!(JoinType); impl ToTokens for JoinType { fn to_tokens( &self, @@ -1333,6 +1323,7 @@ impl ToTokens for JoinType { } } +impl_display_for_to_tokens!(JoinConstraint); impl ToTokens for JoinConstraint { fn to_tokens( &self, @@ -1354,6 +1345,7 @@ impl ToTokens for JoinConstraint { } } +impl_display_for_to_tokens!(GroupBy); impl ToTokens for GroupBy { fn to_tokens( &self, @@ -1371,6 +1363,7 @@ impl ToTokens for GroupBy { } } +impl_display_for_to_tokens!(Name); impl ToTokens for Name { fn to_tokens( &self, @@ -1381,12 +1374,7 @@ impl ToTokens for Name { } } -impl Display for Name { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.displayer(&BlankContext).fmt(f) - } -} - +impl_display_for_to_tokens!(QualifiedName); impl ToTokens for QualifiedName { fn to_tokens( &self, @@ -1406,12 +1394,7 @@ impl ToTokens for QualifiedName { } } -impl Display for QualifiedName { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.displayer(&BlankContext).fmt(f) - } -} - +impl_display_for_to_tokens!(AlterTableBody); impl ToTokens for AlterTableBody { fn to_tokens( &self, @@ -1445,6 +1428,7 @@ impl ToTokens for AlterTableBody { } } +impl_display_for_to_tokens!(CreateTableBody); impl ToTokens for CreateTableBody { fn to_tokens( &self, @@ -1481,6 +1465,7 @@ impl ToTokens for CreateTableBody { } } +impl_display_for_to_tokens!(ColumnDefinition); impl ToTokens for ColumnDefinition { fn to_tokens( &self, @@ -1498,6 +1483,7 @@ impl ToTokens for ColumnDefinition { } } +impl_display_for_to_tokens!(NamedColumnConstraint); impl ToTokens for NamedColumnConstraint { fn to_tokens( &self, @@ -1512,6 +1498,7 @@ impl ToTokens for NamedColumnConstraint { } } +impl_display_for_to_tokens!(ColumnConstraint); impl ToTokens for ColumnConstraint { fn to_tokens( &self, @@ -1602,6 +1589,7 @@ impl ToTokens for ColumnConstraint { } } +impl_display_for_to_tokens!(NamedTableConstraint); impl ToTokens for NamedTableConstraint { fn to_tokens( &self, @@ -1616,6 +1604,7 @@ impl ToTokens for NamedTableConstraint { } } +impl_display_for_to_tokens!(TableConstraint); impl ToTokens for TableConstraint { fn to_tokens( &self, @@ -1685,6 +1674,7 @@ impl ToTokens for TableConstraint { } } +impl_display_for_to_tokens!(SortOrder); impl ToTokens for SortOrder { fn to_tokens( &self, @@ -1701,6 +1691,7 @@ impl ToTokens for SortOrder { } } +impl_display_for_to_tokens!(NullsOrder); impl ToTokens for NullsOrder { fn to_tokens( &self, @@ -1718,6 +1709,7 @@ impl ToTokens for NullsOrder { } } +impl_display_for_to_tokens!(ForeignKeyClause); impl ToTokens for ForeignKeyClause { fn to_tokens( &self, @@ -1737,6 +1729,7 @@ impl ToTokens for ForeignKeyClause { } } +impl_display_for_to_tokens!(RefArg); impl ToTokens for RefArg { fn to_tokens( &self, @@ -1767,6 +1760,7 @@ impl ToTokens for RefArg { } } +impl_display_for_to_tokens!(RefAct); impl ToTokens for RefAct { fn to_tokens( &self, @@ -1792,6 +1786,7 @@ impl ToTokens for RefAct { } } +impl_display_for_to_tokens!(DeferSubclause); impl ToTokens for DeferSubclause { fn to_tokens( &self, @@ -1809,6 +1804,7 @@ impl ToTokens for DeferSubclause { } } +impl_display_for_to_tokens!(InitDeferredPred); impl ToTokens for InitDeferredPred { fn to_tokens( &self, @@ -1826,6 +1822,7 @@ impl ToTokens for InitDeferredPred { } } +impl_display_for_to_tokens!(IndexedColumn); impl ToTokens for IndexedColumn { fn to_tokens( &self, @@ -1844,6 +1841,7 @@ impl ToTokens for IndexedColumn { } } +impl_display_for_to_tokens!(Indexed); impl ToTokens for Indexed { fn to_tokens( &self, @@ -1864,6 +1862,7 @@ impl ToTokens for Indexed { } } +impl_display_for_to_tokens!(SortedColumn); impl ToTokens for SortedColumn { fn to_tokens( &self, @@ -1881,6 +1880,7 @@ impl ToTokens for SortedColumn { } } +impl_display_for_to_tokens!(Limit); impl ToTokens for Limit { fn to_tokens( &self, @@ -1897,6 +1897,7 @@ impl ToTokens for Limit { } } +impl_display_for_to_tokens!(InsertBody); impl ToTokens for InsertBody { fn to_tokens( &self, @@ -1919,6 +1920,7 @@ impl ToTokens for InsertBody { } } +impl_display_for_to_tokens!(Set); impl ToTokens for Set { fn to_tokens( &self, @@ -1937,6 +1939,7 @@ impl ToTokens for Set { } } +impl_display_for_to_tokens!(PragmaBody); impl ToTokens for PragmaBody { fn to_tokens( &self, @@ -1957,6 +1960,7 @@ impl ToTokens for PragmaBody { } } +impl_display_for_to_tokens!(TriggerTime); impl ToTokens for TriggerTime { fn to_tokens( &self, @@ -1974,6 +1978,7 @@ impl ToTokens for TriggerTime { } } +impl_display_for_to_tokens!(TriggerEvent); impl ToTokens for TriggerEvent { fn to_tokens( &self, @@ -1993,6 +1998,7 @@ impl ToTokens for TriggerEvent { } } +impl_display_for_to_tokens!(TriggerCmd); impl ToTokens for TriggerCmd { fn to_tokens( &self, @@ -2077,6 +2083,7 @@ impl ToTokens for TriggerCmd { } } +impl_display_for_to_tokens!(ResolveType); impl ToTokens for ResolveType { fn to_tokens( &self, @@ -2096,6 +2103,7 @@ impl ToTokens for ResolveType { } } +impl_display_for_to_tokens!(With); impl ToTokens for With { fn to_tokens( &self, @@ -2110,6 +2118,7 @@ impl ToTokens for With { } } +impl_display_for_to_tokens!(CommonTableExpr); impl ToTokens for CommonTableExpr { fn to_tokens( &self, @@ -2139,6 +2148,7 @@ impl ToTokens for CommonTableExpr { } } +impl_display_for_to_tokens!(Type); impl ToTokens for Type { fn to_tokens( &self, @@ -2157,6 +2167,7 @@ impl ToTokens for Type { } } +impl_display_for_to_tokens!(TypeSize); impl ToTokens for TypeSize { fn to_tokens( &self, @@ -2174,6 +2185,7 @@ impl ToTokens for TypeSize { } } +impl_display_for_to_tokens!(TransactionType); impl ToTokens for TransactionType { fn to_tokens( &self, @@ -2191,6 +2203,7 @@ impl ToTokens for TransactionType { } } +impl_display_for_to_tokens!(Upsert); impl ToTokens for Upsert { fn to_tokens( &self, @@ -2210,6 +2223,7 @@ impl ToTokens for Upsert { } } +impl_display_for_to_tokens!(UpsertIndex); impl ToTokens for UpsertIndex { fn to_tokens( &self, @@ -2227,6 +2241,7 @@ impl ToTokens for UpsertIndex { } } +impl_display_for_to_tokens!(UpsertDo); impl ToTokens for UpsertDo { fn to_tokens( &self, @@ -2253,6 +2268,7 @@ impl ToTokens for UpsertDo { } } +impl_display_for_to_tokens!(FunctionTail); impl ToTokens for FunctionTail { fn to_tokens( &self, @@ -2274,6 +2290,7 @@ impl ToTokens for FunctionTail { } } +impl_display_for_to_tokens!(Over); impl ToTokens for Over { fn to_tokens( &self, @@ -2287,6 +2304,7 @@ impl ToTokens for Over { } } +impl_display_for_to_tokens!(WindowDef); impl ToTokens for WindowDef { fn to_tokens( &self, @@ -2299,6 +2317,7 @@ impl ToTokens for WindowDef { } } +impl_display_for_to_tokens!(Window); impl ToTokens for Window { fn to_tokens( &self, @@ -2326,6 +2345,7 @@ impl ToTokens for Window { } } +impl_display_for_to_tokens!(FrameClause); impl ToTokens for FrameClause { fn to_tokens( &self, @@ -2349,6 +2369,7 @@ impl ToTokens for FrameClause { } } +impl_display_for_to_tokens!(FrameMode); impl ToTokens for FrameMode { fn to_tokens( &self, @@ -2366,6 +2387,7 @@ impl ToTokens for FrameMode { } } +impl_display_for_to_tokens!(FrameBound); impl ToTokens for FrameBound { fn to_tokens( &self, @@ -2397,6 +2419,7 @@ impl ToTokens for FrameBound { } } +impl_display_for_to_tokens!(FrameExclude); impl ToTokens for FrameExclude { fn to_tokens( &self, diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 6c5253b00..321d7d40c 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -11411,11 +11411,9 @@ mod tests { assert_eq!(results, expected, "Input: {input_str:?}"); - use crate::ast::fmt::{BlankContext, ToTokens}; // to_string tests for (i, r) in results.iter().enumerate() { - let rstring = r.displayer(&BlankContext).to_string().unwrap(); - + let rstring = r.to_string(); // put new string into parser again let result = Parser::new(rstring.as_bytes()).next().unwrap().unwrap(); let expected = &expected[i]; diff --git a/simulator/model/mod.rs b/simulator/model/mod.rs index ce249baf5..d854da4c2 100644 --- a/simulator/model/mod.rs +++ b/simulator/model/mod.rs @@ -8,11 +8,11 @@ use sql_generation::model::{ select::{CompoundOperator, FromClause, ResultColumn, SelectInner}, transaction::{Begin, Commit, Rollback}, update::Update, - Create, CreateIndex, Delete, Drop, EmptyContext, Insert, Select, + Create, CreateIndex, Delete, Drop, Insert, Select, }, table::{JoinTable, JoinType, SimValue, Table, TableContext}, }; -use turso_parser::ast::{fmt::ToTokens, Distinctness}; +use turso_parser::ast::Distinctness; use crate::{generation::Shadow, runner::env::SimulatorTables}; @@ -306,7 +306,7 @@ impl Shadow for SelectInner { } else { return Err(anyhow::anyhow!( "Failed to evaluate expression in free select ({})", - expr.0.format_with_context(&EmptyContext {}).unwrap() + expr.0 )); } } diff --git a/sql_generation/model/query/mod.rs b/sql_generation/model/query/mod.rs index 5bf0cecde..11d117a53 100644 --- a/sql_generation/model/query/mod.rs +++ b/sql_generation/model/query/mod.rs @@ -4,7 +4,6 @@ pub use delete::Delete; pub use drop::Drop; pub use insert::Insert; pub use select::Select; -use turso_parser::ast::fmt::ToSqlContext; pub mod create; pub mod create_index; @@ -15,20 +14,3 @@ pub mod predicate; pub mod select; pub mod transaction; pub mod update; - -/// Used to print sql strings that already have all the context it needs -pub struct EmptyContext; - -impl ToSqlContext for EmptyContext { - fn get_column_name( - &self, - _table_id: turso_parser::ast::TableInternalId, - _col_idx: usize, - ) -> String { - unreachable!() - } - - fn get_table_name(&self, _id: turso_parser::ast::TableInternalId) -> &str { - unreachable!() - } -} diff --git a/sql_generation/model/query/predicate.rs b/sql_generation/model/query/predicate.rs index 29f0b966c..50a72fee4 100644 --- a/sql_generation/model/query/predicate.rs +++ b/sql_generation/model/query/predicate.rs @@ -1,7 +1,10 @@ use std::fmt::Display; use serde::{Deserialize, Serialize}; -use turso_parser::ast::{self, fmt::ToTokens}; +use turso_parser::ast::{ + self, + fmt::{BlankContext, ToTokens}, +}; use crate::model::table::{SimValue, Table, TableContext}; @@ -142,6 +145,6 @@ pub fn expr_to_value( impl Display for Predicate { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.to_fmt(f) + self.0.displayer(&BlankContext).fmt(f) } } diff --git a/sql_generation/model/query/select.rs b/sql_generation/model/query/select.rs index 6c34888ff..ea4a375db 100644 --- a/sql_generation/model/query/select.rs +++ b/sql_generation/model/query/select.rs @@ -3,13 +3,14 @@ use std::{collections::HashSet, fmt::Display}; pub use ast::Distinctness; use itertools::Itertools; use serde::{Deserialize, Serialize}; -use turso_parser::ast::{self, fmt::ToTokens, SortOrder}; - -use crate::model::{ - query::EmptyContext, - table::{JoinTable, JoinType, JoinedTable, Table}, +use turso_parser::ast::{ + self, + fmt::{BlankContext, ToTokens}, + SortOrder, }; +use crate::model::table::{JoinTable, JoinType, JoinedTable, Table}; + use super::predicate::Predicate; /// `SELECT` or `RETURNING` result column @@ -366,7 +367,7 @@ impl Select { impl Display for Select { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.to_sql_ast().to_fmt_with_context(f, &EmptyContext {}) + self.to_sql_ast().displayer(&BlankContext).fmt(f) } }