mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-26 10:09:50 +00:00
"prepare" -> "describe" and use unnamed statement
This commit is contained in:
parent
5cbd3f9e20
commit
d3df2dd210
@ -120,7 +120,7 @@ where
|
||||
eprintln!("connection established");
|
||||
|
||||
let prepared = conn
|
||||
.prepare(&input.sql)
|
||||
.describe(&input.sql)
|
||||
.await
|
||||
.map_err(|e| parse::Error::new(input.sql_span, e))?;
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ use crate::{
|
||||
error::Error,
|
||||
executor::Executor,
|
||||
pool::{Live, SharedPool},
|
||||
prepared::PreparedStatement,
|
||||
describe::Describe,
|
||||
query::{IntoQueryParameters, QueryParameters},
|
||||
row::FromSqlRow,
|
||||
};
|
||||
@ -78,10 +78,10 @@ pub trait RawConnection: Send {
|
||||
query: &str,
|
||||
) -> crate::Result<<Self::Backend as Backend>::StatementIdent>;
|
||||
|
||||
async fn prepare_describe(
|
||||
async fn describe(
|
||||
&mut self,
|
||||
query: &str,
|
||||
) -> crate::Result<PreparedStatement<Self::Backend>>;
|
||||
) -> crate::Result<Describe<Self::Backend>>;
|
||||
}
|
||||
|
||||
pub struct Connection<DB>(Arc<SharedConnection<DB>>)
|
||||
@ -136,7 +136,7 @@ where
|
||||
///
|
||||
/// UNSTABLE: for use by sqlx-macros only
|
||||
#[doc(hidden)]
|
||||
pub async fn prepare(&self, body: &str) -> crate::Result<PreparedStatement<DB>> {
|
||||
pub async fn describe(&self, body: &str) -> crate::Result<Describe<DB>> {
|
||||
let mut live = self.0.acquire().await;
|
||||
let ret = live.raw.prepare_describe(body).await?;
|
||||
self.0.release(live);
|
||||
|
||||
@ -2,10 +2,9 @@ use crate::Backend;
|
||||
|
||||
use crate::types::HasTypeMetadata;
|
||||
|
||||
/// A prepared statement.
|
||||
pub struct PreparedStatement<DB: Backend> {
|
||||
/// The result of running prepare + describe for the given backend.
|
||||
pub struct Describe<DB: Backend> {
|
||||
///
|
||||
pub identifier: <DB as Backend>::StatementIdent,
|
||||
/// The expected type IDs of bind parameters.
|
||||
pub param_types: Vec<<DB as HasTypeMetadata>::TypeId>,
|
||||
///
|
||||
@ -26,7 +26,7 @@ pub mod serialize;
|
||||
mod sql;
|
||||
pub mod types;
|
||||
|
||||
mod prepared;
|
||||
mod describe;
|
||||
|
||||
mod compiled;
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ use crate::{
|
||||
},
|
||||
MariaDb, MariaDbQueryParameters, MariaDbRow,
|
||||
},
|
||||
prepared::{Column, PreparedStatement},
|
||||
describe::{Column, Describe},
|
||||
Backend, Error, PreparedStatement, Result,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
@ -330,25 +330,7 @@ impl RawConnection for MariaDbRawConnection {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
async fn prepare(&mut self, query: &str) -> crate::Result<u32> {
|
||||
let prepare_ok = self.send_prepare(query).await?;
|
||||
|
||||
for _ in 0..prepare_ok.params {
|
||||
let _ = self.receive().await?;
|
||||
}
|
||||
|
||||
self.check_eof().await?;
|
||||
|
||||
for _ in 0..prepare_ok.columns {
|
||||
let _ = self.receive().await?;
|
||||
}
|
||||
|
||||
self.check_eof().await?;
|
||||
|
||||
Ok(prepare_ok.statement_id)
|
||||
}
|
||||
|
||||
async fn prepare_describe(&mut self, query: &str) -> crate::Result<PreparedStatement<MariaDb>> {
|
||||
async fn describe(&mut self, query: &str) -> crate::Result<Describe<MariaDb>> {
|
||||
let prepare_ok = self.send_prepare(query).await?;
|
||||
|
||||
let mut param_types = Vec::with_capacity(prepare_ok.params as usize);
|
||||
@ -373,8 +355,7 @@ impl RawConnection for MariaDbRawConnection {
|
||||
|
||||
self.check_eof().await?;
|
||||
|
||||
Ok(PreparedStatement {
|
||||
identifier: prepare_ok.statement_id,
|
||||
Ok(Describe {
|
||||
param_types,
|
||||
columns,
|
||||
})
|
||||
|
||||
@ -2,7 +2,7 @@ use super::{Postgres, PostgresQueryParameters, PostgresRawConnection, PostgresRo
|
||||
use crate::{
|
||||
connection::RawConnection,
|
||||
postgres::{error::ProtocolError, raw::Step},
|
||||
prepared::{Column, PreparedStatement},
|
||||
describe::{Column, Describe},
|
||||
query::QueryParameters,
|
||||
url::Url,
|
||||
};
|
||||
@ -104,24 +104,9 @@ impl RawConnection for PostgresRawConnection {
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
async fn prepare(&mut self, body: &str) -> crate::Result<String> {
|
||||
let name = gen_statement_name(body);
|
||||
self.parse(&name, body, &PostgresQueryParameters::new());
|
||||
|
||||
match self.receive().await? {
|
||||
Some(Message::Response(response)) => Err(PostgresDatabaseError(response).into()),
|
||||
Some(Message::ParseComplete) => Ok(name),
|
||||
Some(message) => {
|
||||
Err(ProtocolError(format!("unexpected message: {:?}", message)).into())
|
||||
}
|
||||
None => Err(ProtocolError("expected ParseComplete or ErrorResponse").into()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn prepare_describe(&mut self, body: &str) -> crate::Result<PreparedStatement<Postgres>> {
|
||||
let name = gen_statement_name(body);
|
||||
self.parse(&name, body, &PostgresQueryParameters::new());
|
||||
self.describe(&name);
|
||||
async fn describe(&mut self, body: &str) -> crate::Result<Describe<Postgres>> {
|
||||
self.parse("", body, &PostgresQueryParameters::new());
|
||||
self.describe("");
|
||||
self.sync().await?;
|
||||
|
||||
let param_desc = loop {
|
||||
@ -146,8 +131,7 @@ impl RawConnection for PostgresRawConnection {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(PreparedStatement {
|
||||
identifier: name,
|
||||
Ok(Describe {
|
||||
param_types: param_desc.ids.into_vec(),
|
||||
columns: row_desc
|
||||
.fields
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user