2019 iT 邦幫忙鐵人賽

DAY 23
Software Development

Let's build a DBMS: StellarSQL -- a minimal SQL DBMS written in Rust系列 第 23

StellarSQL 22: Update Components of Database

22: Update Components of Database


It's recommended to read on Gitbook

I modify some parts that I did yesterday.

Table in Database should be stored in HashMap. So checking the table is convenient.

!FILENAME component/

pub struct Database {
    pub name: String,
    pub tables: HashMap<String, Table>,

I decide to store all value in String. For the reasons that, (1) the Field has defined the DataType, so we can format the value if we want, (2) DBMS will not use the real value so frequently (only when processing where, but that's fine to deal in String), and (3) loading and saving data are all in String (actually, string to binary).

Therefore I only remain DataType:

!FILENAME component/

pub enum DataType {

Also, I forget that attributes could have default values. So I add a default in Field. Also, I remove DataValue, because Field is only the definition of a table. Therefore I also update table.

!FILENAME component/

pub struct Field {
    pub name: String,
    pub datatype: DataType,
    pub not_null: bool,
    pub default: Option<String>,
    pub check: Checker,

I also update Table a lot. A Table should be able to store rows, and it might be just a part of data from a huge set of table files. So, it needs to know where is the data from, including which page and which range by the cursors.

!FILENAME component/

pub struct Table {
    /* definition */
    pub name: String,
    pub fields: HashMap<String, Field>, // aka attributes
    pub primary_key: Vec<String>,
    pub foreign_key: Vec<String>,
    pub reference_table: Option<String>,

    /* value */
    pub rows: Vec<Row>,

    /* storage */
    pub page: u64, // which page of this table
    pub cursors: (u64, u64), // cursors of range in a page

I cannot design very well at the moment, so I would find more that should be modified as time goes by. In real practice, I believe I am on the right way.

Quick Link
1.StellarSQL Repository
2.Gitbook of this series

Liu, An-Chi (劉安齊). A software engineer, who loves writing code and promoting CS to people. Welcome to follow me at Facebook Page. More information on Personal Site and Github.

StellarSQL 21: First Implementation for Database Components
StellarSQL 23: Implement Table `insert`
Let's build a DBMS: StellarSQL -- a minimal SQL DBMS written in Rust30