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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
//! Helper macros and traits built around //! [tokio-postgres](https://docs.rs/tokio-postgres/0.5.1/tokio_postgres/index.html) to define //! queries with human readable parameters and return values. //! //! # Example //! //! ``` //! # use tokio_postgres::Client; //! # use postgres_query::{query, FromSqlRow, Result}; //! # fn connect() -> Client { unimplemented!() } //! # async fn foo() -> Result<()> { //! // Connect to the database //! let client: Client = connect(/* ... */); //! //! // Construct the query //! let query = query!( //! "SELECT age, name FROM people WHERE age >= $min_age", //! min_age = 18 //! ); //! //! // Define the structure of the data returned from the query //! #[derive(FromSqlRow)] //! struct Person { //! age: i32, //! name: String, //! } //! //! // Execute the query //! let people: Vec<Person> = query.fetch(&client).await?; //! //! for person in people { //! println!("{} is {} years young", person.name, person.age); //! } //! # Ok(()) //! # } //! ``` //! //! # Queries //! //! The preferred way of constructing a new [`Query`] is through the [`query!`] macro. It uses a //! syntax similar to the `format!(...)` family of macros from the standard library. The first //! parameter is the SQL query and is always given as a string literal (this might be relaxed in the //! future). This string literal may contain parameter bindings on the form `$ident` where `ident` //! is any valid Rust identifier (`$abc`, `$value_123`, etc.). //! //! ``` //! # use postgres_query::query; //! let age = 42; //! let insert_person = query!( //! "INSERT INTO people VALUES ($age, $name)", //! name = "John Wick", // Binds "$name" to "John Wick" //! age, // Binds "$age" to the value of `age` //! ); //! ``` //! //! During compilation the query is converted into the format expected by PostgreSQL: parameter //! bindings are converted to using numbers ($1, $2, etc.) and the actual parameter values are put //! into a 1-indexed array. The code snippet above would be expanded into the following: //! //! ``` //! # use postgres_query::*; //! let age = 42; //! let insert_person = Query::new_static( //! "INSERT INTO people VALUES ($1, $2)", //! vec![&age, &"John Wick"], //! ); //! ``` //! //! //! ## Dynamic Queries //! //! If necessary, queries may be constructed from `&str`s at runtime instead of the usual //! compile-time string literals expected by the `query!` macro. This is achieved by using the //! [`query_dyn!`] macro instead. In addition to dynamic queries, parameter bindings may also be //! dynamically: //! //! ``` //! # use postgres_query::*; //! let mut sql = "SELECT * FROM people WHERE name = $name".to_string(); //! let mut bindings = Vec::new(); //! //! // Add a filter at runtime //! sql += " AND age > $min_age"; //! bindings.push(("min_age", &42 as Parameter)); //! //! let query: Result<Query> = query_dyn!( //! &sql, //! name = "John", //! ..bindings, //! ); //! ``` //! //! Using dynamic queries does introduce some errors that cannot be caught at runtime: such as some //! parameters in the query not having a matching binding. Because of this the value returned by the //! [`query_dyn!`] macro is not a `Query` but a `Result<Query>` which carries an error you must //! handle: //! //! ``` //! # use postgres_query::*; //! let mut sql = "SELECT * FROM people".to_string(); //! sql += " WHERE age <= $max_age AND name = $name"; //! //! let query: Result<Query> = query_dyn!( //! &sql, //! name = "John", //! // Forgot to bind the parameter `max_age`. //! // Will result in an error. //! ); //! //! assert!(query.is_err()); //! ``` //! //! //! # Data Extraction //! //! In addition to helping you define new queries this crate provides the [`FromSqlRow`] trait which //! makes it easy to extract typed values from the resulting rows. The easiest way to implement this //! trait for new `struct`s is to use the included [`derive(FromSqlRow)`] macro. //! //! - If used on a tuple struct, values will be extracted from the corresponding columns based on //! their position in the tuple. //! - If used on a stuct with named fields, values will be extracted from the column with the same //! name as the field. //! //! ``` //! # use postgres_query::*; //! #[derive(FromSqlRow)] //! struct TupleData(i32, String); //! //! #[derive(FromSqlRow)] //! struct NamedData { //! age: i32, //! name: String, //! }; //! ``` //! //! ## Multi-mapping //! //! If you query the same table multiple times it gets tedious to have to redefine structs with the //! same fields over and over. Preferably we would like to reuse the same definition multiple times. //! We can do this be utilizing "multi-mapping". //! //! //! ### Partitions //! //! Multi-mapping works by splitting the columns of rows returned by a query into multiple //! partitions (or slices). For example, if we had the query `SELECT books.*, authors.* FROM ...`, //! we would like to extract the data into two structs: `Book` and `Author`. We accomplish this by //! looking at the columns returned by the database and splitting them into partitions: //! //! ```text //! Columns: id, title, release_date, genre, id, name, birthyear //! Partitions: +------------Book-------------+ +------Author-----+ //! ``` //! //! //! ### Partitioning schemes //! //! There are two supported ways to partition a row: either we specify the number of columns //! required to populate each struct (in the example above: 4 columns for Book and 3 for author), or //! we split on the name of a column. The former should generally only be used when you know the //! number of columns isn't going to change. The latter is less prone to break provided you choose //! an appropriate column to split on (a good candidate is usually `id` as almost all tables have //! this as their first //! column). //! //! You choose which partitioning scheme you want to use by using the provided //! [attributes](./derive.FromSqlRow.html#attributes). In order to accomplish the partitioning in //! the example above we could split on the column name `id`: //! //! ``` //! # use postgres_query::FromSqlRow; //! #[derive(FromSqlRow)] //! struct Book { //! id: i32, //! title: String, //! release_date: String, //! genre: String, //! } //! //! #[derive(FromSqlRow)] //! struct Author { //! id: i32, //! name: String, //! birthyear: i32, //! } //! //! #[derive(FromSqlRow)] //! #[row(split)] //! struct BookAuthor { //! #[row(flatten, split = "id")] //! book: Book, //! #[row(flatten, split = "id")] //! author: Author, //! } //! ``` //! //! Alternatively, we can make `Author` a part of the `Book` struct: //! //! ``` //! # use postgres_query::FromSqlRow; //! #[derive(FromSqlRow)] //! struct Author { //! id: i32, //! name: String, //! birthyear: i32, //! } //! //! #[derive(FromSqlRow)] //! #[row(split)] //! struct Book { //! #[row(split = "id")] //! id: i32, //! title: String, //! release_date: String, //! genre: String, //! //! #[row(flatten, split = "id")] //! author: Author, //! } //! ``` //! //! ### Many-to-one Relationships //! //! In the previous examples we had a `Book` that contained an `Author`. This is what is called a //! many-to-one relationship, since one book only has one author, but many books may share the same //! author (or so we assume anyway). What if you instead had `Author` an author that contained many //! `Book`s? We know that one author may write many books, so that is a one-to-many relationship. We //! can write an extractor for that case as well: //! //! ``` //! # use postgres_query::*; //! # use tokio_postgres::Client; //! # async fn foo() -> Result<()> { //! # let client: Client = unimplemented!(); //! #[derive(FromSqlRow)] //! #[row(split, group)] //! struct Author { //! #[row(split = "id", key)] //! id: i32, //! name: String, //! birthyear: i32, //! //! #[row(split = "id", merge)] //! books: Vec<Book>, //! } //! //! #[derive(FromSqlRow)] //! struct Book { //! id: i32, //! title: String, //! release_date: String, //! genre: String, //! } //! //! let authors: Vec<Author> = query!( //! "SELECT authors.*, books.* //! INNER JOIN books ON books.author = authors.id //! GROUP BY authors.id" //! ) //! .fetch(&client) //! .await?; //! # Ok(()) //! # } //! ``` //! //! See the section on [attributes](./derive.FromSqlRow.html#attributes) for a more advanced //! in-depth explanation of multi-mapping. //! //! //! # Caching queries //! //! From time to time you probably want to execute the same query multiple times, but with different //! parameters. In times like these we can decrease the load on the database by preparing our //! queries before executing them. By wrapping a client in a [`Caching`] struct this behaviour is //! automatically provided for all queries that originate from this crate: //! //! ``` //! # use tokio_postgres::Client; //! # use postgres_query::{query, Result, Caching}; //! # fn connect() -> Client { unimplemented!() } //! # async fn foo() -> Result<()> { //! // Connect to the database //! let client: Client = connect(/* ... */); //! //! // Wrap the client in a query cache //! let cached_client = Caching::new(client); //! //! for age in 0..100i32 { //! let query = query!("SELECT name, weight FROM people WHERE age = $age", age); //! //! // The query is prepared and cached the first time it's executed. //! // All subsequent fetches will use the cached Statement. //! let people: Vec<(String, i32)> = query.fetch(&cached_client).await?; //! //! /* Do something with people */ //! } //! # Ok(()) //! # } //! ``` //! //! [`Query`]: struct.Query.html //! [`query!`]: macro.query.html //! [`query_dyn!`]: macro.query_dyn.html //! [`FromSqlRow`]: extract/trait.FromSqlRow.html //! [`derive(FromSqlRow)`]: derive.FromSqlRow.html //! [`Caching`]: client/struct.Caching.html pub mod client; pub mod execute; pub mod extract; mod error; mod parse; use postgres_types::ToSql; use proc_macro_hack::proc_macro_hack; use std::ops::Deref; pub use crate::client::Caching; pub use crate::error::{Error, Result}; pub use crate::extract::FromSqlRow; /// Extract values from a row. /// /// - If used on a tuple struct, values will be extracted from the corresponding columns based on /// their position in the tuple. /// - If used on a stuct with named fields, values will be extracted from the column with the same /// name as the field. /// /// # Example /// /// ``` /// # use postgres_query::*; /// #[derive(FromSqlRow)] /// struct TupleData(i32, String); /// /// #[derive(FromSqlRow)] /// struct NamedData { /// age: i32, /// name: String, /// }; /// ``` /// /// /// # Attributes /// /// Data extraction can be customized by using the `#[row(...)]` attribute. Attributes can be /// separated into two categories, those which go on the container itself: /// /// - [`#[row(exact)]`](#rowexact) /// - [`#[row(split)]`](#rowsplit) /// - [`#[row(group)]`](#rowgroup) /// - [`#[row(hash)]`](#rowhash) /// /// and those which are placed on the container's fields: /// /// - [`#[row(rename = "...")]`](#rowrename--) /// - [`#[row(flatten)]`](#rowflatten) /// - [`#[row(stride = N)]`](#rowstride--n) /// - [`#[row(split = "...")]`](#rowsplit--) /// - [`#[row(key)]`](#rowkey) /// - [`#[row(merge)]`](#rowmerge) /// /// /// ## Container attributes /// /// These attributes are put on the struct itself. /// /// /// ### `#[row(exact)]` /// /// [Partition](./index.html#multi-mapping) the row according to the number of columns matched by /// each group. /// /// Note that no order is forced upon fields within any group. In the example below, that means that /// even though the `generation` and `origin` fields are flipped relative to the query, the /// extraction will be successful: /// /// ``` /// # use postgres_query::{FromSqlRow, Result, query}; /// # use tokio_postgres::Client; /// # async fn foo() -> Result<()> { /// # let client: Client = unimplemented!(); /// #[derive(FromSqlRow)] /// #[row(exact)] /// struct Family { /// generation: i32, /// origin: String, /// #[row(flatten)] /// parent: Person, /// #[row(flatten)] /// child: Person, /// } /// /// #[derive(FromSqlRow)] /// struct Person { /// id: i32, /// name: String, /// } /// /// let family = query!( /// "SELECT /// 'Germany' as origin, 7 as generation, /// 1 as id, 'Bob' as name, /// 2 as id, 'Ike' as name" /// ) /// .fetch_one::<Family, _>(&client) /// .await?; /// # Ok(()) /// # } /// ``` /// /// ### `#[row(split)]` /// /// [Partition](./index.html#multi-mapping) the row according to the field's [split /// points](extract/fn.split_columns_many.html#split-points). /// /// Split points are introduced by using the [`#[row(split = "...")]`](#rowsplit---1) attribute on /// fields. /// /// ``` /// # use postgres_query::{FromSqlRow, Result, query}; /// # use tokio_postgres::Client; /// # async fn foo() -> Result<()> { /// # let client: Client = unimplemented!(); /// #[derive(FromSqlRow)] /// #[row(split)] /// struct Family { /// generation: i32, /// origin: String, /// #[row(flatten, split = "id")] /// parent: Person, /// #[row(flatten, split = "id")] /// child: Person, /// } /// /// #[derive(FromSqlRow)] /// struct Person { /// id: i32, /// name: String, /// } /// /// let family = query!( /// "SELECT /// 'Germany' as origin, 7 as generation, /// 1 as id, 'Bob' as name, /// 2 as id, 'Ike' as name" /// ) /// .fetch_one::<Family, _>(&client) /// .await?; /// # Ok(()) /// # } /// ``` /// /// /// ### `#[row(group)]` /// /// Enables one-to-many mapping for the container. One-to-many mapping requires that at least one /// field has the `#[row(key)]` attribute and that one other field has the `#[row(merge)]` attribute. /// /// When extracting values from multiple rows, any two **adjacent** rows that are identical on their /// fields marked with `#[row(key)]` will have their fields tagged with `#[row(merge)]` merged. This /// means that in order to get the expected relation back, you may need to include a `GROUP BY` /// statement in your SQL query, hence the name `group`. /// /// ``` /// # use postgres_query::*; /// # use tokio_postgres::Client; /// # async fn foo() -> Result<()> { /// # let client: Client = unimplemented!(); /// #[derive(Debug, FromSqlRow)] /// #[row(group)] /// struct Author { /// #[row(key)] /// name: String, /// /// #[row(merge)] /// books: Vec<Book>, /// } /// /// #[derive(Debug, FromSqlRow)] /// struct Book { /// title: String, /// } /// /// let authors = query!( /// "SELECT 'J.R.R. Tolkien' as name, 'The Fellowship of the Ring' as title /// UNION ALL SELECT 'J.R.R. Tolkien', 'The Two Towers' /// UNION ALL SELECT 'Andrzej Sapkowski', 'The Last Wish' /// UNION ALL SELECT 'J.R.R. Tolkien', 'Return of the King'") /// .fetch::<Author, _>(&client) /// .await?; /// /// assert_eq!(authors[0].name, "J.R.R. Tolkien"); /// assert_eq!(authors[0].books[0].title, "The Fellowship of the Ring"); /// assert_eq!(authors[0].books[1].title, "The Two Towers"); /// /// assert_eq!(authors[1].name, "Andrzej Sapkowski"); /// assert_eq!(authors[1].books[0].title, "The Last Wish"); /// /// assert_eq!(authors[2].name, "J.R.R. Tolkien"); /// assert_eq!(authors[2].books[0].title, "Return of the King"); /// # Ok(()) /// # } /// ``` /// /// /// ### `#[row(hash)]` /// /// Like `#[row(group)]`, but all previous rows are considered when merging. This is accomplished by /// using a `HashMap`, hence the name. This implies that all keys have to implement the `Hash` and /// `Eq` traits: /// /// ``` /// # use postgres_query::*; /// # use tokio_postgres::Client; /// # async fn foo() -> Result<()> { /// # let client: Client = unimplemented!(); /// #[derive(Debug, FromSqlRow)] /// #[row(hash)] /// struct Author { /// #[row(key)] /// name: String, /// /// #[row(merge)] /// books: Vec<Book>, /// } /// /// #[derive(Debug, FromSqlRow)] /// struct Book { /// title: String, /// } /// /// let authors = query!( /// "SELECT 'J.R.R. Tolkien' as name, 'The Fellowship of the Ring' as title /// UNION ALL SELECT 'J.R.R. Tolkien', 'The Two Towers' /// UNION ALL SELECT 'Andrzej Sapkowski', 'The Last Wish' /// UNION ALL SELECT 'J.R.R. Tolkien', 'Return of the King'") /// .fetch::<Author, _>(&client) /// .await?; /// /// assert_eq!(authors[0].name, "J.R.R. Tolkien"); /// assert_eq!(authors[0].books[0].title, "The Fellowship of the Ring"); /// assert_eq!(authors[0].books[1].title, "The Two Towers"); /// assert_eq!(authors[0].books[2].title, "Return of the King"); /// /// assert_eq!(authors[1].name, "Andrzej Sapkowski"); /// assert_eq!(authors[1].books[0].title, "The Last Wish"); /// # Ok(()) /// # } /// ``` /// /// ## Field attributes /// /// These attributes are put on the fields of a container. /// /// /// ### `#[row(rename = "...")]` /// /// Use a name other than that of the field when looking up the name of the column. /// /// ``` /// # use postgres_query::FromSqlRow; /// #[derive(FromSqlRow)] /// struct Person { /// age: i32, /// // matches the column named "first_name" instead of "name" /// #[row(rename = "first_name")] /// name: String, /// } /// ``` /// /// ### `#[row(flatten)]` /// /// Flatten the contents of this field into its container by recursively calling `FromSqlRow` on the /// field's type. This removes one level of nesting: /// /// ``` /// # use postgres_query::{FromSqlRow, query, Result}; /// # use tokio_postgres::Client; /// # async fn foo() -> Result<()> { /// # let client: Client = unimplemented!(); /// #[derive(FromSqlRow)] /// struct Customer { /// id: i32, /// #[row(flatten)] /// info: Person, /// } /// /// #[derive(FromSqlRow)] /// struct Person { /// name: String, /// age: i32 /// } /// /// let customer: Customer = query!("SELECT 14 as id, 'Bob' as name, 47 as age") /// .fetch_one(&client) /// .await?; /// /// assert_eq!(customer.id, 14); /// assert_eq!(customer.info.name, "Bob"); /// assert_eq!(customer.info.age, 47); /// # Ok(()) /// # } /// ``` /// /// ### `#[row(stride = N)]` /// /// Puts this field into a partition with exactly `N` columns. Only available when using the /// `#[row(exact)]` attribute on the container, /// /// ``` /// # use postgres_query::{FromSqlRow, query, Result}; /// # use tokio_postgres::Client; /// # async fn foo() -> Result<()> { /// # let client: Client = unimplemented!(); /// #[derive(Debug, FromSqlRow)] /// struct Person { /// id: i32, /// name: String, /// } /// /// #[derive(Debug, FromSqlRow)] /// #[row(exact)] /// struct Family { /// // Matches first 4 columns /// #[row(flatten, stride = 4)] /// parent: Person, /// // Matches last 3 columns /// #[row(flatten, stride = 3)] /// child: Person, /// } /// /// let family = query!( /// "SELECT /// 11 as generation, /// 1 as id, 'Bob' as name, 42 as age, /// 2 as id, 'Ike' as name, 14 as age" /// ) /// .fetch_one::<Family, _>(&client) /// .await?; /// /// assert_eq!(family.parent.id, 1); /// assert_eq!(family.parent.name, "Bob"); /// assert_eq!(family.child.id, 2); /// assert_eq!(family.child.name, "Ike"); /// # Ok(()) /// # } /// ``` /// /// ### `#[row(split = "...")]` /// /// Introduce an additional [split](extract/fn.split_columns_many.html#split-points) right /// before this field. Requires that the container has the `split` attribute as well. /// /// Intuitively this splits the row in two parts: every field before this attribute matches the /// columns before the split and every field afterwards matches the second remaining columns. /// /// ``` /// # use postgres_query::{FromSqlRow}; /// #[derive(FromSqlRow)] /// #[row(split)] /// struct User { /// // `id` and `name` will only match the columns before `email` /// id: i32, /// name: String, /// #[row(split = "email")] /// // `email`, `address` and `shoe_size` will only /// // match the columns after and including `email` /// email: String, /// address: String, /// shoe_size: i32, /// } /// ``` /// /// Note that the first split always matches first occurence of that column. This can result in some /// subtle bugs: /// /// ``` /// # use postgres_query::{FromSqlRow, query}; /// #[derive(FromSqlRow)] /// #[row(split)] /// struct Family { /// #[row(flatten)] /// parent: Person, /// #[row(flatten, split = "id")] /// child: Person, /// } /// /// #[derive(FromSqlRow)] /// struct Person { /// name: String, /// age: i32 /// } /// /// let query = query!("SELECT parent.*, child.* FROM ..."); /// /// // Imagine the query above results in the following columns: /// // /// // Columns: id, name, id, name /// // Splits: | /// // Partitions: +-parent-+ +-----child------+ /// ``` /// /// The split causes `parent` to match against all columns before the first `id`, ie. an empty /// partition. This would cause an error when executing the query. /// /// A correct split would look like this: /// /// ``` /// # use postgres_query::{FromSqlRow, query}; /// # #[derive(FromSqlRow)] struct Person; /// #[derive(FromSqlRow)] /// #[row(split)] /// struct Family { /// #[row(flatten, split = "id")] /// parent: Person, /// #[row(flatten, split = "id")] /// child: Person, /// } /// ``` /// /// /// ### `#[row(key)]` /// /// Specifies this field to be a `key` field. `key` fields are compared against each other when /// extracting values from multiple rows. Rows are merged if the key fields in each row are /// identical. You may have multiple `key` fields within a single container, but none of them may /// have the `#[row(merge)]` attribute. Multiple `key` fields will be treated as a tuple in /// comparisons. /// /// /// ### `#[row(merge)]` /// /// Specifies this field to be a `merge` field. This requires that the field's type implements the /// [`Merge`] trait. When two rows have been deemed to be equal based on the `key` fields, the /// corresponding `merge` fields in those rows will be merged. You may specify multiple `merge` /// fields within one container, but none of them may have the `#[row(key)]` attribute. /// /// [`Merge`]: extract/trait.Merge.html pub use postgres_query_macro::FromSqlRow; /// Constructs a new query at compile-time. See also `query_dyn!`. /// /// # Usage /// /// This macro expands to an expression with the type `Query`. /// /// The first parameter is the SQL query and is always given as a string literal. This string /// literal may contain parameter bindings on the form `$ident` where `ident` is any valid Rust /// identifier (`$abc`, `$value_123`, etc.). The order of the parameters does not matter. /// /// ``` /// # use postgres_query::query; /// let age = 42; /// let insert_person = query!( /// "INSERT INTO people VALUES ($age, $name)", /// name = "John Wick", // Binds "$name" to "John Wick" /// age, // Binds "$age" to the value of `age` /// ); /// ``` /// /// During compilation the query is converted into the format expected by PostgreSQL: parameter /// bindings are converted to using numbers (`$1`, `$2`, etc.) and the actual parameter values are /// put into a 1-indexed array. The code snippet above would be expanded into the following: /// /// ``` /// # use postgres_query::*; /// let age = 42; /// let insert_person = Query::new_static( /// "INSERT INTO people VALUES ($1, $2)", /// vec![&age, &"John Wick"], /// ); /// ``` #[macro_export] macro_rules! query { ($($tt:tt)*) => { $crate::__query_static!($($tt)*) }; } /// Constructs a new query dynamically at runtime. See also `query!`. /// /// # Usage /// /// This macro expands to an expression with the type `Result<Query>`. /// /// The first parameter is the SQL query and is always given as a `&str`. This string may contain /// parameter bindings on the form `$ident` where `ident` is any valid Rust identifier (`$abc`, /// `$value_123`, etc.). The order of the parameters does not matter. /// /// ``` /// # use postgres_query::{query_dyn, Result}; /// # fn foo() -> Result<()> { /// // We can construct the actual query at runtime /// let mut sql = "INSERT INTO people VALUES".to_owned(); /// sql.push_str("($age, $name)"); /// /// let age = 42; /// /// let insert_person = query_dyn!( /// &sql, /// name = "John Wick", // Binds "$name" to "John Wick" /// age, // Binds "$age" to the value of `age` /// )?; /// # Ok(()) /// # } /// ``` /// /// The query and all the parameters are passed into `Query::parse`, so the above would be expanded /// into: /// /// ``` /// # use postgres_query::Query; /// // We can construct the actual query at runtime /// let mut sql = "INSERT INTO people VALUES".to_string(); /// sql.push_str("($age, $name)"); /// /// let age = 42; /// /// let insert_person = Query::parse( /// &sql, /// &[("name", &"John Wick"), ("age", &age)], /// ); /// ``` /// /// /// ## Dynamic Binding /// /// Optionally, you may also choose to include additional bindings at runtime by using the /// `..bindings` syntax. This is supported for any type that implements `IntoIterator<Item = (&str, /// Parameter)>`, ie. `Vec<(&str, Parameter)>`, `HashMap<&str, Parameter>`, `Option<(&str, /// Parameter)>`, iterators, and so on. /// /// Dynamic bindings may be mixed with static bindings: /// /// ``` /// # use postgres_query::{query_dyn, Parameter, Result}; /// # fn foo() -> Result<()> { /// let mut bindings = Vec::new(); /// /// // We use the `as Parameter` to please the type checker. /// // Alternatively, we could specify the type for bindings: `Vec<(&str, Parameter)>`. /// bindings.push(("age", &42 as Parameter)); /// bindings.push(("name", &"John Wick" as Parameter)); /// /// let sql = "INSERT INTO people VALUES ($age, $name, $height)".to_string(); /// let insert_person = query_dyn!( /// &sql, /// height = 192, /// ..bindings, /// )?; /// # Ok(()) /// # } /// ``` /// /// /// # A larger example /// /// Let's say that we wanted to dynamically add filters to our query: /// /// ``` /// # use postgres_query::{query_dyn, Parameter, Query, Result}; /// # fn foo() -> Result<()> { /// // We have the query we want to execute /// let mut sql = "SELECT * FROM people".to_string(); /// /// // and some filters we got from the user. /// let age_filter: Option<i32> = Some(32); /// let name_filter: Option<&str> = None; /// /// // Then we dynamically build a list of filters and bindings to use: /// let mut filters = Vec::new(); /// let mut bindings = Vec::new(); /// /// // We add the filters as needed. /// if let Some(age) = age_filter.as_ref() { /// filters.push("age > $min_age"); /// bindings.push(("min_age", age as Parameter)); /// } /// /// if let Some(name) = name_filter.as_ref() { /// filters.push("name LIKE $name"); /// bindings.push(("name", name as Parameter)); /// } /// /// // And add them to the query. /// if filters.len() > 0 { /// sql += &format!(" WHERE {}", filters.join(" AND ")); /// } /// /// // Then we can use it as normal. /// let query: Query = query_dyn!(&sql, ..bindings)?; /// # Ok(()) /// # } /// ``` #[macro_export] macro_rules! query_dyn { ($($tt:tt)*) => { $crate::__query_dynamic!($($tt)*) }; } #[proc_macro_hack] #[doc(hidden)] pub use postgres_query_macro::{query_dynamic as __query_dynamic, query_static as __query_static}; /// A shorthand for types that can be treated as SQL parameters. /// /// A common use case for this type alias is when using dynamic bindings and you have to please the /// type checker: /// /// ``` /// # use postgres_query::{Parameter, query_dyn, Result}; /// # fn foo() -> Result<()> { /// let mut bindings = Vec::new(); /// /// // Without the `as Parameter` the compiler assumes the type to be `&i32`. /// bindings.push(("age", &32 as Parameter)); /// /// // Which would cause problems when adding something that is not an integer. /// bindings.push(("name", &"John" as Parameter)); /// /// let query = query_dyn!( /// "SELECT * FROM people WHERE age > $age AND name = $name", /// ..bindings /// )?; /// # Ok(()) /// # } /// ``` /// /// Alternatively we could just set the type on the container explicitly: /// /// ``` /// # use postgres_query::Parameter; /// let mut bindings: Vec<(&str, Parameter)> = Vec::new(); /// ``` pub type Parameter<'a> = &'a (dyn ToSql + Sync); /// A static query with dynamic parameters. /// /// # Usage /// /// ## Constructing /// /// The preferred way of constructing a [`Query`] is by using the [`query!`] and [`query_dyn!`] /// macros. /// /// You may also use the `Query::parse`, `Query::new_static` or `Query::new` methods. /// /// /// ## Executing /// /// When executing the query you have two options, either: /// /// 1. use the provided methods: `execute`, `fetch`, `query`, etc. /// 2. use the `sql` and `parameters` fields as arguments to the standard [`Client`] methods /// /// ``` /// # use tokio_postgres::{Client, Row}; /// # use postgres_query::{query, FromSqlRow, Result}; /// # fn connect() -> Client { unimplemented!() } /// # async fn foo() -> Result<(), Box<dyn std::error::Error>> { /// #[derive(FromSqlRow)] /// struct Person { /// age: i32, /// name: String, /// } /// /// let client: Client = connect(/* ... */); /// let query = query!("SELECT age, name FROM people"); /// /// // Option 1 /// let people: Vec<Person> = query.fetch(&client).await?; /// /// // Option 2 /// let rows: Vec<Row> = client.query(query.sql(), query.parameters()).await?; /// let people: Vec<Person> = Person::from_row_multi(&rows)?; /// # Ok(()) /// # } /// ``` /// /// [`Query`]: struct.Query.html /// [`query!`]: macro.query.html /// [`query_dyn!`]: macro.query_dyn.html /// [`Client`]: https://docs.rs/tokio-postgres/0.5.1/tokio_postgres/struct.Client.html #[derive(Debug, Clone)] pub struct Query<'a> { sql: Sql, parameters: Vec<Parameter<'a>>, } #[derive(Debug, Clone)] enum Sql { Static(&'static str), Dynamic(String), } impl<'a> Query<'a> { /// Create a new query an already prepared string. /// /// IMPORTANT: This does not allow you to pass named parameter bindings (`$name`, `$abc_123`, /// etc.). For that behaviour, refer to the `query!` macro. Instead bindings and parameters are /// given in the same format required by `tokio_postgres` (`$1`, `$2`, ...). pub fn new(sql: String, parameters: Vec<Parameter<'a>>) -> Query<'a> { Query { sql: Sql::Dynamic(sql), parameters, } } /// Create a new query with a static query string. /// /// IMPORTANT: This does not allow you to pass named parameter bindings (`$name`, `$abc_123`, /// etc.), For that behaviour, refer to the `query_dyn!` macro. Instead bindings and parameters /// are given in the same format required by `tokio_postgres` (`$1`, `$2`, ...). pub fn new_static(sql: &'static str, parameters: Vec<Parameter<'a>>) -> Query<'a> { Query { sql: Sql::Static(sql), parameters, } } /// Parses a string that may contain parameter bindings on the form `$abc_123`. This is the same /// function that is called when passing dynamically generated strings to the `query_dyn!` /// macro. /// /// Because this is a function there will some runtime overhead unlike the `query!` macro which /// has zero overhead when working with string literals. pub fn parse(text: &str, bindings: &[(&str, Parameter<'a>)]) -> Result<Query<'a>> { let (sql, parameters) = parse::parse(text, bindings)?; Ok(Query { sql: Sql::Dynamic(sql), parameters, }) } /// Get this query as an SQL string. pub fn sql(&'a self) -> &'a str { &self.sql } /// Get the parameters of this query in the order expected by the query returned by /// `Query::sql`. pub fn parameters(&'a self) -> &[Parameter<'a>] { &self.parameters } } impl Deref for Sql { type Target = str; fn deref(&self) -> &Self::Target { match self { Sql::Static(text) => text, Sql::Dynamic(text) => &text, } } } #[cfg(test)] mod tests { use super::*; use crate::error::ParseError; macro_rules! is_match { ($expr:expr, $pattern:pat) => { match $expr { $pattern => true, _ => false, } }; } #[test] fn parse_query_without_bindings() { let query = Query::parse("SELECT 123, 'abc'", &[]).unwrap(); assert_eq!(query.sql(), "SELECT 123, 'abc'"); } #[test] fn parse_query_single_binding() { let query = Query::parse("SELECT $number", &[("number", &123)]).unwrap(); assert_eq!(query.sql(), "SELECT $1"); } #[test] fn parse_query_missing_identifier_eof() { let query = Query::parse("SELECT $", &[]); assert!(is_match!( query.unwrap_err(), Error::Parse(ParseError::EmptyIdentifier { found: None }) )); } #[test] fn parse_query_missing_identifier() { let query = Query::parse("SELECT $ FROM users", &[]); assert!(is_match!( query.unwrap_err(), Error::Parse(ParseError::EmptyIdentifier { found: Some(' ') }) )); } }