I need to code a small/simple database application using C, for my CS degree (so using SQLite or any other available application is not an option. In other words, I do need to re-invent the wheel here).
My idea is to use a B-Tree to store the items of each table. The problem I am facing is that tables need to be flexible to hold an unknown number of columns, and each column can be either a STRING or an INT. For example, with this command:
CREATE TABLE student (STRING name, INT age)
I would need to create a table that holds a string and an integer. With this command instead:
CREATE TABLE grade (INT grade1, INT grade2, INT grade3)
I would need to create a table that holds three integers.
How can achieve such flexibility?
My only idea so far is to create a struct with several unions inside it, where each union can be either a STRING or an INT. I would also need to put a lot of unions inside, to be sure to accommodate all the columns requested by the table. For example:
struct table{
union{
int number;
char *text;
}column1;
union{
int number;
char *text;
}column2;
union{
int number;
char *text;
}column3;
....
};
Is there a better way to do this?
i can only think of two very different approaches:
structs and compiles the database engine.most common is #2, but #1 has the advantage that the C compiler ‘knows’ the table structure, and will type-check your columns.