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

Record retrieval

class db
{
    int    get_field_int(string const & table_name, int record_number, string const & field_name);
    long   get_field_long (string const & table_name, int record_number, string const & field_name);
    string get_field_string(string const & table_name, int record_number, string const & field_name);
    int    get_field_ref(string const & table_name, int record_number, string const & field_name);

    int    get_number_children(string const & table_name, int record_number, string const & child_table_name);
    int    get_child(string const & table_name, int record_number, string const & child_table_name, int index_child);
}

class db    //equivalent methods with indexes:
{
    int    get_field_int(int table_number, int record_number, int field_number);
    long   get_field_long(int table_number, int record_number, int field_number);
    string get_field_string(int table_number, int record_number, int field_number);
    int    get_field_ref(int table_number, int record_number, int field_number);

    int    get_number_children(int table_number, int record_number, int child_table);
    int    get_child(int table_number, int record_number, int child_table_number, int index_child);
}

Sample

string myname=mydb.get_field_string("employee", 0, "name");
string mysurname=mydb.get_field_string("employee", 0, "surname");
int myage=mydb.get_field_int("employee", 0, "age");
int myidcompany=mydb.get_field_ref("employee", 0, "IDcompany");

string mycompanyname=mydb.get_field_string("company", myidcompany, "name");
string mycompanysector=mydb.get_field_string("sector", myidcompany, "sector");

//retrieve all the employee of a company
int nb_employee=mydb.get_number_children("company", 0, "employee");
for (int i=0;i<nb_employee;i++)
{
    int myemployeenumber=mydb.get_child("company", 0, "employee", i);
    string myemployeename=mydb.get_field_string("employee", myemployeenumber, "name");
    //do some work on myemployeename...
}

... get_field_...(string const & table_name, int record_number, string const & field_name)
...get_field_...(int table_number, int record_number, int field_number)

With these methods, you can get the value of a field in a given table, a record number, and a given field.

string myname=mydb.get_field_string("employee", 3, "name");
int myage=mydb.get_field_int("employee", 3, "age");

Note: if you are not sure if a given record exist, you shall test it with the method record_exist (otherwise an exception is risen).
You can only get the value of a given type if the field is of this type (i.e. there is no cast), so you have to convert yourself the values returned.

//example with a pointer stored as a long, HTREEITEM stored from a treeview item:
long mypointer=mydb.get_field_long("treeview_items", 0, "hitem");
HTREEITEM myhtreeitem=(HTREEITEM) mypointer;
//or:
HREEITEM myhtreeitem=(HTREEITEM) mydb.get_field_long("treeview_items", 0, "hitem");

int get_number_children(string const & table_name, int record_number, string const & child_table_name)
int get_number_children(int table_number, int record_number, int child_table_number)

This method gives you the number of children of a given table. What is the definition of a child? suppose you have a table "company" and a table "employee", with a field "IDcompany" which is a reference to a record in the table "company".
If you select a record in the table "company", you can get the number of "employee" that refer to this particular record (of "company"). A child is a record which has a db_ref type as one of his fields (a reference):
If the table "employee" contains a reference towards "company", then "employee" is a child of "company".

//we suppose the tables "company" and "employee" have been created
mydb.new_record("employee");
mydb.set_field_string("name", "john");
mydb.set_field_ref("IDcompany", 3);
mydb.commit_record();
//...
mydb.new_record("employee");
mydb.set_field_string("name", "other employee");
mydb.set_field_ref("IDcompany", 3);
mydb.commit_record();
//...
int nb_children=mydb.get_number_children("company", 3, "employee");
//there shall be 2 children: "john" and "other employee".
You can get these values with:
for (int i=0;i<nb_children;i++)
{
    int myemployee=mydb.get_child("company", 3, "employee", i);
    string myemployee_name=mydb.get_field_string("employee", myemployee, "name");
}

Result: the call of get_number_children gives you the number of records that are related to a given table. Please note that you need to give the child_table_name (in this case "employee") because several tables can have a relation with a given table...Note also that the children are kept in memory (therefore it is very fast to access them this way).

int get_child(string const & table_name, int record_number, string const & child_table_name, int index_child)
int get_child(int table_number, int record_number, int child_table_number, int index_child)

This method gives you the index of a child of a given table (for a given child table). Please note that the result is the record number of the child (in the child table). You then use the get_field_... to get the value of a given field.
Important note: the "index_child" is the index of the child (i.e. if a given record has 2 children, they are accessed through index 0 and 1).

int nb_children=mydb.get_number_children("company", 3, "employee");
//there shall be 2 children: "john" and "other employee".
You can get these values with:
for (int i=0;i<nb_children;i++)
{
    int myemployee=mydb.get_child("company", 3, "employee", i);
    string myemployee_name=mydb.get_field_string("employee", myemployee, "name");
}

Note: we may add a new method here to avoid the call to two methods to get the value of a given field. Maybe something like "get_child_field_string" would be less confusing than having to call two methods. But this shall be in next release...