sqlx_core_oldapi/
database.rs

1//! Traits to represent a database driver.
2//!
3//! # Support
4//!
5//! ## Tier 1
6//!
7//! Tier 1 support can be thought of as "guaranteed to work". Automated testing is setup to
8//! ensure a high level of stability and functionality.
9//!
10//! | Database | Version | Driver |
11//! | - | - | - |
12//! | [MariaDB] | 10.1+ | [`mysql`] |
13//! | [Microsoft SQL Server] | 2019 | [`mssql`] |
14//! | [MySQL] | 5.6, 5.7, 8.0 | [`mysql`] |
15//! | [PostgreSQL] | 9.5+ | [`postgres`] |
16//! | [SQLite] | 3.20.1+ | [`sqlite`] |
17//!
18//! [MariaDB]: https://mariadb.com/
19//! [MySQL]: https://www.mysql.com/
20//! [Microsoft SQL Server]: https://www.microsoft.com/en-us/sql-server
21//! [PostgreSQL]: https://www.postgresql.org/
22//! [SQLite]: https://www.sqlite.org/
23//!
24//! [`mysql`]: crate::mysql
25//! [`postgres`]: crate::postgres
26//! [`mssql`]: crate::mssql
27//! [`sqlite`]: crate::sqlite
28//!
29//! ## Tier 2
30//!
31//! Tier 2 support can be thought as "should work". No specific automated testing is done,
32//! at this time, but there are efforts to ensure compatibility. Tier 2 support also includes
33//! database distributions that provide protocols that closely match a database from Tier 1.
34//!
35//! _No databases are in tier 2 at this time._
36//!
37//! # `Any`
38//!
39//! Selecting a database driver is, by default, a compile-time decision. SQLx is designed this way
40//! to take full advantage of the performance and type safety made available by Rust.
41//!
42//! We recognize that you may wish to make a runtime decision to decide the database driver. The
43//! [`Any`](crate::any) driver is provided for that purpose.
44//!
45//! ## Example
46//!
47//! ```rust,ignore
48//! // connect to SQLite
49//! let conn = AnyConnection::connect("sqlite://file.db").await?;
50//!
51//! // connect to Postgres, no code change
52//! // required, decided by the scheme of the URL
53//! let conn = AnyConnection::connect("postgres://localhost/sqlx").await?;
54//! ```
55
56use std::fmt::Debug;
57
58use crate::arguments::Arguments;
59use crate::column::Column;
60use crate::connection::Connection;
61use crate::row::Row;
62use crate::statement::Statement;
63use crate::transaction::TransactionManager;
64use crate::type_info::TypeInfo;
65use crate::value::{Value, ValueRef};
66
67/// A database driver.
68///
69/// This trait encapsulates a complete set of traits that implement a driver for a
70/// specific database (e.g., MySQL, PostgreSQL).
71pub trait Database:
72    'static
73    + Sized
74    + Send
75    + Debug
76    + for<'r> HasValueRef<'r, Database = Self>
77    + for<'q> HasArguments<'q, Database = Self>
78    + for<'q> HasStatement<'q, Database = Self>
79{
80    /// The concrete `Connection` implementation for this database.
81    type Connection: Connection<Database = Self>;
82
83    /// The concrete `TransactionManager` implementation for this database.
84    type TransactionManager: TransactionManager<Database = Self>;
85
86    /// The concrete `Row` implementation for this database.
87    type Row: Row<Database = Self>;
88
89    /// The concrete `QueryResult` implementation for this database.
90    type QueryResult: 'static + Sized + Send + Sync + Default + Extend<Self::QueryResult>;
91
92    /// The concrete `Column` implementation for this database.
93    type Column: Column<Database = Self>;
94
95    /// The concrete `TypeInfo` implementation for this database.
96    type TypeInfo: TypeInfo;
97
98    /// The concrete type used to hold an owned copy of the not-yet-decoded value that was
99    /// received from the database.
100    type Value: Value<Database = Self> + 'static;
101}
102
103/// Associate [`Database`] with a [`ValueRef`](crate::value::ValueRef) of a generic lifetime.
104///
105/// ---
106///
107/// The upcoming Rust feature, [Generic Associated Types], should obviate
108/// the need for this trait.
109///
110/// [Generic Associated Types]: https://github.com/rust-lang/rust/issues/44265
111pub trait HasValueRef<'r> {
112    type Database: Database;
113
114    /// The concrete type used to hold a reference to the not-yet-decoded value that has just been
115    /// received from the database.
116    type ValueRef: ValueRef<'r, Database = Self::Database>;
117}
118
119/// Associate [`Database`] with an [`Arguments`](crate::arguments::Arguments) of a generic lifetime.
120///
121/// ---
122///
123/// The upcoming Rust feature, [Generic Associated Types], should obviate
124/// the need for this trait.
125///
126/// [Generic Associated Types]: https://github.com/rust-lang/rust/issues/44265
127pub trait HasArguments<'q> {
128    type Database: Database;
129
130    /// The concrete `Arguments` implementation for this database.
131    type Arguments: Arguments<'q, Database = Self::Database>;
132
133    /// The concrete type used as a buffer for arguments while encoding.
134    type ArgumentBuffer;
135}
136
137/// Associate [`Database`] with a [`Statement`](crate::statement::Statement) of a generic lifetime.
138///
139/// ---
140///
141/// The upcoming Rust feature, [Generic Associated Types], should obviate
142/// the need for this trait.
143///
144/// [Generic Associated Types]: https://github.com/rust-lang/rust/issues/44265
145pub trait HasStatement<'q> {
146    type Database: Database;
147
148    /// The concrete `Statement` implementation for this database.
149    type Statement: Statement<'q, Database = Self::Database>;
150}
151
152/// A [`Database`] that maintains a client-side cache of prepared statements.
153pub trait HasStatementCache {}