2022-03-18 19:50:47 +00:00
|
|
|
use crate::sql::cond::Cond;
|
2022-04-01 09:15:52 +00:00
|
|
|
use crate::sql::data::Data;
|
2022-03-23 14:01:28 +00:00
|
|
|
use crate::sql::fetch::Fetchs;
|
2022-03-25 18:43:22 +00:00
|
|
|
use crate::sql::field::Fields;
|
2022-02-26 23:30:19 +00:00
|
|
|
use crate::sql::group::Groups;
|
|
|
|
use crate::sql::limit::Limit;
|
|
|
|
use crate::sql::order::Orders;
|
2022-04-01 09:15:52 +00:00
|
|
|
use crate::sql::output::Output;
|
2022-02-26 23:30:19 +00:00
|
|
|
use crate::sql::split::Splits;
|
|
|
|
use crate::sql::start::Start;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::sql::statements::create::CreateStatement;
|
|
|
|
use crate::sql::statements::delete::DeleteStatement;
|
|
|
|
use crate::sql::statements::insert::InsertStatement;
|
|
|
|
use crate::sql::statements::relate::RelateStatement;
|
|
|
|
use crate::sql::statements::select::SelectStatement;
|
|
|
|
use crate::sql::statements::update::UpdateStatement;
|
2022-02-26 23:30:19 +00:00
|
|
|
use crate::sql::version::Version;
|
2022-02-06 01:14:56 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2022-02-26 23:30:19 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-05-13 20:46:56 +00:00
|
|
|
pub enum Statement<'a> {
|
|
|
|
Select(&'a SelectStatement),
|
|
|
|
Create(&'a CreateStatement),
|
|
|
|
Update(&'a UpdateStatement),
|
|
|
|
Relate(&'a RelateStatement),
|
|
|
|
Delete(&'a DeleteStatement),
|
|
|
|
Insert(&'a InsertStatement),
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> From<&'a SelectStatement> for Statement<'a> {
|
|
|
|
fn from(v: &'a SelectStatement) -> Self {
|
2022-02-06 01:14:56 +00:00
|
|
|
Statement::Select(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> From<&'a CreateStatement> for Statement<'a> {
|
|
|
|
fn from(v: &'a CreateStatement) -> Self {
|
2022-02-06 01:14:56 +00:00
|
|
|
Statement::Create(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> From<&'a UpdateStatement> for Statement<'a> {
|
|
|
|
fn from(v: &'a UpdateStatement) -> Self {
|
2022-02-06 01:14:56 +00:00
|
|
|
Statement::Update(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> From<&'a RelateStatement> for Statement<'a> {
|
|
|
|
fn from(v: &'a RelateStatement) -> Self {
|
2022-02-06 01:14:56 +00:00
|
|
|
Statement::Relate(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> From<&'a DeleteStatement> for Statement<'a> {
|
|
|
|
fn from(v: &'a DeleteStatement) -> Self {
|
2022-02-06 01:14:56 +00:00
|
|
|
Statement::Delete(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> From<&'a InsertStatement> for Statement<'a> {
|
|
|
|
fn from(v: &'a InsertStatement) -> Self {
|
2022-02-06 01:14:56 +00:00
|
|
|
Statement::Insert(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> fmt::Display for Statement<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Statement::Select(v) => write!(f, "{}", v),
|
|
|
|
Statement::Create(v) => write!(f, "{}", v),
|
|
|
|
Statement::Update(v) => write!(f, "{}", v),
|
|
|
|
Statement::Relate(v) => write!(f, "{}", v),
|
|
|
|
Statement::Delete(v) => write!(f, "{}", v),
|
|
|
|
Statement::Insert(v) => write!(f, "{}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 23:30:19 +00:00
|
|
|
|
2022-05-13 20:46:56 +00:00
|
|
|
impl<'a> Statement<'a> {
|
2022-04-05 20:41:49 +00:00
|
|
|
// Check the type of statement
|
|
|
|
#[inline]
|
|
|
|
pub fn is_select(&self) -> bool {
|
|
|
|
matches!(self, Statement::Select(_))
|
|
|
|
}
|
2022-03-25 18:43:22 +00:00
|
|
|
// Returns any query fields if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn expr(&self) -> Option<&Fields> {
|
2022-03-25 18:43:22 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => Some(&v.expr),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 09:15:52 +00:00
|
|
|
// Returns any SET clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn data(&self) -> Option<&Data> {
|
2022-04-01 09:15:52 +00:00
|
|
|
match self {
|
|
|
|
Statement::Create(v) => v.data.as_ref(),
|
|
|
|
Statement::Update(v) => v.data.as_ref(),
|
2022-05-30 15:32:26 +00:00
|
|
|
Statement::Relate(v) => v.data.as_ref(),
|
|
|
|
Statement::Insert(v) => v.update.as_ref(),
|
2022-04-01 09:15:52 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns any WHERE clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn conds(&self) -> Option<&Cond> {
|
2022-03-18 19:50:47 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.cond.as_ref(),
|
|
|
|
Statement::Update(v) => v.cond.as_ref(),
|
|
|
|
Statement::Delete(v) => v.cond.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns any SPLIT clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn split(&self) -> Option<&Splits> {
|
2022-02-26 23:30:19 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.split.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns any GROUP clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn group(&self) -> Option<&Groups> {
|
2022-02-26 23:30:19 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.group.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns any ORDER clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn order(&self) -> Option<&Orders> {
|
2022-02-26 23:30:19 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.order.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 14:01:28 +00:00
|
|
|
// Returns any FETCH clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn fetch(&self) -> Option<&Fetchs> {
|
2022-03-23 14:01:28 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.fetch.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 23:30:19 +00:00
|
|
|
// Returns any START clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn start(&self) -> Option<&Start> {
|
2022-02-26 23:30:19 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.start.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns any LIMIT clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn limit(&self) -> Option<&Limit> {
|
2022-02-26 23:30:19 +00:00
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.limit.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 15:32:26 +00:00
|
|
|
// Returns any VERSION clause if specified
|
|
|
|
#[inline]
|
|
|
|
pub fn version(&self) -> Option<&Version> {
|
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.version.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 09:15:52 +00:00
|
|
|
// Returns any RETURN clause if specified
|
2022-04-01 10:18:14 +00:00
|
|
|
#[inline]
|
2022-04-05 13:53:27 +00:00
|
|
|
pub fn output(&self) -> Option<&Output> {
|
2022-04-01 09:15:52 +00:00
|
|
|
match self {
|
|
|
|
Statement::Create(v) => v.output.as_ref(),
|
|
|
|
Statement::Update(v) => v.output.as_ref(),
|
|
|
|
Statement::Relate(v) => v.output.as_ref(),
|
|
|
|
Statement::Delete(v) => v.output.as_ref(),
|
|
|
|
Statement::Insert(v) => v.output.as_ref(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 20:46:56 +00:00
|
|
|
// Returns any RETURN clause if specified
|
|
|
|
#[inline]
|
|
|
|
pub fn parallel(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Statement::Select(v) => v.parallel,
|
|
|
|
Statement::Create(v) => v.parallel,
|
|
|
|
Statement::Update(v) => v.parallel,
|
|
|
|
Statement::Relate(v) => v.parallel,
|
|
|
|
Statement::Delete(v) => v.parallel,
|
|
|
|
Statement::Insert(v) => v.parallel,
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 23:30:19 +00:00
|
|
|
}
|