mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
Update
This commit is contained in:
@@ -2,6 +2,7 @@ pub mod config;
|
||||
#[cfg(feature = "db_sqlite")]
|
||||
pub mod sqlite;
|
||||
pub mod traits;
|
||||
pub mod user;
|
||||
|
||||
pub use config::check_db_config;
|
||||
pub use config::PixivDownloaderDbConfig;
|
||||
@@ -10,6 +11,7 @@ pub use config::PixivDownloaderSqliteConfig;
|
||||
#[cfg(feature = "db_sqlite")]
|
||||
pub use sqlite::{PixivDownloaderSqlite, SqliteError};
|
||||
pub use traits::PixivDownloaderDb;
|
||||
pub use user::User;
|
||||
pub type PixivDownloaderDbError = Box<dyn std::fmt::Display + Send + Sync>;
|
||||
|
||||
use crate::{get_helper, gettext};
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use super::super::{
|
||||
PixivDownloaderDb, PixivDownloaderDbConfig, PixivDownloaderDbError, PixivDownloaderSqliteConfig,
|
||||
PixivDownloaderDb, PixivDownloaderDbConfig, PixivDownloaderDbError,
|
||||
PixivDownloaderSqliteConfig, User,
|
||||
};
|
||||
use super::SqliteError;
|
||||
use bytes::BytesMut;
|
||||
use futures_util::lock::Mutex;
|
||||
use rusqlite::{Connection, OpenFlags, Transaction};
|
||||
use rusqlite::{Connection, OpenFlags, OptionalExtension, Transaction};
|
||||
use std::collections::HashMap;
|
||||
|
||||
const AUTHORS_TABLE: &'static str = "CREATE TABLE authors (
|
||||
@@ -169,6 +171,23 @@ impl PixivDownloaderSqlite {
|
||||
Ok(tables)
|
||||
}
|
||||
|
||||
async fn _get_user(&self, id: u64) -> Result<Option<User>, SqliteError> {
|
||||
let con = self.db.lock().await;
|
||||
Ok(con
|
||||
.query_row("SELECT * FROM users WHERE id = ?;", [id], |row| {
|
||||
let password: Vec<u8> = row.get(3)?;
|
||||
let password: &[u8] = &password;
|
||||
Ok(User {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
username: row.get(2)?,
|
||||
password: BytesMut::from(password),
|
||||
is_admin: row.get(4)?,
|
||||
})
|
||||
})
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
async fn _read_version(&self) -> Result<Option<[u8; 4]>, SqliteError> {
|
||||
let con = self.db.lock().await;
|
||||
let mut stmt = con.prepare("SELECT v1, v2, v3, v4 FROM version WHERE id='main';")?;
|
||||
@@ -220,6 +239,10 @@ impl PixivDownloaderDb for PixivDownloaderSqlite {
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_user(&self, id: u64) -> Result<Option<User>, PixivDownloaderDbError> {
|
||||
Ok(self._get_user(id).await?)
|
||||
}
|
||||
|
||||
async fn init(&self) -> Result<(), PixivDownloaderDbError> {
|
||||
if !self._check_database().await? {
|
||||
self._create_table().await?;
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
use super::PixivDownloaderDbConfig;
|
||||
use super::PixivDownloaderDbError;
|
||||
use super::User;
|
||||
|
||||
#[async_trait]
|
||||
pub trait PixivDownloaderDb {
|
||||
/// Create a new instance of database
|
||||
/// * `cfg` - The database configuration
|
||||
fn new<R: AsRef<PixivDownloaderDbConfig> + ?Sized>(
|
||||
cfg: &R,
|
||||
) -> Result<Self, PixivDownloaderDbError>
|
||||
where
|
||||
Self: Sized + Send + Sync;
|
||||
/// Get a user by ID
|
||||
/// * `id`: The user's ID
|
||||
async fn get_user(&self, id: u64) -> Result<Option<User>, PixivDownloaderDbError>;
|
||||
/// Initialize the database (create tables, migrate data, etc.)
|
||||
async fn init(&self) -> Result<(), PixivDownloaderDbError>;
|
||||
}
|
||||
|
||||
15
src/db/user.rs
Normal file
15
src/db/user.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use bytes::BytesMut;
|
||||
|
||||
/// A user in the database
|
||||
pub struct User {
|
||||
/// The user ID
|
||||
pub id: u64,
|
||||
/// The user's name
|
||||
pub name: String,
|
||||
/// Unique user name
|
||||
pub username: String,
|
||||
/// hashed password
|
||||
pub password: BytesMut,
|
||||
/// Whether the user is an admin
|
||||
pub is_admin: bool,
|
||||
}
|
||||
Reference in New Issue
Block a user