From f7c9715f40f18dc512ba68b2f60fb57e2a7484f7 Mon Sep 17 00:00:00 2001 From: Arpan Abhishek Date: Fri, 8 Apr 2022 15:52:42 +0530 Subject: [PATCH] add `first` method to select statement and corresponding tests --- src/query/select.rs | 30 ++++++++++++++++++++++++++++++ tests/mysql/query.rs | 12 ++++++++++++ tests/postgres/query.rs | 12 ++++++++++++ tests/sqlite/query.rs | 12 ++++++++++++ 4 files changed, 66 insertions(+) diff --git a/src/query/select.rs b/src/query/select.rs index 5a1709143..bbc4f2956 100644 --- a/src/query/select.rs +++ b/src/query/select.rs @@ -1709,6 +1709,36 @@ impl SelectStatement { self } + /// First rows to return. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let query = Query::select() + /// .column(Glyph::Aspect) + /// .from(Glyph::Table) + /// .first(10) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(MysqlQueryBuilder), + /// r#"SELECT `aspect` FROM `glyph` LIMIT 10"# + /// ); + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"SELECT "aspect" FROM "glyph" LIMIT 10"# + /// ); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"SELECT "aspect" FROM "glyph" LIMIT 10"# + /// ); + /// ``` + pub fn first(&mut self, first: u64) -> &mut Self { + Self::reset_offset(self).limit(first) + } + /// Row locking (if supported). /// /// # Examples diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index 883435280..97707e9a7 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -14,6 +14,18 @@ fn select_1() { ); } +#[test] +fn select_1a() { + assert_eq!( + Query::select() + .columns(vec![Char::Character, Char::SizeW, Char::SizeH]) + .from(Char::Table) + .first(10) + .to_string(MysqlQueryBuilder), + "SELECT `character`, `size_w`, `size_h` FROM `character` LIMIT 10" + ); +} + #[test] fn select_2() { assert_eq!( diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index f6993c4ac..d1b80f0d4 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -14,6 +14,18 @@ fn select_1() { ); } +#[test] +fn select_1a() { + assert_eq!( + Query::select() + .columns(vec![Char::Character, Char::SizeW, Char::SizeH]) + .from(Char::Table) + .first(10) + .to_string(PostgresQueryBuilder), + r#"SELECT "character", "size_w", "size_h" FROM "character" LIMIT 10"# + ); +} + #[test] fn select_2() { assert_eq!( diff --git a/tests/sqlite/query.rs b/tests/sqlite/query.rs index a7ad4c34e..91de2f9be 100644 --- a/tests/sqlite/query.rs +++ b/tests/sqlite/query.rs @@ -14,6 +14,18 @@ fn select_1() { ); } +#[test] +fn select_1a() { + assert_eq!( + Query::select() + .columns(vec![Char::Character, Char::SizeW, Char::SizeH]) + .from(Char::Table) + .first(10) + .to_string(SqliteQueryBuilder), + r#"SELECT "character", "size_w", "size_h" FROM "character" LIMIT 10"# + ); +} + #[test] fn select_2() { assert_eq!(