Matricsoft - software development
products > Quickdb > table creation
description
database creation
table creation
record creation
record retrieval
record operations
file operations
database operations
definition operations
exceptions
relations
header file

Table creation

class db
{
    void new_table();
    int  add_field(string const & field_name, EType field_type);
    int  add_field_ref(string const & field_name, string const & table_name);
    int  commit_table(string const & table_name);
}

Sample

mydb.new_table();
mydb.add_field("name", db_string);
mydb.add_field("sector", db_string);
mydb.commit_table("company");

mydb.new_table();
mydb.add_field("name", db_string);
mydb.add_field("surname", db_string);
mydb.add_field("age", db_int);
mydb.add_field_ref("IDcompany", "company");
mydb.commit_table("employee");

void new_table()

This method prepares a new table for the database.

mydb.new_table();

Result: you can now set the definitions of the fields of the table.
Note: this table is now in a temporary object, which will be bound to the database when commited.

int add_field(string const & field_name, EType field_type)

This method creates a new field in the table, with a given name and a given type.

mydb.add_field("name", db_string);
mydb.add_field("surname", db_string);
mydb.add_field("age", db_int);

Result: your field are defined, but the definition will not be definitive until you commit the table.
Note: you will then be able to access the fields (later) with either the name of the field ("name", "surname", "age") or the index of the field (0, 1, 2) which is the value returned by add_field.

int add_field_ref(string const & field_name, string const & table_name)

This method creates a field of a special type: db_ref.
This means that the value kept for this field would be the index of a field in a relationed table.
For instance, you can have 2 tables in your database: "employee" and "company". You can define here the fact that each employee works for 1 company:

//supposing that a table "company" has already been defined
//supposing that you are creating the "employee" table:
mydb.add_field("IDcompany", "company");

Result: this field represents now an index in the foreign table.
Important note: when you set up a relation, it is mandatory that the foreign table (here "company") already exist. This can lead to some limitations, but you can go to help on relations.

int commit_table(string const & table_name)

This method takes the table definition, makes some optimisations, and save the new table in the database.

mydb.commit_table("employee");

Result: your database has been commited. The return value is the index of the table (you can now access this table as "employee" or as 0 (if it is the first table created).