From 007d27768d87d6e3ff5a0cacd29cacce8e7ffdc2 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Thu, 27 May 2021 02:31:47 +0800 Subject: [PATCH] Refactor --- src/query/select.rs | 73 ++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/src/query/select.rs b/src/query/select.rs index f140ad7c..54e25e77 100644 --- a/src/query/select.rs +++ b/src/query/select.rs @@ -91,32 +91,38 @@ where } } +macro_rules! select_query { + () => { + /// Get a mutable ref to the query builder + pub fn query(&mut self) -> &mut SelectStatement { + &mut self.query + } + + /// Get an immutable ref to the query builder + pub fn as_query(&self) -> &SelectStatement { + &self.query + } + + /// Take ownership of the query builder + pub fn into_query(self) -> SelectStatement { + self.query + } + + /// Build the query as [`Statement`] + pub fn build(&self, builder: B) -> Statement + where + B: QueryBuilder, + { + self.as_query().build(builder).into() + } + }; +} + impl Select where E: EntityTrait, { - /// Get a mutable ref to the query builder - pub fn query(&mut self) -> &mut SelectStatement { - &mut self.query - } - - /// Get an immutable ref to the query builder - pub fn as_query(&self) -> &SelectStatement { - &self.query - } - - /// Take ownership of the query builder - pub fn into_query(self) -> SelectStatement { - self.query - } - - /// Build the query as [`Statement`] - pub fn build(&self, builder: B) -> Statement - where - B: QueryBuilder, - { - self.as_query().build(builder).into() - } + select_query!(); } impl SelectTwo @@ -124,26 +130,5 @@ where E: EntityTrait, F: EntityTrait, { - /// Get a mutable ref to the query builder - pub fn query(&mut self) -> &mut SelectStatement { - &mut self.query - } - - /// Get an immutable ref to the query builder - pub fn as_query(&self) -> &SelectStatement { - &self.query - } - - /// Take ownership of the query builder - pub fn into_query(self) -> SelectStatement { - self.query - } - - /// Build the query as [`Statement`] - pub fn build(&self, builder: B) -> Statement - where - B: QueryBuilder, - { - self.as_query().build(builder).into() - } + select_query!(); }