| 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`] (Pending a full rewrite) | |
| 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 | |
| 56 | use std::fmt::Debug; |
| 57 | |
| 58 | use crate::arguments::Arguments; |
| 59 | use crate::column::Column; |
| 60 | use crate::connection::Connection; |
| 61 | use crate::row::Row; |
| 62 | |
| 63 | use crate::statement::Statement; |
| 64 | use crate::transaction::TransactionManager; |
| 65 | use crate::type_info::TypeInfo; |
| 66 | use crate::value::{Value, ValueRef}; |
| 67 | |
| 68 | /// A database driver. |
| 69 | /// |
| 70 | /// This trait encapsulates a complete set of traits that implement a driver for a |
| 71 | /// specific database (e.g., MySQL, PostgreSQL). |
| 72 | pub trait Database: 'static + Sized + Send + Debug { |
| 73 | /// The concrete `Connection` implementation for this database. |
| 74 | type Connection: Connection<Database = Self>; |
| 75 | |
| 76 | /// The concrete `TransactionManager` implementation for this database. |
| 77 | type TransactionManager: TransactionManager<Database = Self>; |
| 78 | |
| 79 | /// The concrete `Row` implementation for this database. |
| 80 | type Row: Row<Database = Self>; |
| 81 | |
| 82 | /// The concrete `QueryResult` implementation for this database. |
| 83 | type QueryResult: 'static + Sized + Send + Sync + Default + Extend<Self::QueryResult>; |
| 84 | |
| 85 | /// The concrete `Column` implementation for this database. |
| 86 | type Column: Column<Database = Self>; |
| 87 | |
| 88 | /// The concrete `TypeInfo` implementation for this database. |
| 89 | type TypeInfo: TypeInfo; |
| 90 | |
| 91 | /// The concrete type used to hold an owned copy of the not-yet-decoded value that was |
| 92 | /// received from the database. |
| 93 | type Value: Value<Database = Self> + 'static; |
| 94 | /// The concrete type used to hold a reference to the not-yet-decoded value that has just been |
| 95 | /// received from the database. |
| 96 | type ValueRef<'r>: ValueRef<'r, Database = Self>; |
| 97 | |
| 98 | /// The concrete `Arguments` implementation for this database. |
| 99 | type Arguments<'q>: Arguments<'q, Database = Self>; |
| 100 | /// The concrete type used as a buffer for arguments while encoding. |
| 101 | type ArgumentBuffer<'q>; |
| 102 | |
| 103 | /// The concrete `Statement` implementation for this database. |
| 104 | type Statement<'q>: Statement<'q, Database = Self>; |
| 105 | |
| 106 | /// The display name for this database driver. |
| 107 | const NAME: &'static str; |
| 108 | |
| 109 | /// The schemes for database URLs that should match this driver. |
| 110 | const URL_SCHEMES: &'static [&'static str]; |
| 111 | } |
| 112 | |
| 113 | /// A [`Database`] that maintains a client-side cache of prepared statements. |
| 114 | pub trait HasStatementCache {} |