Debian Codegraph @

src/options/mod.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#};
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#};
5
6 pub use ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/::PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#;
7
8 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{connectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/::LogSettingsrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#, netrust-analyzer cargo sqlx-core 0.8.3 net/::tlsrust-analyzer cargo sqlx-core 0.8.3 net/tls/::CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#};
9
10 mod connectrust-analyzer cargo sqlx-postgres 0.8.3 options/connect/;
11 mod parserust-analyzer cargo sqlx-postgres 0.8.3 options/parse/;
12 mod pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/;
13 mod ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/;
14
15 /// Options and flags which can be used to configure a PostgreSQL connection.
16 ///
17 /// A value of `PgConnectOptions` can be parsed from a connection URL,
18 /// as described by [libpq](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING).
19 ///
20 /// The general form for a connection URL is:
21 ///
22 /// ```text
23 /// postgresql://[user[:password]@][host][:port][/dbname][?param1=value1&...]
24 /// ```
25 ///
26 /// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
27 /// containing a connection URL and then further adjust options if necessary (see example below).
28 ///
29 /// ## Parameters
30 ///
31 /// |Parameter|Default|Description|
32 /// |---------|-------|-----------|
33 /// | `sslmode` | `prefer` | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated. See [`PgSslMode`]. |
34 /// | `sslrootcert` | `None` | Sets the name of a file containing a list of trusted SSL Certificate Authorities. |
35 /// | `statement-cache-capacity` | `100` | The maximum number of prepared statements stored in the cache. Set to `0` to disable. |
36 /// | `host` | `None` | Path to the directory containing a PostgreSQL unix domain socket, which will be used instead of TCP if set. |
37 /// | `hostaddr` | `None` | Same as `host`, but only accepts IP addresses. |
38 /// | `application-name` | `None` | The name will be displayed in the pg_stat_activity view and included in CSV log entries. |
39 /// | `user` | result of `whoami` | PostgreSQL user name to connect as. |
40 /// | `password` | `None` | Password to be used if the server demands password authentication. |
41 /// | `port` | `5432` | Port number to connect to at the server host, or socket file name extension for Unix-domain connections. |
42 /// | `dbname` | `None` | The database name. |
43 /// | `options` | `None` | The runtime parameters to send to the server at connection start. |
44 ///
45 /// The URL scheme designator can be either `postgresql://` or `postgres://`.
46 /// Each of the URL parts is optional.
47 ///
48 /// ```text
49 /// postgresql://
50 /// postgresql://localhost
51 /// postgresql://localhost:5433
52 /// postgresql://localhost/mydb
53 /// postgresql://user@localhost
54 /// postgresql://user:secret@localhost
55 /// postgresql://localhost?dbname=mydb&user=postgres&password=postgres
56 /// ```
57 ///
58 /// # Example
59 ///
60 /// ```rust,no_run
61 /// use sqlx::{Connection, ConnectOptions};
62 /// use sqlx::postgres::{PgConnectOptions, PgConnection, PgPool, PgSslMode};
63 ///
64 /// # async fn example() -> sqlx::Result<()> {
65 /// // URL connection string
66 /// let conn = PgConnection::connect("postgres://localhost/mydb").await?;
67 ///
68 /// // Manually-constructed options
69 /// let conn = PgConnectOptions::new()
70 /// .host("secret-host")
71 /// .port(2525)
72 /// .username("secret-user")
73 /// .password("secret-password")
74 /// .ssl_mode(PgSslMode::Require)
75 /// .connect()
76 /// .await?;
77 ///
78 /// // Modifying options parsed from a string
79 /// let mut opts: PgConnectOptions = "postgres://localhost/mydb".parse()?;
80 ///
81 /// // Change the log verbosity level for queries.
82 /// // Information about SQL queries is logged at `DEBUG` level by default.
83 /// opts = opts.log_statements(log::LevelFilter::Trace);
84 ///
85 /// let pool = PgPool::connect_with(opts).await?;
86 /// # Ok(())
87 /// # }
88 /// ```
89 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
90 pub struct PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions# {
91 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
92 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port.: u16,
93 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) socketrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#socket.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>,
94 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#username.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
95 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#password.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
96 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
97 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode.: PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#,
98 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) ssl_root_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_root_cert.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#>,
99 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) ssl_client_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_cert.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#>,
100 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) ssl_client_keyrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_key.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#>,
101 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) statement_cache_capacityrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#statement_cache_capacity.: usize,
102 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) application_namerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#application_name.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
103 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) log_settingsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#log_settings.: LogSettingsrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#,
104 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) extra_float_digitsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#extra_float_digits.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, str>>,
105 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
106 }
107
108 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions# {
109 fn defaultrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions][Default]default().() -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions][Default] {
110 Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions][Default]::new_without_pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new_without_pgpass().().apply_pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]apply_pgpass().()
111 }
112 }
113
114 impl PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions# {
115 /// Creates a new, default set of options ready for configuration.
116 ///
117 /// By default, this reads the following environment variables and sets their
118 /// equivalent options.
119 ///
120 /// * `PGHOST`
121 /// * `PGPORT`
122 /// * `PGUSER`
123 /// * `PGPASSWORD`
124 /// * `PGDATABASE`
125 /// * `PGSSLROOTCERT`
126 /// * `PGSSLCERT`
127 /// * `PGSSLKEY`
128 /// * `PGSSLMODE`
129 /// * `PGAPPNAME`
130 ///
131 /// # Example
132 ///
133 /// ```rust
134 /// # use sqlx_postgres::PgConnectOptions;
135 /// let options = PgConnectOptions::new();
136 /// ```
137 pub fn newrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new().() -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
138 Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]::new_without_pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new_without_pgpass().().apply_pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]apply_pgpass().()
139 }
140
141 pub fn new_without_pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new_without_pgpass().() -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
142 let port = varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGPORT")
143 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
144 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|v| v.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().())
145 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(5432);
146
147 let host = varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGHOST").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| default_hostrust-analyzer cargo sqlx-postgres 0.8.3 options/default_host().(port));
148
149 let username = varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGUSER").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(whoamirust-analyzer cargo whoami 1.6.1 crate/::usernamerust-analyzer cargo whoami 1.6.1 api/username().);
150
151 let database = varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGDATABASE").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
152
153 PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions# {
154 portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port.,
155 hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host.,
156 socketrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#socket.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
157 usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#username.,
158 passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#password.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGPASSWORD").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(),
159 databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database.,
160 ssl_root_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_root_cert.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGSSLROOTCERT").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::fromrust-analyzer cargo sqlx-core 0.8.3 net/tls/impl#[CertificateInput][`From<String>`]from().),
161 ssl_client_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_cert.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGSSLCERT").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::fromrust-analyzer cargo sqlx-core 0.8.3 net/tls/impl#[CertificateInput][`From<String>`]from().),
162 ssl_client_keyrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_key.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGSSLKEY").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::fromrust-analyzer cargo sqlx-core 0.8.3 net/tls/impl#[CertificateInput][`From<String>`]from().),
163 ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGSSLMODE")
164 .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()
165 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|v| v.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().())
166 .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().(),
167 statement_cache_capacityrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#statement_cache_capacity.: 100,
168 application_namerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#application_name.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGAPPNAME").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(),
169 extra_float_digitsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#extra_float_digits.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("2".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
170 log_settingsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#log_settings.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo sqlx-core 0.8.3 connection/impl#[LogSettings][Default]default().(),
171 optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options.: varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("PGOPTIONS").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(),
172 }
173 }
174
175 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) fn apply_pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]apply_pgpass().(mut self) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
176 if self.passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#password..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
177 self.passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#password. = pgpassrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/::load_passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password().(
178 &self.hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host.,
179 self.portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port.,
180 &self.usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#username.,
181 self.databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().(),
182 );
183 }
184
185 self
186 }
187
188 /// Sets the name of the host to connect to.
189 ///
190 /// If a host name begins with a slash, it specifies
191 /// Unix-domain communication rather than TCP/IP communication; the value is the name of
192 /// the directory in which the socket file is stored.
193 ///
194 /// The default behavior when host is not specified, or is empty,
195 /// is to connect to a Unix-domain socket
196 ///
197 /// # Example
198 ///
199 /// ```rust
200 /// # use sqlx_postgres::PgConnectOptions;
201 /// let options = PgConnectOptions::new()
202 /// .host("localhost");
203 /// ```
204 pub fn hostrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]host().(mut self, host: &str) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
205 host.clone_intorust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]clone_into().(&mut self.hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host.);
206 self
207 }
208
209 /// Sets the port to connect to at the server host.
210 ///
211 /// The default port for PostgreSQL is `5432`.
212 ///
213 /// # Example
214 ///
215 /// ```rust
216 /// # use sqlx_postgres::PgConnectOptions;
217 /// let options = PgConnectOptions::new()
218 /// .port(5432);
219 /// ```
220 pub fn portrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]port().(mut self, port: u16) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
221 self.portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port. = port;
222 self
223 }
224
225 /// Sets a custom path to a directory containing a unix domain socket,
226 /// switching the connection method from TCP to the corresponding socket.
227 ///
228 /// By default set to `None`.
229 pub fn socketrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]socket().(mut self, path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
230 self.socketrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#socket. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().());
231 self
232 }
233
234 /// Sets the username to connect as.
235 ///
236 /// Defaults to be the same as the operating system name of
237 /// the user running the application.
238 ///
239 /// # Example
240 ///
241 /// ```rust
242 /// # use sqlx_postgres::PgConnectOptions;
243 /// let options = PgConnectOptions::new()
244 /// .username("postgres");
245 /// ```
246 pub fn usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]username().(mut self, username: &str) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
247 username.clone_intorust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]clone_into().(&mut self.usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#username.);
248 self
249 }
250
251 /// Sets the password to use if the server demands password authentication.
252 ///
253 /// # Example
254 ///
255 /// ```rust
256 /// # use sqlx_postgres::PgConnectOptions;
257 /// let options = PgConnectOptions::new()
258 /// .username("root")
259 /// .password("safe-and-secure");
260 /// ```
261 pub fn passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]password().(mut self, password: &str) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
262 self.passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#password. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(password.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().());
263 self
264 }
265
266 /// Sets the database name. Defaults to be the same as the user name.
267 ///
268 /// # Example
269 ///
270 /// ```rust
271 /// # use sqlx_postgres::PgConnectOptions;
272 /// let options = PgConnectOptions::new()
273 /// .database("postgres");
274 /// ```
275 pub fn databaserust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]database().(mut self, database: &str) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
276 self.databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(database.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().());
277 self
278 }
279
280 /// Sets whether or with what priority a secure SSL TCP/IP connection will be negotiated
281 /// with the server.
282 ///
283 /// By default, the SSL mode is [`Prefer`](PgSslMode::Prefer), and the client will
284 /// first attempt an SSL connection but fallback to a non-SSL connection on failure.
285 ///
286 /// Ignored for Unix domain socket communication.
287 ///
288 /// # Example
289 ///
290 /// ```rust
291 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
292 /// let options = PgConnectOptions::new()
293 /// .ssl_mode(PgSslMode::Require);
294 /// ```
295 pub fn ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_mode().(mut self, mode: PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
296 self.ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode. = mode;
297 self
298 }
299
300 /// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
301 /// If the file exists, the server's certificate will be verified to be signed by
302 /// one of these authorities.
303 ///
304 /// # Example
305 ///
306 /// ```rust
307 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
308 /// let options = PgConnectOptions::new()
309 /// // Providing a CA certificate with less than VerifyCa is pointless
310 /// .ssl_mode(PgSslMode::VerifyCa)
311 /// .ssl_root_cert("./ca-certificate.crt");
312 /// ```
313 pub fn ssl_root_certrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_root_cert().(mut self, cert: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
314 self.ssl_root_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_root_cert. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::Filerust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#File#(cert.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()));
315 self
316 }
317
318 /// Sets the name of a file containing SSL client certificate.
319 ///
320 /// # Example
321 ///
322 /// ```rust
323 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
324 /// let options = PgConnectOptions::new()
325 /// // Providing a CA certificate with less than VerifyCa is pointless
326 /// .ssl_mode(PgSslMode::VerifyCa)
327 /// .ssl_client_cert("./client.crt");
328 /// ```
329 pub fn ssl_client_certrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_client_cert().(mut self, cert: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
330 self.ssl_client_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_cert. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::Filerust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#File#(cert.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()));
331 self
332 }
333
334 /// Sets the SSL client certificate as a PEM-encoded byte slice.
335 ///
336 /// This should be an ASCII-encoded blob that starts with `-----BEGIN CERTIFICATE-----`.
337 ///
338 /// # Example
339 /// Note: embedding SSL certificates and keys in the binary is not advised.
340 /// This is for illustration purposes only.
341 ///
342 /// ```rust
343 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
344 ///
345 /// const CERT: &[u8] = b"\
346 /// -----BEGIN CERTIFICATE-----
347 /// <Certificate data here.>
348 /// -----END CERTIFICATE-----";
349 ///
350 /// let options = PgConnectOptions::new()
351 /// // Providing a CA certificate with less than VerifyCa is pointless
352 /// .ssl_mode(PgSslMode::VerifyCa)
353 /// .ssl_client_cert_from_pem(CERT);
354 /// ```
355 pub fn ssl_client_cert_from_pemrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_client_cert_from_pem().(mut self, cert: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
356 self.ssl_client_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_cert. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::Inlinerust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#Inline#(cert.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()));
357 self
358 }
359
360 /// Sets the name of a file containing SSL client key.
361 ///
362 /// # Example
363 ///
364 /// ```rust
365 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
366 /// let options = PgConnectOptions::new()
367 /// // Providing a CA certificate with less than VerifyCa is pointless
368 /// .ssl_mode(PgSslMode::VerifyCa)
369 /// .ssl_client_key("./client.key");
370 /// ```
371 pub fn ssl_client_keyrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_client_key().(mut self, key: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
372 self.ssl_client_keyrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_key. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::Filerust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#File#(key.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()));
373 self
374 }
375
376 /// Sets the SSL client key as a PEM-encoded byte slice.
377 ///
378 /// This should be an ASCII-encoded blob that starts with `-----BEGIN PRIVATE KEY-----`.
379 ///
380 /// # Example
381 /// Note: embedding SSL certificates and keys in the binary is not advised.
382 /// This is for illustration purposes only.
383 ///
384 /// ```rust
385 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
386 ///
387 /// const KEY: &[u8] = b"\
388 /// -----BEGIN PRIVATE KEY-----
389 /// <Private key data here.>
390 /// -----END PRIVATE KEY-----";
391 ///
392 /// let options = PgConnectOptions::new()
393 /// // Providing a CA certificate with less than VerifyCa is pointless
394 /// .ssl_mode(PgSslMode::VerifyCa)
395 /// .ssl_client_key_from_pem(KEY);
396 /// ```
397 pub fn ssl_client_key_from_pemrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_client_key_from_pem().(mut self, key: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
398 self.ssl_client_keyrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_key. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::Inlinerust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#Inline#(key.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()));
399 self
400 }
401
402 /// Sets PEM encoded trusted SSL Certificate Authorities (CA).
403 ///
404 /// # Example
405 ///
406 /// ```rust
407 /// # use sqlx_postgres::{PgSslMode, PgConnectOptions};
408 /// let options = PgConnectOptions::new()
409 /// // Providing a CA certificate with less than VerifyCa is pointless
410 /// .ssl_mode(PgSslMode::VerifyCa)
411 /// .ssl_root_cert_from_pem(vec![]);
412 /// ```
413 pub fn ssl_root_cert_from_pemrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]ssl_root_cert_from_pem().(mut self, pem_certificate: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
414 self.ssl_root_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_root_cert. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CertificateInputrust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#::Inlinerust-analyzer cargo sqlx-core 0.8.3 net/tls/CertificateInput#Inline#(pem_certificate));
415 self
416 }
417
418 /// Sets the capacity of the connection's statement cache in a number of stored
419 /// distinct statements. Caching is handled using LRU, meaning when the
420 /// amount of queries hits the defined limit, the oldest statement will get
421 /// dropped.
422 ///
423 /// The default cache capacity is 100 statements.
424 pub fn statement_cache_capacityrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]statement_cache_capacity().(mut self, capacity: usize) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
425 self.statement_cache_capacityrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#statement_cache_capacity. = capacity;
426 self
427 }
428
429 /// Sets the application name. Defaults to None
430 ///
431 /// # Example
432 ///
433 /// ```rust
434 /// # use sqlx_postgres::PgConnectOptions;
435 /// let options = PgConnectOptions::new()
436 /// .application_name("my-app");
437 /// ```
438 pub fn application_namerust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]application_name().(mut self, application_name: &str) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
439 self.application_namerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#application_name. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(application_name.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().());
440 self
441 }
442
443 /// Sets or removes the `extra_float_digits` connection option.
444 ///
445 /// This changes the default precision of floating-point values returned in text mode (when
446 /// not using prepared statements such as calling methods of [`Executor`] directly).
447 ///
448 /// Historically, Postgres would by default round floating-point values to 6 and 15 digits
449 /// for `float4`/`REAL` (`f32`) and `float8`/`DOUBLE` (`f64`), respectively, which would mean
450 /// that the returned value may not be exactly the same as its representation in Postgres.
451 ///
452 /// The nominal range for this value is `-15` to `3`, where negative values for this option
453 /// cause floating-points to be rounded to that many fewer digits than normal (`-1` causes
454 /// `float4` to be rounded to 5 digits instead of six, or 14 instead of 15 for `float8`),
455 /// positive values cause Postgres to emit that many extra digits of precision over default
456 /// (or simply use maximum precision in Postgres 12 and later),
457 /// and 0 means keep the default behavior (or the "old" behavior described above
458 /// as of Postgres 12).
459 ///
460 /// SQLx sets this value to 3 by default, which tells Postgres to return floating-point values
461 /// at their maximum precision in the hope that the parsed value will be identical to its
462 /// counterpart in Postgres. This is also the default in Postgres 12 and later anyway.
463 ///
464 /// However, older versions of Postgres and alternative implementations that talk the Postgres
465 /// protocol may not support this option, or the full range of values.
466 ///
467 /// If you get an error like "unknown option `extra_float_digits`" when connecting, try
468 /// setting this to `None` or consult the manual of your database for the allowed range
469 /// of values.
470 ///
471 /// For more information, see:
472 /// * [Postgres manual, 20.11.2: Client Connection Defaults; Locale and Formatting][20.11.2]
473 /// * [Postgres manual, 8.1.3: Numeric Types; Floating-point Types][8.1.3]
474 ///
475 /// [`Executor`]: crate::executor::Executor
476 /// [20.11.2]: https://www.postgresql.org/docs/current/runtime-config-client.html#RUNTIME-CONFIG-CLIENT-FORMAT
477 /// [8.1.3]: https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-FLOAT
478 ///
479 /// ### Examples
480 /// ```rust
481 /// # use sqlx_postgres::PgConnectOptions;
482 ///
483 /// let mut options = PgConnectOptions::new()
484 /// // for Redshift and Postgres 10
485 /// .extra_float_digits(2);
486 ///
487 /// let mut options = PgConnectOptions::new()
488 /// // don't send the option at all (Postgres 9 and older)
489 /// .extra_float_digits(None);
490 /// ```
491 pub fn extra_float_digitsrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]extra_float_digits().(mut self, extra_float_digits: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i8>>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions] {
492 self.extra_float_digitsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#extra_float_digits. = extra_float_digits.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|it| it.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
493 self
494 }
495
496 /// Set additional startup options for the connection as a list of key-value pairs.
497 ///
498 /// # Example
499 ///
500 /// ```rust
501 /// # use sqlx_postgres::PgConnectOptions;
502 /// let options = PgConnectOptions::new()
503 /// .options([("geqo", "off"), ("statement_timeout", "5min")]);
504 /// ```
505 pub fn optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]options().<K, V, I>(mut self, options: I) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]
506 where
507 K: Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#,
508 V: Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#,
509 I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = (K, V)>,
510 {
511 // Do this in here so `options_str` is only set if we have an option to insert
512 let options_str = self.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options..get_or_insert_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]get_or_insert_with().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().);
513 for (k, v) in options {
514 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().options_str.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
515 options_str.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(' ');
516 }
517
518 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(options_str, "-c {k}={v}").expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write an option to the string");
519 }
520 self
521 }
522
523 /// We try using a socket if hostname starts with `/` or if socket parameter
524 /// is specified.
525 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) fn fetch_socketrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]fetch_socket().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
526 match self.socketrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#socket. {
527 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref socket) => {
528 let full_path = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}/.s.PGSQL.{}", socket.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), self.portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port.);
529 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(full_path)
530 }
531 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# if self.hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host..starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('/') => {
532 let full_path = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}/.s.PGSQL.{}", self.hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host., self.portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port.);
533 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(full_path)
534 }
535 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
536 }
537 }
538 }
539
540 impl PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions# {
541 /// Get the current host.
542 ///
543 /// # Example
544 ///
545 /// ```rust
546 /// # use sqlx_postgres::PgConnectOptions;
547 /// let options = PgConnectOptions::new()
548 /// .host("127.0.0.1");
549 /// assert_eq!(options.get_host(), "127.0.0.1");
550 /// ```
551 pub fn get_hostrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_host().(&self) -> &str {
552 &self.hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host.
553 }
554
555 /// Get the server's port.
556 ///
557 /// # Example
558 ///
559 /// ```rust
560 /// # use sqlx_postgres::PgConnectOptions;
561 /// let options = PgConnectOptions::new()
562 /// .port(6543);
563 /// assert_eq!(options.get_port(), 6543);
564 /// ```
565 pub fn get_portrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_port().(&self) -> u16 {
566 self.portrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#port.
567 }
568
569 /// Get the socket path.
570 ///
571 /// # Example
572 ///
573 /// ```rust
574 /// # use sqlx_postgres::PgConnectOptions;
575 /// let options = PgConnectOptions::new()
576 /// .socket("/tmp");
577 /// assert!(options.get_socket().is_some());
578 /// ```
579 pub fn get_socketrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_socket().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
580 self.socketrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#socket..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()
581 }
582
583 /// Get the server's port.
584 ///
585 /// # Example
586 ///
587 /// ```rust
588 /// # use sqlx_postgres::PgConnectOptions;
589 /// let options = PgConnectOptions::new()
590 /// .username("foo");
591 /// assert_eq!(options.get_username(), "foo");
592 /// ```
593 pub fn get_usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_username().(&self) -> &str {
594 &self.usernamerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#username.
595 }
596
597 /// Get the current database name.
598 ///
599 /// # Example
600 ///
601 /// ```rust
602 /// # use sqlx_postgres::PgConnectOptions;
603 /// let options = PgConnectOptions::new()
604 /// .database("postgres");
605 /// assert!(options.get_database().is_some());
606 /// ```
607 pub fn get_databaserust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_database().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
608 self.databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
609 }
610
611 /// Get the SSL mode.
612 ///
613 /// # Example
614 ///
615 /// ```rust
616 /// # use sqlx_postgres::{PgConnectOptions, PgSslMode};
617 /// let options = PgConnectOptions::new();
618 /// assert!(matches!(options.get_ssl_mode(), PgSslMode::Prefer));
619 /// ```
620 pub fn get_ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_ssl_mode().(&self) -> PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode# {
621 self.ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode.
622 }
623
624 /// Get the application name.
625 ///
626 /// # Example
627 ///
628 /// ```rust
629 /// # use sqlx_postgres::PgConnectOptions;
630 /// let options = PgConnectOptions::new()
631 /// .application_name("service");
632 /// assert!(options.get_application_name().is_some());
633 /// ```
634 pub fn get_application_namerust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_application_name().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
635 self.application_namerust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#application_name..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
636 }
637
638 /// Get the options.
639 ///
640 /// # Example
641 ///
642 /// ```rust
643 /// # use sqlx_postgres::PgConnectOptions;
644 /// let options = PgConnectOptions::new()
645 /// .options([("foo", "bar")]);
646 /// assert!(options.get_options().is_some());
647 /// ```
648 pub fn get_optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]get_options().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
649 self.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
650 }
651 }
652
653 fn default_hostrust-analyzer cargo sqlx-postgres 0.8.3 options/default_host().(port: u16) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
654 // try to check for the existence of a unix socket and uses that
655 let socket = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(".s.PGSQL.{port}");
656 let candidates = [
657 "/var/run/postgresql", // Debian
658 "/private/tmp", // OSX (homebrew)
659 "/tmp", // Default
660 ];
661
662 for candidate in &candidates {
663 if Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(candidate).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(&socket).existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
664 return candidate.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
665 }
666 }
667
668 // fallback to localhost if no socket was found
669 "localhost".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()
670 }
671
672 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
673 fn test_options_formattingrust-analyzer cargo sqlx-postgres 0.8.3 options/test_options_formatting().() {
674 let options = PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#::newrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new().().optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]options().([("geqo", "off")]);
675 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(options.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("-c geqo=off".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
676 let options = options.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]options().([("search_path", "sqlx")]);
677 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
678 options.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options.,
679 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("-c geqo=off -c search_path=sqlx".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())
680 );
681 let options = PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#::newrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new().().optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]options().([("geqo", "off"), ("statement_timeout", "5min")]);
682 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
683 options.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options.,
684 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("-c geqo=off -c statement_timeout=5min".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())
685 );
686 let options = PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#::newrust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]new().();
687 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(options.optionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#options., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
688 }