Skip to content

Commit d813228

Browse files
committed
feat(postgres): use flat command-list instead of hashmap
1 parent 1713ea1 commit d813228

File tree

4 files changed

+24
-44
lines changed

4 files changed

+24
-44
lines changed

sqlx-core/src/postgres/connection/establish.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl PgConnection {
4040
params.push(("application_name", application_name));
4141
}
4242

43-
for (k, v) in options.options.iter() {
44-
params.push((k, v));
43+
if let Some(ref options) = options.options {
44+
params.push(("options", options));
4545
}
4646

4747
stream

sqlx-core/src/postgres/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub use error::{PgDatabaseError, PgErrorPosition};
3232
pub use listener::{PgListener, PgNotification};
3333
pub use message::PgSeverity;
3434
pub use options::{PgConnectOptions, PgSslMode};
35-
pub(crate) use options::parse_options;
3635
pub use query_result::PgQueryResult;
3736
pub use row::PgRow;
3837
pub use statement::PgStatement;

sqlx-core/src/postgres/options/mod.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashMap;
21
use std::env::var;
32
use std::path::{Path, PathBuf};
43

@@ -87,7 +86,7 @@ pub struct PgConnectOptions {
8786
pub(crate) statement_cache_capacity: usize,
8887
pub(crate) application_name: Option<String>,
8988
pub(crate) log_settings: LogSettings,
90-
pub(crate) options: HashMap<String, String>,
89+
pub(crate) options: Option<String>,
9190
}
9291

9392
impl Default for PgConnectOptions {
@@ -148,9 +147,7 @@ impl PgConnectOptions {
148147
statement_cache_capacity: 100,
149148
application_name: var("PGAPPNAME").ok(),
150149
log_settings: Default::default(),
151-
options: var("PGOPTIONS")
152-
.and_then(|v| Ok(parse_options(&v).unwrap_or_default().into_iter().collect()))
153-
.unwrap_or_default(),
150+
options: var("PGOPTIONS").ok(),
154151
}
155152
}
156153

@@ -332,16 +329,23 @@ impl PgConnectOptions {
332329
/// ```rust
333330
/// # use sqlx_core::postgres::PgConnectOptions;
334331
/// let options = PgConnectOptions::new()
335-
/// .options(&[("geqo", "off"), ("statement_timeout", "5min")]);
332+
/// .options([("geqo", "off"), ("statement_timeout", "5min")]);
336333
/// ```
337334
pub fn options<K, V, I>(mut self, options: I) -> Self
338335
where
339336
K: ToString,
340337
V: ToString,
341338
I: IntoIterator<Item = (K, V)>,
342339
{
340+
let mut options_str = String::new();
343341
for (k, v) in options {
344-
self.options.insert(k.to_string(), v.to_string());
342+
options_str += &format!("-c {}={}", k.to_string(), v.to_string());
343+
}
344+
if let Some(ref mut v) = self.options {
345+
v.push(' ');
346+
v.push_str(&options_str);
347+
} else {
348+
self.options = Some(options_str);
345349
}
346350
self
347351
}
@@ -363,24 +367,6 @@ impl PgConnectOptions {
363367
}
364368
}
365369

366-
/// Parse a libpq style options string
367-
pub(crate) fn parse_options(input: &str) -> Option<Vec<(String, String)>> {
368-
let mut options = Vec::new();
369-
for part in input.split(' ') {
370-
let part = part.trim();
371-
if part.is_empty() || part == "-c" {
372-
continue;
373-
}
374-
let pair = part.splitn(2, '=').collect::<Vec<_>>();
375-
if pair.len() != 2 {
376-
return None;
377-
}
378-
options.push((pair[0].to_string(), pair[1].to_string()));
379-
}
380-
381-
Some(options)
382-
}
383-
384370
fn default_host(port: u16) -> String {
385371
// try to check for the existence of a unix socket and uses that
386372
let socket = format!(".s.PGSQL.{}", port);

sqlx-core/src/postgres/options/parse.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::Error;
2-
use crate::postgres::{parse_options, PgConnectOptions};
2+
use crate::postgres::PgConnectOptions;
33
use percent_encoding::percent_decode_str;
44
use std::net::IpAddr;
55
use std::str::FromStr;
@@ -86,8 +86,11 @@ impl FromStr for PgConnectOptions {
8686
"application_name" => options = options.application_name(&*value),
8787

8888
"options" => {
89-
if let Some(value) = parse_options(&value) {
90-
options = options.options(value);
89+
if let Some(options) = options.options.as_mut() {
90+
options.push(' ');
91+
options.push_str(&*value);
92+
} else {
93+
options.options = Some(value.to_string());
9194
}
9295
}
9396

@@ -209,16 +212,12 @@ fn it_parses_socket_correctly_with_username_percent_encoded() {
209212
}
210213
#[test]
211214
fn it_parses_libpq_options_correctly() {
212-
let uri = "postgres:///?options=-c%20synchronous_commit%3Doff%20-c%20search_path%3Dpostgres";
215+
let uri = "postgres:///?options=-c%20synchronous_commit%3Doff%20--search_path%3Dpostgres";
213216
let opts = PgConnectOptions::from_str(uri).unwrap();
214217

215218
assert_eq!(
216-
Some(&"off".to_string()),
217-
opts.options.get("synchronous_commit")
218-
);
219-
assert_eq!(
220-
Some(&"postgres".to_string()),
221-
opts.options.get("search_path")
219+
Some("-c synchronous_commit=off --search_path=postgres".into()),
220+
opts.options
222221
);
223222
}
224223
#[test]
@@ -227,11 +226,7 @@ fn it_parses_sqlx_options_correctly() {
227226
let opts = PgConnectOptions::from_str(uri).unwrap();
228227

229228
assert_eq!(
230-
Some(&"off".to_string()),
231-
opts.options.get("synchronous_commit")
232-
);
233-
assert_eq!(
234-
Some(&"postgres".to_string()),
235-
opts.options.get("search_path")
229+
Some("-c synchronous_commit=off -c search_path=postgres".into()),
230+
opts.options
236231
);
237232
}

0 commit comments

Comments
 (0)