sqlx/sqlx-postgres/src/statement.rs
Joey de Waal 469f22788e
breaking: add SqlStr (#3723)
* refactor: introduce `SqlSafeStr` API

* rebase main

* Add SqlStr + remove Statement lifetime

* Update the definition of Executor and AnyConnectionBackend + update Postgres driver

* Update MySql driver

* Update Sqlite driver

* remove debug clone count

* Reduce the amount of SqlStr clones

* improve QueryBuilder error message

* cargo fmt

* fix clippy warnings

* fix doc test

* Avoid panic in `QueryBuilder::reset`

* Use `QueryBuilder` when removing all test db's

* Add comment to `SqlStr`

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>

* Update sqlx-core/src/query_builder.rs

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>

* Add `Clone` as supertrait to `Statement`

* Move `Connection`, `AnyConnectionBackend` and `TransactionManager` to `SqlStr`

* Replace `sql_cloned` with `sql` in `Statement`

* Update `Executor` trait

* Update unit tests + QueryBuilder changes

* Remove code in comments

* Update comment in `QueryBuilder`

* Fix clippy warnings

* Update `Migrate` comment

* Small changes

* Move `Migration` to `SqlStr`

---------

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
2025-07-07 00:35:54 -07:00

84 lines
2.3 KiB
Rust

use super::{PgColumn, PgTypeInfo};
use crate::column::ColumnIndex;
use crate::error::Error;
use crate::ext::ustr::UStr;
use crate::{PgArguments, Postgres};
use std::sync::Arc;
use sqlx_core::sql_str::SqlStr;
pub(crate) use sqlx_core::statement::Statement;
use sqlx_core::{Either, HashMap};
#[derive(Debug, Clone)]
pub struct PgStatement {
pub(crate) sql: SqlStr,
pub(crate) metadata: Arc<PgStatementMetadata>,
}
#[derive(Debug, Default)]
pub(crate) struct PgStatementMetadata {
pub(crate) columns: Vec<PgColumn>,
// This `Arc` is not redundant; it's used to avoid deep-copying this map for the `Any` backend.
// See `sqlx-postgres/src/any.rs`
pub(crate) column_names: Arc<HashMap<UStr, usize>>,
pub(crate) parameters: Vec<PgTypeInfo>,
}
impl Statement for PgStatement {
type Database = Postgres;
fn into_sql(self) -> SqlStr {
self.sql
}
fn sql(&self) -> &SqlStr {
&self.sql
}
fn parameters(&self) -> Option<Either<&[PgTypeInfo], usize>> {
Some(Either::Left(&self.metadata.parameters))
}
fn columns(&self) -> &[PgColumn] {
&self.metadata.columns
}
impl_statement_query!(PgArguments);
}
impl ColumnIndex<PgStatement> for &'_ str {
fn index(&self, statement: &PgStatement) -> Result<usize, Error> {
statement
.metadata
.column_names
.get(*self)
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
.copied()
}
}
// #[cfg(feature = "any")]
// impl<'q> From<PgStatement<'q>> for crate::any::AnyStatement<'q> {
// #[inline]
// fn from(statement: PgStatement<'q>) -> Self {
// crate::any::AnyStatement::<'q> {
// columns: statement
// .metadata
// .columns
// .iter()
// .map(|col| col.clone().into())
// .collect(),
// column_names: statement.metadata.column_names.clone(),
// parameters: Some(Either::Left(
// statement
// .metadata
// .parameters
// .iter()
// .map(|ty| ty.clone().into())
// .collect(),
// )),
// sql: statement.sql,
// }
// }
// }