start to use sqlite

This commit is contained in:
2023-07-15 09:27:16 +02:00
parent 509ec65a62
commit e3422ed255
3 changed files with 97 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
use std::net::{Ipv4Addr, SocketAddrV4};
use rusqlite::{Connection, Result};
use std::collections::hash_map::HashMap;
const PATH: &str = "mdeploy.db";
@@ -12,19 +13,23 @@ pub struct ConfServer{
pub struct MInstance {
id: i64,
ip: Ipv4Addr,
ip: String,
image: i64,
}
pub struct instance {
pub struct InstanceStorage {
con: Connection,
instances: HashMap<String, MInstance>,
images: HashMap<String, i64>,
}
impl instance {
impl InstanceStorage {
pub fn new() -> Result<Self> {
let con = Connection::open(PATH)?;
let ret = Self {
con : con,
instances: HashMap::new(),
images: HashMap::new(),
};
ret.create_table()?;
Ok(ret)
@@ -38,15 +43,82 @@ impl instance {
CREATE TABLE IF NOT EXISTS l_instances(
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT,
image INTEGER
domain TEXT,
image INTEGER,
FOREIGN KEY(INTEGER) REFERENCES images(id));
COMMIT;"
)
}
fn insert_image(&self, image: String){
fn insert_image(&self, image: String) -> Result<()>{
let mut stmt = self.con.prepare("INSERT INTO images values(?1)")?;
stmt.execute([image])?;
Ok(())
}
fn load_instances(&mut self) -> Result<()> {
let mut stmt = self.con.prepare("select id, ip, domain, image from l_instances")?;
let mut rows = stmt.query([])?;
for row in rows.next()? {
let mut domain = row.get::<_, String>(2)?;
self.instances.insert(domain, MInstance {
id: row.get::<_, i64>(0)?,
ip: row.get::<_, String>(1)?,
image: row.get::<_, i64>(3)?
,
});
}
Ok(())
}
fn load_images(&mut self) -> Result<()> {
let mut stmt = self.con.prepare("select id, name from images")?;
let mut rows = stmt.query([])?;
for row in rows.next()? {
self.images.insert(row.get::<_, String>(1)?, row.get::<_, i64>(0)?);
}
Ok(())
}
fn get_image_id(&self, image: String) -> Result<i64> {
let mut stmt = self.con.prepare("select id from images where name = ?1")?;
let mut rows = stmt.query([image])?;
let id = rows.next()?.unwrap().get::<_, i64>(0)?;
Ok(id)
}
fn get_instance_id(&self, domain: String) -> Result<i64> {
let mut stmt = self.con.prepare("select id from l_instances where domain = ?1")?;
let mut rows = stmt.query([domain])?;
let id = rows.next()?.unwrap().get::<_, i64>(0)?;
Ok(id)
}
fn new_instance(&mut self, domain: String, image: String, ip: String) -> Result<i64>{
let image_id = self.create_or_get_image_id(image.clone(), 0)?;
let mut stmt = self.con.prepare("INSERT INTO l_instances values(?1, ?2, ?3)")?;
stmt.execute([ip.clone(), image, image_id.to_string()])?;
let instance_id = self.get_instance_id(domain.clone())?;
self.instances.insert(domain.clone(), MInstance{
id: instance_id,
ip: ip,
image: image_id
});
Ok(instance_id)
}
fn create_or_get_image_id(&self, image: String, mut depth: i64) -> Result<i64> {
if depth > 0 {
return Err(rusqlite::Error::QueryReturnedNoRows);
}
depth+=1;
match self.get_image_id(image.clone()) {
Ok(id) => Ok(id),
Err(_e) => {
self.insert_image(image.clone())?;
self.create_or_get_image_id(image, depth)
}
}
}
}