3个版本

0.1.2 2020年10月7日
0.1.1 2020年7月20日
0.1.0 2020年7月19日

#2761 in 数据库接口

MIT许可证

285KB
5K SLoC

extremedb

McObject eXtremeDB绑定库,为Rust语言提供。

此包实现了高级、安全的Rust API。

有关更多信息及示例,请参阅extremedb crate文档。请注意,构建此包和运行示例需要安装现有的eXtremeDB 8.2版本。可以在McObject网站申请评估版本的eXtremeDB。


lib.rs:

extremedb是McObject的eXtremeDB数据库管理系统的包装器。

此crate目前支持eXtremeDB SQL API,以及配置eXtremeDB运行时、创建和打开数据库以及管理数据库连接所必需的核心功能的一部分。计划在未来版本中实现核心数据操作API。

有关eXtremeDB数据库管理系统的更多信息,请参阅文档页面

构建

此crate依赖于extremedb_sys crate,它有一些先决条件和需要设置的一些环境变量。有关更多信息,请参阅extremedb_sys crate参考。

可选功能

  • sql — SQL引擎。
  • rsql — 远程SQL引擎(SQL服务器和客户端)。
  • sequences — 序列(垂直存储)。

SQL示例

以下示例演示了创建内存数据库并使用DDL和DML SQL语句来操作其模式和内容。

use extremedb::connection::Connection;
use extremedb::database::{Database, Params};
use extremedb::device::{Assignment, Device};
use extremedb::runtime::Runtime;
use extremedb::sql::engine::{Engine, LocalEngine};
use extremedb::Result;

fn main() -> Result<()> {
    // Size of the memory region to allocate for the database.
    const DB_SIZE: usize = 1024 * 1024;

    // eXtremeDB runtime must be started before any operations can be performed.
    let runtime = Runtime::start(vec![]);

    // This example creates a conventional memory device, and will not work with the
    // shared memory runtime.
    assert!(!runtime.info().multiprocess_access_supported());

    // Initialize database parameters. SQL databases require these three
    // parameters to be explicitly set.
    let mut db_params = Params::new();
    db_params
        .ddl_dict_size(32768)
        .max_classes(100)
        .max_indexes(1000);

    // Allocate one in-memory device for the database.
    let mut devs = vec![Device::new_mem_conv(Assignment::Database, DB_SIZE)?];

    // Open the database using the parameters and devices defined above.
    let db = Database::open(&runtime, "test_db", None, &mut devs, db_params)?;

    // Establish a connection to the database.
    let conn = Connection::new(&db)?;

    // Create a local SQL engine on top of the database connection.
    let engine = LocalEngine::new(&conn)?;

    // Use SQL DDL to create a new table.
    engine.execute_statement("CREATE TABLE TestTable(i integer, s string);", &[])?;

    // Insert two rows into the table.
    engine.execute_statement("INSERT INTO TestTable(i, s) VALUES(?, ?);", &[&1, &"Hello"])?;
    engine.execute_statement("INSERT INTO TestTable(i, s) VALUES(?, ?);", &[&2, &"World"])?;

    // Select the rows.
    let ds = engine.execute_query("SELECT i, s FROM TestTable ORDER BY i;", &[])?;

    // The returned data source is not expected to be empty in case of a
    // SELECT statement.
    assert!(ds.is_some());
    let ds = ds.unwrap();

    // Create a cursor to iterate over the data source.
    let mut cur = ds.cursor()?;

    // Read the first row.
    {
        // Advance the cursor to the first row. It is positioned
        // before-the-first row initially.
        assert_eq!(cur.advance()?, true);

        // Get reference to the record which the cursor points at.
        // Since cur.advance() above has returned true, the current record
        // must not be None.
        let rec = cur.current_record();
        assert!(rec.is_some());
        let rec = rec.unwrap();

        // Get the references to the values in the columns at indexes 0 and 1.
        let i_val = rec.get_at(0)?;
        let s_val = rec.get_at(1)?;

        // SQL value references must be explicitly converted to target types.
        assert_eq!(i_val.to_i64()?, 1);
        assert_eq!(s_val.to_string()?, "Hello");
    }

    // The second row must be available.
    assert_eq!(cur.advance()?, true);

    // No more rows are expected.
    assert_eq!(cur.advance()?, false);

    Ok(())
}

依赖项