Skip to content

Commit ffb29b0

Browse files
committed
Use Cow<'static, str> instead of String
1 parent dbd2fc6 commit ffb29b0

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

sqlx-core/src/sqlite/options/connect.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ impl ConnectOptions for SqliteConnectOptions {
1818
let mut conn = establish(self).await?;
1919

2020
// send an initial sql statement comprised of options
21+
let mut init = String::new();
22+
2123
for (key, value) in self.pragmas.iter() {
22-
conn.execute(&*format!("PRAGMA {} = {};", key, value)).await?;
24+
init += &format!("PRAGMA {} = {}; ", key, value);
2325
}
2426

27+
conn.execute(&*init).await?;
28+
2529
Ok(conn)
2630
})
2731
}

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct SqliteConnectOptions {
5959
pub(crate) statement_cache_capacity: usize,
6060
pub(crate) busy_timeout: Duration,
6161
pub(crate) log_settings: LogSettings,
62-
pub(crate) pragmas: IndexMap<String, String>,
62+
pub(crate) pragmas: IndexMap<Cow<'static, str>, Cow<'static, str>>,
6363
}
6464

6565
impl Default for SqliteConnectOptions {
@@ -71,14 +71,14 @@ impl Default for SqliteConnectOptions {
7171
impl SqliteConnectOptions {
7272
pub fn new() -> Self {
7373
// set default pragmas
74-
let mut pragmas = IndexMap::new();
74+
let mut pragmas: IndexMap<Cow<'static, str>, Cow<'static, str>> = IndexMap::new();
7575

7676
let locking_mode: SqliteLockingMode = Default::default();
7777
let auto_vacuum: SqliteAutoVacuum = Default::default();
7878

7979
// page_size must be set before any other action on the database.
80-
pragmas.insert("page_size".into(), 4096.to_string());
81-
80+
pragmas.insert("page_size".into(), "4096".into());
81+
8282
// Note that locking_mode should be set before journal_mode; see
8383
// https://www.sqlite.org/wal.html#use_of_wal_without_shared_memory .
8484
pragmas.insert("locking_mode".into(), locking_mode.as_str().into());
@@ -209,17 +209,21 @@ impl SqliteConnectOptions {
209209
self
210210
}
211211

212-
/// Sets custom initial pragmas for the database connection.
213-
pub fn pragma(mut self, key: &str, value: &str) -> Self {
214-
self.pragmas.insert(key.into(), value.into());
215-
self
216-
}
217-
218212
/// Sets the [page_size](https://www.sqlite.org/pragma.html#pragma_page_size) setting for the database connection.
219213
///
220214
/// The default page_size setting is 4096.
221215
pub fn page_size(mut self, page_size: u32) -> Self {
222-
self.pragmas.insert("page_size".into(), page_size.to_string());
216+
self.pragmas
217+
.insert("page_size".into(), page_size.to_string().into());
218+
self
219+
}
220+
221+
/// Sets custom initial pragma for the database connection.
222+
pub fn pragma<T>(mut self, key: T, value: T) -> Self
223+
where
224+
T: Into<Cow<'static, str>>,
225+
{
226+
self.pragmas.insert(key.into(), value.into());
223227
self
224228
}
225229
}

0 commit comments

Comments
 (0)