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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
//! PostgreSQL specific expression methods use super::operators::*; use expression::{AsExpression, Expression}; use sql_types::{Array, Nullable, Text}; /// PostgreSQL specific methods which are present on all expressions. pub trait PgExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `IS NOT DISTINCT FROM` expression. /// /// This behaves identically to the `=` operator, except that `NULL` is /// treated as a normal value. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # use schema::users::dsl::*; /// # let connection = establish_connection(); /// let distinct = users.select(id).filter(name.is_distinct_from("Sean")); /// let not_distinct = users.select(id).filter(name.is_not_distinct_from("Sean")); /// assert_eq!(Ok(2), distinct.first(&connection)); /// assert_eq!(Ok(1), not_distinct.first(&connection)); /// # } /// ``` fn is_not_distinct_from<T>(self, other: T) -> IsNotDistinctFrom<Self, T::Expression> where T: AsExpression<Self::SqlType>, { IsNotDistinctFrom::new(self, other.as_expression()) } /// Creates a PostgreSQL `IS DISTINCT FROM` expression. /// /// This behaves identically to the `!=` operator, except that `NULL` is /// treated as a normal value. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # use schema::users::dsl::*; /// # let connection = establish_connection(); /// let distinct = users.select(id).filter(name.is_distinct_from("Sean")); /// let not_distinct = users.select(id).filter(name.is_not_distinct_from("Sean")); /// assert_eq!(Ok(2), distinct.first(&connection)); /// assert_eq!(Ok(1), not_distinct.first(&connection)); /// # } /// ``` fn is_distinct_from<T>(self, other: T) -> IsDistinctFrom<Self, T::Expression> where T: AsExpression<Self::SqlType>, { IsDistinctFrom::new(self, other.as_expression()) } } impl<T: Expression> PgExpressionMethods for T {} use super::date_and_time::{AtTimeZone, DateTimeLike}; use sql_types::VarChar; /// PostgreSQL specific methods present on timestamp expressions. pub trait PgTimestampExpressionMethods: Expression + Sized { /// Creates a PostgreSQL "AT TIME ZONE" expression. /// /// When this is called on a `TIMESTAMP WITHOUT TIME ZONE` column, /// the value will be treated as if were in the given time zone, /// and then converted to UTC. /// /// When this is called on a `TIMESTAMP WITH TIME ZONE` column, /// the value will be converted to the given time zone, /// and then have its time zone information removed. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # #[cfg(feature = "chrono")] /// # extern crate chrono; /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # timestamps (timestamp) { /// # timestamp -> Timestamp, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(all(feature = "postgres", feature = "chrono"))] /// # fn run_test() -> QueryResult<()> { /// # use timestamps::dsl::*; /// # use chrono::*; /// # let connection = establish_connection(); /// # connection.execute("CREATE TABLE timestamps (\"timestamp\" /// # timestamp NOT NULL)")?; /// let christmas_morning = NaiveDate::from_ymd(2017, 12, 25) /// .and_hms(8, 0, 0); /// diesel::insert_into(timestamps) /// .values(timestamp.eq(christmas_morning)) /// .execute(&connection)?; /// /// let utc_time = timestamps /// .select(timestamp.at_time_zone("UTC")) /// .first(&connection)?; /// assert_eq!(christmas_morning, utc_time); /// /// let eastern_time = timestamps /// .select(timestamp.at_time_zone("EST")) /// .first(&connection)?; /// let five_hours_later = christmas_morning + Duration::hours(5); /// assert_eq!(five_hours_later, eastern_time); /// # Ok(()) /// # } /// # /// # #[cfg(not(all(feature = "postgres", feature = "chrono")))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn at_time_zone<T>(self, timezone: T) -> AtTimeZone<Self, T::Expression> where T: AsExpression<VarChar>, { AtTimeZone::new(self, timezone.as_expression()) } } impl<T: Expression> PgTimestampExpressionMethods for T where T::SqlType: DateTimeLike {} /// PostgreSQL specific methods present on array expressions. pub trait PgArrayExpressionMethods<ST>: Expression<SqlType = Array<ST>> + Sized { /// Creates a PostgreSQL `&&` expression. /// /// This operator returns whether two arrays have common elements. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array<VarChar>, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = establish_connection(); /// # conn.execute("DROP TABLE IF EXISTS posts").unwrap(); /// # conn.execute("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)").unwrap(); /// # /// diesel::insert_into(posts) /// .values(&vec![ /// tags.eq(vec!["cool", "awesome"]), /// tags.eq(vec!["awesome", "great"]), /// tags.eq(vec!["cool", "great"]), /// ]) /// .execute(&conn)?; /// /// let data = posts.select(id) /// .filter(tags.overlaps_with(vec!["horrid", "cool"])) /// .load::<i32>(&conn)?; /// assert_eq!(vec![1, 3], data); /// /// let data = posts.select(id) /// .filter(tags.overlaps_with(vec!["cool", "great"])) /// .load::<i32>(&conn)?; /// assert_eq!(vec![1, 2, 3], data); /// /// let data = posts.select(id) /// .filter(tags.overlaps_with(vec!["horrid"])) /// .load::<i32>(&conn)?; /// assert!(data.is_empty()); /// # Ok(()) /// # } /// ``` fn overlaps_with<T>(self, other: T) -> OverlapsWith<Self, T::Expression> where T: AsExpression<Self::SqlType>, { OverlapsWith::new(self, other.as_expression()) } /// Creates a PostgreSQL `@>` expression. /// /// This operator returns whether an array contains another array. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array<VarChar>, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = establish_connection(); /// # conn.execute("DROP TABLE IF EXISTS posts").unwrap(); /// # conn.execute("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)").unwrap(); /// # /// diesel::insert_into(posts) /// .values(tags.eq(vec!["cool", "awesome"])) /// .execute(&conn)?; /// /// let cool_posts = posts.select(id) /// .filter(tags.contains(vec!["cool"])) /// .load::<i32>(&conn)?; /// assert_eq!(vec![1], cool_posts); /// /// let amazing_posts = posts.select(id) /// .filter(tags.contains(vec!["cool", "amazing"])) /// .load::<i32>(&conn)?; /// assert!(amazing_posts.is_empty()); /// # Ok(()) /// # } /// ``` fn contains<T>(self, other: T) -> Contains<Self, T::Expression> where T: AsExpression<Self::SqlType>, { Contains::new(self, other.as_expression()) } /// Creates a PostgreSQL `<@` expression. /// /// This operator returns whether an array is contained by another array. /// `foo.contains(bar)` is the same as `bar.is_contained_by(foo)` /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array<VarChar>, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = establish_connection(); /// # conn.execute("DROP TABLE IF EXISTS posts").unwrap(); /// # conn.execute("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)").unwrap(); /// # /// diesel::insert_into(posts) /// .values(tags.eq(vec!["cool", "awesome"])) /// .execute(&conn)?; /// /// let data = posts.select(id) /// .filter(tags.is_contained_by(vec!["cool", "awesome", "amazing"])) /// .load::<i32>(&conn)?; /// assert_eq!(vec![1], data); /// /// let data = posts.select(id) /// .filter(tags.is_contained_by(vec!["cool"])) /// .load::<i32>(&conn)?; /// assert!(data.is_empty()); /// # Ok(()) /// # } /// ``` fn is_contained_by<T>(self, other: T) -> IsContainedBy<Self, T::Expression> where T: AsExpression<Self::SqlType>, { IsContainedBy::new(self, other.as_expression()) } } impl<T, ST> PgArrayExpressionMethods<ST> for T where T: Expression<SqlType = Array<ST>> {} use expression::operators::{Asc, Desc}; /// PostgreSQL expression methods related to sorting. /// /// This trait is only implemented for `Asc` and `Desc`. Although `.asc` is /// implicit if no order is given, you will need to call `.asc()` explicitly in /// order to call these methods. pub trait PgSortExpressionMethods: Sized { /// Specify that nulls should come before other values in this ordering. /// /// Normally, nulls come last when sorting in ascending order and first /// when sorting in descending order. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # nullable_numbers (nullable_number) { /// # nullable_number -> Nullable<Integer>, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::nullable_numbers::dsl::*; /// # let connection = connection_no_data(); /// # connection.execute("CREATE TABLE nullable_numbers (nullable_number INTEGER)")?; /// diesel::insert_into(nullable_numbers) /// .values(&vec![ /// nullable_number.eq(None), /// nullable_number.eq(Some(1)), /// nullable_number.eq(Some(2)), /// ]) /// .execute(&connection)?; /// /// let asc_default_nulls = nullable_numbers.select(nullable_number) /// .order(nullable_number.asc()) /// .load(&connection)?; /// assert_eq!(vec![Some(1), Some(2), None], asc_default_nulls); /// /// let asc_nulls_first = nullable_numbers.select(nullable_number) /// .order(nullable_number.asc().nulls_first()) /// .load(&connection)?; /// assert_eq!(vec![None, Some(1), Some(2)], asc_nulls_first); /// # Ok(()) /// # } /// ``` fn nulls_first(self) -> NullsFirst<Self> { NullsFirst::new(self) } /// Specify that nulls should come after other values in this ordering. /// /// Normally, nulls come last when sorting in ascending order and first /// when sorting in descending order. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # nullable_numbers (nullable_number) { /// # nullable_number -> Nullable<Integer>, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::nullable_numbers::dsl::*; /// # let connection = connection_no_data(); /// # connection.execute("CREATE TABLE nullable_numbers (nullable_number INTEGER)")?; /// diesel::insert_into(nullable_numbers) /// .values(&vec![ /// nullable_number.eq(None), /// nullable_number.eq(Some(1)), /// nullable_number.eq(Some(2)), /// ]) /// .execute(&connection)?; /// /// let desc_default_nulls = nullable_numbers.select(nullable_number) /// .order(nullable_number.desc()) /// .load(&connection)?; /// assert_eq!(vec![None, Some(2), Some(1)], desc_default_nulls); /// /// let desc_nulls_last = nullable_numbers.select(nullable_number) /// .order(nullable_number.desc().nulls_last()) /// .load(&connection)?; /// assert_eq!(vec![Some(2), Some(1), None], desc_nulls_last); /// # Ok(()) /// # } /// ``` fn nulls_last(self) -> NullsLast<Self> { NullsLast::new(self) } } impl<T> PgSortExpressionMethods for Asc<T> {} impl<T> PgSortExpressionMethods for Desc<T> {} /// PostgreSQL specific methods present on text expressions. pub trait PgTextExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `ILIKE` expression /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::animals::dsl::*; /// # let connection = establish_connection(); /// let starts_with_s = animals /// .select(species) /// .filter(name.ilike("s%").or(species.ilike("s%"))) /// .get_results::<String>(&connection)?; /// assert_eq!(vec!["spider"], starts_with_s); /// # Ok(()) /// # } /// ``` fn ilike<T: AsExpression<Text>>(self, other: T) -> ILike<Self, T::Expression> { ILike::new(self.as_expression(), other.as_expression()) } /// Creates a PostgreSQL `NOT ILIKE` expression /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::animals::dsl::*; /// # let connection = establish_connection(); /// let doesnt_start_with_s = animals /// .select(species) /// .filter(name.not_ilike("s%").and(species.not_ilike("s%"))) /// .get_results::<String>(&connection)?; /// assert_eq!(vec!["dog"], doesnt_start_with_s); /// # Ok(()) /// # } /// ``` fn not_ilike<T: AsExpression<Text>>(self, other: T) -> NotILike<Self, T::Expression> { NotILike::new(self.as_expression(), other.as_expression()) } } #[doc(hidden)] /// Marker trait used to implement `PgTextExpressionMethods` on the appropriate /// types. Once coherence takes associated types into account, we can remove /// this trait. pub trait TextOrNullableText {} impl TextOrNullableText for Text {} impl TextOrNullableText for Nullable<Text> {} impl<T> PgTextExpressionMethods for T where T: Expression, T::SqlType: TextOrNullableText, { }