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