use std::net::{SocketAddrV4, TcpStream};
pub struct Connection {
tcp_stream: Option<TcpStream>,
}
impl Connection {
fn new(tcp_stream: TcpStream) -> Connection {
Connection {
tcp_stream: Some(tcp_stream)
}
}
fn fail() -> Connection {
Connection {
tcp_stream: None
}
}
pub fn is_not_connected(&self) -> bool {
self.tcp_stream.is_none()
}
pub fn find() {
println!("Find")
}
pub fn insert(&self, key: String, column_family: String, column: String, value: String) {
if self.is_not_connected() {}
println!("Insert")
}
pub fn delete() {
println!("Delete")
}
}
pub fn create_connection(db_addr: SocketAddrV4) -> Connection {
let result = TcpStream::connect(db_addr);
if result.is_err() {
println!("Connect to LynxDB server {} failed !", db_addr);
return Connection::fail();
}
let tcp_stream = result.unwrap();
Connection::new(tcp_stream)
}
#[cfg(test)]
mod tests {
use std::net::{Ipv4Addr, SocketAddrV4};
use super::*;
fn init_connection() -> Connection {
let host = Ipv4Addr::new(127, 0, 0, 1);
let db_addr = SocketAddrV4::new(host, 7820);
create_connection(db_addr)
}
#[test]
fn test_001() {
let connection = init_connection();
assert!(connection.is_not_connected())
}
#[test]
fn test_002() {
let connection = init_connection();
connection.insert("key", "column_family", "column", "value");
}
}