/* test.c */

#include <stdio.h>
#include <stdlib.h>
#include <dbmgr.h>
#include <test.h>

void main( void);

char *dbNames[] =
    {
    "Customer",
    "acctNbr",
    "company",
    "Address",
    "zip",
    "telephone",
    "Invoice",
    "invNbr",
    "InvoiceLine"
    };

#define CUSTOMER        dbNames[0]
#define ACCTNBR         dbNames[1]
#define COMPANY         dbNames[2]
#define ADDRESS         dbNames[3]
#define ZIP             dbNames[4]
#define TELEPHONE       dbNames[5]
#define INVOICE         dbNames[6]
#define INVNBR          dbNames[7]
#define INVOICELINE     dbNames[8]

struct Customer customer[3] =
    {
    {1000L,"Fred Gwynne Company",0.00},
    {1001L,"CompuServe Incorporated",0.00},
    {1002L,"Microsoft",0.00},
    };

struct Address address[4] =
    {
    {"124 South Columbus St.","Columbus","Oh","43201","6142883222"},
    {"2000 Arlington Center Blvd","Columbus","Oh","43228","6144598600"},
    {"12 North Fork Ave.","Redmond","WA","55555","5332341234"},
    {"13 North Fork Ave.","Redmond","WA","55555","5332341235"},
    };


void main( void)
    {
    long acctNbr = 1001L;
    struct currency_index currency;
    struct Customer t_customer;
    struct Address t_address;

    if( DbOpen( "test.dbd"))
        {
        printf( "Error opening test.dbd\n");
        exit( 1);
        }

    /* Customer 1000 */
    DbRecordAdd( CUSTOMER, &customer[0]);
    DbRecordAdd( ADDRESS, &address[0]);
    DbSetAdd( CUSTOMER, ADDRESS);

    /* Customer 1001 */
    DbRecordAdd( CUSTOMER, &customer[1]);
    DbRecordAdd( ADDRESS, &address[1]);
    DbSetAdd( CUSTOMER, ADDRESS);

    /* Customer 1002 */
    DbRecordAdd( CUSTOMER, &customer[2]);
    DbRecordAdd( ADDRESS, &address[2]);
    DbSetAdd( CUSTOMER, ADDRESS);
    DbRecordAdd( ADDRESS, &address[3]);
    DbSetAdd( CUSTOMER, ADDRESS);

    /* Get the first customer by account number */
    DbRecordFindFirst( CUSTOMER, ACCTNBR);
    DbRecordGetCurrent( CUSTOMER, ACCTNBR, &t_customer);

    /* Save the currency */
    DbRecordGetCurrency( CUSTOMER, &currency);

    /* Get the last customer */
    DbRecordGetLast( CUSTOMER, ACCTNBR, &t_customer);

    /* Get the addresses */
    DbSetFindFirst( CUSTOMER, ADDRESS);
    DbSetGetFirst( CUSTOMER, ADDRESS, &t_address);
    DbSetGetNext( CUSTOMER, ADDRESS, &t_address);

    /* Restore the customer currency and retrieve the current customer */
    DbRecordUpdCurrency( CUSTOMER, &currency);
    DbRecordGetCurrent( CUSTOMER, ACCTNBR, &t_customer);

    /* Get customer 1001 */
    DbRecordGetByKey( CUSTOMER, ACCTNBR, &t_customer, &acctNbr);

    /* Get address */
    DbRecordGetByKey( ADDRESS, ZIP, &t_address, "55555");

    /* Get the owner of the address */
    DbSetGetOwner( CUSTOMER, ADDRESS, &t_customer);

    DbFlush();
    DbClose();
    }
