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

Record creation

class db
{
    void new_record(string const & table_name);

    void set_field(string const & field_name, int field_value);
    void set_field(string const & field_name, long field_value);
    void set_field(string const & field_name, string field_value);
    void set_field_ref(string const & field_name, int field_value);

    int  commit_record(int record_number=-1);
}

class db    //equivalent methods with indexes:
{
    void new_record(int table_number);

    void set_field(int field_number, long field_value);
    void set_field(int field_number, int field_value);
    void set_field(int field_number, string field_value);
    void set_field_ref(int field_number, int field_value);

    int  commit_record(int record_number=-1);
}

Sample

mydb.new_record("company");
mydb.set_field("name", "matricsoft");
mydb.set_field("sector", "software development");
int record_number=mydb.commit_record();

mydb.new_record("employee");
mydb.set_field("name", "john");
mydb.set_field("surname", "doe");
mydb.set_field("age", 32);
mydb.set_field_ref("IDcompany", record_number);
mydb.commit_record();

void new_record(string const & table_name)
void new_record(int table_number)

This method creates a new record (temporary object) to fill-in a record when it is commited in the database.
You can access the table by its name or by its index.

mydb.new_record("company");
mydb.new_record(0);//if the table is the first one

Result: you can now fill-in the data for this record.

void set_field(...)

With this set of methods, you can set the fields value. The type you enter as a parameter determines which method will be called. A small warning: those functions throw an exception if the type defined for this field does not correspond to what has been entered (there is no type cast). Therefore, you shall make a cast to be sure the value is of the good type (above all for int, and longs...):

mydb.set_field("name", "john");
mydb.set_field("surname", "doe");
mydb.set_field("age", (int) 32); //important: (int)

//if you have an int variable, no problem:
int myage=32;
mydb.set_field("age", myage);

//if you have a long, better make a cast:
long myage=32;
mydb.set_field("age", (int) myage);

Result: the temporary record is filled-in
Note: the type in the definition of a field (db_string, db_int, db_long... must correspond to the type of the value in the method set_field (string or char *, int, long...). Otherwise, an exception is risen: the two types shall correspond.

Important note: if you want to keep pointers in memory, you can use the db_long type. For instance, you add an item in a treeview, you can then keep the handle of this item in the database (this handle, HTREEITEM is a pointer, and can be kept as long):

HTREEITEM myitem=mytreeview.add_elt("parameters");
mydb.new_record("treeview_items");
mydb.set_field("hitem", (long) myitem);

That's the approach that has been used in other software creation: you can keep a separate database for all your gui values, such as the treeview items...

void set_field_ref(string const & field_name, int field_value)
void set_field_ref(string const & field_name, int field_value)

This method is similar to set_field_ref, except that the type is predefined (db_ref) and the parameter value is the index of a record in the foreign table.

mydb.set_field_ref("IDcompany", 4);

Result: this record will now have the field IDcompany equal to the index of the record number 4 of the foreign table.
Note: When you set a field which is a reference, an index is created that describes the children of a given record (see help on relations).

int commit_record(int record_number=-1)

This method commits the record in the database.
If you wish to edit a determined record, just set the parameter record_number to the record you want to edit, and data will be replaced.

mydb.commit_record(); //new record created
mydb.commit_record(4); //the fifth record will be replaced by the new one

Note: the record number (returned by this function) never changes, even if you delete records that have a lower index in the table (this enables you not to update your gui for instance).