use sqlx::SqlitePool;
use uuid::Uuid;
pub async fn insert_message(
db: &SqlitePool,
session_id: &str,
role: &str,
text: &str,
blocks: Option<&str>,
) -> Result<(), sqlx::Error> {
let id = Uuid::new_v4().to_string();
let now = chrono::Utc::now().to_rfc3339();
sqlx::query("INSERT INTO chat_messages (id, session_id, role, text, created_at, blocks) VALUES (?, ?, ?, ?, ?, ?)")
.bind(&id)
.bind(session_id)
.bind(role)
.bind(text)
.bind(&now)
.bind(blocks)
.execute(db)
.await?;
Ok(())
}
pub async fn list_messages(
db: &SqlitePool,
session_id: &str,
) -> Result<Vec<(String, String, String, String, Option<String>)>, sqlx::Error> {
let rows: Vec<(String, String, String, String, Option<String>)> = sqlx::query_as(
"SELECT role, text, created_at, id, blocks FROM chat_messages WHERE session_id = ? ORDER BY created_at ASC",
)
.bind(session_id)
.fetch_all(db)
.await?;
Ok(rows)
}
pub async fn sync_messages(
db: &SqlitePool,
session_id: &str,
messages: &[(String, String, Option<String>)],
) -> Result<(), sqlx::Error> {
for (role, text, blocks) in messages {
if text.is_empty() {
continue;
}
let exists: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM chat_messages WHERE session_id = ? AND role = ? AND text = ?",
)
.bind(session_id)
.bind(role)
.bind(text)
.fetch_one(db)
.await?;
if exists > 0 {
if let Some(blocks) = blocks {
sqlx::query(
"UPDATE chat_messages SET blocks = ? WHERE session_id = ? AND role = ? AND text = ?",
)
.bind(blocks)
.bind(session_id)
.bind(role)
.bind(text)
.execute(db)
.await?;
}
continue;
}
let id = Uuid::new_v4().to_string();
let now = chrono::Utc::now().to_rfc3339();
sqlx::query("INSERT INTO chat_messages (id, session_id, role, text, created_at, blocks) VALUES (?, ?, ?, ?, ?, ?)")
.bind(&id)
.bind(session_id)
.bind(role)
.bind(text)
.bind(&now)
.bind(blocks)
.execute(db)
.await?;
}
Ok(())
}