surrealdb_core/sql/
query.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::sql::fmt::Pretty;
use crate::sql::function::Function;
use crate::sql::model::Model;
use crate::sql::statements::CreateStatement;
use crate::sql::statements::DeleteStatement;
use crate::sql::statements::InsertStatement;
use crate::sql::statements::KillStatement;
use crate::sql::statements::LiveStatement;
use crate::sql::statements::RelateStatement;
use crate::sql::statements::SelectStatement;
use crate::sql::statements::UpdateStatement;
use crate::sql::statements::UpsertStatement;
use crate::sql::statements::{DefineStatement, RemoveStatement};
use crate::sql::{Statement, Statements};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use std::fmt::{self, Display, Formatter};
use std::ops::{Deref, DerefMut};
use std::str;

pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Query";

#[revisioned(revision = 1)]
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[serde(rename = "$surrealdb::private::sql::Query")]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct Query(pub Statements);

impl From<DefineStatement> for Query {
	fn from(s: DefineStatement) -> Self {
		Query(Statements(vec![Statement::Define(s)]))
	}
}

impl From<RemoveStatement> for Query {
	fn from(s: RemoveStatement) -> Self {
		Query(Statements(vec![Statement::Remove(s)]))
	}
}

impl From<SelectStatement> for Query {
	fn from(s: SelectStatement) -> Self {
		Query(Statements(vec![Statement::Select(s)]))
	}
}

impl From<CreateStatement> for Query {
	fn from(s: CreateStatement) -> Self {
		Query(Statements(vec![Statement::Create(s)]))
	}
}

impl From<UpsertStatement> for Query {
	fn from(s: UpsertStatement) -> Self {
		Query(Statements(vec![Statement::Upsert(s)]))
	}
}

impl From<UpdateStatement> for Query {
	fn from(s: UpdateStatement) -> Self {
		Query(Statements(vec![Statement::Update(s)]))
	}
}

impl From<RelateStatement> for Query {
	fn from(s: RelateStatement) -> Self {
		Query(Statements(vec![Statement::Relate(s)]))
	}
}

impl From<DeleteStatement> for Query {
	fn from(s: DeleteStatement) -> Self {
		Query(Statements(vec![Statement::Delete(s)]))
	}
}

impl From<InsertStatement> for Query {
	fn from(s: InsertStatement) -> Self {
		Query(Statements(vec![Statement::Insert(s)]))
	}
}

impl From<LiveStatement> for Query {
	fn from(s: LiveStatement) -> Self {
		Query(Statements(vec![Statement::Live(s)]))
	}
}

impl From<KillStatement> for Query {
	fn from(s: KillStatement) -> Self {
		Query(Statements(vec![Statement::Kill(s)]))
	}
}

impl From<Function> for Query {
	fn from(f: Function) -> Self {
		Query(Statements(vec![Statement::Value(f.into())]))
	}
}

impl From<Model> for Query {
	fn from(m: Model) -> Self {
		Query(Statements(vec![Statement::Value(m.into())]))
	}
}

impl From<Statement> for Query {
	fn from(s: Statement) -> Self {
		Query(Statements(vec![s]))
	}
}

impl From<Vec<Statement>> for Query {
	fn from(s: Vec<Statement>) -> Self {
		Query(Statements(s))
	}
}

impl Deref for Query {
	type Target = Vec<Statement>;
	fn deref(&self) -> &Self::Target {
		&self.0 .0
	}
}

impl DerefMut for Query {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.0 .0
	}
}

impl IntoIterator for Query {
	type Item = Statement;
	type IntoIter = std::vec::IntoIter<Self::Item>;
	fn into_iter(self) -> Self::IntoIter {
		self.0.into_iter()
	}
}

impl Display for Query {
	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
		write!(Pretty::from(f), "{}", &self.0)
	}
}