move HasX types into the module where X is

This commit is contained in:
Ryan Leckey 2020-03-25 01:44:00 -07:00
parent 63aa3e8f0f
commit 918a797581
10 changed files with 75 additions and 93 deletions

View file

@ -2,9 +2,10 @@
use futures_core::future::BoxFuture;
use crate::database::{Database, HasRow};
use crate::database::Database;
use crate::executor::Execute;
use crate::pool::Pool;
use crate::row::HasRow;
/// Represents a result set, which is generated by executing a query against the database.
///
@ -70,3 +71,18 @@ where
pub(crate) mod private {
pub trait Sealed {}
}
/// Associate [`Database`] with a [`Cursor`] of a generic lifetime.
///
/// ---
///
/// The upcoming Rust feature, [Generic Associated Types], should obviate
/// the need for this trait.
///
/// [Generic Associated Types]: https://www.google.com/search?q=generic+associated+types+rust&oq=generic+associated+types+rust&aqs=chrome..69i57j0l5.3327j0j7&sourceid=chrome&ie=UTF-8
pub trait HasCursor<'c, 'q> {
type Database: Database;
/// The concrete `Cursor` implementation for this database.
type Cursor: Cursor<'c, 'q, Database = Self::Database>;
}

View file

@ -2,10 +2,11 @@ use std::fmt::{Debug, Display};
use crate::arguments::Arguments;
use crate::connection::Connect;
use crate::cursor::Cursor;
use crate::cursor::HasCursor;
use crate::error::DatabaseError;
use crate::row::Row;
use crate::row::HasRow;
use crate::types::TypeInfo;
use crate::value::HasRawValue;
/// A database driver.
///
@ -13,11 +14,10 @@ use crate::types::TypeInfo;
/// database (e.g., **MySQL**, **Postgres**).
pub trait Database
where
Self: Sized + Send + 'static,
Self: Debug + Sized + Send + 'static,
Self: for<'c> HasRow<'c, Database = Self>,
Self: for<'c> HasRawValue<'c>,
Self: for<'c> HasRawValue<'c, Database = Self>,
Self: for<'c, 'q> HasCursor<'c, 'q, Database = Self>,
Self: Debug,
{
/// The concrete `Connection` implementation for this database.
type Connection: Connect<Database = Self>;
@ -33,55 +33,10 @@ where
/// The Rust type used as the buffer when encoding arguments.
///
/// For example, **Postgres** and **MySQL** use `Vec<u8>`; however, **SQLite** uses `Vec<SqliteArgumentValue>`.
/// For example, **Postgres** and **MySQL** use `Vec<u8>`;
/// however, **SQLite** uses `Vec<SqliteArgumentValue>`.
type RawBuffer: Default;
/// The concrete `DatabaseError` type used to report errors from the database.
type Error: DatabaseError + Send + Sync;
}
/// Associate [`Database`] with a `RawValue` of a generic lifetime.
///
/// ---
///
/// The upcoming Rust feature, [Generic Associated Types], should obviate
/// the need for this trait.
///
/// [Generic Associated Types]: https://www.google.com/search?q=generic+associated+types+rust&oq=generic+associated+types+rust&aqs=chrome..69i57j0l5.3327j0j7&sourceid=chrome&ie=UTF-8
pub trait HasRawValue<'c> {
/// The Rust type used to hold a not-yet-decoded value that has just been
/// received from the database.
///
/// For example, **Postgres** and **MySQL** use `&'c [u8]`; however, **SQLite** uses `SqliteValue<'c>`.
type RawValue;
}
/// Associate [`Database`] with a [`Cursor`] of a generic lifetime.
///
/// ---
///
/// The upcoming Rust feature, [Generic Associated Types], should obviate
/// the need for this trait.
///
/// [Generic Associated Types]: https://www.google.com/search?q=generic+associated+types+rust&oq=generic+associated+types+rust&aqs=chrome..69i57j0l5.3327j0j7&sourceid=chrome&ie=UTF-8
pub trait HasCursor<'c, 'q> {
type Database: Database;
/// The concrete `Cursor` implementation for this database.
type Cursor: Cursor<'c, 'q, Database = Self::Database>;
}
/// Associate [`Database`] with a [`Row`] of a generic lifetime.
///
/// ---
///
/// The upcoming Rust feature, [Generic Associated Types], should obviate
/// the need for this trait.
///
/// [Generic Associated Types]: https://www.google.com/search?q=generic+associated+types+rust&oq=generic+associated+types+rust&aqs=chrome..69i57j0l5.3327j0j7&sourceid=chrome&ie=UTF-8
pub trait HasRow<'c> {
type Database: Database;
/// The concrete `Row` implementation for this database.
type Row: Row<'c, Database = Self::Database>;
}

View file

@ -1,6 +1,7 @@
//! Types and traits for decoding values from the database.
use crate::database::{Database, HasRawValue};
use crate::database::Database;
use crate::value::HasRawValue;
/// A type that can be decoded from the database.
pub trait Decode<'de, DB>

