add some functions for interacting with message table

This commit is contained in:
Horhik 2021-03-18 09:05:00 +03:00
parent 25f73f6a36
commit 6842d4e5b2
3 changed files with 99 additions and 27 deletions

View file

@ -2,31 +2,53 @@ use super::types::Message;
use rusqlite::{params, Connection, Result}; use rusqlite::{params, Connection, Result};
pub fn select_message_by_id(id: u64, conn: &Connection) -> Result<Message> { fn ret_mes(row: &rusqlite::Row<'_>) -> Result<Message> {
unimplemented!();
}
pub fn select_all_user_message(id: u64, conn: &Connection) -> Result<Vec<Message>> {
unimplemented!();
}
pub fn select_n_last_messages(user_id: u32, start: u32, count: u32, conn: &Connection) -> Result<Vec<Message>> {
let mut selected = conn.prepare("SELECT * FROM messages WHERE user_id = ?1 AND id > ?2 LIMIT ?3")?;
let message_iter = selected.query_map(params![user_id, start, count], |row| {
Ok(Message { Ok(Message {
id: row.get(0)?, id: row.get(0)?,
date: row.get(1)?, user_id: row.get(1)?,
user_id: row.get(2)?, date: row.get(2)?,
message: row.get(3)?, message: row.get(3)?,
}) })
})?;
let mut users: Vec<Message> = Vec::new();
for message in message_iter {
log::info!("User: {:?}", (&message));
users.push(message?);
} }
log::info!("All users loaded to memory");
Ok(users) pub fn select_message_by_id(user_id: u32, id: u32, conn: &Connection) -> Result<Message> {
let mut selected = conn.prepare("SELECT * FROM messages WHERE id = ?1 AND user_id = ?2")?;
let mut message_iter = selected.query_map(params![id, user_id], |row| ret_mes(row))?;
let message = message_iter.next().unwrap();
log::info!("Message {:} founded", id);
message
}
pub fn select_all_user_message(id: u32, conn: &Connection) -> Result<Vec<Message>> {
let mut selected =
conn.prepare("SELECT * FROM messages WHERE user_id = ?1 ORDER BY date DESC")?;
let message_iter = selected.query_map(params![id], |row| ret_mes(row))?;
let mut messages: Vec<Message> = Vec::new();
for message in message_iter {
log::info!("Message: {:?}", (&message));
messages.push(message?);
}
log::info!("All messages loaded to memory");
Ok(messages)
}
pub fn select_n_last_messages(
user_id: u32,
start: u32,
count: u32,
conn: &Connection,
) -> Result<Vec<Message>> {
let mut selected = conn.prepare(
"SELECT * FROM messages WHERE user_id = ?1 AND id >= ?2 LIMIT ?3 ORDER BY date DESC",
)?;
let message_iter = selected.query_map(params![user_id, start, count], |row| ret_mes(row))?;
let mut messages: Vec<Message> = Vec::new();
for message in message_iter {
log::info!("Message: {:?}", (&message));
messages.push(message?);
}
log::info!("All messages loaded to memory");
Ok(messages)
} }
pub fn add_message(message: Message, conn: &Connection) -> Result<()> { pub fn add_message(message: Message, conn: &Connection) -> Result<()> {

View file

@ -22,7 +22,7 @@ fn create_db(conn: &Connection) -> Result<()> {
match conn.execute( match conn.execute(
"CREATE TABLE messages ( "CREATE TABLE messages (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
user_id TEXT NOT NULL, user_id INTEGER NOT NULL,
date datetime NOT NULL, date datetime NOT NULL,
message TEXT NOT NULL message TEXT NOT NULL
)", )",
@ -46,3 +46,49 @@ pub fn start_db() -> Result<Connection> {
Connection::open(DB_PATH) Connection::open(DB_PATH)
} }
} }
// ### A little stupid test ### //
//let conn = db::start_db().unwrap();
/*
users::add_user(db::types::User{
id: 9349,
name: "Nick".to_string(),
sign_key: "string".to_string(),
insert_key: fcpv2::types::SSK::parse("SSK@Rgt0qM8D24DltliV2-JE9tYLcrgGAKeDwkz41I3JBPs,p~c8c7FXcJjhcf2vA-Xm0Mjyw1o~xn7L2-T8zlBA1IU").unwrap(),
messages_count: 1,
}, &conn);
let time: chrono::DateTime<chrono::offset::FixedOffset> =
chrono::DateTime::parse_from_rfc3339("2021-03-18T04:22:42.501Z").unwrap();
db::messages::add_message(
db::types::Message {
user_id: 9349,
id: 4,
date: time.naive_utc(),
message: "HI?".to_string(),
},
&conn,
)
.unwrap();
db::messages::add_message(
db::types::Message {
user_id: 9349,
id: 5,
date: time.naive_utc(),
message: "I AM NICK!".to_string(),
},
&conn,
)
.unwrap();
db::messages::add_message(
db::types::Message {
user_id: 9349,
id: 6,
date: time.naive_utc(),
message: "I'LL FIND that".to_string(),
},
&conn,
)
.unwrap();
let messages = db::messages::select_message_by_id(9349, 3, &conn).unwrap();
*/

View file

@ -48,6 +48,7 @@ use std::{
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
SimpleLogger::new().init().unwrap(); SimpleLogger::new().init().unwrap();
let conn = db::start_db().unwrap(); let conn = db::start_db().unwrap();
/*
users::add_user(db::types::User{ users::add_user(db::types::User{
id: 9349, id: 9349,
name: "Nick".to_string(), name: "Nick".to_string(),
@ -60,9 +61,9 @@ fn main() -> io::Result<()> {
db::messages::add_message( db::messages::add_message(
db::types::Message { db::types::Message {
user_id: 9349, user_id: 9349,
id: 1, id: 4,
date: time.naive_utc(), date: time.naive_utc(),
message: "hey duude".to_string(), message: "HI?".to_string(),
}, },
&conn, &conn,
) )
@ -70,9 +71,9 @@ fn main() -> io::Result<()> {
db::messages::add_message( db::messages::add_message(
db::types::Message { db::types::Message {
user_id: 9349, user_id: 9349,
id: 2, id: 5,
date: time.naive_utc(), date: time.naive_utc(),
message: "what do you think".to_string(), message: "I AM NICK!".to_string(),
}, },
&conn, &conn,
) )
@ -80,14 +81,17 @@ fn main() -> io::Result<()> {
db::messages::add_message( db::messages::add_message(
db::types::Message { db::types::Message {
user_id: 9349, user_id: 9349,
id: 3, id: 6,
date: time.naive_utc(), date: time.naive_utc(),
message: "about that".to_string(), message: "I'LL FIND that".to_string(),
}, },
&conn, &conn,
) )
.unwrap(); .unwrap();
let messages = db::messages::select_message_by_id(9349, 3, &conn).unwrap();
*/
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) = let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel(); mpsc::channel();