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