fix some database managements
This commit is contained in:
100
src/conf/mod.rs
100
src/conf/mod.rs
@@ -1,6 +1,7 @@
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use rusqlite::{Connection, Result};
|
||||
use std::collections::hash_map::HashMap;
|
||||
use fallible_iterator::FallibleIterator;
|
||||
|
||||
pub mod storage;
|
||||
|
||||
const PATH: &str = "mdeploy.db";
|
||||
|
||||
@@ -11,16 +12,15 @@ pub struct ConfServer{
|
||||
ip_base_limit: u8,
|
||||
}
|
||||
|
||||
pub struct MInstance {
|
||||
pub struct Instance {
|
||||
id: i64,
|
||||
ip: String,
|
||||
image: i64,
|
||||
pub ip: String,
|
||||
pub domain: String,
|
||||
pub image: i64,
|
||||
}
|
||||
|
||||
pub struct InstanceStorage {
|
||||
con: Connection,
|
||||
instances: HashMap<String, MInstance>,
|
||||
images: HashMap<String, i64>,
|
||||
}
|
||||
|
||||
impl InstanceStorage {
|
||||
@@ -28,8 +28,6 @@ impl InstanceStorage {
|
||||
let con = Connection::open(PATH)?;
|
||||
let ret = Self {
|
||||
con : con,
|
||||
instances: HashMap::new(),
|
||||
images: HashMap::new(),
|
||||
};
|
||||
ret.create_table()?;
|
||||
Ok(ret)
|
||||
@@ -45,75 +43,87 @@ impl InstanceStorage {
|
||||
ip TEXT,
|
||||
domain TEXT,
|
||||
image INTEGER,
|
||||
FOREIGN KEY(INTEGER) REFERENCES images(id));
|
||||
FOREIGN KEY(image) REFERENCES images(id));
|
||||
COMMIT;"
|
||||
)
|
||||
}
|
||||
|
||||
fn insert_image(&self, image: String) -> Result<()>{
|
||||
let mut stmt = self.con.prepare("INSERT INTO images values(?1)")?;
|
||||
let mut stmt = self.con.prepare("INSERT INTO images(name) values(?1)")?;
|
||||
stmt.execute([image])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_instances(&mut self) -> Result<()> {
|
||||
fn load_instances(&mut self) -> Result<Vec<Instance>> {
|
||||
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(())
|
||||
rows.map(|r| Ok(Instance{
|
||||
id: r.get::<_, i64>(0)?,
|
||||
ip: r.get::<_, String>(1)?,
|
||||
domain: r.get::<_, String>(2)?,
|
||||
image: r.get::<_, i64>(3)?,
|
||||
})).collect()
|
||||
}
|
||||
|
||||
fn load_images(&mut self) -> Result<()> {
|
||||
fn load_images(&mut self) -> Result<Vec<(String, i64)>> {
|
||||
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(())
|
||||
rows.map(|row| Ok((row.get::<_, String>(1)?, row.get::<_, i64>(0)?))).collect()
|
||||
}
|
||||
|
||||
fn get_image_id(&self, image: String) -> Result<i64> {
|
||||
fn get_image_id(&self, image: String) -> Result<Option<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)
|
||||
Ok(match rows.next()? {
|
||||
Some(i) => Some(i.get::<_, i64>(0)?),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_instance_id(&self, domain: String) -> Result<i64> {
|
||||
pub fn get_image_name(&self, id: i64) -> Result<String> {
|
||||
let mut stmt = self.con.prepare("select name from images where id = ?1")?;
|
||||
let mut rows = stmt.query([id])?;
|
||||
Ok(rows.next()?.unwrap().get::<_, String>(0)?)
|
||||
}
|
||||
|
||||
fn get_instance_id(&self, domain: String) -> Result<Option<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)
|
||||
Ok(match rows.next()? {
|
||||
Some(i) => Some(i.get::<_, i64>(0)?),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
|
||||
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,
|
||||
pub fn new_instance(&mut self, ip: String, domain: String, image: i64) -> Result<Instance>{
|
||||
//let image_id = self.create_or_get_image_id(image.clone(), 0)?;
|
||||
let mut stmt = self.con.prepare("INSERT INTO l_instances(ip, domain, image) values(?1, ?2, ?3)")?;
|
||||
stmt.execute([ip.clone(),
|
||||
domain.clone(),
|
||||
image.to_string()])?;
|
||||
Ok(Instance {
|
||||
id: image,
|
||||
ip: ip,
|
||||
image: image_id
|
||||
});
|
||||
Ok(instance_id)
|
||||
domain: domain,
|
||||
image: image,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn image_to_id(&self, image: String) -> Result<i64> {
|
||||
self.create_or_get_image_id(image, 0)
|
||||
}
|
||||
|
||||
fn create_or_get_image_id(&self, image: String, mut depth: i64) -> Result<i64> {
|
||||
if depth > 0 {
|
||||
if depth > 1 {
|
||||
return Err(rusqlite::Error::QueryReturnedNoRows);
|
||||
}
|
||||
depth+=1;
|
||||
match self.get_image_id(image.clone()) {
|
||||
Ok(id) => Ok(id),
|
||||
Ok(Some(id)) => Ok(id),
|
||||
Ok(None) => {
|
||||
self.insert_image(image.clone())?;
|
||||
self.create_or_get_image_id(image, depth)
|
||||
}
|
||||
Err(_e) => {
|
||||
self.insert_image(image.clone())?;
|
||||
self.create_or_get_image_id(image, depth)
|
||||
|
||||
Reference in New Issue
Block a user