toasty_core/stmt/values.rs
1use super::{Expr, ExprSet, Query};
2
3/// A `VALUES` clause: a set of row expressions.
4///
5/// Used as the source for `INSERT` statements and as a query body type that
6/// produces literal rows without reading from a table.
7///
8/// # Examples
9///
10/// ```ignore
11/// use toasty_core::stmt::{Values, Expr};
12///
13/// let values = Values::new(vec![Expr::null()]);
14/// assert_eq!(values.rows.len(), 1);
15/// assert!(!values.is_empty());
16/// ```
17#[derive(Debug, Default, Clone, PartialEq)]
18pub struct Values {
19 /// The row expressions. Each element is one row to insert or return.
20 pub rows: Vec<Expr>,
21}
22
23impl Values {
24 /// Creates a `Values` from a vector of row expressions.
25 pub fn new(rows: Vec<Expr>) -> Self {
26 Self { rows }
27 }
28
29 /// Returns `true` if there are no rows.
30 pub fn is_empty(&self) -> bool {
31 self.rows.is_empty()
32 }
33
34 /// Returns `true` if all rows are constant expressions.
35 pub fn is_const(&self) -> bool {
36 self.rows.iter().all(|row| row.is_const())
37 }
38}
39
40impl From<Values> for ExprSet {
41 fn from(value: Values) -> Self {
42 Self::Values(value)
43 }
44}
45
46impl From<Values> for Query {
47 fn from(value: Values) -> Self {
48 Self::builder(value).build()
49 }
50}
51
52impl From<Expr> for Values {
53 fn from(value: Expr) -> Self {
54 Self { rows: vec![value] }
55 }
56}