Skip to main content

reifydb_engine/bulk_insert/primitive/
table.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::params::Params;
5
6use crate::bulk_insert::builder::{BulkInsertBuilder, ValidationMode};
7
8/// Buffered table insert operation
9#[derive(Debug, Clone)]
10pub struct PendingTableInsert {
11	pub namespace: String,
12	pub table: String,
13	pub rows: Vec<Params>,
14}
15
16impl PendingTableInsert {
17	pub fn new(namespace: String, table: String) -> Self {
18		Self {
19			namespace,
20			table,
21			rows: Vec::new(),
22		}
23	}
24
25	pub fn add_row(&mut self, params: Params) {
26		self.rows.push(params);
27	}
28
29	pub fn add_rows<I: IntoIterator<Item = Params>>(&mut self, iter: I) {
30		self.rows.extend(iter);
31	}
32}
33
34/// Builder for inserting rows into a specific table.
35///
36/// Created by calling `table()` on a `BulkInsertBuilder`.
37/// Call `done()` to finish and return to the main builder.
38pub struct TableInsertBuilder<'a, 'e, V: ValidationMode> {
39	parent: &'a mut BulkInsertBuilder<'e, V>,
40	pending: PendingTableInsert,
41}
42
43impl<'a, 'e, V: ValidationMode> TableInsertBuilder<'a, 'e, V> {
44	/// Create a new table insert builder.
45	pub(crate) fn new(parent: &'a mut BulkInsertBuilder<'e, V>, namespace: String, table: String) -> Self {
46		Self {
47			parent,
48			pending: PendingTableInsert::new(namespace, table),
49		}
50	}
51
52	/// Add a single row from named params.
53	///
54	/// # Example
55	///
56	/// ```ignore
57	/// builder.row(params!{ id: 1, name: "Alice" })
58	/// ```
59	pub fn row(mut self, params: Params) -> Self {
60		self.pending.add_row(params);
61		self
62	}
63
64	/// Add multiple rows from an iterator.
65	///
66	/// # Example
67	///
68	/// ```ignore
69	/// let rows = vec![
70	///     params!{ id: 1, name: "Alice" },
71	///     params!{ id: 2, name: "Bob" },
72	/// ];
73	/// builder.rows(rows)
74	/// ```
75	pub fn rows<I>(mut self, iter: I) -> Self
76	where
77		I: IntoIterator<Item = Params>,
78	{
79		self.pending.add_rows(iter);
80		self
81	}
82
83	/// Finish this table insert and return to the main builder.
84	///
85	/// This allows chaining to insert into additional targets.
86	pub fn done(self) -> &'a mut BulkInsertBuilder<'e, V> {
87		self.parent.add_table_insert(self.pending);
88		self.parent
89	}
90}