mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
refactor(postgres): remove from facade
the template changed a lot and it needs re-done
This commit is contained in:
parent
3415ce947c
commit
ae13a8d618
@ -1,2 +0,0 @@
|
||||
mod connection;
|
||||
mod options;
|
||||
@ -1,59 +0,0 @@
|
||||
use crate::blocking::{Close, Connect, Connection, Runtime};
|
||||
use crate::postgres::connection::PostgresConnection;
|
||||
use crate::{Blocking, Result};
|
||||
|
||||
impl PostgresConnection<Blocking> {
|
||||
/// Open a new database connection.
|
||||
///
|
||||
/// For detailed information, refer to the async version of
|
||||
/// this: [`connect`](#method.connect).
|
||||
///
|
||||
/// Implemented with [`Connect::connect`].
|
||||
#[inline]
|
||||
pub fn connect(url: &str) -> Result<Self> {
|
||||
sqlx_postgres::PostgresConnection::<Blocking>::connect(url).map(Self)
|
||||
}
|
||||
|
||||
/// Checks if a connection to the database is still valid.
|
||||
///
|
||||
/// For detailed information, refer to the async version of
|
||||
/// this: [`ping`](#method.ping).
|
||||
///
|
||||
/// Implemented with [`Connection::ping`].
|
||||
#[inline]
|
||||
pub fn ping(&mut self) -> Result<()> {
|
||||
self.0.ping()
|
||||
}
|
||||
|
||||
/// Explicitly close this database connection.
|
||||
///
|
||||
/// For detailed information, refer to the async version of
|
||||
/// this: [`close`](#method.close).
|
||||
///
|
||||
/// Implemented with [`Close::close`].
|
||||
#[inline]
|
||||
pub fn close(self) -> Result<()> {
|
||||
self.0.close()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Close<Rt> for PostgresConnection<Rt> {
|
||||
#[inline]
|
||||
fn close(self) -> Result<()> {
|
||||
self.0.close()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Connect<Rt> for PostgresConnection<Rt> {
|
||||
#[inline]
|
||||
fn connect(url: &str) -> Result<Self> {
|
||||
sqlx_postgres::PostgresConnection::<Rt>::connect(url).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Connection<Rt> for PostgresConnection<Rt> {
|
||||
#[inline]
|
||||
fn ping(&mut self) -> Result<()> {
|
||||
self.0.ping()
|
||||
}
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
use crate::blocking::{ConnectOptions, Runtime};
|
||||
use crate::postgres::{PostgresConnectOptions, PostgresConnection};
|
||||
use crate::{Blocking, Result};
|
||||
|
||||
impl PostgresConnectOptions<Blocking> {
|
||||
/// Open a new database connection with the configured connection options.
|
||||
///
|
||||
/// For detailed information, refer to the async version of
|
||||
/// this: [`connect`](#method.connect).
|
||||
///
|
||||
/// Implemented with [`ConnectOptions::connect`].
|
||||
#[inline]
|
||||
pub fn connect(&self) -> Result<PostgresConnection<Blocking>> {
|
||||
<sqlx_postgres::PostgresConnectOptions<Blocking> as ConnectOptions<Blocking>>::connect(&self.0)
|
||||
.map(PostgresConnection::<Blocking>)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> ConnectOptions<Rt> for PostgresConnectOptions<Rt> {
|
||||
#[inline]
|
||||
fn connect(&self) -> Result<Self::Connection>
|
||||
where
|
||||
Self::Connection: Sized,
|
||||
{
|
||||
<sqlx_postgres::PostgresConnectOptions<Rt> as ConnectOptions<Rt>>::connect(&self.0)
|
||||
.map(PostgresConnection::<Rt>)
|
||||
}
|
||||
}
|
||||
@ -1,93 +0,0 @@
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use futures_util::future::{BoxFuture, FutureExt};
|
||||
|
||||
use super::{Postgres, PostgresConnectOptions};
|
||||
#[cfg(feature = "async")]
|
||||
use crate::{Async, Result};
|
||||
use crate::{Close, Connect, Connection, DefaultRuntime, Runtime};
|
||||
|
||||
/// A single connection (also known as a session) to a MySQL database server.
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub struct PostgresConnection<Rt: Runtime = DefaultRuntime>(
|
||||
pub(super) sqlx_postgres::PostgresConnection<Rt>,
|
||||
);
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<Rt: Async> PostgresConnection<Rt> {
|
||||
/// Open a new database connection.
|
||||
///
|
||||
/// A value of [`PostgresConnectOptions`] is parsed from the provided
|
||||
/// connection `url`.
|
||||
///
|
||||
/// ```text
|
||||
/// postgres://[[user[:password]@]host][/database][?properties]
|
||||
/// ```
|
||||
///
|
||||
/// Implemented with [`Connect::connect`][crate::Connect::connect].
|
||||
pub async fn connect(url: &str) -> Result<Self> {
|
||||
sqlx_postgres::PostgresConnection::<Rt>::connect(url).await.map(Self)
|
||||
}
|
||||
|
||||
/// Checks if a connection to the database is still valid.
|
||||
///
|
||||
/// Implemented with [`Connection::ping`][crate::Connection::ping].
|
||||
pub async fn ping(&mut self) -> Result<()> {
|
||||
self.0.ping().await
|
||||
}
|
||||
|
||||
/// Explicitly close this database connection.
|
||||
///
|
||||
/// This method is **not required** for safe and consistent operation. However, it is
|
||||
/// recommended to call it instead of letting a connection `drop` as MySQL
|
||||
/// will be faster at cleaning up resources.
|
||||
///
|
||||
/// Implemented with [`Close::close`][crate::Close::close].
|
||||
pub async fn close(self) -> Result<()> {
|
||||
self.0.close().await
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Debug for PostgresConnection<Rt> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Close<Rt> for PostgresConnection<Rt> {
|
||||
#[cfg(feature = "async")]
|
||||
#[inline]
|
||||
fn close(self) -> BoxFuture<'static, Result<()>>
|
||||
where
|
||||
Rt: Async,
|
||||
{
|
||||
self.close().boxed()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Connect<Rt> for PostgresConnection<Rt> {
|
||||
type Options = PostgresConnectOptions<Rt>;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
#[inline]
|
||||
fn connect(url: &str) -> BoxFuture<'_, Result<Self>>
|
||||
where
|
||||
Rt: Async,
|
||||
{
|
||||
Self::connect(url).boxed()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Connection<Rt> for PostgresConnection<Rt> {
|
||||
type Database = Postgres;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
#[inline]
|
||||
fn ping(&mut self) -> BoxFuture<'_, Result<()>>
|
||||
where
|
||||
Rt: Async,
|
||||
{
|
||||
self.ping().boxed()
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
use sqlx_core::HasOutput;
|
||||
|
||||
use super::PostgresConnection;
|
||||
use crate::{Database, Runtime};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Postgres;
|
||||
|
||||
impl<Rt: Runtime> Database<Rt> for Postgres {
|
||||
type Connection = PostgresConnection<Rt>;
|
||||
}
|
||||
|
||||
impl<'x> HasOutput<'x> for Postgres {
|
||||
type Output = &'x mut Vec<u8>;
|
||||
}
|
||||
@ -1,95 +0,0 @@
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use futures_util::future::{BoxFuture, FutureExt};
|
||||
|
||||
use crate::postgres::PostgresConnection;
|
||||
#[cfg(feature = "async")]
|
||||
use crate::Async;
|
||||
use crate::{ConnectOptions, DefaultRuntime, Error, Result, Runtime};
|
||||
|
||||
mod builder;
|
||||
mod getters;
|
||||
|
||||
/// Options which can be used to configure how a MySQL connection is opened.
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub struct PostgresConnectOptions<Rt: Runtime = DefaultRuntime>(
|
||||
pub(super) sqlx_postgres::PostgresConnectOptions<Rt>,
|
||||
);
|
||||
|
||||
impl<Rt: Runtime> PostgresConnectOptions<Rt> {
|
||||
/// Creates a default set of connection options.
|
||||
///
|
||||
/// Implemented with [`Default`](#impl-Default).
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Parses connection options from a connection URL.
|
||||
///
|
||||
/// ```text
|
||||
/// postgres://[[user[:password]@]host][/database][?properties]
|
||||
/// ```
|
||||
///
|
||||
/// Implemented with [`FromStr`](#impl-FromStr).
|
||||
///
|
||||
#[inline]
|
||||
pub fn parse(url: &str) -> Result<Self> {
|
||||
Ok(Self(url.parse()?))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<Rt: Async> PostgresConnectOptions<Rt> {
|
||||
/// Open a new database connection with the configured connection options.
|
||||
///
|
||||
/// Implemented with [`ConnectOptions::connect`].
|
||||
#[inline]
|
||||
pub async fn connect(&self) -> Result<PostgresConnection<Rt>> {
|
||||
<sqlx_postgres::PostgresConnectOptions<Rt> as ConnectOptions<Rt>>::connect(&self.0)
|
||||
.await
|
||||
.map(PostgresConnection)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> ConnectOptions<Rt> for PostgresConnectOptions<Rt> {
|
||||
type Connection = PostgresConnection<Rt>;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
#[inline]
|
||||
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection>>
|
||||
where
|
||||
Self::Connection: Sized,
|
||||
Rt: Async,
|
||||
{
|
||||
self.connect().boxed()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Debug for PostgresConnectOptions<Rt> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Default for PostgresConnectOptions<Rt> {
|
||||
fn default() -> Self {
|
||||
Self(sqlx_postgres::PostgresConnectOptions::<Rt>::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> Clone for PostgresConnectOptions<Rt> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Rt: Runtime> FromStr for PostgresConnectOptions<Rt> {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(url: &str) -> Result<Self> {
|
||||
Ok(Self(url.parse()?))
|
||||
}
|
||||
}
|
||||
@ -1,74 +0,0 @@
|
||||
use std::path::Path;
|
||||
|
||||
use super::PostgresConnectOptions;
|
||||
use crate::Runtime;
|
||||
|
||||
impl<Rt: Runtime> PostgresConnectOptions<Rt> {
|
||||
/// Sets the hostname of the database server.
|
||||
///
|
||||
/// If the hostname begins with a slash (`/`), it is interpreted as the absolute path
|
||||
/// to a Unix domain socket file instead of a hostname of a server.
|
||||
///
|
||||
/// Defaults to `localhost`.
|
||||
///
|
||||
#[inline]
|
||||
pub fn host(&mut self, host: impl AsRef<str>) -> &mut Self {
|
||||
self.0.host(host);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the path of the Unix domain socket to connect to.
|
||||
///
|
||||
/// Overrides [`host()`](#method.host) and [`port()`](#method.port).
|
||||
///
|
||||
#[inline]
|
||||
pub fn socket(&mut self, socket: impl AsRef<Path>) -> &mut Self {
|
||||
self.0.socket(socket);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the TCP port number of the database server.
|
||||
///
|
||||
/// Defaults to `3306`.
|
||||
///
|
||||
#[inline]
|
||||
pub fn port(&mut self, port: u16) -> &mut Self {
|
||||
self.0.port(port);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the username to be used for authentication.
|
||||
// FIXME: Specify what happens when you do NOT set this
|
||||
pub fn username(&mut self, username: impl AsRef<str>) -> &mut Self {
|
||||
self.0.username(username);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the password to be used for authentication.
|
||||
#[inline]
|
||||
pub fn password(&mut self, password: impl AsRef<str>) -> &mut Self {
|
||||
self.0.password(password);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the default database for the connection.
|
||||
#[inline]
|
||||
pub fn database(&mut self, database: impl AsRef<str>) -> &mut Self {
|
||||
self.0.database(database);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the character set for the connection.
|
||||
#[inline]
|
||||
pub fn charset(&mut self, charset: impl AsRef<str>) -> &mut Self {
|
||||
self.0.charset(charset);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the timezone for the connection.
|
||||
#[inline]
|
||||
pub fn timezone(&mut self, timezone: impl AsRef<str>) -> &mut Self {
|
||||
self.0.timezone(timezone);
|
||||
self
|
||||
}
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
use std::path::Path;
|
||||
|
||||
use super::PostgresConnectOptions;
|
||||
use crate::Runtime;
|
||||
|
||||
impl<Rt: Runtime> PostgresConnectOptions<Rt> {
|
||||
/// Returns the hostname of the database server.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_host(&self) -> &str {
|
||||
self.0.get_host()
|
||||
}
|
||||
|
||||
/// Returns the TCP port number of the database server.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_port(&self) -> u16 {
|
||||
self.0.get_port()
|
||||
}
|
||||
|
||||
/// Returns the path to the Unix domain socket, if one is configured.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_socket(&self) -> Option<&Path> {
|
||||
self.0.get_socket()
|
||||
}
|
||||
|
||||
/// Returns the default database name.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_database(&self) -> Option<&str> {
|
||||
self.0.get_database()
|
||||
}
|
||||
|
||||
/// Returns the username to be used for authentication.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_username(&self) -> Option<&str> {
|
||||
self.0.get_username()
|
||||
}
|
||||
|
||||
/// Returns the password to be used for authentication.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_password(&self) -> Option<&str> {
|
||||
self.0.get_password()
|
||||
}
|
||||
|
||||
/// Returns the character set for the connection.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_charset(&self) -> &str {
|
||||
self.0.get_charset()
|
||||
}
|
||||
|
||||
/// Returns the timezone for the connection.
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn get_timezone(&self) -> &str {
|
||||
self.0.get_timezone()
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user