1pub trait Database: sqlx::Database {
9 const SYSTEM: &'static str;
12
13 fn connection_attributes(
18 pool: &sqlx::Pool<Self>,
19 ) -> (Option<String>, Option<u16>, Option<String>);
20
21 fn rows_affected(result: &<Self as sqlx::Database>::QueryResult) -> u64;
27}
28
29#[cfg(any(feature = "postgres", feature = "mysql"))]
36fn url_based_connection_attributes<O: sqlx::ConnectOptions>(
37 options: &O,
38) -> (Option<String>, Option<u16>, Option<String>) {
39 let url = options.to_url_lossy();
40 let host = url.host_str().map(String::from);
41 let port = url.port();
42 let namespace = url
43 .path_segments()
44 .and_then(|mut segments| segments.next().map(String::from));
45 (host, port, namespace)
46}
47
48#[cfg(feature = "sqlite")]
49impl Database for sqlx::Sqlite {
50 const SYSTEM: &'static str = "sqlite";
51
52 fn connection_attributes(
53 pool: &sqlx::Pool<Self>,
54 ) -> (Option<String>, Option<u16>, Option<String>) {
55 let namespace = pool
56 .connect_options()
57 .get_filename()
58 .to_str()
59 .map(String::from);
60 (None, None, namespace)
61 }
62
63 fn rows_affected(result: &sqlx::sqlite::SqliteQueryResult) -> u64 {
64 result.rows_affected()
65 }
66}
67
68#[cfg(feature = "postgres")]
69impl Database for sqlx::Postgres {
70 const SYSTEM: &'static str = "postgresql";
71
72 fn connection_attributes(
73 pool: &sqlx::Pool<Self>,
74 ) -> (Option<String>, Option<u16>, Option<String>) {
75 url_based_connection_attributes(pool.connect_options().as_ref())
76 }
77
78 fn rows_affected(result: &sqlx::postgres::PgQueryResult) -> u64 {
79 result.rows_affected()
80 }
81}
82
83#[cfg(feature = "mysql")]
84impl Database for sqlx::MySql {
85 const SYSTEM: &'static str = "mysql";
86
87 fn connection_attributes(
88 pool: &sqlx::Pool<Self>,
89 ) -> (Option<String>, Option<u16>, Option<String>) {
90 url_based_connection_attributes(pool.connect_options().as_ref())
91 }
92
93 fn rows_affected(result: &sqlx::mysql::MySqlQueryResult) -> u64 {
94 result.rows_affected()
95 }
96}