use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
pub enum Error {
General(String),
}
#[derive(Debug, Clone)]
pub enum Value {
Int32(i32),
UInt32(u32),
String(String),
}
impl ToString for Value {
fn to_string(&self) -> String {
match self {
Value::Int32(n) => format!("{}", n),
Value::UInt32(n) => format!("{}", n),
Value::String(s) => format!("'{}'", s),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub trait Driver: Sync + Send {
fn connect(&self, url: &str) -> Result<Rc<RefCell<dyn Connection + 'static>>>;
}
pub trait Connection {
fn create(&mut self, sql: &str) -> Result<Rc<RefCell<dyn Statement + '_>>>;
fn prepare(&mut self, sql: &str) -> Result<Rc<RefCell<dyn Statement + '_>>>;
}
pub trait Statement {
fn execute_query(&mut self, params: &[Value]) -> Result<Rc<RefCell<dyn ResultSet + '_>>>;
fn execute_update(&mut self, params: &[Value]) -> Result<u64>;
}
pub trait ResultSet {
fn meta_data(&self) -> Result<Rc<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>>>;
}
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
}
}