Skip to content

Commit 2ab2f2e

Browse files
committed
Reduce the amount of SqlStr clones
1 parent a4aa4f6 commit 2ab2f2e

File tree

9 files changed

+35
-23
lines changed

9 files changed

+35
-23
lines changed

sqlx-core/src/any/statement.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ impl Statement for AnyStatement {
3232
}
3333
}
3434

35-
fn sql(&self) -> SqlStr {
35+
fn sql_cloned(&self) -> SqlStr {
3636
self.sql.clone()
3737
}
3838

39+
fn into_sql(self) -> SqlStr {
40+
self.sql
41+
}
42+
3943
fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
4044
match &self.parameters {
4145
Some(Either::Left(types)) => Some(Either::Left(types)),
@@ -64,8 +68,7 @@ impl<'i> ColumnIndex<AnyStatement> for &'i str {
6468
impl<'q> AnyStatement {
6569
#[doc(hidden)]
6670
pub fn try_from_statement<S>(
67-
query: SqlStr,
68-
statement: &S,
71+
statement: S,
6972
column_names: Arc<HashMap<UStr, usize>>,
7073
) -> crate::Result<Self>
7174
where
@@ -91,7 +94,7 @@ impl<'q> AnyStatement {
9194
.collect::<Result<Vec<_>, _>>()?;
9295

9396
Ok(Self {
94-
sql: query,
97+
sql: statement.into_sql(),
9598
columns,
9699
column_names,
97100
parameters,

sqlx-core/src/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ where
4747
#[inline]
4848
fn sql(self) -> SqlStr {
4949
match self.statement {
50-
Either::Right(statement) => statement.sql(),
50+
Either::Right(statement) => statement.sql_cloned(),
5151
Either::Left(sql) => sql,
5252
}
5353
}

sqlx-core/src/statement.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ pub trait Statement: Send + Sync {
2525
fn to_owned(&self) -> <Self::Database as Database>::Statement;
2626

2727
/// Get the original SQL text used to create this statement.
28-
fn sql(&self) -> SqlStr;
28+
fn sql_cloned(&self) -> SqlStr;
29+
30+
fn into_sql(self) -> SqlStr;
2931

3032
/// Get the expected parameters for this statement.
3133
///

sqlx-mysql/src/any.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,9 @@ impl AnyConnectionBackend for MySqlConnection {
132132
_parameters: &[AnyTypeInfo],
133133
) -> BoxFuture<'c, sqlx_core::Result<AnyStatement>> {
134134
Box::pin(async move {
135-
let statement = Executor::prepare_with(self, sql.clone(), &[]).await?;
136-
AnyStatement::try_from_statement(
137-
sql,
138-
&statement,
139-
statement.metadata.column_names.clone(),
140-
)
135+
let statement = Executor::prepare_with(self, sql, &[]).await?;
136+
let column_names = statement.metadata.column_names.clone();
137+
AnyStatement::try_from_statement(statement, column_names)
141138
})
142139
}
143140

sqlx-mysql/src/statement.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ impl Statement for MySqlStatement {
3333
}
3434
}
3535

36-
fn sql(&self) -> SqlStr {
36+
fn sql_cloned(&self) -> SqlStr {
3737
self.sql.clone()
3838
}
3939

40+
fn into_sql(self) -> SqlStr {
41+
self.sql
42+
}
43+
4044
fn parameters(&self) -> Option<Either<&[MySqlTypeInfo], usize>> {
4145
Some(Either::Right(self.metadata.parameters))
4246
}

sqlx-postgres/src/any.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,9 @@ impl AnyConnectionBackend for PgConnection {
132132
_parameters: &[AnyTypeInfo],
133133
) -> BoxFuture<'c, sqlx_core::Result<AnyStatement>> {
134134
Box::pin(async move {
135-
let statement = Executor::prepare_with(self, sql.clone(), &[]).await?;
136-
AnyStatement::try_from_statement(
137-
sql,
138-
&statement,
139-
statement.metadata.column_names.clone(),
140-
)
135+
let statement = Executor::prepare_with(self, sql, &[]).await?;
136+
let colunn_names = statement.metadata.column_names.clone();
137+
AnyStatement::try_from_statement(statement, colunn_names)
141138
})
142139
}
143140

sqlx-postgres/src/statement.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ impl Statement for PgStatement {
3434
}
3535
}
3636

37-
fn sql(&self) -> SqlStr {
37+
fn sql_cloned(&self) -> SqlStr {
3838
self.sql.clone()
3939
}
4040

41+
fn into_sql(self) -> SqlStr {
42+
self.sql
43+
}
44+
4145
fn parameters(&self) -> Option<Either<&[PgTypeInfo], usize>> {
4246
Some(Either::Left(&self.metadata.parameters))
4347
}

sqlx-sqlite/src/any.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ impl AnyConnectionBackend for SqliteConnection {
128128
_parameters: &[AnyTypeInfo],
129129
) -> BoxFuture<'c, sqlx_core::Result<AnyStatement>> {
130130
Box::pin(async move {
131-
let statement = Executor::prepare_with(self, sql.clone(), &[]).await?;
132-
AnyStatement::try_from_statement(sql, &statement, statement.column_names.clone())
131+
let statement = Executor::prepare_with(self, sql, &[]).await?;
132+
let column_names = statement.column_names.clone();
133+
AnyStatement::try_from_statement(statement, column_names)
133134
})
134135
}
135136

sqlx-sqlite/src/statement/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ impl Statement for SqliteStatement {
3636
}
3737
}
3838

39-
fn sql(&self) -> SqlStr {
39+
fn sql_cloned(&self) -> SqlStr {
4040
self.sql.clone()
4141
}
4242

43+
fn into_sql(self) -> SqlStr {
44+
self.sql
45+
}
46+
4347
fn parameters(&self) -> Option<Either<&[SqliteTypeInfo], usize>> {
4448
Some(Either::Right(self.parameters))
4549
}

0 commit comments

Comments
 (0)