YACDB
Yet Another C(rappy) Database
db_ops.h File Reference

High(ish) level instructions for general database operations. More...

#include "../../diskio/include/diskio.h"
#include "../../diskio/include/db_structs.h"
#include "../../diskio/include/diskiod.h"
#include "record.h"
#include "cursor.h"
#include "cursor_bt.h"
#include <string.h>
#include <assert.h>
Include dependency graph for db_ops.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TABLES   0
 The id, and the address of the TABLES table.
 
#define COLUMNS   1
 The id, and the address of the COLUMNS table.
 
#define TABLES_NAMES   2
 The address of the TABLES_NAMES table.
 
#define COLUMNS_TIDX   3
 The address of the COLUMNS_NAMES table.
 
#define COLUMNS_NAMES   4
 The address of the COLUMNS_NAMES table.
 

Functions

int initialize_tables ()
 Create initial tables. More...
 
void * get_row (Page root, Key key, Cursor **cursor)
 Get row from table with root root.
 
struct recordget_n_record (Page root, Key key, size_t n)
 Retrieves the nth record from the row with key key.
 
Page get_table_addr (Key table_id)
 Get the table tree address of the given table.
 
Key get_table_id (char *table_name)
 Get the table id from a table name.
 
Key get_next_id (Key root_id)
 Get the next available index in the table.
 
Key insert_row (Key root_id, struct record **records, size_t size)
 Insert a row into the table with key root_id.
 
Key advanced_insert_row (Key tidx, struct record **records, size_t size)
 
Page create_table (char *table_name, char **column_names, short nb_cols)
 Creates a new table in the database. More...
 
short * get_column_offsets (Key table_id, char *col_names[], size_t nb_cols)
 Get the offsets of a list of columns in a table. More...
 
char * select_row_columns (char *table_name, Key key, char *col_names[], size_t nb_cols)
 Selects a row from a table with the given key. More...
 
char * select_all (char *table_name, char *col_names[], size_t nb_cols)
 Selects all rows from a table. More...
 
char * select_where (char *table_name, char *col_names[], size_t nb_cols, char *where_col_name, struct record *where_record)
 Selects all rows from a table where the given column is equal to the given value. More...
 
void exit_db ()
 Exits and closes the database properly.
 

Detailed Description

High(ish) level instructions for general database operations.

Author
Jack Royer
Date
2022-05-29

Function Documentation

◆ create_table()

Page create_table ( char *  table_name,
char **  column_names,
short  nb_cols 
)

Creates a new table in the database.

Parameters
table_nameThe name of the table to create.
column_namesThe names of the columns in the table.
nb_colsThe number of columns in the table.
Returns
The identifier of the newly created table, -1 if an error occurred.

◆ get_column_offsets()

short * get_column_offsets ( Key  table_id,
char *  col_names[],
size_t  nb_cols 
)

Get the offsets of a list of columns in a table.

For a table TABLE with columns idx, COL 0, COL 1, COL 2; the offset of COL 2 is 2.

Use this function to get all the offsets at once as it is a costly operation.

Parameters
table_idThe identifier of the table (it can be obtained with get_table_id)
column_namesThe names of the columns
nb_colsThe number of columns
Returns
The offsets of the columns in the table, NULL if an error occurred.

◆ initialize_tables()

int initialize_tables ( )

Create initial tables.

Creates the initial tables in the database if they are missing. There are 2 tables:

  • TABLES: a table of all tables in the database.
  • COLUMNS: a table of all columns in the database.

These trees have "hidden" columns: they are marked with an underscore.

TABLES table

It's root is on page TABLES, it has index TABLES.

The table has the following columns:

Column Offset Type Description
_idx - Key The index of the table in the database
name 0 char * The name of the table
_root 1 Page the root page of the table
_next_id 2 Key the next available id for a row in the table

COLUMNS table

It's root is on page COLUMNS, it has index COLUMNS. It points to the index trees associated with each column.

The table has the following columns:

Column Offset Type Description
_idx - Key The index of the column in the database
table_idx 0 Key The index of the table
name 1 char * The name of the column
_offset 2 short The offset of the column in the table
_root 3 Page The root page of the index tree

◆ select_all()

char * select_all ( char *  table_name,
char *  col_names[],
size_t  nb_cols 
)

Selects all rows from a table.

Parameters
table_nameThe name of the table
col_namesThe names of the columns the user wants to select
nb_colsThe number of columns the user wants to select, if 0 select all
Returns
char* The result of the query, NULL if an error occured

◆ select_row_columns()

char * select_row_columns ( char *  table_name,
Key  key,
char *  col_names[],
size_t  nb_cols 
)

Selects a row from a table with the given key.

Parameters
table_nameThe name of the table to select from
keyThe key of the row to select
col_namesThe names of the columns the user wants to select
nb_colsThe number of columns to select

◆ select_where()

char * select_where ( char *  table_name,
char *  col_names[],
size_t  nb_cols,
char *  where_col_name,
struct record where_record 
)

Selects all rows from a table where the given column is equal to the given value.

Parameters
table_nameThe name of the table
col_namesThe names of the columns the user wants to select
nb_colsThe number of columns the user wants to select, if 0 select all
where_col_nameThe name of the column to compare
where_recordThe value to compare
Returns
char* The result of the query, NULL if an error occured