-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmysql_database.rs
73 lines (64 loc) · 2.06 KB
/
mysql_database.rs
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
#![allow(clippy::expect_fun_call)]
use diesel::connection::SimpleConnection;
use diesel::dsl::sql;
use diesel::sql_types::Bool;
use diesel::*;
pub struct Database {
url: String,
}
impl Database {
pub fn new(url: &str) -> Self {
Database { url: url.into() }
}
pub fn create(self) -> Self {
let (database, mysql_url) = self.split_url();
let mut conn = MysqlConnection::establish(&mysql_url).unwrap();
diesel::sql_query(format!("CREATE DATABASE `{}`", database))
.execute(&mut conn)
.unwrap();
self
}
pub fn exists(&self) -> bool {
MysqlConnection::establish(&self.url).is_ok()
}
pub fn table_exists(&self, table: &str) -> bool {
select(sql::<Bool>(&format!(
"EXISTS \
(SELECT 1 \
FROM information_schema.tables \
WHERE table_name = '{}'
AND table_schema = DATABASE())",
table
)))
.get_result(&mut self.conn())
.unwrap()
}
pub fn conn(&self) -> MysqlConnection {
MysqlConnection::establish(&self.url)
.expect(&format!("Failed to open connection to {}", &self.url))
}
pub fn execute(&self, command: &str) {
self.conn()
.batch_execute(command)
.expect(&format!("Error executing command {}", command));
}
fn split_url(&self) -> (String, String) {
let mut split: Vec<&str> = self.url.split('/').collect();
let database = split.pop().unwrap();
let mysql_url = format!("{}/{}", split.join("/"), "information_schema");
(database.into(), mysql_url)
}
}
impl Drop for Database {
fn drop(&mut self) {
let (database, mysql_url) = self.split_url();
let mut conn = try_drop!(
MysqlConnection::establish(&mysql_url),
"Couldn't connect to database"
);
try_drop!(
diesel::sql_query(format!("DROP DATABASE IF EXISTS `{}`", database)).execute(&mut conn),
"Couldn't drop database"
);
}
}