Skip to main content

dbx_core/sql/planner/logical/
insert.rs

1//! INSERT statement planning
2
3use crate::error::{DbxError, DbxResult};
4use crate::sql::planner::types::*;
5use sqlparser::ast::{Insert, SetExpr};
6
7use super::LogicalPlanner;
8
9impl LogicalPlanner {
10    /// INSERT INTO → LogicalPlan 변환
11    pub(super) fn plan_insert(&self, insert: &Insert) -> DbxResult<LogicalPlan> {
12        let table = insert.table_name.to_string();
13
14        // Extract column names
15        let column_names: Vec<String> = insert.columns.iter().map(|c| c.value.clone()).collect();
16
17        // Parse VALUES clause
18        let values = if let Some(source) = &insert.source {
19            match source.body.as_ref() {
20                SetExpr::Values(values_set) => {
21                    let mut rows = Vec::new();
22                    for row in &values_set.rows {
23                        let mut row_exprs = Vec::new();
24                        for expr in row {
25                            row_exprs.push(self.plan_expr(expr)?);
26                        }
27                        rows.push(row_exprs);
28                    }
29                    rows
30                }
31                _ => {
32                    return Err(DbxError::SqlNotSupported {
33                        feature: "INSERT with SELECT".to_string(),
34                        hint: "Only INSERT INTO ... VALUES (...) is supported".to_string(),
35                    });
36                }
37            }
38        } else {
39            return Err(DbxError::SqlNotSupported {
40                feature: "INSERT without VALUES".to_string(),
41                hint: "INSERT INTO ... VALUES (...) is required".to_string(),
42            });
43        };
44
45        Ok(LogicalPlan::Insert {
46            table,
47            columns: column_names,
48            values,
49        })
50    }
51}