View file

@ -1,6 +1,7 @@
use futures_core::future::BoxFuture;
use crate::database::{Database, HasCursor};
use crate::cursor::HasCursor;
use crate::database::Database;
use crate::describe::Describe;
/// A type that contains or can provide a database connection to use for executing queries

View file

@ -25,6 +25,7 @@ mod io;
pub mod connection;
pub mod cursor;
pub mod database;
pub mod value;
#[macro_use]
pub mod executor;

View file

@ -2,8 +2,8 @@ use futures_core::future::BoxFuture;
use super::PoolConnection;
use crate::connection::Connect;
use crate::cursor::Cursor;
use crate::database::{Database, HasCursor};
use crate::cursor::{Cursor, HasCursor};
use crate::database::Database;
use crate::describe::Describe;
use crate::executor::Execute;
use crate::executor::{Executor, RefExecutor};

View file

@ -6,10 +6,11 @@ use futures_util::future::ready;
use futures_util::TryFutureExt;
use crate::arguments::Arguments;
use crate::cursor::Cursor;
use crate::database::{Database, HasCursor, HasRow};
use crate::cursor::{Cursor, HasCursor};
use crate::database::Database;
use crate::encode::Encode;
use crate::executor::{Execute, Executor, RefExecutor};
use crate::row::HasRow;
use crate::types::Type;
/// Raw SQL query with bind parameters. Returned by [`query`][crate::query::query].

View file

@ -1,8 +1,9 @@
//! Contains the `ColumnIndex`, `Row`, and `FromRow` traits.
use crate::database::{Database, HasRawValue};
use crate::database::Database;
use crate::decode::Decode;
use crate::types::Type;
use crate::value::HasRawValue;
/// A type that can be used to index into a [`Row`].
///
@ -149,6 +150,21 @@ pub(crate) mod private_row {
pub trait Sealed {}
}
/// Associate [`Database`] with a [`Row`] of a generic lifetime.
///
/// ---
///
/// The upcoming Rust feature, [Generic Associated Types], should obviate
/// the need for this trait.
///
/// [Generic Associated Types]: https://www.google.com/search?q=generic+associated+types+rust&oq=generic+associated+types+rust&aqs=chrome..69i57j0l5.3327j0j7&sourceid=chrome&ie=UTF-8
pub trait HasRow<'c> {
type Database: Database;
/// The concrete `Row` implementation for this database.
type Row: Row<'c, Database = Self::Database>;
}
/// A record that can be built from a row returned by the database.
///
/// In order to use [`query_as`] the output type must implement `FromRow`.
@ -292,38 +308,6 @@ macro_rules! impl_map_row_for_row {
};
}
#[allow(unused_macros)]
macro_rules! impl_column_index_for_row {
($R:ident) => {
impl<'c> crate::row::ColumnIndex<'c, $R<'c>> for usize {
fn index(
&self,
row: &$R<'c>,
) -> crate::Result<<$R<'c> as crate::row::Row<'c>>::Database, usize> {
let len = crate::row::Row::len(row);
if *self >= len {
return Err(crate::Error::ColumnIndexOutOfBounds { len, index: *self });
}
Ok(*self)
}
}
impl<'c> crate::row::ColumnIndex<'c, $R<'c>> for str {
fn index(
&self,
row: &$R<'c>,
) -> crate::Result<<$R<'c> as crate::row::Row<'c>>::Database, usize> {
row.columns
.get(self)
.ok_or_else(|| crate::Error::ColumnNotFound((*self).into()))
.map(|&index| index as usize)
}
}
};
}
#[allow(unused_macros)]
macro_rules! impl_from_row_for_row {
($R:ident) => {

View file

@ -3,8 +3,8 @@ use std::ops::{Deref, DerefMut};
use futures_core::future::BoxFuture;
use crate::connection::Connection;
use crate::cursor::HasCursor;
use crate::database::Database;
use crate::database::HasCursor;
use crate::describe::Describe;
use crate::executor::{Execute, Executor, RefExecutor};
use crate::runtime::spawn;

23
sqlx-core/src/value.rs Normal file
View file

@ -0,0 +1,23 @@
use crate::database::Database;
/// Associate [`Database`] with a `RawValue` of a generic lifetime.
///
/// ---
///
/// The upcoming Rust feature, [Generic Associated Types], should obviate
/// the need for this trait.
///
/// [Generic Associated Types]: https://www.google.com/search?q=generic+associated+types+rust&oq=generic+associated+types+rust&aqs=chrome..69i57j0l5.3327j0j7&sourceid=chrome&ie=UTF-8
pub trait HasRawValue<'c> {
type Database: Database;
/// The Rust type used to hold a not-yet-decoded value that has just been
/// received from the database.
type RawValue: RawValue<'c, Database = Self::Database>;
}
pub trait RawValue<'c> {
type Database: Database;
fn type_info(&self) -> <Self::Database as Database>::TypeInfo;
}