Skip to main content

Database

Struct Database 

Source
pub struct Database { /* private fields */ }
Expand description

数据库主结构

使用 enum 包装两种引擎模式,提供统一的 API

Implementations§

Source§

impl Database

Source

pub fn new() -> Self

创建新的内存数据库实例

Source

pub fn open(path: &Path) -> DbResult<Self>

打开持久化数据库

Source

pub fn create(path: &Path) -> DbResult<Self>

创建新的持久化数据库(如果已存在则覆盖)

Source

pub fn create_table(&self, name: &str, columns: Vec<Column>) -> DbResult<()>

创建表

Source

pub fn drop_table(&self, name: &str) -> DbResult<()>

删除表

Source

pub fn has_table(&self, name: &str) -> bool

检查表是否存在

Source

pub fn insert( &self, table: &str, values: Vec<(&str, DbValue)>, ) -> DbResult<RowId>

插入数据

Source

pub fn get(&self, table: &str, row_id: RowId) -> DbResult<Option<Row>>

根据 row_id 获取单行

Source

pub fn query(&self, table: &str) -> QueryBuilder

查询构建器

内存模式:返回完整的 QueryBuilder,支持所有功能 持久化模式:同样支持 QueryBuilder(通过内部 MemoryEngine 的 Arc)

Source

pub fn update(&self, table: &str) -> UpdateBuilder

更新构建器

内存模式:返回完整的 UpdateBuilder,支持所有功能 持久化模式:同样支持 UpdateBuilder(通过内部 MemoryEngine 的 Arc)

Source

pub fn delete(&self, table: &str) -> DeleteBuilder

删除构建器

内存模式:返回完整的 DeleteBuilder,支持所有功能 持久化模式:同样支持 DeleteBuilder(通过内部 MemoryEngine 的 Arc)

Source

pub fn transaction<F, T>(&self, f: F) -> DbResult<T>
where F: FnOnce(&mut Transaction<'_>) -> DbResult<T>,

执行事务

事务在两种模式下都支持:

  • 内存模式:支持回滚(通过 WriteLog)
  • 持久化模式:支持回滚 + WAL 日志标记
Source

pub fn create_index(&self, table: &str, column: &str) -> DbResult<()>

为表列创建索引(单列,向后兼容)

Source

pub fn create_composite_index( &self, table: &str, columns: &[&str], ) -> DbResult<()>

创建复合索引

Source

pub fn create_unique_index(&self, table: &str, columns: &[&str]) -> DbResult<()>

创建唯一复合索引

Source

pub fn drop_index(&self, table: &str, column: &str) -> DbResult<bool>

删除索引

Source

pub fn drop_composite_index( &self, table: &str, columns: &[&str], ) -> DbResult<bool>

删除复合索引

Source

pub fn has_index(&self, table: &str, column: &str) -> bool

检查列是否有索引

Source

pub fn has_composite_index(&self, table: &str, columns: &[&str]) -> bool

检查复合索引是否存在

Trait Implementations§

Source§

impl Default for Database

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.