1use crate::{
16 BranchSqlSelector, BranchSqlStatement, BranchSqlValue, DeleteStatement, ExprKind,
17 InsertStatement, SelectProjection, SelectStatement, SqlArithmeticOperand, SqlAssignment,
18 SqlAssignmentValue, SqlBound, SqlColumnDefault, SqlExpression, SqlStatement, SqlValue,
19 UpdateStatement,
20};
21use hawdb_core::{HawDBError, Result};
22use std::collections::BTreeSet;
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct PreparedPostgresStatement {
26 pub statement: SqlStatement,
27 pub parameters: Vec<PostgresParameterMetadata>,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct PostgresParameterMetadata {
32 pub position: usize,
33}
34
35pub fn prepare_postgres_sql(input: &str) -> Result<PreparedPostgresStatement> {
36 let statement = crate::parse_postgres_sql(input)?;
37 let mut positions = BTreeSet::new();
38 collect_statement_parameters(&statement, &mut positions);
39 if !positions.is_empty()
40 && matches!(
41 &statement,
42 SqlStatement::CreateTable(_)
43 | SqlStatement::CreateIndex(_)
44 | SqlStatement::AlterTableAddColumn(_)
45 )
46 {
47 return Err(HawDBError::Semantic(
48 "PostgreSQL schema statements do not accept parameters".to_string(),
49 ));
50 }
51 let maximum = positions.last().copied().unwrap_or(0);
52 let expected = (1..=maximum).collect::<BTreeSet<_>>();
53 if positions != expected {
54 return Err(HawDBError::Semantic(format!(
55 "PostgreSQL parameter positions must be dense from $1 through ${maximum}; found {positions:?}"
56 )));
57 }
58 Ok(PreparedPostgresStatement {
59 statement,
60 parameters: positions
61 .into_iter()
62 .map(|position| PostgresParameterMetadata { position })
63 .collect(),
64 })
65}
66
67fn collect_statement_parameters(statement: &SqlStatement, positions: &mut BTreeSet<usize>) {
68 match statement {
69 SqlStatement::Explain(explain) => {
70 collect_statement_parameters(&explain.statement, positions)
71 }
72 SqlStatement::Select(select) => collect_select_parameters(select, positions),
73 SqlStatement::Insert(insert) => collect_insert_parameters(insert, positions),
74 SqlStatement::Update(update) => collect_update_parameters(update, positions),
75 SqlStatement::Delete(delete) => collect_delete_parameters(delete, positions),
76 SqlStatement::CreateTable(create) => {
77 for column in &create.columns {
78 if let Some(value) = &column.default {
79 collect_column_default_parameter(value, positions);
80 }
81 }
82 }
83 SqlStatement::AlterTableAddColumn(alter) => {
84 if let Some(value) = &alter.column.default {
85 collect_column_default_parameter(value, positions);
86 }
87 }
88 SqlStatement::CreateIndex(_) => {}
89 SqlStatement::Branch(branch) => collect_branch_parameters(branch, positions),
90 }
91}
92
93fn collect_branch_parameters(branch: &BranchSqlStatement, positions: &mut BTreeSet<usize>) {
94 match branch {
95 BranchSqlStatement::ShowBranches(statement) => {
96 collect_bound_parameter(Some(statement.limit), positions);
97 collect_bound_parameter(statement.offset, positions);
98 }
99 BranchSqlStatement::ShowBranch(statement) => {
100 collect_branch_selector(&statement.selector, positions)
101 }
102 BranchSqlStatement::UseBranch(selector) => collect_branch_selector(selector, positions),
103 BranchSqlStatement::ShowCurrentBranch => {}
104 BranchSqlStatement::CreateBranch(statement) => {
105 if let Some(name) = &statement.name {
106 collect_branch_value(name, positions);
107 }
108 collect_branch_selector(&statement.source, positions);
109 collect_bound_parameter(Some(statement.expected_source_revision), positions);
110 collect_branch_value(&statement.request_key, positions);
111 if let Some(owner) = &statement.owner {
112 collect_branch_value(owner, positions);
113 }
114 }
115 BranchSqlStatement::DropBranch(statement) => {
116 collect_branch_value(&statement.id, positions);
117 collect_bound_parameter(Some(statement.expected_metadata_revision), positions);
118 }
119 }
120}
121
122fn collect_branch_selector(selector: &BranchSqlSelector, positions: &mut BTreeSet<usize>) {
123 let (BranchSqlSelector::Name(value) | BranchSqlSelector::Id(value)) = selector;
124 collect_branch_value(value, positions);
125}
126
127fn collect_branch_value(value: &BranchSqlValue, positions: &mut BTreeSet<usize>) {
128 if let BranchSqlValue::Parameter(position) = value {
129 positions.insert(*position);
130 }
131}
132
133fn collect_column_default_parameter(default: &SqlColumnDefault, positions: &mut BTreeSet<usize>) {
134 if let SqlColumnDefault::Literal(value) = default {
135 collect_value_parameter(value, positions);
136 }
137}
138
139fn collect_select_parameters(select: &SelectStatement, positions: &mut BTreeSet<usize>) {
140 for projection in &select.projection {
141 if let SelectProjection::Expression { expression, .. } = projection {
142 collect_expression_parameters(expression, positions);
143 }
144 }
145 for join in &select.joins {
146 collect_expression_parameters(&join.on, positions);
147 }
148 if let Some(predicate) = &select.selection {
149 collect_expression_parameters(predicate, positions);
150 }
151 if let Some(predicate) = &select.having {
152 collect_expression_parameters(predicate, positions);
153 }
154 for order in &select.order_by {
155 collect_expression_parameters(&order.expression, positions);
156 }
157 collect_bound_parameter(select.limit, positions);
158 collect_bound_parameter(select.offset, positions);
159}
160
161fn collect_insert_parameters(insert: &InsertStatement, positions: &mut BTreeSet<usize>) {
162 for row in &insert.rows {
163 for value in row {
164 collect_value_parameter(value, positions);
165 }
166 }
167 if let Some(conflict) = &insert.on_conflict
168 && let crate::SqlConflictAction::DoUpdate(assignments) = &conflict.action
169 {
170 collect_assignment_parameters(assignments, positions);
171 }
172}
173
174fn collect_update_parameters(update: &UpdateStatement, positions: &mut BTreeSet<usize>) {
175 collect_assignment_parameters(&update.assignments, positions);
176 if let Some(predicate) = &update.selection {
177 collect_expression_parameters(predicate, positions);
178 }
179}
180
181fn collect_delete_parameters(delete: &DeleteStatement, positions: &mut BTreeSet<usize>) {
182 if let Some(predicate) = &delete.selection {
183 collect_expression_parameters(predicate, positions);
184 }
185}
186
187fn collect_assignment_parameters(assignments: &[SqlAssignment], positions: &mut BTreeSet<usize>) {
188 for assignment in assignments {
189 match &assignment.value {
190 SqlAssignmentValue::Value(value) => collect_value_parameter(value, positions),
191 SqlAssignmentValue::Column(_) => {}
192 SqlAssignmentValue::Arithmetic { left, right, .. } => {
193 collect_arithmetic_operand_parameter(left, positions);
194 collect_arithmetic_operand_parameter(right, positions);
195 }
196 }
197 }
198}
199
200fn collect_arithmetic_operand_parameter(
201 operand: &SqlArithmeticOperand,
202 positions: &mut BTreeSet<usize>,
203) {
204 if let SqlArithmeticOperand::Value(value) = operand {
205 collect_value_parameter(value, positions);
206 }
207}
208
209fn collect_expression_parameters(expression: &SqlExpression, positions: &mut BTreeSet<usize>) {
210 expression.visit(&mut |expression| {
211 if let ExprKind::Value(value) = &expression.kind {
212 collect_value_parameter(value, positions);
213 }
214 });
215}
216
217fn collect_value_parameter(value: &SqlValue, positions: &mut BTreeSet<usize>) {
218 if let SqlValue::Parameter(position) = value {
219 positions.insert(*position);
220 }
221}
222
223fn collect_bound_parameter(bound: Option<SqlBound>, positions: &mut BTreeSet<usize>) {
224 if let Some(SqlBound::Parameter(position)) = bound {
225 positions.insert(position);
226 }
227}