sqlx_core_oldapi/any/
kind.rs

1use crate::error::Error;
2use std::str::FromStr;
3
4#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5pub enum AnyKind {
6    #[cfg(feature = "postgres")]
7    Postgres,
8
9    #[cfg(feature = "mysql")]
10    MySql,
11
12    #[cfg(feature = "sqlite")]
13    Sqlite,
14
15    #[cfg(feature = "mssql")]
16    Mssql,
17
18    #[cfg(feature = "odbc")]
19    Odbc,
20}
21
22impl FromStr for AnyKind {
23    type Err = Error;
24
25    fn from_str(url: &str) -> Result<Self, Self::Err> {
26        match url {
27            #[cfg(feature = "postgres")]
28            _ if url.starts_with("postgres:") || url.starts_with("postgresql:") => {
29                Ok(AnyKind::Postgres)
30            }
31
32            #[cfg(not(feature = "postgres"))]
33            _ if url.starts_with("postgres:") || url.starts_with("postgresql:") => {
34                Err(Error::Configuration("database URL has the scheme of a PostgreSQL database but the `postgres` feature is not enabled".into()))
35            }
36
37            #[cfg(feature = "mysql")]
38            _ if url.starts_with("mysql:") || url.starts_with("mariadb:") => {
39                Ok(AnyKind::MySql)
40            }
41
42            #[cfg(not(feature = "mysql"))]
43            _ if url.starts_with("mysql:") || url.starts_with("mariadb:") => {
44                Err(Error::Configuration("database URL has the scheme of a MySQL database but the `mysql` feature is not enabled".into()))
45            }
46
47            #[cfg(feature = "sqlite")]
48            _ if url.starts_with("sqlite:") => {
49                Ok(AnyKind::Sqlite)
50            }
51
52            #[cfg(not(feature = "sqlite"))]
53            _ if url.starts_with("sqlite:") => {
54                Err(Error::Configuration("database URL has the scheme of a SQLite database but the `sqlite` feature is not enabled".into()))
55            }
56
57            #[cfg(feature = "mssql")]
58            _ if url.starts_with("mssql:") || url.starts_with("sqlserver:") => {
59                Ok(AnyKind::Mssql)
60            }
61
62            #[cfg(not(feature = "mssql"))]
63            _ if url.starts_with("mssql:") || url.starts_with("sqlserver:") => {
64                Err(Error::Configuration("database URL has the scheme of a MSSQL database but the `mssql` feature is not enabled".into()))
65            }
66
67            #[cfg(feature = "odbc")]
68            _ if url.starts_with("odbc:") || Self::is_odbc_connection_string(url) => {
69                Ok(AnyKind::Odbc)
70            }
71
72            #[cfg(not(feature = "odbc"))]
73            _ if url.starts_with("odbc:") || Self::is_odbc_connection_string(url) => {
74                Err(Error::Configuration("database URL has the scheme of an ODBC database but the `odbc` feature is not enabled".into()))
75            }
76
77            _ => Err(Error::Configuration(format!("unrecognized database url: {:?}", url).into()))
78        }
79    }
80}
81
82impl AnyKind {
83    fn is_odbc_connection_string(s: &str) -> bool {
84        let s_upper = s.to_uppercase();
85        s_upper.starts_with("DSN=")
86            || s_upper.starts_with("DRIVER=")
87            || s_upper.starts_with("FILEDSN=")
88            || (s_upper.contains("DRIVER=") && s_upper.contains(';'))
89    }
90}