⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wtest.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************
 *                                                              *
 * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.    *
 *                                                              *
 ****************************************************************/
#include <platform.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wddb.h"

#define DB_MEMORY_SIZE  4 * 1024 * 1024
#ifndef MCO_PLATFORM_X64
    #define PAGESIZE        96
#else 
    #define PAGESIZE        192
#endif 

const int MAP_ADDRESS = 0x20000000;

#include "w_data.h"

/* Load data to the database */
int LoadData(mco_db_h db)
{

    MCO_RET rc;
    mco_trans_h t;
    int i = 0;
    uint2 len;
    Record Obj;

    struct emp_data* e_ptr = EmpData;

    while (e_ptr->name)
    {
        /* let's store records in DB */
        /* begin the transaction  */
        rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);

        if (rc == MCO_S_OK)
        {

            /* allocate a new object */
            rc = Record_new(t, &Obj);

            if (rc == MCO_S_OK)
            {
                i++;
                /* fill the attributes */
                Record_iNumber_put(&Obj, i);

                len = (uint2)wcslen(e_ptr->dep);
                Record_wcCode_put(&Obj, e_ptr->dep, len);

                len = (uint2)wcslen(e_ptr->name);
                Record_wsName_put(&Obj, e_ptr->name, len);

                len = (uint2)wcslen(e_ptr->dep);
                Record_wcDepartment_put(&Obj, e_ptr->dep, len);

                len = (uint2)wcslen(e_ptr->job);
                Record_wsJob_put(&Obj, e_ptr->job, len);

                if (e_ptr->prev_jobs)
                {
                    uint2 j, n = 0;
                    wchar_t** prev_j = e_ptr->prev_jobs;
                    while (*prev_j++)
                    {
                        n++;
                    }
                    prev_j = e_ptr->prev_jobs;
                    Record_vwsPrevJob_alloc(&Obj, n);
                    for (j = 0; j < n; j++)
                    {
                        Record_vwsPrevJob_put(&Obj, j, prev_j[j], (uint2)wcslen(prev_j[j]));
                    }
                } 

                rc = mco_trans_commit(t);
            }
            else
            {

                printf("Couldn't allocate a new object\n");
                mco_trans_rollback(t);
            };

        }
        else
        {
            printf("Unable to start a transaction\n");
        };
        e_ptr++;
    };
    return 1;
}


/* Show attrs of a record */
void ShowObj(Record* hObj)
{
    wchar_t buffer[500];
    uint4 i;
    uint2 len;

    Record_iNumber_get(hObj, &i);
    printf("# %d: ", i);

    Record_wcCode_get(hObj, buffer, 500);
    buffer[8] = (wchar_t)0;
    printf("Code='%ls' ", buffer);

    Record_wsName_get(hObj, buffer, 500, &len);
    buffer[len] = (wchar_t)0;
    printf("Name='%ls' ", buffer);

    Record_wcDepartment_get(hObj, buffer, 500);
    buffer[20] = (wchar_t)0;
    printf("Department='%ls' ", buffer);

    Record_wsJob_get(hObj, buffer, 500, &len);
    buffer[len] = (wchar_t)0;
    printf("Job='%ls'\n", buffer);
};

/* Show data using tree-indexes routine */

void ShowData(mco_db_h db)
{

    MCO_RET rc;
    mco_trans_h t;
    mco_cursor_t csr, csr2;
    Record Obj;
    int i, j;

    /* navigate downward thru the items from the first to the last */
    printf("List of the first 10 records:\n");

    /* open a transaction */
    rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
    if (rc == MCO_S_OK)
    {

        /* open the cursor */
        rc = Record_I_Order_index_cursor(t, &csr);
        if (rc == MCO_S_OK)
        {

            /* jump to the first record */
            rc = mco_cursor_first(t, &csr);

            /* take 10 items */
            for (i = 0; i < 10 && rc == MCO_S_OK; i++)
            {

                /* get record from cursor */
                rc = Record_from_cursor(t, &csr, &Obj);

                if (rc == MCO_S_OK)
                {

                    /* show it */
                    ShowObj(&Obj);

                    /* move to the next item */
                    rc = mco_cursor_next(t, &csr);
                };
            };
        }
        else
        {
            printf("Unable to open a cursor");
        };

        /*  close the transaction */
        rc = mco_trans_commit(t);
    }
    else
    {
        printf("Unable to open a transaction");
    };
    printf("\n");

    /* navigate upward thru the items from the last to the first */
    printf("List of the last 10 records:\n");

    /* open a transaction */
    rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
    if (rc == MCO_S_OK)
    {

        /* open the cursor */
        rc = Record_I_Order_index_cursor(t, &csr);
        if (rc == MCO_S_OK)
        {

            /* move to last item */
            rc = mco_cursor_last(t, &csr);

            /* show last 10 items */
            for (i = 0; i < 10 && rc == MCO_S_OK; i++)
            {

                /* get the record */
                rc = Record_from_cursor(t, &csr, &Obj);

                if (rc == MCO_S_OK)
                {

                    /* show	it */
                    ShowObj(&Obj);

                    /* move to the next item */
                    rc = mco_cursor_prev(t, &csr);
                };
            };
        }
        else
        {
            printf("Unable to a open cursor");
        };
        rc = mco_trans_commit(t);
    }
    else
    {
        printf("Unable to open a transaction");
    };
    printf("\n");

    /* looking for specified item */
    printf("Search for the first record, where department is QA\n");

    /* open a transaction */
    rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
    if (rc == MCO_S_OK)
    {

        /* open the cursor */
        rc = Record_I_Department_index_cursor(t, &csr);
        if (rc == MCO_S_OK)
        {

            wchar_t* wbuf = L"QA";
            uint2 len;
            len = 2;

            /* search an item by key value */
            rc = Record_I_Department_search(t, &csr, MCO_EQ, wbuf, len);

            /* analyze the results */
            switch (rc)
            {
                case MCO_S_OK:
                    {
                        /* Item's found. Display it. */
                        rc = Record_from_cursor(t, &csr, &Obj);

                        if (rc == MCO_S_OK)
                        {
                            ShowObj(&Obj);

                            /* Locate it in the I_Index cursor */
                            rc = Record_I_Index_locate(t, &csr2, &Obj);

                            if (rc == MCO_S_OK)
                            {

                                /* And show next one */
                                if (mco_cursor_next(t, &csr2) == MCO_S_OK && Record_from_cursor(t, &csr2, &Obj) ==
                                    MCO_S_OK)
                                {
                                    ShowObj(&Obj);
                                }
                            }
                            ;
                        }
                        ;
                    }
                    ;
                    break;
                case MCO_S_NOTFOUND:
                    printf("Not found.\n");
                    break;
                default:
                    printf("Error while searching.\n");
                    break;
            };
        }
        else
        {
            printf("Unable to open a cursor");
        };

        /* close the transaction */
        rc = mco_trans_commit(t);
    }
    else
    {
        printf("Unable to open a transaction");
    };
    printf("\n");


    /* using composite index I_DiffSum */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -