index_join

Directory

eXtremeDB_rt/samples/index_join

Description

demonstrates using an index to join two classes.

Used API

Code snippet

 
    MCO_RET find_coworkers(mco_db_h db, char * search_name)
    {
        MCO_RET rc = MCO_S_OK;
        mco_trans_h t;
        char str[100];
    
        //Find co-workers of search_emp
        rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
        if (MCO_S_OK == rc) {
            char name[20];
            mco_uint2 len;
            mco_uint2 dept_no1, dept_no2;
            Employee emp1, emp2;
            Department dept1;
            mco_cursor_t csr;
    
            // 1. Find the Employee object by name and extract dept_no
            rc = Employee_Iname_find(t, search_name, (mco_uint2)strlen(search_name), &emp1);
            if (MCO_S_OK == rc) {
                printf("\n\n%s's co-workers in ", search_name);
                Employee_dept_no_get(&emp1, &dept_no1);
                Employee_Idept_name_index_cursor(t, &csr);
    
                // 2. Find the Department object by its dept_no and display the Department name
                rc = Department_Idept_no_find(t, dept_no1, &dept1);
                if (MCO_S_OK == rc) {
                    Department_name_get(&dept1, name, sizeof(name), &len);
                    printf("%s are:\n", name);
    
                    // 3. Position the cursor in the Idept_name index to the first object with this dept_no
                    rc = Employee_Idept_name_search(t, &csr, MCO_GE, dept_no1, "", 0);
                    // Scroll forward through the cursor and display the names found...
                    while (MCO_S_OK == rc) {
                      // Check if the current Employee is same as the found Employee
                      Employee_from_cursor(t, &csr, &emp2);
                      Employee_name_get(&emp2, name, sizeof(name), &len);
                      // If the two names are not equal, display the name (if dept_no is still the same)
                      if (0 != strcmp(name, search_name)) {
                        // Verify that the dept_no is still the same, otherwise break out of loop
                        Employee_dept_no_get(&emp2, &dept_no2);
                        if (dept_no1 != dept_no2) break;
                        printf("\n\t%s", name);
                      }
                      rc = mco_cursor_next(t, &csr);
                    }

What to expect as an output

 
Create departments:

	0) Acct, Accounting, dept_no=101
	1) Eng, Engineering, dept_no=102
	2) CS, Customer Service, dept_no=103
	3) TS, Technical Support, dept_no=104

Create employees and join each to a department:

	0) John, dept_no=101
	1) Samuel, dept_no=101
	2) Thomas, dept_no=101
	3) David, dept_no=102
	4) James, dept_no=102
	5) Robert, dept_no=102
	6) William, dept_no=103
	7) Kevin, dept_no=103
	8) Alex, dept_no=103
	9) Daniel, dept_no=103
	10) Diego, dept_no=103
	11) Brandon, dept_no=104

William's co-workers in Customer Service are:

	Alex
	Daniel
	Diego
	Kevin