mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-01-19 23:26:32 +00:00
Document new connection string params
This commit is contained in:
parent
2c2a277666
commit
f969798cb6
@ -98,7 +98,7 @@ impl MySqlConnection {
|
||||
|
||||
Ok(Self {
|
||||
stream,
|
||||
cache_statement: StatementCache::new(options.statement_cache_size),
|
||||
cache_statement: StatementCache::new(options.statement_cache_capacity),
|
||||
scratch_row_columns: Default::default(),
|
||||
scratch_row_column_names: Default::default(),
|
||||
})
|
||||
|
||||
@ -68,6 +68,14 @@ impl FromStr for MySqlSslMode {
|
||||
/// mysql://[host][/database][?properties]
|
||||
/// ```
|
||||
///
|
||||
/// ## Properties
|
||||
///
|
||||
/// |Parameter|Default|Description|
|
||||
/// |---------|-------|-----------|
|
||||
/// | `ssl-mode` | `PREFERRED` | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated. See [`MySqlSslMode`]. |
|
||||
/// | `ssl-ca` | `None` | Sets the name of a file containing a list of trusted SSL Certificate Authorities. |
|
||||
/// | `statement-cache-capacity` | `100` | The maximum number of prepared statements stored in the cache. Set to `0` to disable. |
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
@ -92,6 +100,8 @@ impl FromStr for MySqlSslMode {
|
||||
/// # })
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`MySqlSslMode`]: enum.MySqlSslMode.html
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MySqlConnectOptions {
|
||||
pub(crate) host: String,
|
||||
@ -101,7 +111,7 @@ pub struct MySqlConnectOptions {
|
||||
pub(crate) database: Option<String>,
|
||||
pub(crate) ssl_mode: MySqlSslMode,
|
||||
pub(crate) ssl_ca: Option<PathBuf>,
|
||||
pub(crate) statement_cache_size: usize,
|
||||
pub(crate) statement_cache_capacity: usize,
|
||||
}
|
||||
|
||||
impl Default for MySqlConnectOptions {
|
||||
@ -121,7 +131,7 @@ impl MySqlConnectOptions {
|
||||
database: None,
|
||||
ssl_mode: MySqlSslMode::Preferred,
|
||||
ssl_ca: None,
|
||||
statement_cache_size: 100,
|
||||
statement_cache_capacity: 100,
|
||||
}
|
||||
}
|
||||
|
||||
@ -193,14 +203,14 @@ impl MySqlConnectOptions {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the size of the connection's statement cache in a number of stored
|
||||
/// Sets the capacity of the connection's statement cache in a number of stored
|
||||
/// distinct statements. Caching is handled using LRU, meaning when the
|
||||
/// amount of queries hits the defined limit, the oldest statement will get
|
||||
/// dropped.
|
||||
///
|
||||
/// The default cache size is 100 statements.
|
||||
pub fn statement_cache_size(mut self, size: usize) -> Self {
|
||||
self.statement_cache_size = size;
|
||||
/// The default cache capacity is 100 statements.
|
||||
pub fn statement_cache_capacity(mut self, capacity: usize) -> Self {
|
||||
self.statement_cache_capacity = capacity;
|
||||
self
|
||||
}
|
||||
}
|
||||
@ -244,8 +254,8 @@ impl FromStr for MySqlConnectOptions {
|
||||
options = options.ssl_ca(&*value);
|
||||
}
|
||||
|
||||
"statement-cache-size" => {
|
||||
options = options.statement_cache_size(value.parse()?);
|
||||
"statement-cache-capacity" => {
|
||||
options = options.statement_cache_capacity(value.parse()?);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
|
||||
@ -139,7 +139,7 @@ impl PgConnection {
|
||||
transaction_status,
|
||||
pending_ready_for_query_count: 0,
|
||||
next_statement_id: 1,
|
||||
cache_statement: StatementCache::new(options.statement_cache_size),
|
||||
cache_statement: StatementCache::new(options.statement_cache_capacity),
|
||||
cache_type_oid: HashMap::new(),
|
||||
cache_type_info: HashMap::new(),
|
||||
scratch_row_columns: Default::default(),
|
||||
|
||||
@ -69,6 +69,15 @@ impl FromStr for PgSslMode {
|
||||
/// postgresql://[user[:password]@][host][:port][/dbname][?param1=value1&...]
|
||||
/// ```
|
||||
///
|
||||
/// ## Parameters
|
||||
///
|
||||
/// |Parameter|Default|Description|
|
||||
/// |---------|-------|-----------|
|
||||
/// | `sslmode` | `prefer` | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated. See [`PgSqlSslMode`]. |
|
||||
/// | `sslrootcert` | `None` | Sets the name of a file containing a list of trusted SSL Certificate Authorities. |
|
||||
/// | `statement-cache-capacity` | `100` | The maximum number of prepared statements stored in the cache. Set to `0` to disable. |
|
||||
///
|
||||
///
|
||||
/// The URI scheme designator can be either `postgresql://` or `postgres://`.
|
||||
/// Each of the URI parts is optional.
|
||||
///
|
||||
@ -106,6 +115,8 @@ impl FromStr for PgSslMode {
|
||||
/// # })
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`PgSqlSslMode`]: enum.PgSslMode.html
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PgConnectOptions {
|
||||
pub(crate) host: String,
|
||||
@ -115,7 +126,7 @@ pub struct PgConnectOptions {
|
||||
pub(crate) database: Option<String>,
|
||||
pub(crate) ssl_mode: PgSslMode,
|
||||
pub(crate) ssl_root_cert: Option<PathBuf>,
|
||||
pub(crate) statement_cache_size: usize,
|
||||
pub(crate) statement_cache_capacity: usize,
|
||||
}
|
||||
|
||||
impl Default for PgConnectOptions {
|
||||
@ -163,7 +174,7 @@ impl PgConnectOptions {
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or_default(),
|
||||
statement_cache_size: 100,
|
||||
statement_cache_capacity: 100,
|
||||
}
|
||||
}
|
||||
|
||||
@ -288,14 +299,14 @@ impl PgConnectOptions {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the size of the connection's statement cache in a number of stored
|
||||
/// Sets the capacity of the connection's statement cache in a number of stored
|
||||
/// distinct statements. Caching is handled using LRU, meaning when the
|
||||
/// amount of queries hits the defined limit, the oldest statement will get
|
||||
/// dropped.
|
||||
///
|
||||
/// The default cache size is 100 statements.
|
||||
pub fn statement_cache_size(mut self, size: usize) -> Self {
|
||||
self.statement_cache_size = size;
|
||||
/// The default cache capacity is 100 statements.
|
||||
pub fn statement_cache_capacity(mut self, capacity: usize) -> Self {
|
||||
self.statement_cache_capacity = capacity;
|
||||
self
|
||||
}
|
||||
}
|
||||
@ -358,8 +369,8 @@ impl FromStr for PgConnectOptions {
|
||||
options = options.ssl_root_cert(&*value);
|
||||
}
|
||||
|
||||
"statement-cache-size" => {
|
||||
options = options.statement_cache_size(value.parse()?);
|
||||
"statement-cache-capacity" => {
|
||||
options = options.statement_cache_capacity(value.parse()?);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
|
||||
@ -89,7 +89,7 @@ pub(super) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
|
||||
Ok(SqliteConnection {
|
||||
handle,
|
||||
worker: StatementWorker::new(),
|
||||
statements: StatementCache::new(options.statement_cache_size),
|
||||
statements: StatementCache::new(options.statement_cache_capacity),
|
||||
statement: None,
|
||||
scratch_row_column_names: Default::default(),
|
||||
})
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use std::path::PathBuf;
|
||||
use std::{io, str::FromStr};
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::error::BoxDynError;
|
||||
|
||||
@ -10,7 +10,7 @@ use crate::error::BoxDynError;
|
||||
pub struct SqliteConnectOptions {
|
||||
pub(crate) filename: PathBuf,
|
||||
pub(crate) in_memory: bool,
|
||||
pub(crate) statement_cache_size: usize,
|
||||
pub(crate) statement_cache_capacity: usize,
|
||||
}
|
||||
|
||||
impl Default for SqliteConnectOptions {
|
||||
@ -24,9 +24,20 @@ impl SqliteConnectOptions {
|
||||
Self {
|
||||
filename: PathBuf::from(":memory:"),
|
||||
in_memory: false,
|
||||
statement_cache_size: 100,
|
||||
statement_cache_capacity: 100,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the capacity of the connection's statement cache in a number of stored
|
||||
/// distinct statements. Caching is handled using LRU, meaning when the
|
||||
/// amount of queries hits the defined limit, the oldest statement will get
|
||||
/// dropped.
|
||||
///
|
||||
/// The default cache capacity is 100 statements.
|
||||
pub fn statement_cache_capacity(mut self, capacity: usize) -> Self {
|
||||
self.statement_cache_capacity = capacity;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for SqliteConnectOptions {
|
||||
@ -36,7 +47,7 @@ impl FromStr for SqliteConnectOptions {
|
||||
let mut options = Self {
|
||||
filename: PathBuf::new(),
|
||||
in_memory: false,
|
||||
statement_cache_size: 100,
|
||||
statement_cache_capacity: 100,
|
||||
};
|
||||
|
||||
// remove scheme
|
||||
@ -44,26 +55,10 @@ impl FromStr for SqliteConnectOptions {
|
||||
.trim_start_matches("sqlite://")
|
||||
.trim_start_matches("sqlite:");
|
||||
|
||||
let mut splitted = s.split("?");
|
||||
|
||||
match splitted.next() {
|
||||
Some(":memory:") => options.in_memory = true,
|
||||
Some(s) => options.filename = s.parse()?,
|
||||
None => unreachable!(),
|
||||
}
|
||||
|
||||
match splitted.next().map(|s| s.split("=")) {
|
||||
Some(mut splitted) => {
|
||||
if splitted.next() == Some("statement-cache-size") {
|
||||
options.statement_cache_size = splitted
|
||||
.next()
|
||||
.ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidInput, "Invalid connection string")
|
||||
})?
|
||||
.parse()?
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
if s == ":memory:" {
|
||||
options.in_memory = true;
|
||||
} else {
|
||||
options.filename = s.parse()?;
|
||||
}
|
||||
|
||||
Ok(options)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user