surrealpatch/core/src/sql/query.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

use crate::sql::fmt::Pretty;
use crate::sql::statements::{DefineStatement, RemoveStatement};
use crate::sql::{Statement, Statements};
use derive::Store;
use revision::revisioned;
2020-06-29 15:36:01 +00:00
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use std::fmt::{self, Display, Formatter};
use std::ops::Deref;
2020-06-29 15:36:01 +00:00
use std::str;
2023-08-18 22:51:56 +00:00
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Query";
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[revisioned(revision = 1)]
2023-08-18 22:51:56 +00:00
#[serde(rename = "$surrealdb::private::sql::Query")]
2024-01-09 15:34:52 +00:00
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Query(pub Statements);
2020-06-29 15:36:01 +00:00
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<Statement> for Query {
fn from(s: Statement) -> Self {
Query(Statements(vec![s]))
}
}
impl Deref for Query {
type Target = Vec<Statement>;
fn deref(&self) -> &Self::Target {
&self.0 .0
2020-06-29 15:36:01 +00:00
}
}
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)
2020-06-29 15:36:01 +00:00
}
}