-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrust-mysql-crud.rs
131 lines (116 loc) · 3.45 KB
/
rust-mysql-crud.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use mysql::*;
use mysql::prelude::*;
use std::env;
/// Database
///
/// Provides functionalities for database interactions.
///
/// Author: Mokter Hossain
/// Email: [email protected]
/// Website: www.gglink.uk
/// Github: https://github.com/moktermd08
/// LinkedIn: https://www.linkedin.com/in/mr-mokter/
/// Twitter: https://twitter.com/moGGLink
pub struct Database {
host: String,
user: String,
password: String,
database: String,
connection: Option<PooledConn>,
}
impl Database {
/// Creates a new `Database` instance with the given credentials.
pub fn new() -> Self {
Database {
host: "localhost".to_string(),
user: "user".to_string(),
password: "password".to_string(),
database: "database".to_string(),
connection: None,
}
}
/// Establishes a database connection.
///
/// Returns `true` on successful connection, `false` on failure.
///
/// # Errors
/// Returns `Err` if the connection to the database fails.
pub fn connect(&mut self) -> Result<bool, Box<dyn std::error::Error>> {
let url = format!(
"mysql://{}:{}@{}/{}",
self.user, self.password, self.host, self.database
);
let pool = Pool::new(url)?;
self.connection = Some(pool.get_conn()?);
Ok(true)
}
/// Closes the database connection.
///
/// Returns `true` on success, `false` on failure.
pub fn disconnect(&mut self) -> bool {
self.connection.take().is_some()
}
/// Sanitizes user input.
///
/// # Arguments
///
/// * `data` - The user input data.
///
/// # Returns
///
/// The sanitized data.
pub fn sanitize_input(&self, data: &str) -> String {
// In Rust, prepared statements automatically handle SQL escaping,
// so we just need to handle the other sanitization steps.
let data = data.trim();
let data = html_escape::encode_text(data); // Escapes HTML in the text.
data.to_string()
}
/// Sanitizes output.
///
/// # Arguments
///
/// * `data` - The data to be sent to the client.
///
/// # Returns
///
/// The sanitized data.
pub fn sanitize_output(data: &str) -> String {
html_escape::encode_text(data).to_string()
}
}
/// MySQLDatabase
///
/// A concrete implementation of the `Database` struct for MySQL databases.
pub struct MySQLDatabase {
// Here you can add any additional fields or methods specific to MySQLDatabase.
// For this example, we'll just reuse the Database struct.
db: Database,
}
impl MySQLDatabase {
pub fn new() -> Self {
MySQLDatabase {
db: Database::new(),
}
}
// Here you can implement the additional methods specific to MySQLDatabase.
}
fn main() {
// Example usage
let mut db = MySQLDatabase::new();
match db.db.connect() {
Ok(_) => {
println!("Connected successfully.");
let input = "<h1>Title</h1>";
let sanitized_input = db.db.sanitize_input(input);
println!("Sanitized Input: {}", sanitized_input);
let output = "<h1>Title</h1>";
let sanitized_output = Database::sanitize_output(output);
println!("Sanitized Output: {}", sanitized_output);
}
Err(e) => {
println!("Failed to connect to the database: {}", e);
}
}
db.db.disconnect();
}