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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#![forbid(unsafe_code)]
pub mod time_util;
pub mod mysql;
pub mod postgres;
#[derive(Debug)]
pub enum Error {
General(String),
}
#[derive(Debug, Clone)]
pub enum Value {
String(String),
}
impl ToString for Value {
fn to_string(&self) -> String {
match self {
Value::String(s) => s.clone(),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub trait Driver: Sync + Send {
fn connect(&self, url: &str) -> Result<Box<dyn Connection>>;
}
pub trait Connection: Send {
fn create(&mut self, sql: &str) -> Result<Box<dyn Statement + '_>>;
fn prepare(&mut self, sql: &str) -> Result<Box<dyn Statement + '_>>;
fn is_valid(&mut self) -> bool;
}
pub trait Statement {
fn execute_query(&mut self, params: &[Value]) -> Result<Box<dyn ResultSet + '_>>;
fn execute_update(&mut self, params: &[Value]) -> Result<u64>;
}
pub trait ResultSet: Send {
fn meta_data(&self) -> Result<Box<dyn ResultSetMetaData>>;
fn next(&mut self) -> bool;
fn get_i8(&self, i: u64) -> Result<Option<i8>>;
fn get_i16(&self, i: u64) -> Result<Option<i16>>;
fn get_i32(&self, i: u64) -> Result<Option<i32>>;
fn get_i64(&self, i: u64) -> Result<Option<i64>>;
fn get_f32(&self, i: u64) -> Result<Option<f32>>;
fn get_f64(&self, i: u64) -> Result<Option<f64>>;
fn get_string(&self, i: u64) -> Result<Option<String>>;
fn get_bytes(&self, i: u64) -> Result<Option<Vec<u8>>>;
fn get_date(&self, i: u64) -> Result<Option<String>>;
}
pub trait ResultSetMetaData {
fn num_columns(&self) -> u64;
fn column_name(&self, i: u64) -> String;
fn column_type(&self, i: u64) -> DataType;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DataType {
Bool,
Byte,
Char,
Short,
Integer,
Float,
Double,
Decimal,
Date,
Time,
Datetime,
Utf8,
Binary,
}
#[derive(Debug, Clone)]
pub struct Column {
name: String,
data_type: DataType,
}
impl Column {
pub fn new(name: &str, data_type: DataType) -> Self {
Column {
name: name.to_owned(),
data_type,
}
}
}
impl ResultSetMetaData for Vec<Column> {
fn num_columns(&self) -> u64 {
self.len() as u64
}
fn column_name(&self, i: u64) -> String {
self[i as usize].name.clone()
}
fn column_type(&self, i: u64) -> DataType {
self[i as usize].data_type
}
}